Skip to content

Commit

Permalink
feat(repository): add model() to RepositoryMixin
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed May 14, 2020
1 parent 0dee61a commit 69d4e5a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
11 changes: 7 additions & 4 deletions packages/boot/src/booters/model.booter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
// 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 {CoreBindings} from '@loopback/core';
import {
ApplicationWithRepositories,
ModelMetadataHelper,
} from '@loopback/repository';
import debugFactory from 'debug';
import {BootBindings} from '../keys';
import {ArtifactOptions, booter} from '../types';
Expand All @@ -26,7 +29,7 @@ const debug = debugFactory('loopback:boot:model-booter');
export class ModelBooter extends BaseArtifactBooter {
constructor(
@inject(CoreBindings.APPLICATION_INSTANCE)
public app: Application,
public app: ApplicationWithRepositories,
@inject(BootBindings.PROJECT_ROOT) projectRoot: string,
@config()
public modelConfig: ArtifactOptions = {},
Expand All @@ -53,7 +56,7 @@ export class ModelBooter extends BaseArtifactBooter {

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');
const binding = this.app.model(cls);
debug('Binding created for model class %s: %j', cls.name, binding);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
Repository,
RepositoryMixin,
} from '../../..';
import {model, property} from '../../../decorators';

describe('RepositoryMixin', () => {
it('mixed class has .repository()', () => {
Expand Down Expand Up @@ -316,3 +317,26 @@ describe('RepositoryMixin dataSource', () => {
}
}
});

describe('RepositoryMixin model', () => {
it('mixes into the target class', () => {
const myApp = new AppWithRepoMixin();
expect(typeof myApp.model).to.be.eql('function');
});

it('binds a model class', () => {
const myApp = new AppWithRepoMixin();
const binding = myApp.model(MyModel);
expect(binding.key).to.eql('models.MyModel');
expect(binding.tagMap).to.have.property('model');
expect(myApp.getSync('models.MyModel')).to.eql(MyModel);
});

@model()
class MyModel {
@property()
name: string;
}

class AppWithRepoMixin extends RepositoryMixin(Application) {}
});
11 changes: 11 additions & 0 deletions packages/repository/src/mixins/repository.mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,16 @@ export function RepositoryMixin<T extends MixinTarget<Application>>(
}
}

/**
* Register a model class
* @param modelClass - Model class
*/
model<M extends Class<unknown>>(modelClass: M) {
return this.bind<M>(`models.${modelClass.name}`)
.to(modelClass)
.tag('model');
}

/**
* Add a component to this application. Also mounts
* all the components repositories.
Expand Down Expand Up @@ -264,6 +274,7 @@ export interface ApplicationWithRepositories extends Application {
dataSource: Class<D> | D,
name?: string,
): Binding<D>;
model<M extends Class<unknown>>(modelClass: M): Binding<M>;
component(component: Class<unknown>, name?: string): Binding;
mountComponentRepositories(component: Class<unknown>): void;
migrateSchema(options?: SchemaMigrationOptions): Promise<void>;
Expand Down

0 comments on commit 69d4e5a

Please sign in to comment.