From bc740e062ec28878c18980c24e3a1c0917af63ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Mon, 27 Apr 2020 14:25:44 +0200 Subject: [PATCH] feat: remove hand-written datasource `stop()` method MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use the `stop()` method provided by `juggler.DataSource` instead. Signed-off-by: Miroslav Bajtoš --- docs/site/OpenAPI-generator.md | 26 +--- .../src/datasources/db.datasource.ts | 22 +-- .../src/datasources/db1.datasource.ts | 22 +-- .../src/datasources/db2.datasource.ts | 22 +-- .../src/datasources/ds.datasource.ts | 22 +-- .../datasource/templates/datasource.ts.ejs | 26 +--- .../datasource.integration.snapshots.js | 130 ++++-------------- .../openapi-client.integration.snapshots.js | 52 ++----- .../src/datasources/pet-store.datasource.ts | 26 +--- 9 files changed, 54 insertions(+), 294 deletions(-) diff --git a/docs/site/OpenAPI-generator.md b/docs/site/OpenAPI-generator.md index 171966bb90e8..6c935bb6ab79 100644 --- a/docs/site/OpenAPI-generator.md +++ b/docs/site/OpenAPI-generator.md @@ -401,12 +401,7 @@ connection to the endpoint that exposes an OpenAPI spec. %} ```ts -import { - inject, - lifeCycleObserver, - LifeCycleObserver, - ValueOrPromise, -} from '@loopback/core'; +import {inject, lifeCycleObserver, LifeCycleObserver} from '@loopback/core'; import {juggler} from '@loopback/repository'; const config = { @@ -417,6 +412,10 @@ const config = { positional: true, }; +// Observe application's life cycle to disconnect the datasource when +// application is stopped. This allows the application to be shut down +// gracefully. The `stop()` method is inherited from `juggler.DataSource`. +// Learn more at https://loopback.io/doc/en/lb4/Life-cycle.html @lifeCycleObserver('datasource') export class Test2DataSource extends juggler.DataSource implements LifeCycleObserver { @@ -429,21 +428,6 @@ export class Test2DataSource extends juggler.DataSource ) { super(dsConfig); } - - /** - * Start the datasource when application is started - */ - start(): ValueOrPromise { - // 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 { - return super.disconnect(); - } } ``` diff --git a/examples/multi-tenancy/src/datasources/db.datasource.ts b/examples/multi-tenancy/src/datasources/db.datasource.ts index a12d7875b123..4f8298ba4d4e 100644 --- a/examples/multi-tenancy/src/datasources/db.datasource.ts +++ b/examples/multi-tenancy/src/datasources/db.datasource.ts @@ -1,9 +1,4 @@ -import { - inject, - lifeCycleObserver, - LifeCycleObserver, - ValueOrPromise, -} from '@loopback/core'; +import {inject, lifeCycleObserver, LifeCycleObserver} from '@loopback/core'; import {juggler} from '@loopback/repository'; const config = { @@ -25,19 +20,4 @@ export class DbDataSource extends juggler.DataSource ) { super(dsConfig); } - - /** - * Start the datasource when application is started - */ - start(): ValueOrPromise { - // 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 { - return super.disconnect(); - } } diff --git a/examples/multi-tenancy/src/datasources/db1.datasource.ts b/examples/multi-tenancy/src/datasources/db1.datasource.ts index f5f29ee08546..c6e61e1212da 100644 --- a/examples/multi-tenancy/src/datasources/db1.datasource.ts +++ b/examples/multi-tenancy/src/datasources/db1.datasource.ts @@ -1,9 +1,4 @@ -import { - inject, - lifeCycleObserver, - LifeCycleObserver, - ValueOrPromise, -} from '@loopback/core'; +import {inject, lifeCycleObserver, LifeCycleObserver} from '@loopback/core'; import {juggler} from '@loopback/repository'; const config = { @@ -23,19 +18,4 @@ export class Db1DataSource extends juggler.DataSource ) { super(dsConfig); } - - /** - * Start the datasource when application is started - */ - start(): ValueOrPromise { - // 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 { - return super.disconnect(); - } } diff --git a/examples/multi-tenancy/src/datasources/db2.datasource.ts b/examples/multi-tenancy/src/datasources/db2.datasource.ts index 4575e0a22022..6dc167b2f03f 100644 --- a/examples/multi-tenancy/src/datasources/db2.datasource.ts +++ b/examples/multi-tenancy/src/datasources/db2.datasource.ts @@ -1,9 +1,4 @@ -import { - inject, - lifeCycleObserver, - LifeCycleObserver, - ValueOrPromise, -} from '@loopback/core'; +import {inject, lifeCycleObserver, LifeCycleObserver} from '@loopback/core'; import {juggler} from '@loopback/repository'; const config = { @@ -23,19 +18,4 @@ export class Db2DataSource extends juggler.DataSource ) { super(dsConfig); } - - /** - * Start the datasource when application is started - */ - start(): ValueOrPromise { - // 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 { - return super.disconnect(); - } } diff --git a/examples/validation-app/src/datasources/ds.datasource.ts b/examples/validation-app/src/datasources/ds.datasource.ts index 6f5371984219..8103fd5b9c82 100644 --- a/examples/validation-app/src/datasources/ds.datasource.ts +++ b/examples/validation-app/src/datasources/ds.datasource.ts @@ -3,12 +3,7 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT -import { - inject, - lifeCycleObserver, - LifeCycleObserver, - ValueOrPromise, -} from '@loopback/core'; +import {inject, lifeCycleObserver, LifeCycleObserver} from '@loopback/core'; import {juggler} from '@loopback/repository'; const config = { @@ -30,19 +25,4 @@ export class DsDataSource extends juggler.DataSource ) { super(dsConfig); } - - /** - * Start the datasource when application is started - */ - start(): ValueOrPromise { - // 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 { - return super.disconnect(); - } } diff --git a/packages/cli/generators/datasource/templates/datasource.ts.ejs b/packages/cli/generators/datasource/templates/datasource.ts.ejs index 6f2c1b35e58a..76c638f5aba6 100644 --- a/packages/cli/generators/datasource/templates/datasource.ts.ejs +++ b/packages/cli/generators/datasource/templates/datasource.ts.ejs @@ -1,13 +1,12 @@ -import { - inject, - lifeCycleObserver, - LifeCycleObserver, - ValueOrPromise, -} from '@loopback/core'; +import {inject, lifeCycleObserver, LifeCycleObserver} from '@loopback/core'; import {juggler} from '@loopback/repository'; const config = <%- dsConfigString %>; +// Observe application's life cycle to disconnect the datasource when +// application is stopped. This allows the application to be shut down +// gracefully. The `stop()` method is inherited from `juggler.DataSource`. +// Learn more at https://loopback.io/doc/en/lb4/Life-cycle.html @lifeCycleObserver('datasource') export class <%= className %>DataSource extends juggler.DataSource implements LifeCycleObserver { @@ -20,19 +19,4 @@ export class <%= className %>DataSource extends juggler.DataSource ) { super(dsConfig); } - - /** - * Start the datasource when application is started - */ - start(): ValueOrPromise { - // 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 { - return super.disconnect(); - } } diff --git a/packages/cli/snapshots/integration/generators/datasource.integration.snapshots.js b/packages/cli/snapshots/integration/generators/datasource.integration.snapshots.js index 0994a570985e..d61e5f47a64f 100644 --- a/packages/cli/snapshots/integration/generators/datasource.integration.snapshots.js +++ b/packages/cli/snapshots/integration/generators/datasource.integration.snapshots.js @@ -8,12 +8,7 @@ 'use strict'; exports[`lb4 datasource integration basic datasource scaffolds correct file with args 1`] = ` -import { - inject, - lifeCycleObserver, - LifeCycleObserver, - ValueOrPromise, -} from '@loopback/core'; +import {inject, lifeCycleObserver, LifeCycleObserver} from '@loopback/core'; import {juggler} from '@loopback/repository'; const config = { @@ -23,6 +18,10 @@ const config = { file: undefined }; +// Observe application's life cycle to disconnect the datasource when +// application is stopped. This allows the application to be shut down +// gracefully. The \`stop()\` method is inherited from \`juggler.DataSource\`. +// Learn more at https://loopback.io/doc/en/lb4/Life-cycle.html @lifeCycleObserver('datasource') export class DsDataSource extends juggler.DataSource implements LifeCycleObserver { @@ -35,33 +34,13 @@ export class DsDataSource extends juggler.DataSource ) { super(dsConfig); } - - /** - * Start the datasource when application is started - */ - start(): ValueOrPromise { - // 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 { - return super.disconnect(); - } } `; exports[`lb4 datasource integration basic datasource scaffolds correct file with input 1`] = ` -import { - inject, - lifeCycleObserver, - LifeCycleObserver, - ValueOrPromise, -} from '@loopback/core'; +import {inject, lifeCycleObserver, LifeCycleObserver} from '@loopback/core'; import {juggler} from '@loopback/repository'; const config = { @@ -71,6 +50,10 @@ const config = { file: undefined }; +// Observe application's life cycle to disconnect the datasource when +// application is stopped. This allows the application to be shut down +// gracefully. The \`stop()\` method is inherited from \`juggler.DataSource\`. +// Learn more at https://loopback.io/doc/en/lb4/Life-cycle.html @lifeCycleObserver('datasource') export class DsDataSource extends juggler.DataSource implements LifeCycleObserver { @@ -83,33 +66,13 @@ export class DsDataSource extends juggler.DataSource ) { super(dsConfig); } - - /** - * Start the datasource when application is started - */ - start(): ValueOrPromise { - // 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 { - return super.disconnect(); - } } `; exports[`lb4 datasource integration correctly coerces setting input of type number 1`] = ` -import { - inject, - lifeCycleObserver, - LifeCycleObserver, - ValueOrPromise, -} from '@loopback/core'; +import {inject, lifeCycleObserver, LifeCycleObserver} from '@loopback/core'; import {juggler} from '@loopback/repository'; const config = { @@ -124,6 +87,10 @@ const config = { schema: undefined }; +// Observe application's life cycle to disconnect the datasource when +// application is stopped. This allows the application to be shut down +// gracefully. The \`stop()\` method is inherited from \`juggler.DataSource\`. +// Learn more at https://loopback.io/doc/en/lb4/Life-cycle.html @lifeCycleObserver('datasource') export class DsDataSource extends juggler.DataSource implements LifeCycleObserver { @@ -136,33 +103,13 @@ export class DsDataSource extends juggler.DataSource ) { super(dsConfig); } - - /** - * Start the datasource when application is started - */ - start(): ValueOrPromise { - // 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 { - return super.disconnect(); - } } `; exports[`lb4 datasource integration correctly coerces setting input of type object and array 1`] = ` -import { - inject, - lifeCycleObserver, - LifeCycleObserver, - ValueOrPromise, -} from '@loopback/core'; +import {inject, lifeCycleObserver, LifeCycleObserver} from '@loopback/core'; import {juggler} from '@loopback/repository'; const config = { @@ -179,6 +126,10 @@ const config = { crud: false }; +// Observe application's life cycle to disconnect the datasource when +// application is stopped. This allows the application to be shut down +// gracefully. The \`stop()\` method is inherited from \`juggler.DataSource\`. +// Learn more at https://loopback.io/doc/en/lb4/Life-cycle.html @lifeCycleObserver('datasource') export class DsDataSource extends juggler.DataSource implements LifeCycleObserver { @@ -191,33 +142,13 @@ export class DsDataSource extends juggler.DataSource ) { super(dsConfig); } - - /** - * Start the datasource when application is started - */ - start(): ValueOrPromise { - // 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 { - return super.disconnect(); - } } `; exports[`lb4 datasource integration scaffolds correct file with cloudant input 1`] = ` -import { - inject, - lifeCycleObserver, - LifeCycleObserver, - ValueOrPromise, -} from '@loopback/core'; +import {inject, lifeCycleObserver, LifeCycleObserver} from '@loopback/core'; import {juggler} from '@loopback/repository'; const config = { @@ -230,6 +161,10 @@ const config = { modelIndex: undefined }; +// Observe application's life cycle to disconnect the datasource when +// application is stopped. This allows the application to be shut down +// gracefully. The \`stop()\` method is inherited from \`juggler.DataSource\`. +// Learn more at https://loopback.io/doc/en/lb4/Life-cycle.html @lifeCycleObserver('datasource') export class DsDataSource extends juggler.DataSource implements LifeCycleObserver { @@ -242,21 +177,6 @@ export class DsDataSource extends juggler.DataSource ) { super(dsConfig); } - - /** - * Start the datasource when application is started - */ - start(): ValueOrPromise { - // 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 { - return super.disconnect(); - } } `; diff --git a/packages/cli/snapshots/integration/generators/openapi-client.integration.snapshots.js b/packages/cli/snapshots/integration/generators/openapi-client.integration.snapshots.js index 81f85bb06e5d..cfd964207a06 100644 --- a/packages/cli/snapshots/integration/generators/openapi-client.integration.snapshots.js +++ b/packages/cli/snapshots/integration/generators/openapi-client.integration.snapshots.js @@ -1022,12 +1022,7 @@ export * from './pet-store.datasource'; exports[`openapi-generator with --client does not generates files for server with --no-server 2`] = ` -import { - inject, - lifeCycleObserver, - LifeCycleObserver, - ValueOrPromise, -} from '@loopback/core'; +import {inject, lifeCycleObserver, LifeCycleObserver} from '@loopback/core'; import {juggler} from '@loopback/repository'; const config = { @@ -1038,6 +1033,10 @@ const config = { positional: true }; +// Observe application's life cycle to disconnect the datasource when +// application is stopped. This allows the application to be shut down +// gracefully. The \`stop()\` method is inherited from \`juggler.DataSource\`. +// Learn more at https://loopback.io/doc/en/lb4/Life-cycle.html @lifeCycleObserver('datasource') export class PetStoreDataSource extends juggler.DataSource implements LifeCycleObserver { @@ -1050,21 +1049,6 @@ export class PetStoreDataSource extends juggler.DataSource ) { super(dsConfig); } - - /** - * Start the datasource when application is started - */ - start(): ValueOrPromise { - // 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 { - return super.disconnect(); - } } @@ -1715,12 +1699,7 @@ export * from './pet-store.datasource'; exports[`openapi-generator with --client generates all files for both server and client 4`] = ` -import { - inject, - lifeCycleObserver, - LifeCycleObserver, - ValueOrPromise, -} from '@loopback/core'; +import {inject, lifeCycleObserver, LifeCycleObserver} from '@loopback/core'; import {juggler} from '@loopback/repository'; const config = { @@ -1731,6 +1710,10 @@ const config = { positional: true }; +// Observe application's life cycle to disconnect the datasource when +// application is stopped. This allows the application to be shut down +// gracefully. The \`stop()\` method is inherited from \`juggler.DataSource\`. +// Learn more at https://loopback.io/doc/en/lb4/Life-cycle.html @lifeCycleObserver('datasource') export class PetStoreDataSource extends juggler.DataSource implements LifeCycleObserver { @@ -1743,21 +1726,6 @@ export class PetStoreDataSource extends juggler.DataSource ) { super(dsConfig); } - - /** - * Start the datasource when application is started - */ - start(): ValueOrPromise { - // 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 { - return super.disconnect(); - } } diff --git a/packages/cli/test/fixtures/openapi/3.0/src/datasources/pet-store.datasource.ts b/packages/cli/test/fixtures/openapi/3.0/src/datasources/pet-store.datasource.ts index 8a9646bdc1e7..2670bcca32a8 100644 --- a/packages/cli/test/fixtures/openapi/3.0/src/datasources/pet-store.datasource.ts +++ b/packages/cli/test/fixtures/openapi/3.0/src/datasources/pet-store.datasource.ts @@ -3,12 +3,7 @@ // This file is licensed under the MIT License. // License text available at https://opensource.org/licenses/MIT -import { - inject, - lifeCycleObserver, - LifeCycleObserver, - ValueOrPromise, -} from '@loopback/core'; +import {inject, lifeCycleObserver, LifeCycleObserver} from '@loopback/core'; import {juggler} from '@loopback/repository'; const config = { @@ -19,6 +14,10 @@ const config = { positional: false, }; +// Observe application's life cycle to disconnect the datasource when +// application is stopped. This allows the application to be shut down +// gracefully. The `stop()` method is inherited from `juggler.DataSource`. +// Learn more at https://loopback.io/doc/en/lb4/Life-cycle.html @lifeCycleObserver('datasource') export class PetStoreDataSource extends juggler.DataSource implements LifeCycleObserver { @@ -31,19 +30,4 @@ export class PetStoreDataSource extends juggler.DataSource ) { super(dsConfig); } - - /** - * Start the datasource when application is started - */ - start(): ValueOrPromise { - // 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 { - return super.disconnect(); - } }