From ddf1ff996ee216cb2d46e3de336e6722e7cb6722 Mon Sep 17 00:00:00 2001 From: jannyHou Date: Thu, 7 May 2020 14:47:49 -0400 Subject: [PATCH] feat(booter-lb3app): bind lb3 models Co-authored-by: nabdelgadir " --- .../acceptance/booter-lb3app.acceptance.ts | 16 ++++++++++ packages/booter-lb3app/src/lb3app.booter.ts | 29 ++++++++++++++----- 2 files changed, 37 insertions(+), 8 deletions(-) diff --git a/packages/booter-lb3app/src/__tests__/acceptance/booter-lb3app.acceptance.ts b/packages/booter-lb3app/src/__tests__/acceptance/booter-lb3app.acceptance.ts index 557b42d43af4..402418af143d 100644 --- a/packages/booter-lb3app/src/__tests__/acceptance/booter-lb3app.acceptance.ts +++ b/packages/booter-lb3app/src/__tests__/acceptance/booter-lb3app.acceptance.ts @@ -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); + }); + }); }); diff --git a/packages/booter-lb3app/src/lb3app.booter.ts b/packages/booter-lb3app/src/lb3app.booter.ts index 88050609e5dc..585c9115d375 100644 --- a/packages/booter-lb3app/src/lb3app.booter.ts +++ b/packages/booter-lb3app/src/lb3app.booter.ts @@ -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: @@ -148,5 +159,7 @@ export interface Lb3AppBooterOptions { interface Lb3Application extends ExpressApplication { handler(name: 'rest'): ExpressRequestHandler; - dataSources?: {[name: string]: unknown}; + dataSources?: Record; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + models?: Record; }