lib/src/orm/services/Models.ts

36 lines
1.2 KiB
TypeScript
Raw Normal View History

2021-06-03 03:36:25 +00:00
import {Model} from '../model/Model'
import {Instantiable, Singleton, Inject} from '../../di'
import {CommandLine} from '../../cli'
import {templateModel} from '../template/model'
import {CanonicalStatic} from '../../service/CanonicalStatic'
import {CanonicalDefinition} from '../../service/Canonical'
2021-06-02 01:59:40 +00:00
/**
* 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']
2021-06-03 03:36:25 +00:00
2021-06-02 01:59:40 +00:00
protected canonicalItem = 'model'
2021-06-03 03:36:25 +00:00
2021-06-02 01:59:40 +00:00
protected suffix = '.model.js'
2021-06-03 03:36:25 +00:00
public async up(): Promise<void> {
2021-06-02 01:59:40 +00:00
await super.up()
2021-06-03 03:36:25 +00:00
this.cli.registerTemplate(templateModel)
2021-06-02 01:59:40 +00:00
}
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
}
}