Commit f60acc15 authored by nanahira's avatar nanahira

first

parent e56c799f
Pipeline #11225 passed with stages
in 1 minute and 43 seconds
# compiled output
/dist
/node_modules
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# OS
.DS_Store
# Tests
/coverage
/.nyc_output
# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace
# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
/data
/output
/config.yaml
.git*
Dockerfile
.dockerignore
/tests
webpack.config.js
dist/*
build/*
*.js
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
],
root: true,
env: {
node: true,
jest: true,
},
ignorePatterns: ['.eslintrc.js'],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
},
};
# compiled output
/dist
/node_modules
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# OS
.DS_Store
# Tests
/coverage
/.nyc_output
# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace
# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
/data
/output
/config.yaml
stages:
- build
- combine
- deploy
variables:
GIT_DEPTH: "1"
CONTAINER_TEST_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG
CONTAINER_TEST_ARM_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG-arm
CONTAINER_TEST_X86_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_REF_SLUG-x86
CONTAINER_RELEASE_IMAGE: $CI_REGISTRY_IMAGE:latest
before_script:
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
build-x86:
stage: build
tags:
- docker
script:
- TARGET_IMAGE=$CONTAINER_TEST_X86_IMAGE
- docker build --pull -t $TARGET_IMAGE .
- docker push $TARGET_IMAGE
build-arm:
stage: build
tags:
- docker-arm
script:
- TARGET_IMAGE=$CONTAINER_TEST_ARM_IMAGE
- docker build --pull -t $TARGET_IMAGE .
- docker push $TARGET_IMAGE
combine:
stage: combine
tags:
- docker
script:
- TARGET_IMAGE=$CONTAINER_TEST_IMAGE
- SOURCE_IMAGE_2=$CONTAINER_TEST_ARM_IMAGE
- SOURCE_IMAGE_1=$CONTAINER_TEST_X86_IMAGE
- docker pull $SOURCE_IMAGE_1
- docker pull $SOURCE_IMAGE_2
- docker manifest create $TARGET_IMAGE --amend $SOURCE_IMAGE_1 --amend
$SOURCE_IMAGE_2
- docker manifest push $TARGET_IMAGE
deploy_latest:
stage: deploy
tags:
- docker
script:
- TARGET_IMAGE=$CONTAINER_RELEASE_IMAGE
- SOURCE_IMAGE=$CONTAINER_TEST_IMAGE
- docker pull $SOURCE_IMAGE
- docker tag $SOURCE_IMAGE $TARGET_IMAGE
- docker push $TARGET_IMAGE
only:
- master
deploy_tag:
stage: deploy
tags:
- docker
script:
- TARGET_IMAGE=$CI_REGISTRY_IMAGE:$CI_COMMIT_TAG
- SOURCE_IMAGE=$CONTAINER_TEST_IMAGE
- docker pull $SOURCE_IMAGE
- docker tag $SOURCE_IMAGE $TARGET_IMAGE
- docker push $TARGET_IMAGE
only:
- tags
stages:
- install
- build
- deploy
variables:
GIT_DEPTH: "1"
npm_ci:
stage: install
tags:
- linux
script:
- npm ci
artifacts:
paths:
- node_modules
.build_base:
stage: build
tags:
- linux
dependencies:
- npm_ci
build:
extends:
- .build_base
script:
- npm run build
artifacts:
paths:
- dist/
unit-test:
extends:
- .build_base
script:
- npm run test
deploy_npm:
stage: deploy
dependencies:
- build
tags:
- linux
script:
- apt update;apt -y install coreutils
- echo $NPMRC | base64 --decode > ~/.npmrc
- npm publish . || true
only:
- master
/install-npm.sh
.git*
/data
/output
/config.yaml
.idea
.dockerignore
Dockerfile
/src
/coverage
/tests
/dist/tests
{
"singleQuote": true,
"trailingComma": "all"
}
\ No newline at end of file
FROM node:bullseye-slim as base
LABEL Author="Nanahira <nanahira@momobako.com>"
RUN apt update && apt -y install python3 build-essential && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /var/log/*
WORKDIR /usr/src/app
COPY ./package*.json ./
FROM base as builder
RUN npm ci && npm cache clean --force
COPY . ./
RUN npm run build
FROM base
ENV NODE_ENV production
RUN npm ci && npm cache clean --force
COPY --from=builder /usr/src/app/dist ./dist
CMD [ "npm", "start" ]
The MIT License (MIT)
Copyright (c) 2021 Nanahira
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
# koishi-entities
Class and decorator style of Koishi database model definitions.
\ No newline at end of file
Class and decorator style of Koishi database model definitions.
## Documentation
https://koishi.js.org/guide/misc/decorators.html
#!/bin/bash
npm install --save-dev \
@types/node \
typescript \
'@typescript-eslint/eslint-plugin@^4.28.2' \
'@typescript-eslint/parser@^4.28.2 '\
'eslint@^7.30.0' \
'eslint-config-prettier@^8.3.0' \
'eslint-plugin-prettier@^3.4.0' \
prettier \
jest \
@types/jest \
ts-jest \
rimraf
This source diff could not be displayed because it is too large. You can view the blob instead.
{
"name": "koishi-entities",
"description": "Class and decorator style of Koishi database model definitions.",
"version": "1.0.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"lint": "eslint --fix .",
"build": "rimraf dist && tsc",
"test": "jest --passWithNoTests",
"start": "node dist/index.js"
},
"repository": {
"type": "git",
"url": "https://code.mycard.moe/3rdeye/koishi-entities.git"
},
"author": "Nanahira <nanahira@momobako.com>",
"license": "MIT",
"keywords": [],
"bugs": {
"url": "https://code.mycard.moe/3rdeye/koishi-entities/issues"
},
"homepage": "https://code.mycard.moe/3rdeye/koishi-entities",
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "tests",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
},
"devDependencies": {
"@types/jest": "^27.4.1",
"@types/lodash": "^4.14.180",
"@types/node": "^17.0.23",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^3.4.1",
"jest": "^27.5.1",
"prettier": "^2.6.1",
"rimraf": "^3.0.2",
"ts-jest": "^27.1.4",
"typescript": "^4.6.3"
},
"peerDependencies": {
"koishi": "^4.5.0"
},
"dependencies": {
"@koishijs/plugin-database-memory": "^1.1.0",
"lodash": "^4.17.21",
"typed-reflector": "^1.0.10"
}
}
import { ClassType, ModelFieldDef, PrimaryKeyDef } from './def';
import { Metadata } from './meta/meta';
import { Flatten, Keys, Model, Tables } from 'koishi';
import { inferType } from './utils';
export const DefineModel = (name: Keys<Tables>): ClassDecorator =>
Metadata.set('ModelTableName', name);
export const ModelField =
<T = any>(def?: ModelFieldDef<T>): PropertyDecorator =>
(obj, key) =>
Metadata.set(
'ModelField',
def || inferType(Reflect.getMetadata('design:type', obj, key)),
'ModelFieldKeys',
)(obj, key);
export const Primary = (): PropertyDecorator =>
Metadata.set('ModelPrimaryKey', { autoIncrement: false });
export const PrimaryGenerated = (): PropertyDecorator =>
Metadata.set('ModelPrimaryKey', { autoIncrement: true });
export const Foreign = <K extends Keys<Tables>>(
referencingTable: K,
referencingColumn: Keys<Flatten<Tables[K]>>,
): PropertyDecorator =>
Metadata.set('ModelForeignKey', [referencingTable, referencingColumn]);
export const Unique = (
identifier: string | number = '_default',
): PropertyDecorator => Metadata.append('ModelUnique', identifier);
export const ChildModel =
(cls?: ClassType): PropertyDecorator =>
(obj, key) => {
if (!cls) {
cls = Reflect.getMetadata('design:type', obj, key);
}
Metadata.set('ChildModel', cls, 'ChildModelKeys')(obj, key);
};
import { Model } from 'koishi';
export type ModelFieldDef<T = any> =
| Model.Field<T>
| Model.Field.Shorthand<Model.Field.Type<T>>;
export type ClassType<T = any> = { new (...args: any[]): T };
export interface PrimaryKeyDef {
autoIncrement: boolean;
}
export * from './def';
import { ClassType, ModelFieldDef, PrimaryKeyDef } from '../def';
import { Keys, Tables } from 'koishi';
export interface MetadataArrayMap {
ModelUnique: string | number;
ModelFieldKeys: string;
ChildModelKeys: string;
}
export interface MetadataMap {
ModelField: ModelFieldDef;
ModelTableName: Keys<Tables>;
ModelPrimaryKey: PrimaryKeyDef;
ModelForeignKey: [string, string];
ChildModel: ClassType;
}
import { MetadataArrayMap, MetadataMap } from './def';
import { MetadataSetter, Reflector } from 'typed-reflector';
export const reflector = new Reflector<MetadataMap, MetadataArrayMap>();
export const Metadata = new MetadataSetter<MetadataMap, MetadataArrayMap>();
import { Context, Keys, Model } from 'koishi';
import { ClassType } from './def';
import { reflector } from './meta/meta';
class ModelRegistrar<T = any> {
constructor(private cls: ClassType<T>, private prefix = '') {}
getTableName() {
return reflector.get('ModelTableName', this.cls);
}
getFields(): Model.Field.Extension<T> {
const keys = reflector.getArray('ModelFieldKeys', this.cls);
const result: Model.Field.Extension<T> = {};
for (const key of keys) {
const field = reflector.get('ModelField', this.cls, key);
if (field) {
result[this.prefix + key] = field;
}
}
return result;
}
getExtensions(): Model.Extension<T> {
const keys = reflector.getArray('ModelFieldKeys', this.cls);
const result: Model.Extension<T> = {};
const primaryKeys: string[] = [];
const uniqueMap = new Map<number | string, string[]>();
for (const key of keys) {
// primary keys
const primary = reflector.get('ModelPrimaryKey', this.cls, key);
if (primary) {
primaryKeys.push(this.prefix + key);
if (primary.autoIncrement) {
result.autoInc = true;
}
}
// foreign keys
const foreign = reflector.get('ModelForeignKey', this.cls, key);
if (foreign) {
if (!result.foreign) {
result.foreign = {};
}
result.foreign[this.prefix + key] = foreign;
}
// unique
const uniqueEntries = reflector.getArray('ModelUnique', this.cls, key);
for (const unique of uniqueEntries) {
if (!uniqueMap.has(unique)) {
uniqueMap.set(unique, []);
}
uniqueMap.get(unique).push(this.prefix + key);
}
}
if (primaryKeys.length) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
result.primary = primaryKeys;
}
const uniqueDefs = Array.from(uniqueMap.values());
if (uniqueDefs.length) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
result.unique = uniqueDefs;
}
return result;
}
private mergeFields(fieldDefs: Model.Field.Extension[]) {
let result: Model.Field.Extension<T> = {};
for (const fieldDef of fieldDefs) {
result = { ...result, ...fieldDef };
}
return result;
}
private mergeExtensions(extDefs: Model.Extension<T>[]) {
const result: Model.Extension<T> = {
autoInc: extDefs.some((ext) => ext.autoInc),
};
for (const extDef of extDefs) {
if (extDef.primary) {
if (!result.primary) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
result.primary = [];
}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
result.primary = (result.primary as Keys<T, any>[]).concat(
extDef.primary,
);
}
if (extDef.unique) {
if (!result.unique) {
result.unique = [];
}
result.unique = result.unique.concat(extDef.unique);
}
if (extDef.foreign) {
result.foreign = { ...(result.foreign || {}), ...extDef.foreign };
}
}
return result;
}
getChildDict() {
const keys = reflector.getArray('ChildModelKeys', this.cls);
const result: { [K in keyof T]?: ClassType<T> } = {};
for (const key of keys) {
const child = reflector.get('ChildModel', this.cls, key);
if (child) {
result[key] = child;
}
}
return result;
}
getInternal() {
let internal = { [this.prefix]: this.cls.prototype };
const childDict = this.getChildDict();
for (const key in childDict) {
const child = childDict[key];
const prefix = this.prefix + key + '.';
const childReg = new ModelRegistrar(child, prefix);
internal = { ...internal, ...childReg.getInternal() };
}
return internal;
}
private getChildModelResults() {
const children = this.getChildDict();
const results: [Model.Field.Extension, Model.Extension][] = [];
for (const key of Object.keys(children)) {
const child = children[key];
if (child) {
const childRegistrar = new ModelRegistrar(
child,
this.prefix + key + '.',
);
results.push(childRegistrar.getModelResult());
}
}
return {
fields: results.map((r) => r[0]),
extensions: results.map((r) => r[1]),
};
}
getModelResult(): [Model.Field.Extension<T>, Model.Extension<T>] {
const fields = this.getFields();
const extensions = this.getExtensions();
const childResults = this.getChildModelResults();
return [
this.mergeFields([fields, ...childResults.fields]),
this.mergeExtensions([extensions, ...childResults.extensions]),
];
}
}
export function registerModel(
ctx: Context,
cls: { new (...args: any[]): any },
) {
const registrar = new ModelRegistrar(cls);
const tableName = registrar.getTableName();
if (!tableName) {
throw new Error(`Model of ${cls.name} is not defined`);
}
ctx.model.extend(tableName, ...registrar.getModelResult());
ctx.model.config[tableName].internal = registrar.getInternal();
}
import { ModelFieldDef } from '../def';
// eslint-disable-next-line @typescript-eslint/ban-types
export function inferType(type: Function): ModelFieldDef {
switch (type) {
case String:
return 'string';
case Number:
return 'integer';
case Boolean:
return 'integer(4)';
case Date:
return 'timestamp';
case Array:
return 'list';
default:
return 'json';
}
}
import {
ChildModel,
DefineModel,
Foreign,
ModelField,
PrimaryGenerated,
Unique,
} from '../src/decorators';
import { App } from 'koishi';
import { registerModel } from '../src/register';
import MemoryDatabase from '@koishijs/plugin-database-memory';
declare module 'koishi' {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface Tables {
dress: Dress;
}
}
class DressProperty {
@ModelField('string(8)')
color: string;
@ModelField('integer(7)')
size: string;
getProperty() {
return `${this.color} ${this.size}`;
}
}
@DefineModel('dress')
class Dress {
@PrimaryGenerated()
@ModelField('integer(11)')
id: number;
@Unique()
@ModelField()
name: string; // test if it can infer type
getName() {
return this.name;
}
@ModelField('integer(11)')
@Foreign('dress', 'id')
parentId: number;
@ChildModel()
properties: DressProperty;
}
describe('Model test', () => {
let app: App;
beforeEach(async () => {
app = new App();
registerModel(app, Dress);
app.plugin(MemoryDatabase);
await app.start();
});
it('should register model fields', () => {
const { dress } = app.model.config;
expect(dress.fields.id.type).toBe('integer');
expect(dress.fields.id.length).toBe(11);
expect(dress.fields.name.type).toBe('string');
const colorConfig = dress.fields['properties.color'];
expect(colorConfig.type).toBe('string');
expect(colorConfig.length).toBe(8);
const sizeConfig = dress.fields['properties.size'];
expect(sizeConfig.type).toBe('integer');
expect(sizeConfig.length).toBe(7);
});
it('should register model extras', () => {
const { dress } = app.model.config;
expect(dress.primary[0]).toBe('id');
expect(dress.unique[0][0]).toBe('name');
expect(dress.foreign.parentId).toStrictEqual(['dress', 'id']);
});
it('should make class instance', async () => {
await app.database.upsert('dress', [
{
id: 777,
name: 'Dress of Shigma',
properties: {
color: 'red',
size: 10,
},
},
]);
const [dress] = await app.database.get('dress', { id: 777 });
expect(dress.id).toBe(777);
expect(dress.getName()).toBe('Dress of Shigma');
expect(dress.properties.getProperty()).toBe('red 10');
});
});
{
"compilerOptions": {
"outDir": "dist",
"module": "commonjs",
"target": "es2021",
"esModuleInterop": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"declaration": true,
"sourceMap": true,
"skipLibCheck": true
},
"compileOnSave": true,
"allowJs": true,
"include": [
"*.ts",
"src/**/*.ts",
"test/**/*.ts",
"tests/**/*.ts"
]
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment