-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(cli): bind model classes during boot #5378
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| // Copyright IBM Corp. 2020. All Rights Reserved. | ||
| // Node module: @loopback/boot | ||
| // This file is licensed under the MIT License. | ||
| // License text available at https://opensource.org/licenses/MIT | ||
|
|
||
| import {Entity, Model} from '@loopback/repository'; | ||
|
|
||
| export class Model1 extends Model {} | ||
|
|
||
| export class Model2 extends Entity {} |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| // Copyright IBM Corp. 2019,2020. All Rights Reserved. | ||
| // Node module: @loopback/boot | ||
| // This file is licensed under the MIT License. | ||
| // License text available at https://opensource.org/licenses/MIT | ||
|
|
||
| import {expect, TestSandbox} from '@loopback/testlab'; | ||
| import {resolve} from 'path'; | ||
| import {BooterApp} from '../fixtures/application'; | ||
|
|
||
| describe('repository booter integration tests', () => { | ||
| const sandbox = new TestSandbox(resolve(__dirname, '../../.sandbox')); | ||
|
|
||
| const MODELS_TAG = 'model'; | ||
|
|
||
| let app: BooterApp; | ||
|
|
||
| beforeEach('reset sandbox', () => sandbox.reset()); | ||
| beforeEach(getApp); | ||
|
|
||
| it('boots repositories when app.boot() is called', async () => { | ||
| const expectedBindings = [ | ||
| 'models.Model1', | ||
| 'models.Model2', | ||
| 'models.NoEntity', | ||
| 'models.Product', | ||
| ]; | ||
|
|
||
| await app.boot(); | ||
|
|
||
| const bindings = app.findByTag(MODELS_TAG).map(b => b.key); | ||
| expect(bindings.sort()).to.eql(expectedBindings.sort()); | ||
| }); | ||
|
|
||
| async function getApp() { | ||
| await sandbox.copyFile(resolve(__dirname, '../fixtures/application.js')); | ||
| await sandbox.copyFile( | ||
| resolve(__dirname, '../fixtures/no-entity.model.js'), | ||
| 'models/no-entity.model.js', | ||
| ); | ||
| await sandbox.copyFile( | ||
| resolve(__dirname, '../fixtures/product.model.js'), | ||
| 'models/product.model.js', | ||
| ); | ||
|
|
||
| await sandbox.copyFile( | ||
| resolve(__dirname, '../fixtures/multiple-models.model.js'), | ||
| 'models/multiple-models.model.js', | ||
| ); | ||
|
|
||
| const MyApp = require(resolve(sandbox.path, 'application.js')).BooterApp; | ||
| app = new MyApp(); | ||
| } | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| // Copyright IBM Corp. 2020. All Rights Reserved. | ||
| // Node module: @loopback/boot | ||
| // This file is licensed under the MIT License. | ||
| // License text available at https://opensource.org/licenses/MIT | ||
|
|
||
| import {config, Constructor, inject} from '@loopback/context'; | ||
| import {Application, CoreBindings} from '@loopback/core'; | ||
| import {ModelMetadataHelper} from '@loopback/repository'; | ||
| import debugFactory from 'debug'; | ||
| import {BootBindings} from '../keys'; | ||
| import {ArtifactOptions, booter} from '../types'; | ||
| import {BaseArtifactBooter} from './base-artifact.booter'; | ||
|
|
||
| const debug = debugFactory('loopback:boot:model-booter'); | ||
|
|
||
| /** | ||
| * A class that extends BaseArtifactBooter to boot the 'Model' artifact type. | ||
| * | ||
| * Supported phases: configure, discover, load | ||
| * | ||
| * @param app - Application instance | ||
| * @param projectRoot - Root of User Project relative to which all paths are resolved | ||
| * @param bootConfig - Model Artifact Options Object | ||
| */ | ||
| @booter('models') | ||
| export class ModelBooter extends BaseArtifactBooter { | ||
| constructor( | ||
| @inject(CoreBindings.APPLICATION_INSTANCE) | ||
| public app: Application, | ||
| @inject(BootBindings.PROJECT_ROOT) projectRoot: string, | ||
| @config() | ||
| public modelConfig: ArtifactOptions = {}, | ||
| ) { | ||
| super( | ||
| projectRoot, | ||
| // Set Model Booter Options if passed in via bootConfig | ||
| Object.assign({}, ModelDefaults, modelConfig), | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Uses super method to get a list of Artifact classes. Boot each file by | ||
| * creating a DataSourceConstructor and binding it to the application class. | ||
| */ | ||
| async load() { | ||
| await super.load(); | ||
|
|
||
| for (const cls of this.classes) { | ||
| if (!isModelClass(cls)) { | ||
| debug('Skipping class %s - no @model is found', cls.name); | ||
hacksparrow marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| continue; | ||
| } | ||
|
|
||
| debug('Bind class: %s', cls.name); | ||
| // We are binding the model class itself | ||
| const binding = this.app.bind(`models.${cls.name}`).to(cls).tag('model'); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @raymondfeng can you please extract the code building the binding to a standalone function that can be used to bind models manually, in cases where const binding = createModelClassBinding(cls);
app.add(binding);Similar helpers we already have: |
||
| debug('Binding created for model class %s: %j', cls.name, binding); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Default ArtifactOptions for DataSourceBooter. | ||
| */ | ||
| export const ModelDefaults: ArtifactOptions = { | ||
| dirs: ['models'], | ||
| extensions: ['.model.js'], | ||
achrinza marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| nested: true, | ||
| }; | ||
|
|
||
| function isModelClass(cls: Constructor<unknown>) { | ||
| return ModelMetadataHelper.getModelMetadata(cls) != null; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.