From f0e9f461d61b56bf5c7c20abc790deb3f4dec599 Mon Sep 17 00:00:00 2001 From: Hage Yaapa Date: Fri, 20 Sep 2019 21:33:59 +0530 Subject: [PATCH] fix: migrate LB3 models mounted on LB4 app Migrates LB4 models mounted on LB4 an app. --- .../booter-lb3app/fixtures/app-with-model.js | 18 ++++++++++++++++++ .../acceptance/booter-lb3app.acceptance.ts | 19 ++++++++++++++++++- packages/booter-lb3app/src/lb3app.booter.ts | 15 +++++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 packages/booter-lb3app/fixtures/app-with-model.js diff --git a/packages/booter-lb3app/fixtures/app-with-model.js b/packages/booter-lb3app/fixtures/app-with-model.js new file mode 100644 index 000000000000..df5ae5d305d6 --- /dev/null +++ b/packages/booter-lb3app/fixtures/app-with-model.js @@ -0,0 +1,18 @@ +'use strict'; + +const loopback = require('loopback'); +const boot = require('loopback-boot'); + +const app = (module.exports = loopback()); + +app.dataSource('memory', {connector: 'memory'}); +const Color = app.registry.createModel('Color', {name: String}); +app.model(Color, {dataSource: 'memory'}); + +app.get('/hello', (req, res) => { + res.send('hello'); +}); + +boot(app, __dirname, function(err) { + if (err) throw err; +}); 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 6563ba4fbb99..b5ed56515128 100644 --- a/packages/booter-lb3app/src/__tests__/acceptance/booter-lb3app.acceptance.ts +++ b/packages/booter-lb3app/src/__tests__/acceptance/booter-lb3app.acceptance.ts @@ -3,7 +3,7 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT -import {OperationObject, OpenApiSpec} from '@loopback/rest'; +import {OpenApiSpec, OperationObject} from '@loopback/rest'; import {Client, expect} from '@loopback/testlab'; import * as _ from 'lodash'; import { @@ -215,4 +215,21 @@ describe('booter-lb3app', () => { expect(createOp.summary).to.eql('just a very simple modification'); }); }); + + context('binding LoopBack 3 datasources', () => { + before(async () => { + ({app, client} = await setupApplication({ + lb3app: {path: '../fixtures/app-with-model'}, + })); + }); + + it('binds datasource to the context', async () => { + const expected = require('../../../fixtures/app-with-model').dataSources + .memory; + const dsBindings = app.findByTag('datasource'); + const key = dsBindings[0].key; + const ds = await app.get(key); + expect(ds).to.eql(expected); + }); + }); }); diff --git a/packages/booter-lb3app/src/lb3app.booter.ts b/packages/booter-lb3app/src/lb3app.booter.ts index 2d6b1c644726..235778d70b35 100644 --- a/packages/booter-lb3app/src/lb3app.booter.ts +++ b/packages/booter-lb3app/src/lb3app.booter.ts @@ -5,6 +5,7 @@ import {BootBindings, Booter} from '@loopback/boot'; import {CoreBindings, inject} from '@loopback/core'; +import {AnyObject, DataSource} from '@loopback/repository'; import { ExpressRequestHandler, OpenApiSpec, @@ -62,6 +63,19 @@ export class Lb3AppBooter implements Booter { this.mountRoutesOnly(lb3App, spec); } + const dataSources = lb3App.dataSources; + if (dataSources) { + const visited: DataSource[] = []; + Object.values(dataSources).forEach(ds => { + if (visited.includes(ds)) return; + visited.push(ds); + this.app + .bind(`datasources.lb3-${ds.name}`) + .to(ds) + .tag('datasource'); + }); + } + // TODO(bajtos) Listen for the following events to update the OpenAPI spec: // - modelRemoted // - modelDeleted @@ -137,4 +151,5 @@ export interface Lb3AppBooterOptions { interface Lb3Application extends ExpressApplication { handler(name: 'rest'): ExpressRequestHandler; + dataSources?: AnyObject; }