Commit 79a9f00f authored by nanahira's avatar nanahira

refname type

parent b2e76bbe
import { ClassType, ModelFieldDef, PrimaryKeyDef } from './def'; import { ModelClassType, ModelFieldDef } from './def';
import { Metadata } from './meta/meta'; import { Metadata } from './meta/meta';
import { Flatten, Keys, Model, Tables } from 'koishi'; import { Flatten, Keys, Tables } from 'koishi';
import { inferType } from './utils'; import { inferType } from './utils';
export const DefineModel = (name: Keys<Tables>): ClassDecorator => export const DefineModel = (name: Keys<Tables>): ClassDecorator =>
...@@ -32,7 +32,7 @@ export const Unique = ( ...@@ -32,7 +32,7 @@ export const Unique = (
): PropertyDecorator => Metadata.append('ModelUnique', identifier); ): PropertyDecorator => Metadata.append('ModelUnique', identifier);
export const ChildModel = export const ChildModel =
(cls?: ClassType): PropertyDecorator => (cls?: ModelClassType): PropertyDecorator =>
(obj, key) => { (obj, key) => {
if (!cls) { if (!cls) {
cls = Reflect.getMetadata('design:type', obj, key); cls = Reflect.getMetadata('design:type', obj, key);
......
...@@ -4,7 +4,7 @@ export type ModelFieldDef<T = any> = ...@@ -4,7 +4,7 @@ export type ModelFieldDef<T = any> =
| Model.Field<T> | Model.Field<T>
| Model.Field.Shorthand<Model.Field.Type<T>>; | Model.Field.Shorthand<Model.Field.Type<T>>;
export type ClassType<T = any> = { new (...args: any[]): T }; export type ModelClassType<T = any> = { new (...args: any[]): T };
export interface PrimaryKeyDef { export interface PrimaryKeyDef {
autoIncrement: boolean; autoIncrement: boolean;
......
import { ClassType, ModelFieldDef, PrimaryKeyDef } from '../def'; import { ModelClassType, ModelFieldDef, PrimaryKeyDef } from '../def';
import { Keys, Tables } from 'koishi'; import { Keys, Tables } from 'koishi';
export interface MetadataArrayMap { export interface MetadataArrayMap {
...@@ -12,5 +12,5 @@ export interface MetadataMap { ...@@ -12,5 +12,5 @@ export interface MetadataMap {
ModelTableName: Keys<Tables>; ModelTableName: Keys<Tables>;
ModelPrimaryKey: PrimaryKeyDef; ModelPrimaryKey: PrimaryKeyDef;
ModelForeignKey: [string, string]; ModelForeignKey: [string, string];
ChildModel: ClassType; ChildModel: ModelClassType;
} }
import { Context, Keys, Model } from 'koishi'; import { Context, Keys, Model } from 'koishi';
import { ClassType } from './def'; import { ModelClassType } from './def';
import { reflector } from './meta/meta'; import { reflector } from './meta/meta';
class ModelRegistrar<T = any> { class ModelRegistrar<T = any> {
constructor(private cls: ClassType<T>, private prefix = '') {} constructor(private cls: ModelClassType<T>, private prefix = '') {}
getTableName() { getTableName() {
return reflector.get('ModelTableName', this.cls); return reflector.get('ModelTableName', this.cls);
...@@ -106,7 +106,7 @@ class ModelRegistrar<T = any> { ...@@ -106,7 +106,7 @@ class ModelRegistrar<T = any> {
getChildDict() { getChildDict() {
const keys = reflector.getArray('ChildModelKeys', this.cls); const keys = reflector.getArray('ChildModelKeys', this.cls);
const result: { [K in keyof T]?: ClassType<T> } = {}; const result: { [K in keyof T]?: ModelClassType<T> } = {};
for (const key of keys) { for (const key of keys) {
const child = reflector.get('ChildModel', this.cls, key); const child = reflector.get('ChildModel', this.cls, key);
if (child) { if (child) {
......
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