Skip to content

Commit

Permalink
feat: use resolveJsonModule to load datasource config
Browse files Browse the repository at this point in the history
Enable TypeScript feature "resolveJsonModule" to load datasource
configurations in a type-safe way using regular "import" statements.
  • Loading branch information
bajtos committed Oct 8, 2018
1 parent 0e80e27 commit 73e19ff
Show file tree
Hide file tree
Showing 12 changed files with 23 additions and 22 deletions.
2 changes: 1 addition & 1 deletion docs/site/Repositories.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ context.
```ts
import {inject} from '@loopback/core';
import {juggler, AnyObject} from '@loopback/repository';
const config = require('./redis.datasource.json');
import * as config from './redis.datasource.json';

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

```ts
import {merge} from 'lodash';

const GEO_CODER_CONFIG = require('../src/datasources/geo.datasource.json');
import * as GEO_CODER_CONFIG from '../src/datasources/geo.datasource.json';

function givenGeoService() {
const config = merge({}, GEO_CODER_CONFIG, {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {inject} from '@loopback/core';
import {juggler, AnyObject} from '@loopback/repository';
const config = require('./calculator.datasource.json');
import {juggler} from '@loopback/repository';
import * as config from './calculator.datasource.json';

export class CalculatorDataSource extends juggler.DataSource {
static dataSourceName = 'calculator';

constructor(
@inject('datasources.config.calculator', {optional: true})
dsConfig: AnyObject = config,
dsConfig: object = config,
) {
dsConfig = Object.assign({}, dsConfig, {
// A workaround for the current design flaw where inside our monorepo,
Expand Down
6 changes: 3 additions & 3 deletions examples/todo-list/src/datasources/db.datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
// License text available at https://opensource.org/licenses/MIT

import {inject} from '@loopback/core';
import {juggler, DataSource} from '@loopback/repository';
const config = require('./db.datasource.json');
import {juggler} from '@loopback/repository';
import * as config from './db.datasource.json';

export class DbDataSource extends juggler.DataSource {
static dataSourceName = 'db';

constructor(
@inject('datasources.config.db', {optional: true})
dsConfig: DataSource = config,
dsConfig: object = config,
) {
super(dsConfig);
}
Expand Down
6 changes: 3 additions & 3 deletions examples/todo/src/datasources/db.datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
// License text available at https://opensource.org/licenses/MIT

import {inject} from '@loopback/core';
import {juggler, DataSource} from '@loopback/repository';
const config = require('./db.datasource.json');
import {juggler} from '@loopback/repository';
import * as config from './db.datasource.json';

export class DbDataSource extends juggler.DataSource {
static dataSourceName = 'db';

constructor(
@inject('datasources.config.db', {optional: true})
dsConfig: DataSource = config,
dsConfig: object = config,
) {
super(dsConfig);
}
Expand Down
2 changes: 1 addition & 1 deletion examples/todo/src/datasources/geocoder.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, AnyObject} from '@loopback/repository';
const config = require('./geocoder.datasource.json');
import * as config from './geocoder.datasource.json';

export class GeocoderDataSource extends juggler.DataSource {
static dataSourceName = 'geocoder';
Expand Down
3 changes: 1 addition & 2 deletions examples/todo/test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {merge} from 'lodash';
import * as path from 'path';
import {Todo} from '../src/models/index';
import {GeoPoint} from '../src/services/geocoder.service';
import * as GEO_CODER_CONFIG from '../src/datasources/geocoder.datasource.json';

/*
==============================================================================
Expand Down Expand Up @@ -54,8 +55,6 @@ export const aLocation = {
},
};

const GEO_CODER_CONFIG = require('../src/datasources/geocoder.datasource.json');

export function getProxiedGeoCoderConfig(proxy: HttpCachingProxy) {
return merge({}, GEO_CODER_CONFIG, {
options: {
Expand Down
1 change: 1 addition & 0 deletions packages/build/config/tsconfig.common.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"experimentalDecorators": true,
"noImplicitAny": true,
"strictNullChecks": true,
"resolveJsonModule": true,

"lib": ["es2018", "dom", "esnext.asynciterable"],
"module": "commonjs",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {inject} from '@loopback/core';
import {juggler, AnyObject} from '@loopback/repository';
const config = require('./<%= jsonFileName %>');
import {juggler} from '@loopback/repository';
import * as config from './<%= jsonFileName %>';

export class <%= className %>DataSource extends juggler.DataSource {
static dataSourceName = '<%= name %>';

constructor(
@inject('datasources.config.<%= name %>', {optional: true})
dsConfig: AnyObject = config,
dsConfig: object = config,
) {
super(dsConfig);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,11 @@ function checkBasicDataSourceFiles() {
assert.fileContent(expectedTSFile, /import {inject} from '@loopback\/core';/);
assert.fileContent(
expectedTSFile,
/import {juggler, AnyObject} from '@loopback\/repository';/,
/import {juggler} from '@loopback\/repository';/,
);
assert.fileContent(
expectedTSFile,
/const config = require\('.\/ds.datasource.json'\)/,
/import \* as config from '.\/ds.datasource.json';/,
);
assert.fileContent(
expectedTSFile,
Expand Down
3 changes: 2 additions & 1 deletion tslint.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"linterOptions": {
"exclude": [
"./packages/cli/generators/*/templates/**/*",
"./packages/cli/test/sandbox/**/*"
"./packages/cli/test/sandbox/**/*",
"**/*.json"
]
}
}
3 changes: 2 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"linterOptions": {
"exclude": [
"./packages/cli/generators/*/templates/**/*",
"./packages/cli/test/sandbox/**/*"
"./packages/cli/test/sandbox/**/*",
"**/*.json"
]
}
}

0 comments on commit 73e19ff

Please sign in to comment.