Skip to content
This repository was archived by the owner on Dec 27, 2024. It is now read-only.

Commit 6536d09

Browse files
committed
1 parent baa7c3b commit 6536d09

23 files changed

+3419
-404
lines changed

boot/sakura-api-config.spec.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import {SakuraMongoDbConnection} from '../core/sakura-mongo-db-connection';
2-
import {
3-
testMongoDbUrl,
4-
testSapi
5-
} from '../spec/helpers/sakuraapi';
2+
import {testMongoDbUrl, testSapi} from '../spec/helpers/sakuraapi';
63
import {SakuraApiConfig} from './sakura-api-config';
74

8-
const path = require('path');
5+
import path = require('path');
96

107
describe('sakura-api-config', () => {
118

@@ -22,7 +19,7 @@ describe('sakura-api-config', () => {
2219
it('loads the default config file if config file not found in path', () => {
2320
this.path = this.path.replace('environment.json', 'not_found.json');
2421
try {
25-
let cfg = new SakuraApiConfig().load(this.path);
22+
const cfg = new SakuraApiConfig().load(this.path);
2623
expect(cfg.SAKURA_API_CONFIG_TEST).toEqual('found');
2724
} catch (err) {
2825
fail(err);
@@ -31,7 +28,7 @@ describe('sakura-api-config', () => {
3128

3229
it('loads the config file and properly cascades', () => {
3330
try {
34-
let cfg = new SakuraApiConfig().load(this.path);
31+
const cfg = new SakuraApiConfig().load(this.path);
3532
expect(cfg).toBeDefined();
3633
expect(cfg.baseFile).toBe(true);
3734
expect(cfg.ts).toBe(true);
@@ -53,7 +50,7 @@ describe('sakura-api-config', () => {
5350
process.env.SAKURA_API_CONFIG = path.join(process.cwd(), 'spec/test_config/test-SAKURA_API_CONFIG.json');
5451

5552
try {
56-
let cfg = new SakuraApiConfig().load();
53+
const cfg = new SakuraApiConfig().load();
5754
expect(cfg['env-SAKURA_API_CONFIG']).toBeTruthy();
5855
done();
5956
} catch (err) {
@@ -65,7 +62,7 @@ describe('sakura-api-config', () => {
6562
process.env.SAKURA_API_CONFIG = path.join(process.cwd(), 'spec/test_config/test-SAKURA_API_CONFIG');
6663

6764
try {
68-
let cfg = new SakuraApiConfig().load();
65+
const cfg = new SakuraApiConfig().load();
6966
expect(cfg['env-SAKURA_API_CONFIG']).toBeUndefined();
7067
done();
7168
} catch (err) {
@@ -111,12 +108,12 @@ describe('sakura-api-config', () => {
111108
this.dbConnections = {
112109
dbConnections: [
113110
{
114-
'name': 'testDb1',
115-
'url': `${testMongoDbUrl(sapi)}/test1`
111+
name: 'testDb1',
112+
url: `${testMongoDbUrl(sapi)}/test1`
116113
},
117114
{
118-
'name': 'testDb2',
119-
'url': `${testMongoDbUrl(sapi)}/test2`
115+
name: 'testDb2',
116+
url: `${testMongoDbUrl(sapi)}/test2`
120117
}
121118
]
122119
};
@@ -128,12 +125,12 @@ describe('sakura-api-config', () => {
128125
});
129126

130127
it('returns a SakuraMongoDbConnection object populated with the dbs in the config, but not yet connected', () => {
131-
let conns: SakuraMongoDbConnection = this.config.dataSources(this.dbConnections);
128+
const conns: SakuraMongoDbConnection = this.config.dataSources(this.dbConnections);
132129

133130
expect(conns.getConnection('testDb1')).toBeDefined();
134131
expect(conns.getConnection('testDb2')).toBeDefined();
135132
expect(conns.getDb('testDb1')).toBeUndefined();
136133
expect(conns.getDb('testDb2')).toBeUndefined();
137134
});
138-
})
135+
});
139136
});

boot/sakura-api-config.ts

Lines changed: 51 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,34 @@ const debug = {
1313
*/
1414
export class SakuraApiConfig {
1515

16+
/**
17+
* Same as the instance method, but static, and it won't try to use the last loaded config since that
18+
* requires an instance.
19+
*/
20+
static dataSources(config: { dbConnections?: any[] }): SakuraMongoDbConnection {
21+
config = config || {};
22+
23+
if (!config.dbConnections) {
24+
debug.normal(`.dataSources, no config (config: ${!!config},`
25+
+ `config.dbConnections: ${!!(config || {} as any).dbConnections})`);
26+
27+
config.dbConnections = [];
28+
}
29+
30+
if (!Array.isArray(config.dbConnections)) {
31+
throw new Error('Invalid dbConnections array. The "dbConnections" object should be an array');
32+
}
33+
34+
const dbConns = new SakuraMongoDbConnection();
35+
36+
debug.normal(`Adding ${config.dbConnections.length} dbConnections.`);
37+
for (const conn of config.dbConnections) {
38+
dbConns.addConnection(conn.name, conn.url, conn.mongoClientOptions);
39+
}
40+
41+
return dbConns;
42+
}
43+
1644
/**
1745
* The configuration that was loaded from the various json and ts files and the environmental
1846
* variables that were set at the time the configuration was last loaded.
@@ -82,7 +110,7 @@ export class SakuraApiConfig {
82110
path = path || process.env.SAKURA_API_CONFIG || 'config/environment.json';
83111
debug.normal(`.load path: '${path}'`);
84112

85-
let config = {};
113+
const config = {};
86114
let baseConfig = {};
87115
let baseJsConfig = {};
88116

@@ -98,7 +126,7 @@ export class SakuraApiConfig {
98126
}
99127

100128
// environment.js
101-
let jsPath = changeFileExtension(path, 'js');
129+
const jsPath = changeFileExtension(path, 'js');
102130
debug.normal(`loading ${jsPath}`);
103131
try {
104132
baseJsConfig = require(`${process.cwd()}/${jsPath}`);
@@ -109,13 +137,13 @@ export class SakuraApiConfig {
109137
handleLoadError(err, path, true);
110138
}
111139

112-
let env = process.env.NODE_ENV;
140+
const env = process.env.NODE_ENV;
113141
let envConfig = {};
114142
let envJsConfig = {};
115143
if (env && env.NODE_ENV !== '') {
116144
// environment.{env}.json
117-
let pathParts = path.split('/');
118-
let fileParts = pathParts[pathParts.length - 1].split('.');
145+
const pathParts = path.split('/');
146+
const fileParts = pathParts[pathParts.length - 1].split('.');
119147

120148
fileParts.splice(fileParts.length - 1, 0, env);
121149
pathParts[pathParts.length - 1] = fileParts.join('.');
@@ -152,69 +180,40 @@ export class SakuraApiConfig {
152180
return config;
153181

154182
//////////
155-
function changeFileExtension(path: string, newExtension: string) {
156-
let pathParts = path.split('/');
157-
let fileParts = pathParts[pathParts.length - 1].split('.');
183+
function changeFileExtension(targetPath: string, newExtension: string) {
184+
const pathParts = targetPath.split('/');
185+
const fileParts = pathParts[pathParts.length - 1].split('.');
158186
fileParts[fileParts.length - 1] = newExtension;
159187

160188
pathParts[pathParts.length - 1] = fileParts.join('.');
161189
return pathParts.join('/');
162190
}
163191

164-
function handleLoadError(err: Error, path: string, noDefault: boolean) {
165-
if (err['code'] === 'ENOENT') {
192+
function handleLoadError(err: Error, targetPath: string, noDefault: boolean) {
193+
if ((err as any).code === 'ENOENT') {
166194
// NOOP: the config file is empty, just default to {}
167-
debug.normal(`.load config file empty, defaulting to {} for path: '${path}'`);
195+
debug.normal(`.load config file empty, defaulting to {} for path: '${targetPath}'`);
168196
return;
169197
} else if (err.message.startsWith('Cannot find module')) {
170198
// NOOP: a ts config file wasn't found
171-
debug.normal(`.load config file wasn't found, defaulting to {} for path: '${path}'`);
199+
debug.normal(`.load config file wasn't found, defaulting to {} for path: '${targetPath}'`);
172200
return;
173201
} else if (err.message === 'Unexpected end of JSON input') {
174-
let e = new Error(err.message);
175-
e['code'] = 'INVALID_JSON_EMPTY';
176-
e['path'] = path;
177-
debug.normal(`.load path: '${path}', error:`, err);
178-
throw e;
202+
const jsonInputErr = new Error(err.message);
203+
(jsonInputErr as any).code = 'INVALID_JSON_EMPTY';
204+
(jsonInputErr as any).path = targetPath;
205+
debug.normal(`.load path: '${targetPath}', error:`, err);
206+
throw jsonInputErr;
179207
} else if (err.message.startsWith('Unexpected token')) {
180-
let e = new Error(err.message);
181-
e['code'] = 'INVALID_JSON_INVALID';
182-
e['path'] = path;
183-
debug.normal(`.load path: '${path}', error:`, err);
184-
throw e;
208+
const tokenErr = new Error(err.message);
209+
(tokenErr as any).code = 'INVALID_JSON_INVALID';
210+
(tokenErr as any).path = targetPath;
211+
debug.normal(`.load path: '${targetPath}', error:`, err);
212+
throw tokenErr;
185213
} else {
186-
debug.normal(`.load path: '${path}', error:`, err);
214+
debug.normal(`.load path: '${targetPath}', error:`, err);
187215
throw err;
188216
}
189217
}
190218
}
191-
192-
/**
193-
* Same as the instance method, but static, and it won't try to use the last loaded config since that
194-
* requires an instance.
195-
*/
196-
static dataSources(config: { dbConnections?: any[] }): SakuraMongoDbConnection {
197-
config = config || {};
198-
199-
if (!config.dbConnections) {
200-
debug.normal(`.dataSources, no config (config: ${!!config},`
201-
+ `config.dbConnections: ${!!(config || <any>{}).dbConnections})`);
202-
203-
config.dbConnections = [];
204-
//return null;
205-
}
206-
207-
if (!Array.isArray(config.dbConnections)) {
208-
throw new Error('Invalid dbConnections array. The "dbConnections" object should be an array');
209-
}
210-
211-
let dbConns = new SakuraMongoDbConnection();
212-
213-
debug.normal(`Adding ${config.dbConnections.length} dbConnections.`);
214-
for (let conn of config.dbConnections) {
215-
dbConns.addConnection(conn.name, conn.url, conn.mongoClientOptions);
216-
}
217-
218-
return dbConns;
219-
}
220219
}

core/@model/db.spec.ts

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1+
// tslint:disable:no-shadowed-variable
12
import {ObjectID} from 'mongodb';
23
import {testSapi} from '../../spec/helpers/sakuraapi';
3-
import {
4-
Db,
5-
dbSymbols,
6-
Json
7-
} from './';
4+
import {Db, dbSymbols, Json} from './';
85
import {Model} from './model';
96
import {SakuraApiModel} from './sakura-api-model';
107

@@ -441,14 +438,14 @@ describe('@Db', () => {
441438
};
442439

443440
User.get({filter: {}, project: projection})
444-
.then((results) => {
445-
expect(results[0]._id instanceof ObjectID).toBeTruthy('Should be an instance of ObjectID');
446-
expect(results[0].firstName).toBeUndefined('Projection should have excluded this');
447-
expect(results[0].lastName).toBeUndefined('Projection should have excluded this');
448-
expect(results[0].contact.phone).toBe('123-123-1234');
449-
})
450-
.then(done)
451-
.catch(done.fail);
441+
.then((results) => {
442+
expect(results[0]._id instanceof ObjectID).toBeTruthy('Should be an instance of ObjectID');
443+
expect(results[0].firstName).toBeUndefined('Projection should have excluded this');
444+
expect(results[0].lastName).toBeUndefined('Projection should have excluded this');
445+
expect(results[0].contact.phone).toBe('123-123-1234');
446+
})
447+
.then(done)
448+
.catch(done.fail);
452449
});
453450

454451
});
@@ -569,8 +566,8 @@ describe('@Db', () => {
569566

570567
it('handles falsy properties', () => {
571568
const model = new ChasteModelTest();
572-
(model as any)['firstName'] = 0;
573-
(model as any)['lastName'] = false;
569+
(model as any).firstName = 0;
570+
(model as any).lastName = false;
574571

575572
const result = model.toDb();
576573
expect(result.fn).toBe(0);
@@ -618,3 +615,4 @@ describe('@Db', () => {
618615
});
619616
});
620617
});
618+
// tslint:enable:no-shadowed-variable

core/@model/json.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ import {ObjectID} from 'mongodb';
22
import {testSapi} from '../../spec/helpers/sakuraapi';
33
import {Db} from './db';
44
import {Json} from './json';
5-
import {
6-
Model,
7-
modelSymbols
8-
} from './model';
5+
import {Model, modelSymbols} from './model';
96
import {SakuraApiModel} from './sakura-api-model';
107

118
describe('@Json', () => {
@@ -33,6 +30,7 @@ describe('@Json', () => {
3330
}
3431

3532
aFunction() {
33+
// lint empty
3634
}
3735
}
3836

@@ -43,6 +41,7 @@ describe('@Json', () => {
4341
aThirdProperty: number = 777;
4442

4543
aFunction() {
44+
// lint empty
4645
}
4746
}
4847

@@ -628,7 +627,8 @@ describe('@Json', () => {
628627
});
629628
expect(obj.anotherProperty).toBe(2);
630629
});
631-
it('with the last property defined in the json object winning if there are multiple matching fields for a property', () => {
630+
it('with the last property defined in the json object winning if there are multiple' +
631+
' matching fields for a property', () => {
632632
const obj = Test.fromJson({
633633
anotherProperty: 3,
634634
anp: 2

0 commit comments

Comments
 (0)