Commit 1dacfd82 authored by nanahira's avatar nanahira

add koishi-entities and UseModel

parent 8343ac76
......@@ -4,3 +4,4 @@ export * from './src/decorators';
export * from './src/base-plugin';
export * from './src/def/interfaces';
export * from 'schemastery-gen';
export * from 'koishi-entities';
......@@ -12,6 +12,7 @@
"@types/koa": "^2.13.4",
"@types/koa__router": "^8.0.11",
"koishi-decorators": "^1.3.4",
"koishi-entities": "^1.0.2",
"lodash": "^4.17.21",
"reflect-metadata": "^0.1.13",
"schemastery-gen": "^3.1.2",
......@@ -1044,7 +1045,6 @@
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/@koishijs/orm/-/orm-1.0.0.tgz",
"integrity": "sha512-uWSOVCF2THR/S4PPS5FsvsYNhWcPIHO6pXVlSckV5GmJMDQ4zAppSi4s6MwvBHPRX5CMjicK/T2huUlEA/srOQ==",
"peer": true,
"dependencies": {
"@koishijs/utils": "^5.2.1"
},
......@@ -1065,17 +1065,26 @@
"koishi": "^4.4.2"
}
},
"node_modules/@koishijs/plugin-database-memory": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/@koishijs/plugin-database-memory/-/plugin-database-memory-1.1.0.tgz",
"integrity": "sha512-l6dB+HD5V7prbabdsrgZruOdkbWuv2viobcU2LCJaeTjhhqvchKFYv7SDGu4Ohz4csD/v4Y+qOELbXUar56N+Q==",
"dependencies": {
"@koishijs/orm": "^1.0.0"
},
"peerDependencies": {
"koishi": "^4.5.0"
}
},
"node_modules/@koishijs/segment": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/@koishijs/segment/-/segment-1.1.1.tgz",
"integrity": "sha512-HmLRKJiIX++U+ow+RP0BVGwgAFzDRqwHusUzjoZcZdcnG8yudTsvB6MXnzBMnBL7k9LBxUlfB5P8ukkM1roZFQ==",
"peer": true
"integrity": "sha512-HmLRKJiIX++U+ow+RP0BVGwgAFzDRqwHusUzjoZcZdcnG8yudTsvB6MXnzBMnBL7k9LBxUlfB5P8ukkM1roZFQ=="
},
"node_modules/@koishijs/utils": {
"version": "5.2.1",
"resolved": "https://registry.npmjs.org/@koishijs/utils/-/utils-5.2.1.tgz",
"integrity": "sha512-Yu+QzmpStHAx5ZknSZhp9Zv3rfy6M4b1tuU3Wh8D3gTgZPeo+gqh07X7GyN7SA76Nr472rN8+hDqq5rafuA9iQ==",
"peer": true,
"dependencies": {
"@koishijs/segment": "^1.1.1",
"schemastery": "^3.1.1",
......@@ -1086,7 +1095,6 @@
"version": "8.1.1",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-8.1.1.tgz",
"integrity": "sha512-MpUEN2OodtUzxvKQl72cUF7RQ5EiHsGvSsVG0ia9c5RbWGL2CI4C7EpPS8UTBIplnlzZiNuV56w+FuNxy3ty2Q==",
"peer": true,
"dependencies": {
"has-flag": "^4.0.0"
},
......@@ -4789,6 +4797,19 @@
"koishi": "^4.5.0"
}
},
"node_modules/koishi-entities": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/koishi-entities/-/koishi-entities-1.0.2.tgz",
"integrity": "sha512-b5p4Tdb77ie1JFJgsYmIvZXB6CInUgIX9U7IbmDBg+ka9mfKClWmZXiYCBCuNTW71rQXsBVDdypKfm+iE0rhXQ==",
"dependencies": {
"@koishijs/plugin-database-memory": "^1.1.0",
"lodash": "^4.17.21",
"typed-reflector": "^1.0.10"
},
"peerDependencies": {
"koishi": "^4.5.0"
}
},
"node_modules/leven": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/leven/-/leven-3.1.0.tgz",
......@@ -5795,8 +5816,7 @@
"node_modules/schemastery": {
"version": "3.1.1",
"resolved": "https://registry.npmjs.org/schemastery/-/schemastery-3.1.1.tgz",
"integrity": "sha512-TdbphUvailcX1FqxSq3sqlxdFx/HhBD7TXliq1r4Su7BTChRQimHvrauK/Q75jLVCYaJPACDU92s0JTovmVrtA==",
"peer": true
"integrity": "sha512-TdbphUvailcX1FqxSq3sqlxdFx/HhBD7TXliq1r4Su7BTChRQimHvrauK/Q75jLVCYaJPACDU92s0JTovmVrtA=="
},
"node_modules/schemastery-gen": {
"version": "3.1.2",
......@@ -7587,7 +7607,6 @@
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/@koishijs/orm/-/orm-1.0.0.tgz",
"integrity": "sha512-uWSOVCF2THR/S4PPS5FsvsYNhWcPIHO6pXVlSckV5GmJMDQ4zAppSi4s6MwvBHPRX5CMjicK/T2huUlEA/srOQ==",
"peer": true,
"requires": {
"@koishijs/utils": "^5.2.1"
}
......@@ -7602,17 +7621,23 @@
"ws": "^8.5.0"
}
},
"@koishijs/plugin-database-memory": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/@koishijs/plugin-database-memory/-/plugin-database-memory-1.1.0.tgz",
"integrity": "sha512-l6dB+HD5V7prbabdsrgZruOdkbWuv2viobcU2LCJaeTjhhqvchKFYv7SDGu4Ohz4csD/v4Y+qOELbXUar56N+Q==",
"requires": {
"@koishijs/orm": "^1.0.0"
}
},
"@koishijs/segment": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/@koishijs/segment/-/segment-1.1.1.tgz",
"integrity": "sha512-HmLRKJiIX++U+ow+RP0BVGwgAFzDRqwHusUzjoZcZdcnG8yudTsvB6MXnzBMnBL7k9LBxUlfB5P8ukkM1roZFQ==",
"peer": true
"integrity": "sha512-HmLRKJiIX++U+ow+RP0BVGwgAFzDRqwHusUzjoZcZdcnG8yudTsvB6MXnzBMnBL7k9LBxUlfB5P8ukkM1roZFQ=="
},
"@koishijs/utils": {
"version": "5.2.1",
"resolved": "https://registry.npmjs.org/@koishijs/utils/-/utils-5.2.1.tgz",
"integrity": "sha512-Yu+QzmpStHAx5ZknSZhp9Zv3rfy6M4b1tuU3Wh8D3gTgZPeo+gqh07X7GyN7SA76Nr472rN8+hDqq5rafuA9iQ==",
"peer": true,
"requires": {
"@koishijs/segment": "^1.1.1",
"schemastery": "^3.1.1",
......@@ -7623,7 +7648,6 @@
"version": "8.1.1",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-8.1.1.tgz",
"integrity": "sha512-MpUEN2OodtUzxvKQl72cUF7RQ5EiHsGvSsVG0ia9c5RbWGL2CI4C7EpPS8UTBIplnlzZiNuV56w+FuNxy3ty2Q==",
"peer": true,
"requires": {
"has-flag": "^4.0.0"
}
......@@ -10472,6 +10496,16 @@
"typed-reflector": "^1.0.10"
}
},
"koishi-entities": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/koishi-entities/-/koishi-entities-1.0.2.tgz",
"integrity": "sha512-b5p4Tdb77ie1JFJgsYmIvZXB6CInUgIX9U7IbmDBg+ka9mfKClWmZXiYCBCuNTW71rQXsBVDdypKfm+iE0rhXQ==",
"requires": {
"@koishijs/plugin-database-memory": "^1.1.0",
"lodash": "^4.17.21",
"typed-reflector": "^1.0.10"
}
},
"leven": {
"version": "3.1.0",
"resolved": "https://registry.npmjs.org/leven/-/leven-3.1.0.tgz",
......@@ -11217,8 +11251,7 @@
"schemastery": {
"version": "3.1.1",
"resolved": "https://registry.npmjs.org/schemastery/-/schemastery-3.1.1.tgz",
"integrity": "sha512-TdbphUvailcX1FqxSq3sqlxdFx/HhBD7TXliq1r4Su7BTChRQimHvrauK/Q75jLVCYaJPACDU92s0JTovmVrtA==",
"peer": true
"integrity": "sha512-TdbphUvailcX1FqxSq3sqlxdFx/HhBD7TXliq1r4Su7BTChRQimHvrauK/Q75jLVCYaJPACDU92s0JTovmVrtA=="
},
"schemastery-gen": {
"version": "3.1.2",
......
......@@ -13,6 +13,8 @@ import {
ProvideOptions,
SystemInjectFun,
} from './def';
import { TopLevelAction } from 'koishi-decorators';
import { ModelClassType, registerModel } from 'koishi-entities';
// Export all koishi-decorator decorators
......@@ -124,3 +126,6 @@ export function UsingService(
export const If = <T>(func: Condition<boolean, T>): MethodDecorator =>
Metadata.append('KoishiIf', func);
export const UseModel = (...models: ModelClassType[]): ClassDecorator =>
TopLevelAction((ctx) => models.forEach((m) => registerModel(ctx, m)));
import {
ChildModel,
DefineModel,
Foreign,
ModelField,
PrimaryGenerated,
Unique,
} from 'koishi-entities';
import { 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;
}
}
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;
}
@UseModel(Dress)
@DefinePlugin()
class MyPlugin extends BasePlugin<any> {}
describe('Test of model', () => {
it('should register model', async () => {
const app = new App();
app.plugin(MyPlugin);
expect(app.model.config.dress.fields.name.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