lib/src/orm/services/Models.ts

34 lines
1.1 KiB
TypeScript
Raw Normal View History

2021-06-02 01:59:40 +00:00
import {Model} from "../model/Model";
import {Instantiable, Singleton, Inject} from "../../di";
import {CommandLine} from "../../cli";
import {model_template} from "../template/model";
import {CanonicalStatic} from "../../service/CanonicalStatic";
import {CanonicalDefinition} from "../../service/Canonical";
/**
* Canonical unit responsible for loading the model classes defined by the application.
*/
@Singleton()
export class Models extends CanonicalStatic<Model<any>, Instantiable<Model<any>>> {
@Inject()
protected readonly cli!: CommandLine
protected appPath = ['models']
protected canonicalItem = 'model'
protected suffix = '.model.js'
public async up() {
await super.up()
this.cli.registerTemplate(model_template)
}
public async initCanonicalItem(definition: CanonicalDefinition): Promise<Instantiable<Model<any>>> {
const item = await super.initCanonicalItem(definition)
if ( !(item.prototype instanceof Model) ) {
throw new TypeError(`Invalid controller definition: ${definition.originalName}. Models must extend from @extollo/orm.Model.`)
}
return item
}
}