Commit 6385e748 authored by nanahira's avatar nanahira

add @MixinModel

parent 5693e791
......@@ -12,7 +12,7 @@
"@types/koa": "^2.13.4",
"@types/koa__router": "^8.0.11",
"koishi-decorators": "^1.3.4",
"koishi-entities": "^1.0.3",
"koishi-entities": "^1.0.4",
"lodash": "^4.17.21",
"reflect-metadata": "^0.1.13",
"schemastery-gen": "^3.1.2",
......@@ -4798,9 +4798,9 @@
}
},
"node_modules/koishi-entities": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/koishi-entities/-/koishi-entities-1.0.3.tgz",
"integrity": "sha512-E94ucsSCU5zm1ABme1F9gfZOMc9JGGaiUzWrVzTsJJuK3g5na7VszTGdeVk01vePOg3duZ8UvvyKvG8hTGlSMw==",
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/koishi-entities/-/koishi-entities-1.0.4.tgz",
"integrity": "sha512-SjV/Yf8os+r9Hvk2nlYHsn6uhD8SVMW9d9KbBF5DttyBxJdW2r1noKYuD4odGOWZ+78InrX5uX3DfAwmhAkwaw==",
"dependencies": {
"@koishijs/plugin-database-memory": "^1.1.0",
"lodash": "^4.17.21",
......@@ -10497,9 +10497,9 @@
}
},
"koishi-entities": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/koishi-entities/-/koishi-entities-1.0.3.tgz",
"integrity": "sha512-E94ucsSCU5zm1ABme1F9gfZOMc9JGGaiUzWrVzTsJJuK3g5na7VszTGdeVk01vePOg3duZ8UvvyKvG8hTGlSMw==",
"version": "1.0.4",
"resolved": "https://registry.npmjs.org/koishi-entities/-/koishi-entities-1.0.4.tgz",
"integrity": "sha512-SjV/Yf8os+r9Hvk2nlYHsn6uhD8SVMW9d9KbBF5DttyBxJdW2r1noKYuD4odGOWZ+78InrX5uX3DfAwmhAkwaw==",
"requires": {
"@koishijs/plugin-database-memory": "^1.1.0",
"lodash": "^4.17.21",
......
import 'reflect-metadata';
import { App, Context, Selection } from 'koishi';
import { App, Context, Flatten, Keys, Selection, Tables } from 'koishi';
import { Metadata } from './meta/metadata.decorators';
import {
Condition,
......@@ -14,7 +14,7 @@ import {
SystemInjectFun,
} from './def';
import { TopLevelAction } from 'koishi-decorators';
import { ModelClassType, registerModel } from 'koishi-entities';
import { mixinModel, ModelClassType, registerModel } from 'koishi-entities';
// Export all koishi-decorator decorators
......@@ -129,3 +129,11 @@ export const If = <T>(func: Condition<boolean, T>): MethodDecorator =>
export const UseModel = (...models: ModelClassType[]): ClassDecorator =>
TopLevelAction((ctx) => models.forEach((m) => registerModel(ctx, m)));
export const MixinModel = <K extends Keys<Tables>>(
tableName: K,
classDict: {
[F in Keys<Tables[K]>]?: ModelClassType<Flatten<Tables[K][F]>>;
},
): ClassDecorator =>
TopLevelAction((ctx) => mixinModel(ctx, tableName, classDict));
......@@ -6,16 +6,18 @@ import {
PrimaryGenerated,
Unique,
} from 'koishi-entities';
import { UseModel } from '../src/decorators';
import { MixinModel, UseModel } from '../src/decorators';
import { App } from 'koishi';
import { BasePlugin } from '../src/base-plugin';
import { DefinePlugin } from '../src/register';
declare module 'koishi' {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface Tables {
dress: Dress;
}
interface User {
shirt: Wearing;
}
}
class DressProperty {
......@@ -51,6 +53,30 @@ class Dress {
properties: DressProperty;
}
class WearingPreference {
@ModelField('string(8)')
color: string;
@ModelField('string(12)')
shape: string;
format() {
return `${this.color} ${this.shape}`;
}
}
class Wearing {
@ModelField('string(3)')
size: string;
getSize() {
return this.size;
}
@ChildModel()
preference: WearingPreference;
}
@MixinModel('user', { shirt: Wearing })
@UseModel(Dress)
@DefinePlugin()
class MyPlugin extends BasePlugin<any> {}
......@@ -60,5 +86,6 @@ describe('Test of model', () => {
const app = new App();
app.plugin(MyPlugin);
expect(app.model.config.dress.fields.name.type).toBe('string');
expect(app.model.config.user.fields['shirt.size'].type).toBe('string');
});
});
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