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 10, 2020
1 parent 50c3f06 commit ddf1ff9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 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);
});
});
});
29 changes: 21 additions & 8 deletions packages/booter-lb3app/src/lb3app.booter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,24 @@ export class Lb3AppBooter implements Booter {

const dataSources = lb3App.dataSources;
if (dataSources) {
const visited: unknown[] = [];
Object.keys(dataSources).forEach(key => {
const ds = dataSources[key];
if (visited.includes(ds)) return;
visited.push(ds);
this.app.bind(`lb3-datasources.${key}`).to(ds).tag('lb3-datasource');
});
const visitedDs = new Set();
for (const k in dataSources) {
const ds = dataSources[k];
if (visitedDs.has(ds)) continue;
this.app.bind(`lb3-datasources.${k}`).to(ds).tag('lb3-datasource');
visitedDs.add(ds);
}
}

const models = lb3App.models;
if (models) {
const visitedModels = new Set();
for (const k in models) {
const model = models[k];
if (visitedModels.has(model)) continue;
this.app.bind(`lb3-models.${k}`).to(model).tag('lb3-model');
visitedModels.add(model);
}
}

// TODO(bajtos) Listen for the following events to update the OpenAPI spec:
Expand Down Expand Up @@ -148,5 +159,7 @@ export interface Lb3AppBooterOptions {

interface Lb3Application extends ExpressApplication {
handler(name: 'rest'): ExpressRequestHandler;
dataSources?: {[name: string]: unknown};
dataSources?: Record<string, unknown>;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
models?: Record<string, any>;
}

0 comments on commit ddf1ff9

Please sign in to comment.