Skip to content

Commit

Permalink
refactor(example-todo): use datasource cli generated files and dataso…
Browse files Browse the repository at this point in the history
…urce booters

Signed-off-by: Taranveer Virk <[email protected]>
  • Loading branch information
virkt25 committed Jun 3, 2018
1 parent 2bca363 commit b0a5b3a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 35 deletions.
13 changes: 0 additions & 13 deletions examples/todo/src/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import {ApplicationConfig} from '@loopback/core';
import {RestApplication} from '@loopback/rest';
import {MySequence} from './sequence';
import {db} from './datasources/db.datasource';

/* tslint:disable:no-unused-variable */
// Binding and Booter imports are required to infer types for BootMixin!
Expand Down Expand Up @@ -40,17 +39,5 @@ export class TodoListApplication extends BootMixin(
nested: true,
},
};

this.setupDatasources();
}

setupDatasources() {
// This will allow you to test your application without needing to
// use a "real" datasource!
const datasource =
this.options && this.options.datasource
? new juggler.DataSource(this.options.datasource)
: db;
this.dataSource(datasource);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"name": "db",
"connector": "memory",
"localStorage": "",
"file": "./data/db.json"
}
25 changes: 7 additions & 18 deletions examples/todo/src/datasources/db.datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,11 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import * as path from 'path';
// The juggler reference must exist for consuming code to correctly infer
// type info used in the "db" export (contained in juggler.DataSource).
// tslint:disable-next-line:no-unused-variable
import {juggler} from '@loopback/repository';
import {inject} from '@loopback/core';
import {juggler, DataSource} from '@loopback/repository';

const dsConfigPath = path.resolve(
__dirname,
'../../../config/datasources.json',
);
const config = require(dsConfigPath);

// TODO(bajtos) Ideally, datasources should be created by @loopback/boot
// and registered with the app for dependency injection.
// However, we need to investigate how to access these datasources from
// integration tests where we don't have access to the full app object.
// For example, @loopback/boot can provide a helper function for
// performing a partial boot that creates datasources only.
export const db = new juggler.DataSource(config);
export class DbDataSource extends juggler.DataSource {
constructor(@inject('datasources.config.db') dsConfig: DataSource) {
super(dsConfig);
}
}
6 changes: 6 additions & 0 deletions examples/todo/src/datasources/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright IBM Corp. 2017,2018. All Rights Reserved.
// Node module: @loopback/example-todo
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

export * from './db.datasource';
20 changes: 16 additions & 4 deletions examples/todo/test/acceptance/application.acceptance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {TodoListApplication} from '../../src/application';
import {TodoRepository} from '../../src/repositories/';
import {givenTodo} from '../helpers';
import {Todo} from '../../src/models/';
import {BindingScope} from '@loopback/context';

describe('Application', () => {
let app: TodoListApplication;
Expand All @@ -19,6 +20,21 @@ describe('Application', () => {
before(givenAnApplication);
before(async () => {
await app.boot();

/**
* Override DataSource to not write to file for testing. Since we aren't
* persisting data to file and each injection normally instatiates a new
* instance, we must change the BindingScope to a singleton so only one
* instance is created and used for all injections (preserving access to
* the same memory space).
*/
app.bind('datasources.config.db').to({
name: 'db',
connector: 'memory',
});
app.find('datasources.db')[0].inScope(BindingScope.SINGLETON);

// Start Application
await app.start();
});
before(givenARestServer);
Expand Down Expand Up @@ -109,10 +125,6 @@ describe('Application', () => {
rest: {
port: 0,
},
datasource: {
name: 'db',
connector: 'memory',
},
});
}

Expand Down

0 comments on commit b0a5b3a

Please sign in to comment.