Skip to content

Commit

Permalink
refactor(example-todo): use new datasource CLI and booter
Browse files Browse the repository at this point in the history
  • Loading branch information
virkt25 committed Jun 2, 2018
1 parent 3c0ced6 commit a566b7c
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 36 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"
}
30 changes: 7 additions & 23 deletions examples/todo/src/datasources/db.datasource.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
5 changes: 5 additions & 0 deletions examples/todo/src/datasources/index.ts
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit a566b7c

Please sign in to comment.