Skip to content

Commit

Permalink
feat(booter-lb3app): bind lb3 models
Browse files Browse the repository at this point in the history
Co-authored-by: nabdelgadir <[email protected]>"
  • Loading branch information
jannyHou committed May 7, 2020
1 parent ed39551 commit b004029
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,20 @@ describe('booter-lb3app', () => {
expect(ds).to.eql(expected);
});
});

context('binding LoopBack 3 models', () => {
before(async () => {
({app, client} = await setupApplication({
lb3app: {path: '../fixtures/app-with-model'},
}));
});

it('binds model to the context', async () => {
const expected = require('../../../fixtures/app-with-model').models.Color;
const modelBindings = app.findByTag('lb3-model');
const key = modelBindings[0].key;
const model = await app.get(key);
expect(model).to.eql(expected);
});
});
});
13 changes: 13 additions & 0 deletions packages/booter-lb3app/src/lb3app.booter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,17 @@ export class Lb3AppBooter implements Booter {
});
}

const models = lb3App.models;
if (models) {
const visited: unknown[] = [];
Object.keys(models).forEach(key => {
const model = models[key];
if (visited.includes(model)) return;
visited.push(model);
this.app.bind(`lb3-models.${key}`).to(model).tag('lb3-model');
});
}

// TODO(bajtos) Listen for the following events to update the OpenAPI spec:
// - modelRemoted
// - modelDeleted
Expand Down Expand Up @@ -149,4 +160,6 @@ export interface Lb3AppBooterOptions {
interface Lb3Application extends ExpressApplication {
handler(name: 'rest'): ExpressRequestHandler;
dataSources?: {[name: string]: unknown};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
models?: {[name: string]: any};
}

0 comments on commit b004029

Please sign in to comment.