Skip to content

Commit

Permalink
feat(cli): add lifecycle support for datasources
Browse files Browse the repository at this point in the history
Implements #3675
  • Loading branch information
raymondfeng committed Sep 10, 2019
1 parent 7034bb0 commit 8573173
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
26 changes: 24 additions & 2 deletions packages/cli/generators/datasource/templates/datasource.ts.ejs
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import {inject} from '@loopback/core';
import {
inject,
lifeCycleObserver,
LifeCycleObserver,
ValueOrPromise,
} from '@loopback/core';
import {juggler} from '@loopback/repository';
import * as config from './<%= jsonFileName %>';

export class <%= className %>DataSource extends juggler.DataSource {
@lifeCycleObserver('datasource')
export class <%= className %>DataSource extends juggler.DataSource
implements LifeCycleObserver {
static dataSourceName = '<%= name %>';

constructor(
Expand All @@ -11,4 +18,19 @@ export class <%= className %>DataSource extends juggler.DataSource {
) {
super(dsConfig);
}

/**
* Start the datasource when application is started
*/
start(): ValueOrPromise<void> {
// Add your logic here to be invoked when the application is started
}

/**
* Disconnect the datasource when application is stopped. This allows the
* application to be shut down gracefully.
*/
stop(): ValueOrPromise<void> {
return super.disconnect();
}
}
24 changes: 22 additions & 2 deletions packages/cli/test/integration/generators/datasource.integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,24 @@ function checkBasicDataSourceFiles() {
assert.file(expectedIndexFile);
assert.noFile(path.join(SANDBOX_PATH, 'node_modules/memory'));

assert.fileContent(expectedTSFile, /import {inject} from '@loopback\/core';/);
/*
import {
inject,
lifeCycleObserver,
LifeCycleObserver,
ValueOrPromise,
} from '@loopback/core';
*/
assert.fileContent(
expectedTSFile,
`import {
inject,
lifeCycleObserver,
LifeCycleObserver,
ValueOrPromise,
} from '@loopback/core';`,
);

assert.fileContent(
expectedTSFile,
/import {juggler} from '@loopback\/repository';/,
Expand All @@ -168,10 +185,12 @@ function checkBasicDataSourceFiles() {
expectedTSFile,
/import \* as config from '.\/ds.datasource.json';/,
);
assert.fileContent(expectedTSFile, /@lifeCycleObserver\('datasource'\)/);
assert.fileContent(
expectedTSFile,
/export class DsDataSource extends juggler.DataSource {/,
/export class DsDataSource extends juggler.DataSource/,
);
assert.fileContent(expectedTSFile, /implements LifeCycleObserver {/);
assert.fileContent(expectedTSFile, /static dataSourceName = 'ds';/);
assert.fileContent(expectedTSFile, /constructor\(/);
assert.fileContent(
Expand All @@ -180,6 +199,7 @@ function checkBasicDataSourceFiles() {
);
assert.fileContent(expectedTSFile, /\) \{/);
assert.fileContent(expectedTSFile, /super\(dsConfig\);/);
assert.fileContent(expectedTSFile, /stop\(\)\: ValueOrPromise<void>/);

assert.fileContent(expectedIndexFile, /export \* from '.\/ds.datasource';/);
}

0 comments on commit 8573173

Please sign in to comment.