From a566b7cd7d1ac94511325986389ff498f725030d Mon Sep 17 00:00:00 2001 From: Taranveer Virk Date: Wed, 30 May 2018 20:04:32 -0400 Subject: [PATCH] refactor(example-todo): use new datasource CLI and booter --- examples/todo/src/application.ts | 13 -------- .../datasources/db.datasource.json} | 1 + .../todo/src/datasources/db.datasource.ts | 30 +++++-------------- examples/todo/src/datasources/index.ts | 5 ++++ 4 files changed, 13 insertions(+), 36 deletions(-) rename examples/todo/{config/datasources.json => src/datasources/db.datasource.json} (76%) create mode 100644 examples/todo/src/datasources/index.ts diff --git a/examples/todo/src/application.ts b/examples/todo/src/application.ts index 601a1ebb86d0..f7de4dd1192f 100644 --- a/examples/todo/src/application.ts +++ b/examples/todo/src/application.ts @@ -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! @@ -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); } } diff --git a/examples/todo/config/datasources.json b/examples/todo/src/datasources/db.datasource.json similarity index 76% rename from examples/todo/config/datasources.json rename to examples/todo/src/datasources/db.datasource.json index 1c62a6d15e0f..a68f220be986 100644 --- a/examples/todo/config/datasources.json +++ b/examples/todo/src/datasources/db.datasource.json @@ -1,5 +1,6 @@ { "name": "db", "connector": "memory", + "localStorage": "", "file": "./data/db.json" } diff --git a/examples/todo/src/datasources/db.datasource.ts b/examples/todo/src/datasources/db.datasource.ts index 85034f7b6f84..6e48a1db72cc 100644 --- a/examples/todo/src/datasources/db.datasource.ts +++ b/examples/todo/src/datasources/db.datasource.ts @@ -1,24 +1,8 @@ -// 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 +import {inject} from '@loopback/core'; +import {juggler, DataSource} from '@loopback/repository'; -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'; - -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); + } +} diff --git a/examples/todo/src/datasources/index.ts b/examples/todo/src/datasources/index.ts new file mode 100644 index 000000000000..199d4c4f5c44 --- /dev/null +++ b/examples/todo/src/datasources/index.ts @@ -0,0 +1,5 @@ +// This is an auto-generated file. DO NOT EDIT. + +export * from "./db.datasource"; + +// This is an auto-generated file. DO NOT EDIT.