@sprucelabs/data-stores 26.3.0 → 26.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/.spruce/errors/dataStores/invalidFakeQueryResponse.schema.d.ts +3 -0
- package/build/.spruce/errors/dataStores/invalidFakeQueryResponse.schema.js +24 -0
- package/build/.spruce/errors/errors.types.d.ts +28 -0
- package/build/.spruce/errors/options.types.d.ts +4 -1
- package/build/databases/NeDbDatabase.d.ts +3 -2
- package/build/databases/NeDbDatabase.js +12 -1
- package/build/errors/SpruceError.js +6 -3
- package/build/errors/invalidFakeQueryResponse.builder.d.ts +18 -0
- package/build/errors/invalidFakeQueryResponse.builder.js +20 -0
- package/build/esm/.spruce/errors/errors.types.d.ts +28 -0
- package/build/esm/.spruce/errors/options.types.d.ts +4 -1
- package/build/esm/databases/NeDbDatabase.d.ts +3 -2
- package/build/esm/databases/NeDbDatabase.js +12 -1
- package/build/esm/errors/SpruceError.js +6 -3
- package/build/esm/errors/invalidFakeQueryResponse.builder.d.ts +18 -0
- package/build/esm/errors/invalidFakeQueryResponse.builder.js +18 -0
- package/build/esm/factories/DatabaseFactory.d.ts +6 -4
- package/build/esm/fixtures/DatabaseFixture.d.ts +2 -0
- package/build/esm/fixtures/DatabaseFixture.js +12 -0
- package/build/esm/types/database.types.d.ts +1 -1
- package/build/factories/DatabaseFactory.d.ts +6 -4
- package/build/fixtures/DatabaseFixture.d.ts +2 -0
- package/build/fixtures/DatabaseFixture.js +12 -0
- package/build/types/database.types.d.ts +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const schema_1 = require("@sprucelabs/schema");
|
|
4
|
+
const invalidFakeQueryResponseSchema = {
|
|
5
|
+
id: 'invalidFakeQueryResponse',
|
|
6
|
+
namespace: 'DataStores',
|
|
7
|
+
name: 'Invalid fake query response',
|
|
8
|
+
fields: {
|
|
9
|
+
/** . */
|
|
10
|
+
'query': {
|
|
11
|
+
type: 'text',
|
|
12
|
+
isRequired: true,
|
|
13
|
+
options: undefined
|
|
14
|
+
},
|
|
15
|
+
/** . */
|
|
16
|
+
'response': {
|
|
17
|
+
type: 'raw',
|
|
18
|
+
isRequired: true,
|
|
19
|
+
options: { valueType: `any`, }
|
|
20
|
+
},
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
schema_1.SchemaRegistry.getInstance().trackSchema(invalidFakeQueryResponseSchema);
|
|
24
|
+
exports.default = invalidFakeQueryResponseSchema;
|
|
@@ -221,6 +221,34 @@ export declare namespace SpruceErrors.DataStores {
|
|
|
221
221
|
}
|
|
222
222
|
type InvalidStoreEntity = SchemaEntity<SpruceErrors.DataStores.InvalidStoreSchema>;
|
|
223
223
|
}
|
|
224
|
+
export declare namespace SpruceErrors.DataStores {
|
|
225
|
+
interface InvalidFakeQueryResponse {
|
|
226
|
+
'query': string;
|
|
227
|
+
'response': (any);
|
|
228
|
+
}
|
|
229
|
+
interface InvalidFakeQueryResponseSchema extends SpruceSchema.Schema {
|
|
230
|
+
id: 'invalidFakeQueryResponse';
|
|
231
|
+
namespace: 'DataStores';
|
|
232
|
+
name: 'Invalid fake query response';
|
|
233
|
+
fields: {
|
|
234
|
+
/** . */
|
|
235
|
+
'query': {
|
|
236
|
+
type: 'text';
|
|
237
|
+
isRequired: true;
|
|
238
|
+
options: undefined;
|
|
239
|
+
};
|
|
240
|
+
/** . */
|
|
241
|
+
'response': {
|
|
242
|
+
type: 'raw';
|
|
243
|
+
isRequired: true;
|
|
244
|
+
options: {
|
|
245
|
+
valueType: `any`;
|
|
246
|
+
};
|
|
247
|
+
};
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
type InvalidFakeQueryResponseEntity = SchemaEntity<SpruceErrors.DataStores.InvalidFakeQueryResponseSchema>;
|
|
251
|
+
}
|
|
224
252
|
export declare namespace SpruceErrors.DataStores {
|
|
225
253
|
interface InvalidDbConnectionString {
|
|
226
254
|
}
|
|
@@ -33,6 +33,9 @@ export interface InvalidStoreNameErrorOptions extends SpruceErrors.DataStores.In
|
|
|
33
33
|
export interface InvalidStoreErrorOptions extends SpruceErrors.DataStores.InvalidStore, ISpruceErrorOptions {
|
|
34
34
|
code: 'INVALID_STORE';
|
|
35
35
|
}
|
|
36
|
+
export interface InvalidFakeQueryResponseErrorOptions extends SpruceErrors.DataStores.InvalidFakeQueryResponse, ISpruceErrorOptions {
|
|
37
|
+
code: 'INVALID_FAKE_QUERY_RESPONSE';
|
|
38
|
+
}
|
|
36
39
|
export interface InvalidDbConnectionStringErrorOptions extends SpruceErrors.DataStores.InvalidDbConnectionString, ISpruceErrorOptions {
|
|
37
40
|
code: 'INVALID_DB_CONNECTION_STRING';
|
|
38
41
|
}
|
|
@@ -63,5 +66,5 @@ export interface DuplicateKeyErrorOptions extends SpruceErrors.DataStores.Duplic
|
|
|
63
66
|
export interface DatabaseNotConnectedErrorOptions extends SpruceErrors.DataStores.DatabaseNotConnected, ISpruceErrorOptions {
|
|
64
67
|
code: 'DATABASE_NOT_CONNECTED';
|
|
65
68
|
}
|
|
66
|
-
type ErrorOptions = UnknownStoreErrorErrorOptions | UnknownErrorErrorOptions | UnknownDatabaseErrorErrorOptions | UnableToConnectToDbErrorOptions | ScrambleNotConfiguredErrorOptions | RecordNotFoundErrorOptions | QueryNotFakedErrorOptions | NotImplementedErrorOptions | MongoIdMappingErrorErrorOptions | InvalidStoreNameErrorOptions | InvalidStoreErrorOptions | InvalidDbConnectionStringErrorOptions | InvalidDatabaseNameErrorOptions | InvalidConnectionStringSchemeErrorOptions | IndexNotFoundErrorOptions | IndexExistsErrorOptions | FailedToLoadStoresErrorOptions | FailedToLoadStoreErrorOptions | DuplicateRecordErrorOptions | DuplicateKeyErrorOptions | DatabaseNotConnectedErrorOptions;
|
|
69
|
+
type ErrorOptions = UnknownStoreErrorErrorOptions | UnknownErrorErrorOptions | UnknownDatabaseErrorErrorOptions | UnableToConnectToDbErrorOptions | ScrambleNotConfiguredErrorOptions | RecordNotFoundErrorOptions | QueryNotFakedErrorOptions | NotImplementedErrorOptions | MongoIdMappingErrorErrorOptions | InvalidStoreNameErrorOptions | InvalidStoreErrorOptions | InvalidFakeQueryResponseErrorOptions | InvalidDbConnectionStringErrorOptions | InvalidDatabaseNameErrorOptions | InvalidConnectionStringSchemeErrorOptions | IndexNotFoundErrorOptions | IndexExistsErrorOptions | FailedToLoadStoresErrorOptions | FailedToLoadStoreErrorOptions | DuplicateRecordErrorOptions | DuplicateKeyErrorOptions | DatabaseNotConnectedErrorOptions;
|
|
67
70
|
export default ErrorOptions;
|
|
@@ -40,7 +40,8 @@ export default class NeDbDatabase extends AbstractMutexer implements Database {
|
|
|
40
40
|
createIndex(collection: string, fields: string[]): Promise<void>;
|
|
41
41
|
syncUniqueIndexes(collectionName: string, indexes: string[][]): Promise<void>;
|
|
42
42
|
syncIndexes(collectionName: string, indexes: string[][]): Promise<void>;
|
|
43
|
-
query<T>(query: string, params?:
|
|
43
|
+
query<T>(query: string, params?: any[]): Promise<T[]>;
|
|
44
|
+
private assertValidFakedQueryResponse;
|
|
44
45
|
fakeQuery<T>(query: string, cb: FakeQueryHandler<T>): void;
|
|
45
46
|
}
|
|
46
|
-
export type FakeQueryHandler<T> = (params?: Record<string, any>) => Promise<T> | T;
|
|
47
|
+
export type FakeQueryHandler<T> = (params?: Record<string, any>) => Promise<T[]> | T[];
|
|
@@ -462,13 +462,24 @@ class NeDbDatabase extends AbstractMutexer_1.default {
|
|
|
462
462
|
async query(query, params) {
|
|
463
463
|
const cb = this.fakedQueries[query];
|
|
464
464
|
if (cb) {
|
|
465
|
-
|
|
465
|
+
const results = await cb(params);
|
|
466
|
+
this.assertValidFakedQueryResponse(results, query);
|
|
467
|
+
return results;
|
|
466
468
|
}
|
|
467
469
|
throw new SpruceError_1.default({
|
|
468
470
|
code: 'QUERY_NOT_FAKED',
|
|
469
471
|
query,
|
|
470
472
|
});
|
|
471
473
|
}
|
|
474
|
+
assertValidFakedQueryResponse(results, query) {
|
|
475
|
+
if (!Array.isArray(results)) {
|
|
476
|
+
throw new SpruceError_1.default({
|
|
477
|
+
code: 'INVALID_FAKE_QUERY_RESPONSE',
|
|
478
|
+
query,
|
|
479
|
+
response: results,
|
|
480
|
+
});
|
|
481
|
+
}
|
|
482
|
+
}
|
|
472
483
|
fakeQuery(query, cb) {
|
|
473
484
|
this.fakedQueries[query] = cb;
|
|
474
485
|
}
|
|
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
const error_1 = __importDefault(require("@sprucelabs/error"));
|
|
7
7
|
class SpruceError extends error_1.default {
|
|
8
8
|
friendlyMessage() {
|
|
9
|
-
var _a, _b, _c;
|
|
9
|
+
var _a, _b, _c, _d;
|
|
10
10
|
const { options } = this;
|
|
11
11
|
let message;
|
|
12
12
|
switch (options === null || options === void 0 ? void 0 : options.code) {
|
|
@@ -103,12 +103,15 @@ If you are on a mac, using brew is recommended: https://brew.sh`;
|
|
|
103
103
|
message = 'A Not implemented just happened!';
|
|
104
104
|
break;
|
|
105
105
|
case 'QUERY_NOT_FAKED':
|
|
106
|
-
message = `The query '${options.query}' was not faked. Try this.
|
|
106
|
+
message = `The query '${options.query}' was not faked. Try this.db.fakeQuery(...).`;
|
|
107
|
+
break;
|
|
108
|
+
case 'INVALID_FAKE_QUERY_RESPONSE':
|
|
109
|
+
message = `The query '${options.query}' was faked but the response was not an array. Make sure this.db.fakeQuery('${options.query}', () => []) returns an array.`;
|
|
107
110
|
break;
|
|
108
111
|
default:
|
|
109
112
|
message = super.friendlyMessage();
|
|
110
113
|
}
|
|
111
|
-
const fullMessage =
|
|
114
|
+
const fullMessage = (_d = options.friendlyMessage) !== null && _d !== void 0 ? _d : message;
|
|
112
115
|
return fullMessage;
|
|
113
116
|
}
|
|
114
117
|
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
declare const _default: {
|
|
2
|
+
id: string;
|
|
3
|
+
name: string;
|
|
4
|
+
fields: {
|
|
5
|
+
query: {
|
|
6
|
+
type: "text";
|
|
7
|
+
isRequired: true;
|
|
8
|
+
};
|
|
9
|
+
response: {
|
|
10
|
+
type: "raw";
|
|
11
|
+
isRequired: true;
|
|
12
|
+
options: {
|
|
13
|
+
valueType: string;
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
export default _default;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const schema_1 = require("@sprucelabs/schema");
|
|
4
|
+
exports.default = (0, schema_1.buildErrorSchema)({
|
|
5
|
+
id: 'invalidFakeQueryResponse',
|
|
6
|
+
name: 'Invalid fake query response',
|
|
7
|
+
fields: {
|
|
8
|
+
query: {
|
|
9
|
+
type: 'text',
|
|
10
|
+
isRequired: true,
|
|
11
|
+
},
|
|
12
|
+
response: {
|
|
13
|
+
type: 'raw',
|
|
14
|
+
isRequired: true,
|
|
15
|
+
options: {
|
|
16
|
+
valueType: 'any',
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
});
|
|
@@ -221,6 +221,34 @@ export declare namespace SpruceErrors.DataStores {
|
|
|
221
221
|
}
|
|
222
222
|
type InvalidStoreEntity = SchemaEntity<SpruceErrors.DataStores.InvalidStoreSchema>;
|
|
223
223
|
}
|
|
224
|
+
export declare namespace SpruceErrors.DataStores {
|
|
225
|
+
interface InvalidFakeQueryResponse {
|
|
226
|
+
'query': string;
|
|
227
|
+
'response': (any);
|
|
228
|
+
}
|
|
229
|
+
interface InvalidFakeQueryResponseSchema extends SpruceSchema.Schema {
|
|
230
|
+
id: 'invalidFakeQueryResponse';
|
|
231
|
+
namespace: 'DataStores';
|
|
232
|
+
name: 'Invalid fake query response';
|
|
233
|
+
fields: {
|
|
234
|
+
/** . */
|
|
235
|
+
'query': {
|
|
236
|
+
type: 'text';
|
|
237
|
+
isRequired: true;
|
|
238
|
+
options: undefined;
|
|
239
|
+
};
|
|
240
|
+
/** . */
|
|
241
|
+
'response': {
|
|
242
|
+
type: 'raw';
|
|
243
|
+
isRequired: true;
|
|
244
|
+
options: {
|
|
245
|
+
valueType: `any`;
|
|
246
|
+
};
|
|
247
|
+
};
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
type InvalidFakeQueryResponseEntity = SchemaEntity<SpruceErrors.DataStores.InvalidFakeQueryResponseSchema>;
|
|
251
|
+
}
|
|
224
252
|
export declare namespace SpruceErrors.DataStores {
|
|
225
253
|
interface InvalidDbConnectionString {
|
|
226
254
|
}
|
|
@@ -33,6 +33,9 @@ export interface InvalidStoreNameErrorOptions extends SpruceErrors.DataStores.In
|
|
|
33
33
|
export interface InvalidStoreErrorOptions extends SpruceErrors.DataStores.InvalidStore, ISpruceErrorOptions {
|
|
34
34
|
code: 'INVALID_STORE';
|
|
35
35
|
}
|
|
36
|
+
export interface InvalidFakeQueryResponseErrorOptions extends SpruceErrors.DataStores.InvalidFakeQueryResponse, ISpruceErrorOptions {
|
|
37
|
+
code: 'INVALID_FAKE_QUERY_RESPONSE';
|
|
38
|
+
}
|
|
36
39
|
export interface InvalidDbConnectionStringErrorOptions extends SpruceErrors.DataStores.InvalidDbConnectionString, ISpruceErrorOptions {
|
|
37
40
|
code: 'INVALID_DB_CONNECTION_STRING';
|
|
38
41
|
}
|
|
@@ -63,5 +66,5 @@ export interface DuplicateKeyErrorOptions extends SpruceErrors.DataStores.Duplic
|
|
|
63
66
|
export interface DatabaseNotConnectedErrorOptions extends SpruceErrors.DataStores.DatabaseNotConnected, ISpruceErrorOptions {
|
|
64
67
|
code: 'DATABASE_NOT_CONNECTED';
|
|
65
68
|
}
|
|
66
|
-
type ErrorOptions = UnknownStoreErrorErrorOptions | UnknownErrorErrorOptions | UnknownDatabaseErrorErrorOptions | UnableToConnectToDbErrorOptions | ScrambleNotConfiguredErrorOptions | RecordNotFoundErrorOptions | QueryNotFakedErrorOptions | NotImplementedErrorOptions | MongoIdMappingErrorErrorOptions | InvalidStoreNameErrorOptions | InvalidStoreErrorOptions | InvalidDbConnectionStringErrorOptions | InvalidDatabaseNameErrorOptions | InvalidConnectionStringSchemeErrorOptions | IndexNotFoundErrorOptions | IndexExistsErrorOptions | FailedToLoadStoresErrorOptions | FailedToLoadStoreErrorOptions | DuplicateRecordErrorOptions | DuplicateKeyErrorOptions | DatabaseNotConnectedErrorOptions;
|
|
69
|
+
type ErrorOptions = UnknownStoreErrorErrorOptions | UnknownErrorErrorOptions | UnknownDatabaseErrorErrorOptions | UnableToConnectToDbErrorOptions | ScrambleNotConfiguredErrorOptions | RecordNotFoundErrorOptions | QueryNotFakedErrorOptions | NotImplementedErrorOptions | MongoIdMappingErrorErrorOptions | InvalidStoreNameErrorOptions | InvalidStoreErrorOptions | InvalidFakeQueryResponseErrorOptions | InvalidDbConnectionStringErrorOptions | InvalidDatabaseNameErrorOptions | InvalidConnectionStringSchemeErrorOptions | IndexNotFoundErrorOptions | IndexExistsErrorOptions | FailedToLoadStoresErrorOptions | FailedToLoadStoreErrorOptions | DuplicateRecordErrorOptions | DuplicateKeyErrorOptions | DatabaseNotConnectedErrorOptions;
|
|
67
70
|
export default ErrorOptions;
|
|
@@ -40,7 +40,8 @@ export default class NeDbDatabase extends AbstractMutexer implements Database {
|
|
|
40
40
|
createIndex(collection: string, fields: string[]): Promise<void>;
|
|
41
41
|
syncUniqueIndexes(collectionName: string, indexes: string[][]): Promise<void>;
|
|
42
42
|
syncIndexes(collectionName: string, indexes: string[][]): Promise<void>;
|
|
43
|
-
query<T>(query: string, params?:
|
|
43
|
+
query<T>(query: string, params?: any[]): Promise<T[]>;
|
|
44
|
+
private assertValidFakedQueryResponse;
|
|
44
45
|
fakeQuery<T>(query: string, cb: FakeQueryHandler<T>): void;
|
|
45
46
|
}
|
|
46
|
-
export type FakeQueryHandler<T> = (params?: Record<string, any>) => Promise<T> | T;
|
|
47
|
+
export type FakeQueryHandler<T> = (params?: Record<string, any>) => Promise<T[]> | T[];
|
|
@@ -513,7 +513,9 @@ export default class NeDbDatabase extends AbstractMutexer {
|
|
|
513
513
|
return __awaiter(this, void 0, void 0, function* () {
|
|
514
514
|
const cb = this.fakedQueries[query];
|
|
515
515
|
if (cb) {
|
|
516
|
-
|
|
516
|
+
const results = yield cb(params);
|
|
517
|
+
this.assertValidFakedQueryResponse(results, query);
|
|
518
|
+
return results;
|
|
517
519
|
}
|
|
518
520
|
throw new SpruceError({
|
|
519
521
|
code: 'QUERY_NOT_FAKED',
|
|
@@ -521,6 +523,15 @@ export default class NeDbDatabase extends AbstractMutexer {
|
|
|
521
523
|
});
|
|
522
524
|
});
|
|
523
525
|
}
|
|
526
|
+
assertValidFakedQueryResponse(results, query) {
|
|
527
|
+
if (!Array.isArray(results)) {
|
|
528
|
+
throw new SpruceError({
|
|
529
|
+
code: 'INVALID_FAKE_QUERY_RESPONSE',
|
|
530
|
+
query,
|
|
531
|
+
response: results,
|
|
532
|
+
});
|
|
533
|
+
}
|
|
534
|
+
}
|
|
524
535
|
fakeQuery(query, cb) {
|
|
525
536
|
this.fakedQueries[query] = cb;
|
|
526
537
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import AbstractSpruceError from '@sprucelabs/error';
|
|
2
2
|
export default class SpruceError extends AbstractSpruceError {
|
|
3
3
|
friendlyMessage() {
|
|
4
|
-
var _a, _b, _c;
|
|
4
|
+
var _a, _b, _c, _d;
|
|
5
5
|
const { options } = this;
|
|
6
6
|
let message;
|
|
7
7
|
switch (options === null || options === void 0 ? void 0 : options.code) {
|
|
@@ -98,12 +98,15 @@ If you are on a mac, using brew is recommended: https://brew.sh`;
|
|
|
98
98
|
message = 'A Not implemented just happened!';
|
|
99
99
|
break;
|
|
100
100
|
case 'QUERY_NOT_FAKED':
|
|
101
|
-
message = `The query '${options.query}' was not faked. Try this.
|
|
101
|
+
message = `The query '${options.query}' was not faked. Try this.db.fakeQuery(...).`;
|
|
102
|
+
break;
|
|
103
|
+
case 'INVALID_FAKE_QUERY_RESPONSE':
|
|
104
|
+
message = `The query '${options.query}' was faked but the response was not an array. Make sure this.db.fakeQuery('${options.query}', () => []) returns an array.`;
|
|
102
105
|
break;
|
|
103
106
|
default:
|
|
104
107
|
message = super.friendlyMessage();
|
|
105
108
|
}
|
|
106
|
-
const fullMessage =
|
|
109
|
+
const fullMessage = (_d = options.friendlyMessage) !== null && _d !== void 0 ? _d : message;
|
|
107
110
|
return fullMessage;
|
|
108
111
|
}
|
|
109
112
|
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
declare const _default: {
|
|
2
|
+
id: string;
|
|
3
|
+
name: string;
|
|
4
|
+
fields: {
|
|
5
|
+
query: {
|
|
6
|
+
type: "text";
|
|
7
|
+
isRequired: true;
|
|
8
|
+
};
|
|
9
|
+
response: {
|
|
10
|
+
type: "raw";
|
|
11
|
+
isRequired: true;
|
|
12
|
+
options: {
|
|
13
|
+
valueType: string;
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
export default _default;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { buildErrorSchema } from '@sprucelabs/schema';
|
|
2
|
+
export default buildErrorSchema({
|
|
3
|
+
id: 'invalidFakeQueryResponse',
|
|
4
|
+
name: 'Invalid fake query response',
|
|
5
|
+
fields: {
|
|
6
|
+
query: {
|
|
7
|
+
type: 'text',
|
|
8
|
+
isRequired: true,
|
|
9
|
+
},
|
|
10
|
+
response: {
|
|
11
|
+
type: 'raw',
|
|
12
|
+
isRequired: true,
|
|
13
|
+
options: {
|
|
14
|
+
valueType: 'any',
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
},
|
|
18
|
+
});
|
|
@@ -4,11 +4,13 @@ export default class DatabaseFactory {
|
|
|
4
4
|
private static Adapters;
|
|
5
5
|
private constructor();
|
|
6
6
|
static addAdapter(scheme: string, Adapter: DatabaseConstructor): void;
|
|
7
|
-
static Database(options:
|
|
8
|
-
dbName?: string;
|
|
9
|
-
dbConnectionString: string;
|
|
10
|
-
}): Database;
|
|
7
|
+
static Database(options: DatabaseFactoryOptions): Database;
|
|
11
8
|
private static generateCacheKey;
|
|
12
9
|
static reset(): void;
|
|
13
10
|
}
|
|
14
11
|
export type DatabaseConstructor = new (connectionString: string, options: DatabaseOptions) => Database;
|
|
12
|
+
interface DatabaseFactoryOptions {
|
|
13
|
+
dbName?: string;
|
|
14
|
+
dbConnectionString: string;
|
|
15
|
+
}
|
|
16
|
+
export {};
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { FakeQueryHandler } from '../databases/NeDbDatabase';
|
|
1
2
|
import { Database } from '../types/database.types';
|
|
2
3
|
export interface DatabaseFixtureOptions {
|
|
3
4
|
dbConnectionString?: string;
|
|
@@ -12,6 +13,7 @@ export default class DatabaseFixture {
|
|
|
12
13
|
constructor(options?: DatabaseFixtureOptions);
|
|
13
14
|
connectToDatabase(): Promise<Database>;
|
|
14
15
|
private static connect;
|
|
16
|
+
fakeQuery<T>(query: string, cb: FakeQueryHandler<T>): void;
|
|
15
17
|
static setDefaultConnectOptions(options: DatabaseFixtureOptions): void;
|
|
16
18
|
static generateDbName(): string;
|
|
17
19
|
getDbName(): string;
|
|
@@ -9,6 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
};
|
|
10
10
|
import { SchemaError } from '@sprucelabs/schema';
|
|
11
11
|
import { MONGO_TEST_URI } from '../databases/MongoDatabase.js';
|
|
12
|
+
import SpruceError from '../errors/SpruceError.js';
|
|
12
13
|
import DatabaseFactory from '../factories/DatabaseFactory.js';
|
|
13
14
|
const MEMORY = 'memory://';
|
|
14
15
|
class DatabaseFixture {
|
|
@@ -53,6 +54,17 @@ class DatabaseFixture {
|
|
|
53
54
|
return database;
|
|
54
55
|
});
|
|
55
56
|
}
|
|
57
|
+
fakeQuery(query, cb) {
|
|
58
|
+
const db = DatabaseFixture.activeDatabases[0];
|
|
59
|
+
if (!db) {
|
|
60
|
+
throw new SpruceError({
|
|
61
|
+
code: 'DATABASE_NOT_CONNECTED',
|
|
62
|
+
operationAttempted: 'fakeQuery',
|
|
63
|
+
friendlyMessage: "You can't fake a query until you connect to a database.",
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
db.fakeQuery(query, cb);
|
|
67
|
+
}
|
|
56
68
|
static setDefaultConnectOptions(options) {
|
|
57
69
|
this.defaultOptions = options;
|
|
58
70
|
}
|
|
@@ -26,7 +26,7 @@ export interface Database {
|
|
|
26
26
|
count(collection: string, query?: Record<string, any>): Promise<number>;
|
|
27
27
|
createUniqueIndex(collection: string, fields: UniqueIndex): Promise<void>;
|
|
28
28
|
createIndex(collection: string, fields: Index): Promise<void>;
|
|
29
|
-
query<T>(query: string, params?: Record<string, any>): Promise<T>;
|
|
29
|
+
query<T>(query: string, params?: Record<string, any>): Promise<T[]>;
|
|
30
30
|
}
|
|
31
31
|
export interface DatabaseOptions {
|
|
32
32
|
dbName?: string;
|
|
@@ -4,11 +4,13 @@ export default class DatabaseFactory {
|
|
|
4
4
|
private static Adapters;
|
|
5
5
|
private constructor();
|
|
6
6
|
static addAdapter(scheme: string, Adapter: DatabaseConstructor): void;
|
|
7
|
-
static Database(options:
|
|
8
|
-
dbName?: string;
|
|
9
|
-
dbConnectionString: string;
|
|
10
|
-
}): Database;
|
|
7
|
+
static Database(options: DatabaseFactoryOptions): Database;
|
|
11
8
|
private static generateCacheKey;
|
|
12
9
|
static reset(): void;
|
|
13
10
|
}
|
|
14
11
|
export type DatabaseConstructor = new (connectionString: string, options: DatabaseOptions) => Database;
|
|
12
|
+
interface DatabaseFactoryOptions {
|
|
13
|
+
dbName?: string;
|
|
14
|
+
dbConnectionString: string;
|
|
15
|
+
}
|
|
16
|
+
export {};
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { FakeQueryHandler } from '../databases/NeDbDatabase';
|
|
1
2
|
import { Database } from '../types/database.types';
|
|
2
3
|
export interface DatabaseFixtureOptions {
|
|
3
4
|
dbConnectionString?: string;
|
|
@@ -12,6 +13,7 @@ export default class DatabaseFixture {
|
|
|
12
13
|
constructor(options?: DatabaseFixtureOptions);
|
|
13
14
|
connectToDatabase(): Promise<Database>;
|
|
14
15
|
private static connect;
|
|
16
|
+
fakeQuery<T>(query: string, cb: FakeQueryHandler<T>): void;
|
|
15
17
|
static setDefaultConnectOptions(options: DatabaseFixtureOptions): void;
|
|
16
18
|
static generateDbName(): string;
|
|
17
19
|
getDbName(): string;
|
|
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
const schema_1 = require("@sprucelabs/schema");
|
|
7
7
|
const MongoDatabase_1 = require("../databases/MongoDatabase");
|
|
8
|
+
const SpruceError_1 = __importDefault(require("../errors/SpruceError"));
|
|
8
9
|
const DatabaseFactory_1 = __importDefault(require("../factories/DatabaseFactory"));
|
|
9
10
|
const MEMORY = 'memory://';
|
|
10
11
|
class DatabaseFixture {
|
|
@@ -45,6 +46,17 @@ class DatabaseFixture {
|
|
|
45
46
|
DatabaseFixture.activeDatabases.push(database);
|
|
46
47
|
return database;
|
|
47
48
|
}
|
|
49
|
+
fakeQuery(query, cb) {
|
|
50
|
+
const db = DatabaseFixture.activeDatabases[0];
|
|
51
|
+
if (!db) {
|
|
52
|
+
throw new SpruceError_1.default({
|
|
53
|
+
code: 'DATABASE_NOT_CONNECTED',
|
|
54
|
+
operationAttempted: 'fakeQuery',
|
|
55
|
+
friendlyMessage: "You can't fake a query until you connect to a database.",
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
db.fakeQuery(query, cb);
|
|
59
|
+
}
|
|
48
60
|
static setDefaultConnectOptions(options) {
|
|
49
61
|
this.defaultOptions = options;
|
|
50
62
|
}
|
|
@@ -26,7 +26,7 @@ export interface Database {
|
|
|
26
26
|
count(collection: string, query?: Record<string, any>): Promise<number>;
|
|
27
27
|
createUniqueIndex(collection: string, fields: UniqueIndex): Promise<void>;
|
|
28
28
|
createIndex(collection: string, fields: Index): Promise<void>;
|
|
29
|
-
query<T>(query: string, params?: Record<string, any>): Promise<T>;
|
|
29
|
+
query<T>(query: string, params?: Record<string, any>): Promise<T[]>;
|
|
30
30
|
}
|
|
31
31
|
export interface DatabaseOptions {
|
|
32
32
|
dbName?: string;
|