Skip to content
Merged
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
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>;
}