Skip to content
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

feat(core): add sugar function for model binding #832

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/core/src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,27 @@ export class Application extends Context {
.tag('controller');
}

/**
* Register a model class with this application.
*
* @param modelCtor {Function} The model class
* (constructor function).
* @return {Binding} The newly created binding, you can use the reference to
* further modify the binding, e.g. lock the value to prevent further
* modifications.
*
* ```ts
* class MyModel {
* }
* app.model(MyModel).lock();
* ```
*/
model(modelCtor: Constructor<{}>): Binding {
return this.bind(`models.${modelCtor.name}`)
.toClass(modelCtor)
.tag('model');
}

/**
* Bind a Server constructor to the Application's master context.
* Each server constructor added in this way must provide a unique prefix
Expand Down
6 changes: 6 additions & 0 deletions packages/core/src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ export function mountComponent(app: Application, component: Component) {
}
}

if (component.models) {
for (const modelCtor of component.models) {
app.model(modelCtor);
}
}

if (component.providers) {
for (const providerKey in component.providers) {
app.bind(providerKey).toProvider(component.providers[providerKey]);
Expand Down
16 changes: 16 additions & 0 deletions packages/core/test/acceptance/application.acceptance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@ describe('Bootstrapping the application', () => {
]);
});

it('registers all models from components', async () => {
class ProductModel {}

class ProductComponent {
models = [ProductModel];
}

const app = new Application({
components: [ProductComponent],
});

expect(app.find('models.*').map(b => b.key)).to.eql([
'models.ProductModel',
]);
});

it('injects component dependencies', () => {
class ConfigComponent {
providers = {
Expand Down
18 changes: 18 additions & 0 deletions packages/core/test/unit/application.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@ describe('Application', () => {
}
});

describe('model binding', () => {
let app: Application;
class MyModel {}

beforeEach(givenApp);

it('binds a model', () => {
const binding = app.model(MyModel);
expect(Array.from(binding.tags)).to.containEql('model');
expect(binding.key).to.equal('models.MyModel');
expect(findKeysByTag(app, 'model')).to.containEql(binding.key);
});

function givenApp() {
app = new Application();
}
});

describe('component binding', () => {
let app: Application;
class MyController {}
Expand Down