Skip to content

Commit

Permalink
feat(cli): generate datasource json with '.config.json` extension
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed Nov 7, 2019
1 parent 6a77bb7 commit 0d7bcdf
Show file tree
Hide file tree
Showing 29 changed files with 52 additions and 51 deletions.
2 changes: 1 addition & 1 deletion docs/site/DataSource-generator.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Once all the prompts have been answered, the CLI will do the following:
- Install `@loopback/repository` and the connector package (if it's not a custom
connector).
- Create a file with the connector configuration as follows:
`/src/datasources/${dataSource.dataSourceName}.datasource.json`
`/src/datasources/${dataSource.dataSourceName}.config.json`
- Create a DataSource class which recieves the connector config using
[Dependency Injection](Dependency-injection.md) as follows:
`/src/datasources/${dataSource.dataSourceName}.datasource.ts`
Expand Down
4 changes: 2 additions & 2 deletions docs/site/DataSources.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ It is recommended to use the [`lb4 datasource` command](DataSource-generator.md)
provided by the CLI to generate a DataSource. The CLI will prompt for all
necessary connector information and create the following files:

- `${dataSource.dataSourceName}.datasource.json` containing the connector
- `${dataSource.dataSourceName}.config.json` containing the connector
configuration
- `${dataSource.dataSourceName}.datasource.ts` containing a class extending
`juggler.DataSource`. This class can be used to override the default
Expand All @@ -37,7 +37,7 @@ Example DataSource Class:
```ts
import {inject} from '@loopback/core';
import {juggler} from '@loopback/repository';
import * as config from './db.datasource.json';
import * as config from './db.config.json';

export class DbDataSource extends juggler.DataSource {
static dataSourceName = 'db';
Expand Down
4 changes: 2 additions & 2 deletions docs/site/Inside-Loopback-Application.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,9 @@ We need appropriate drivers to connect with the backend datasources.
> to provide easy to use CRUD operations and connection configurations.
In the example,
[Redis datasource definition](https://github.com/strongloop/loopback4-example-shopping/blob/master/packages/shopping/src/datasources/redis.datasource.json)
[Redis datasource definition](https://github.com/strongloop/loopback4-example-shopping/blob/master/packages/shopping/src/datasources/redis.config.json)
uses the LoopBack `kv-redis` connector and
[MongoDB datasource definition](https://github.com/strongloop/loopback4-example-shopping/blob/master/packages/shopping/src/datasources/mongo.datasource.json)
[MongoDB datasource definition](https://github.com/strongloop/loopback4-example-shopping/blob/master/packages/shopping/src/datasources/mongo.config.json)
uses the LoopBack `mongodb` connector.

## Application startup
Expand Down
8 changes: 4 additions & 4 deletions docs/site/Repositories.md
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ better flexibility, we split the datasource definition into two files. The json
file captures the configuration properties and it can be possibly overridden by
dependency injection.

1. redis.datasource.json
1. redis.config.json

```json
{
Expand All @@ -328,14 +328,14 @@ dependency injection.
2. redis.datasource.ts

The class uses a configuration object to set up a datasource for the Redis
instance. By default, the configuration is loaded from `redis.datasource.json`.
We can override it by binding a new object to `datasources.config.redis` for a
instance. By default, the configuration is loaded from `redis.config.json`. We
can override it by binding a new object to `datasources.config.redis` for a
context.

```ts
import {inject} from '@loopback/core';
import {juggler, AnyObject} from '@loopback/repository';
import * as config from './redis.datasource.json';
import * as config from './redis.config.json';

export class RedisDataSource extends juggler.DataSource {
static dataSourceName = 'redis';
Expand Down
2 changes: 1 addition & 1 deletion docs/site/Testing-your-application.md
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ instance:

```ts
import {merge} from 'lodash';
import * as GEO_CODER_CONFIG from '../datasources/geo.datasource.json';
import * as GEO_CODER_CONFIG from '../datasources/geo.config.json';

function givenGeoService() {
const config = merge({}, GEO_CODER_CONFIG, {
Expand Down
10 changes: 5 additions & 5 deletions docs/site/deployment/Deploying-to-IBM-Cloud.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ Cloudant connector, so data is persisted.

### Step 3: Updating your DataSource

Update `db.datasource.json` to use the Cloudant connector. The value for the
`url` property is just a placeholder and does not need to have the correct
credential because we will be binding the app with the Cloudant service once
it's pushed to IBM Cloud.
Update `db.config.json` to use the Cloudant connector. The value for the `url`
property is just a placeholder and does not need to have the correct credential
because we will be binding the app with the Cloudant service once it's pushed to
IBM Cloud.

```js
{
Expand Down Expand Up @@ -106,7 +106,7 @@ $ npm i loopback-connector-cloudant
import {TodoListApplication} from './application';
import {ApplicationConfig} from '@loopback/core';
// --------- ADD THIS SNIPPET ---------
const datasourceDb = require('./datasources/db.datasource.json');
const datasourceDb = require('./datasources/db.config.json');
const cfenv = require('cfenv');
const appEnv = cfenv.getAppEnv();
// --------- ADD THIS SNIPPET ---------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ JSON configuration file and a .ts file with the Datasource name and added an
entry inside the index.ts file.

```sh
create src/datasources/calculator.datasource.json
create src/datasources/calculator.config.json
create src/datasources/calculator.datasource.ts
update src/datasources/index.ts

Expand Down Expand Up @@ -103,10 +103,10 @@ bind the Node.js method _Multiply_.
}
```

Edit the file `src/datasources/calculator.datasource.json` and add the following
Edit the file `src/datasources/calculator.config.json` and add the following
configuration after the `remoteEnabled: true,` property as follows:

{% include code-caption.html content="src/datasources/calculator.datasource.json" %}
{% include code-caption.html content="src/datasources/calculator.config.json" %}

```ts
"operations": {
Expand Down
2 changes: 1 addition & 1 deletion docs/site/tutorials/todo/todo-tutorial-datasource.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ lb4 datasource
? window.localStorage key to use for persistence (browser only):
? Full path to file for persistence (server only): ./data/db.json

create src/datasources/db.datasource.json
create src/datasources/db.config.json
create src/datasources/db.datasource.ts
update src/datasources/index.ts

Expand Down
4 changes: 2 additions & 2 deletions docs/site/tutorials/todo/todo-tutorial-geocoding-service.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ $ lb4 datasource
? Default options for the request:
? An array of operation templates:
? Use default CRUD mapping: No
create src/datasources/geocoder.datasource.json
create src/datasources/geocoder.config.json
create src/datasources/geocoder.datasource.ts
# npm will install dependencies now
update src/datasources/index.ts
Expand All @@ -59,7 +59,7 @@ Edit the newly created datasource configuration to configure Geocoder API
endpoints. Configuration options provided by REST Connector are described in our
docs here: [REST connector](/doc/en/lb3/REST-connector.html).

{% include code-caption.html content="/src/datasources/geocoder.datasource.json" %}
{% include code-caption.html content="/src/datasources/geocoder.config.json" %}

```json
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import {inject} from '@loopback/core';
import {juggler} from '@loopback/repository';
import * as config from './ds.datasource.json';
import * as config from './ds.config.json';

export class DsDataSource extends juggler.DataSource {
static dataSourceName = 'ds';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import {inject} from '@loopback/core';
import {juggler} from '@loopback/repository';
import * as config from './calculator.datasource.json';
import * as config from './calculator.config.json';

export class CalculatorDataSource extends juggler.DataSource {
static dataSourceName = 'calculator';
Expand Down
2 changes: 1 addition & 1 deletion examples/todo-list/src/datasources/db.datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import {inject} from '@loopback/core';
import {juggler} from '@loopback/repository';
import * as config from './db.datasource.json';
import * as config from './db.config.json';

export class DbDataSource extends juggler.DataSource {
static dataSourceName = 'db';
Expand Down
2 changes: 1 addition & 1 deletion examples/todo/src/__tests__/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import {HttpCachingProxy} from '@loopback/http-caching-proxy';
import {merge} from 'lodash';
import * as path from 'path';
import * as GEO_CODER_CONFIG from '../datasources/geocoder.datasource.json';
import * as GEO_CODER_CONFIG from '../datasources/geocoder.config.json';
import {Todo} from '../models/index';
import {GeocoderService, GeoPoint} from '../services/geocoder.service';

Expand Down
2 changes: 1 addition & 1 deletion examples/todo/src/datasources/db.datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import {inject} from '@loopback/core';
import {juggler} from '@loopback/repository';
import * as config from './db.datasource.json';
import * as config from './db.config.json';

export class DbDataSource extends juggler.DataSource {
static dataSourceName = 'db';
Expand Down
4 changes: 2 additions & 2 deletions examples/todo/src/datasources/geocoder.datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
// License text available at https://opensource.org/licenses/MIT

import {inject} from '@loopback/core';
import {juggler, AnyObject} from '@loopback/repository';
import * as config from './geocoder.datasource.json';
import {AnyObject, juggler} from '@loopback/repository';
import * as config from './geocoder.config.json';

export class GeocoderDataSource extends juggler.DataSource {
static dataSourceName = 'geocoder';
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/generators/datasource/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ module.exports = class DataSourceGenerator extends ArtifactGenerator {
this.artifactInfo.className = utils.toClassName(this.artifactInfo.name);
this.artifactInfo.fileName = utils.toFileName(this.artifactInfo.name);
// prettier-ignore
this.artifactInfo.jsonFileName = `${this.artifactInfo.fileName}.datasource.json`;
this.artifactInfo.jsonFileName = `${this.artifactInfo.fileName}.config.json`;
// prettier-ignore
this.artifactInfo.outFile = `${this.artifactInfo.fileName}.datasource.ts`;

Expand Down
2 changes: 1 addition & 1 deletion packages/cli/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ exports.getDataSourceName = function(datasourcesDir, dataSourceClass) {

exports.dataSourceToJSONFileName = function(dataSourceClass) {
return path.join(
toFileName(dataSourceClass.replace('Datasource', '')) + '.datasource.json',
toFileName(dataSourceClass.replace('Datasource', '')) + '.config.json',
);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
ValueOrPromise,
} from '@loopback/core';
import {juggler} from '@loopback/repository';
import * as config from './ds.datasource.json';
import * as config from './ds.config.json';
@lifeCycleObserver('datasource')
export class DsDataSource extends juggler.DataSource
Expand Down Expand Up @@ -56,7 +56,7 @@ import {
ValueOrPromise,
} from '@loopback/core';
import {juggler} from '@loopback/repository';
import * as config from './ds.datasource.json';
import * as config from './ds.config.json';
@lifeCycleObserver('datasource')
export class DsDataSource extends juggler.DataSource
Expand Down Expand Up @@ -97,7 +97,7 @@ import {
ValueOrPromise,
} from '@loopback/core';
import {juggler} from '@loopback/repository';
import * as config from './ds.datasource.json';
import * as config from './ds.config.json';
@lifeCycleObserver('datasource')
export class DsDataSource extends juggler.DataSource
Expand Down Expand Up @@ -138,7 +138,7 @@ import {
ValueOrPromise,
} from '@loopback/core';
import {juggler} from '@loopback/repository';
import * as config from './ds.datasource.json';
import * as config from './ds.config.json';
@lifeCycleObserver('datasource')
export class DsDataSource extends juggler.DataSource
Expand Down Expand Up @@ -179,7 +179,7 @@ import {
ValueOrPromise,
} from '@loopback/core';
import {juggler} from '@loopback/repository';
import * as config from './ds.datasource.json';
import * as config from './ds.config.json';
@lifeCycleObserver('datasource')
export class DsDataSource extends juggler.DataSource
Expand Down
10 changes: 5 additions & 5 deletions packages/cli/test/fixtures/relation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ exports.SANDBOX_FILES = [
},
{
path: DATASOURCE_APP_PATH,
file: 'dbkv.datasource.json',
file: 'dbkv.config.json',
content: JSON.stringify({
name: 'dbkv',
connector: 'kv-redis',
Expand All @@ -121,15 +121,15 @@ exports.SANDBOX_FILES = [
},
{
path: DATASOURCE_APP_PATH,
file: 'dbmem.datasource.json',
file: 'dbmem.config.json',
content: JSON.stringify({
name: 'dbmem',
connector: 'memory',
}),
},
{
path: DATASOURCE_APP_PATH,
file: 'my-ds.datasource.json',
file: 'my-ds.config.json',
content: JSON.stringify({
name: 'MyDS',
connector: 'memory',
Expand All @@ -142,15 +142,15 @@ exports.SANDBOX_FILES = [
},
{
path: DATASOURCE_APP_PATH,
file: 'restdb.datasource.json',
file: 'restdb.config.json',
content: JSON.stringify({
name: 'restdb',
connector: 'rest',
}),
},
{
path: DATASOURCE_APP_PATH,
file: 'sqlite3.datasource.json',
file: 'sqlite3.config.json',
content: JSON.stringify({
name: 'sqlite3',
connector: 'loopback-connector-sqlite3',
Expand Down
10 changes: 5 additions & 5 deletions packages/cli/test/fixtures/repository/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ exports.SANDBOX_FILES = [
},
{
path: DATASOURCE_APP_PATH,
file: 'dbkv.datasource.json',
file: 'dbkv.config.json',
content: JSON.stringify({
name: 'dbkv',
connector: 'kv-redis',
Expand All @@ -34,15 +34,15 @@ exports.SANDBOX_FILES = [
},
{
path: DATASOURCE_APP_PATH,
file: 'dbmem.datasource.json',
file: 'dbmem.config.json',
content: JSON.stringify({
name: 'dbmem',
connector: 'memory',
}),
},
{
path: DATASOURCE_APP_PATH,
file: 'my-ds.datasource.json',
file: 'my-ds.config.json',
content: JSON.stringify({
name: 'MyDS',
connector: 'memory',
Expand All @@ -55,15 +55,15 @@ exports.SANDBOX_FILES = [
},
{
path: DATASOURCE_APP_PATH,
file: 'restdb.datasource.json',
file: 'restdb.config.json',
content: JSON.stringify({
name: 'restdb',
connector: 'rest',
}),
},
{
path: DATASOURCE_APP_PATH,
file: 'sqlite3.datasource.json',
file: 'sqlite3.config.json',
content: JSON.stringify({
name: 'sqlite3',
connector: 'loopback-connector-sqlite3',
Expand Down
8 changes: 4 additions & 4 deletions packages/cli/test/fixtures/service/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ exports.SANDBOX_FILES = [
},
{
path: DATASOURCE_APP_PATH,
file: 'myds.datasource.json',
file: 'myds.config.json',
content: JSON.stringify({
name: 'myds',
connector: 'soap',
}),
},
{
path: DATASOURCE_APP_PATH,
file: 'map-ds.datasource.json',
file: 'map-ds.config.json',
content: JSON.stringify({
name: 'MapDS',
connector: 'soap',
Expand All @@ -47,7 +47,7 @@ exports.SANDBOX_FILES = [
},
{
path: DATASOURCE_APP_PATH,
file: 'dbmem.datasource.json',
file: 'dbmem.config.json',
content: JSON.stringify({
name: 'dbmem',
connector: 'memory',
Expand All @@ -60,7 +60,7 @@ exports.SANDBOX_FILES = [
},
{
path: DATASOURCE_APP_PATH,
file: 'restdb.datasource.json',
file: 'restdb.config.json',
content: JSON.stringify({
name: 'restdb',
connector: 'rest',
Expand Down
Loading

0 comments on commit 0d7bcdf

Please sign in to comment.