Skip to content

Commit

Permalink
fix: migrate LB3 models mounted on LB4 app
Browse files Browse the repository at this point in the history
Migrates LB4 models mounted on LB4 an app.
  • Loading branch information
Hage Yaapa committed Sep 24, 2019
1 parent 7bf6f12 commit f0e9f46
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
18 changes: 18 additions & 0 deletions 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;
});
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
});
});
});
15 changes: 15 additions & 0 deletions packages/booter-lb3app/src/lb3app.booter.ts
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -137,4 +151,5 @@ export interface Lb3AppBooterOptions {

interface Lb3Application extends ExpressApplication {
handler(name: 'rest'): ExpressRequestHandler;
dataSources?: AnyObject;
}

0 comments on commit f0e9f46

Please sign in to comment.