Skip to content

Commit

Permalink
feat(core): add sugar function for model binding
Browse files Browse the repository at this point in the history
  • Loading branch information
shimks committed Jan 4, 2018
1 parent 1b71f05 commit f7c3886
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
27 changes: 27 additions & 0 deletions packages/core/src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ export class Application extends Context {
this.controller(ctor);
}
}

if (options.models) {
for (const ctor of options.models) {
this.model(ctor);
}
}
}

/**
Expand All @@ -66,6 +72,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
32 changes: 32 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,38 @@ describe('Bootstrapping the application', () => {
]);
});

it('registers models from constructor', () => {
class ProductModel {}

const app = new Application({
models: [ProductModel],
});

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

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

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

0 comments on commit f7c3886

Please sign in to comment.