@sprucelabs/data-stores 26.2.9 → 26.3.1
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/dataStores/queryNotFaked.schema.d.ts +3 -0
- package/build/.spruce/errors/dataStores/queryNotFaked.schema.js +23 -0
- package/build/.spruce/errors/errors.types.d.ts +55 -0
- package/build/.spruce/errors/options.types.d.ts +7 -1
- package/build/databases/MongoDatabase.js +1 -0
- package/build/databases/NeDbDatabase.d.ts +5 -1
- package/build/databases/NeDbDatabase.js +22 -2
- package/build/errors/SpruceError.js +6 -0
- package/build/errors/invalidFakeQueryResponse.builder.d.ts +18 -0
- package/build/errors/invalidFakeQueryResponse.builder.js +20 -0
- package/build/errors/queryNotFaked.builder.d.ts +17 -0
- package/build/errors/queryNotFaked.builder.js +17 -0
- package/build/esm/.spruce/errors/errors.types.d.ts +55 -0
- package/build/esm/.spruce/errors/options.types.d.ts +7 -1
- package/build/esm/databases/MongoDatabase.js +1 -0
- package/build/esm/databases/NeDbDatabase.d.ts +5 -1
- package/build/esm/databases/NeDbDatabase.js +22 -2
- package/build/esm/errors/SpruceError.js +6 -0
- package/build/esm/errors/invalidFakeQueryResponse.builder.d.ts +18 -0
- package/build/esm/errors/invalidFakeQueryResponse.builder.js +18 -0
- package/build/esm/errors/queryNotFaked.builder.d.ts +17 -0
- package/build/esm/errors/queryNotFaked.builder.js +15 -0
- package/build/esm/types/database.types.d.ts +1 -1
- 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;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const schema_1 = require("@sprucelabs/schema");
|
|
4
|
+
const queryNotFakedSchema = {
|
|
5
|
+
id: 'queryNotFaked',
|
|
6
|
+
namespace: 'DataStores',
|
|
7
|
+
name: 'Query not faked',
|
|
8
|
+
fields: {
|
|
9
|
+
/** . */
|
|
10
|
+
'query': {
|
|
11
|
+
type: 'text',
|
|
12
|
+
isRequired: true,
|
|
13
|
+
options: undefined
|
|
14
|
+
},
|
|
15
|
+
/** . */
|
|
16
|
+
'params': {
|
|
17
|
+
type: 'raw',
|
|
18
|
+
options: { valueType: `Record<string, any>`, }
|
|
19
|
+
},
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
schema_1.SchemaRegistry.getInstance().trackSchema(queryNotFakedSchema);
|
|
23
|
+
exports.default = queryNotFakedSchema;
|
|
@@ -134,6 +134,33 @@ export declare namespace SpruceErrors.DataStores {
|
|
|
134
134
|
}
|
|
135
135
|
type RecordNotFoundEntity = SchemaEntity<SpruceErrors.DataStores.RecordNotFoundSchema>;
|
|
136
136
|
}
|
|
137
|
+
export declare namespace SpruceErrors.DataStores {
|
|
138
|
+
interface QueryNotFaked {
|
|
139
|
+
'query': string;
|
|
140
|
+
'params'?: (Record<string, any>) | undefined | null;
|
|
141
|
+
}
|
|
142
|
+
interface QueryNotFakedSchema extends SpruceSchema.Schema {
|
|
143
|
+
id: 'queryNotFaked';
|
|
144
|
+
namespace: 'DataStores';
|
|
145
|
+
name: 'Query not faked';
|
|
146
|
+
fields: {
|
|
147
|
+
/** . */
|
|
148
|
+
'query': {
|
|
149
|
+
type: 'text';
|
|
150
|
+
isRequired: true;
|
|
151
|
+
options: undefined;
|
|
152
|
+
};
|
|
153
|
+
/** . */
|
|
154
|
+
'params': {
|
|
155
|
+
type: 'raw';
|
|
156
|
+
options: {
|
|
157
|
+
valueType: `Record<string, any>`;
|
|
158
|
+
};
|
|
159
|
+
};
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
type QueryNotFakedEntity = SchemaEntity<SpruceErrors.DataStores.QueryNotFakedSchema>;
|
|
163
|
+
}
|
|
137
164
|
export declare namespace SpruceErrors.DataStores {
|
|
138
165
|
interface NotImplemented {
|
|
139
166
|
}
|
|
@@ -194,6 +221,34 @@ export declare namespace SpruceErrors.DataStores {
|
|
|
194
221
|
}
|
|
195
222
|
type InvalidStoreEntity = SchemaEntity<SpruceErrors.DataStores.InvalidStoreSchema>;
|
|
196
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
|
+
}
|
|
197
252
|
export declare namespace SpruceErrors.DataStores {
|
|
198
253
|
interface InvalidDbConnectionString {
|
|
199
254
|
}
|
|
@@ -18,6 +18,9 @@ export interface ScrambleNotConfiguredErrorOptions extends SpruceErrors.DataStor
|
|
|
18
18
|
export interface RecordNotFoundErrorOptions extends SpruceErrors.DataStores.RecordNotFound, ISpruceErrorOptions {
|
|
19
19
|
code: 'RECORD_NOT_FOUND';
|
|
20
20
|
}
|
|
21
|
+
export interface QueryNotFakedErrorOptions extends SpruceErrors.DataStores.QueryNotFaked, ISpruceErrorOptions {
|
|
22
|
+
code: 'QUERY_NOT_FAKED';
|
|
23
|
+
}
|
|
21
24
|
export interface NotImplementedErrorOptions extends SpruceErrors.DataStores.NotImplemented, ISpruceErrorOptions {
|
|
22
25
|
code: 'NOT_IMPLEMENTED';
|
|
23
26
|
}
|
|
@@ -30,6 +33,9 @@ export interface InvalidStoreNameErrorOptions extends SpruceErrors.DataStores.In
|
|
|
30
33
|
export interface InvalidStoreErrorOptions extends SpruceErrors.DataStores.InvalidStore, ISpruceErrorOptions {
|
|
31
34
|
code: 'INVALID_STORE';
|
|
32
35
|
}
|
|
36
|
+
export interface InvalidFakeQueryResponseErrorOptions extends SpruceErrors.DataStores.InvalidFakeQueryResponse, ISpruceErrorOptions {
|
|
37
|
+
code: 'INVALID_FAKE_QUERY_RESPONSE';
|
|
38
|
+
}
|
|
33
39
|
export interface InvalidDbConnectionStringErrorOptions extends SpruceErrors.DataStores.InvalidDbConnectionString, ISpruceErrorOptions {
|
|
34
40
|
code: 'INVALID_DB_CONNECTION_STRING';
|
|
35
41
|
}
|
|
@@ -60,5 +66,5 @@ export interface DuplicateKeyErrorOptions extends SpruceErrors.DataStores.Duplic
|
|
|
60
66
|
export interface DatabaseNotConnectedErrorOptions extends SpruceErrors.DataStores.DatabaseNotConnected, ISpruceErrorOptions {
|
|
61
67
|
code: 'DATABASE_NOT_CONNECTED';
|
|
62
68
|
}
|
|
63
|
-
type ErrorOptions = UnknownStoreErrorErrorOptions | UnknownErrorErrorOptions | UnknownDatabaseErrorErrorOptions | UnableToConnectToDbErrorOptions | ScrambleNotConfiguredErrorOptions | RecordNotFoundErrorOptions | 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;
|
|
64
70
|
export default ErrorOptions;
|
|
@@ -4,6 +4,7 @@ import { QueryOptions } from '../types/query.types';
|
|
|
4
4
|
export default class NeDbDatabase extends AbstractMutexer implements Database {
|
|
5
5
|
private collections;
|
|
6
6
|
private _isConnected;
|
|
7
|
+
private fakedQueries;
|
|
7
8
|
generateId(): string;
|
|
8
9
|
connect(): Promise<void>;
|
|
9
10
|
close(): Promise<void>;
|
|
@@ -39,5 +40,8 @@ export default class NeDbDatabase extends AbstractMutexer implements Database {
|
|
|
39
40
|
createIndex(collection: string, fields: string[]): Promise<void>;
|
|
40
41
|
syncUniqueIndexes(collectionName: string, indexes: string[][]): Promise<void>;
|
|
41
42
|
syncIndexes(collectionName: string, indexes: string[][]): Promise<void>;
|
|
42
|
-
query<T>(): Promise<T>;
|
|
43
|
+
query<T>(query: string, params?: any[]): Promise<T[]>;
|
|
44
|
+
private assertValidFakedQueryResponse;
|
|
45
|
+
fakeQuery<T>(query: string, cb: FakeQueryHandler<T>): void;
|
|
43
46
|
}
|
|
47
|
+
export type FakeQueryHandler<T> = (params?: Record<string, any>) => Promise<T[]> | T[];
|
|
@@ -36,6 +36,7 @@ class NeDbDatabase extends AbstractMutexer_1.default {
|
|
|
36
36
|
super(...arguments);
|
|
37
37
|
this.collections = {};
|
|
38
38
|
this._isConnected = false;
|
|
39
|
+
this.fakedQueries = {};
|
|
39
40
|
}
|
|
40
41
|
generateId() {
|
|
41
42
|
return (0, generateId_1.default)();
|
|
@@ -458,10 +459,29 @@ class NeDbDatabase extends AbstractMutexer_1.default {
|
|
|
458
459
|
await this.dropIndex(collectionName, extra);
|
|
459
460
|
}
|
|
460
461
|
}
|
|
461
|
-
async query() {
|
|
462
|
+
async query(query, params) {
|
|
463
|
+
const cb = this.fakedQueries[query];
|
|
464
|
+
if (cb) {
|
|
465
|
+
const results = await cb(params);
|
|
466
|
+
this.assertValidFakedQueryResponse(results, query);
|
|
467
|
+
return results;
|
|
468
|
+
}
|
|
462
469
|
throw new SpruceError_1.default({
|
|
463
|
-
code: '
|
|
470
|
+
code: 'QUERY_NOT_FAKED',
|
|
471
|
+
query,
|
|
464
472
|
});
|
|
465
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
|
+
}
|
|
483
|
+
fakeQuery(query, cb) {
|
|
484
|
+
this.fakedQueries[query] = cb;
|
|
485
|
+
}
|
|
466
486
|
}
|
|
467
487
|
exports.default = NeDbDatabase;
|
|
@@ -102,6 +102,12 @@ If you are on a mac, using brew is recommended: https://brew.sh`;
|
|
|
102
102
|
case 'NOT_IMPLEMENTED':
|
|
103
103
|
message = 'A Not implemented just happened!';
|
|
104
104
|
break;
|
|
105
|
+
case 'QUERY_NOT_FAKED':
|
|
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.`;
|
|
110
|
+
break;
|
|
105
111
|
default:
|
|
106
112
|
message = super.friendlyMessage();
|
|
107
113
|
}
|
|
@@ -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
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
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: 'queryNotFaked',
|
|
6
|
+
name: 'Query not faked',
|
|
7
|
+
fields: {
|
|
8
|
+
query: {
|
|
9
|
+
type: 'text',
|
|
10
|
+
isRequired: true,
|
|
11
|
+
},
|
|
12
|
+
params: {
|
|
13
|
+
type: 'raw',
|
|
14
|
+
options: { valueType: 'Record<string, any>' },
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
});
|
|
@@ -134,6 +134,33 @@ export declare namespace SpruceErrors.DataStores {
|
|
|
134
134
|
}
|
|
135
135
|
type RecordNotFoundEntity = SchemaEntity<SpruceErrors.DataStores.RecordNotFoundSchema>;
|
|
136
136
|
}
|
|
137
|
+
export declare namespace SpruceErrors.DataStores {
|
|
138
|
+
interface QueryNotFaked {
|
|
139
|
+
'query': string;
|
|
140
|
+
'params'?: (Record<string, any>) | undefined | null;
|
|
141
|
+
}
|
|
142
|
+
interface QueryNotFakedSchema extends SpruceSchema.Schema {
|
|
143
|
+
id: 'queryNotFaked';
|
|
144
|
+
namespace: 'DataStores';
|
|
145
|
+
name: 'Query not faked';
|
|
146
|
+
fields: {
|
|
147
|
+
/** . */
|
|
148
|
+
'query': {
|
|
149
|
+
type: 'text';
|
|
150
|
+
isRequired: true;
|
|
151
|
+
options: undefined;
|
|
152
|
+
};
|
|
153
|
+
/** . */
|
|
154
|
+
'params': {
|
|
155
|
+
type: 'raw';
|
|
156
|
+
options: {
|
|
157
|
+
valueType: `Record<string, any>`;
|
|
158
|
+
};
|
|
159
|
+
};
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
type QueryNotFakedEntity = SchemaEntity<SpruceErrors.DataStores.QueryNotFakedSchema>;
|
|
163
|
+
}
|
|
137
164
|
export declare namespace SpruceErrors.DataStores {
|
|
138
165
|
interface NotImplemented {
|
|
139
166
|
}
|
|
@@ -194,6 +221,34 @@ export declare namespace SpruceErrors.DataStores {
|
|
|
194
221
|
}
|
|
195
222
|
type InvalidStoreEntity = SchemaEntity<SpruceErrors.DataStores.InvalidStoreSchema>;
|
|
196
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
|
+
}
|
|
197
252
|
export declare namespace SpruceErrors.DataStores {
|
|
198
253
|
interface InvalidDbConnectionString {
|
|
199
254
|
}
|
|
@@ -18,6 +18,9 @@ export interface ScrambleNotConfiguredErrorOptions extends SpruceErrors.DataStor
|
|
|
18
18
|
export interface RecordNotFoundErrorOptions extends SpruceErrors.DataStores.RecordNotFound, ISpruceErrorOptions {
|
|
19
19
|
code: 'RECORD_NOT_FOUND';
|
|
20
20
|
}
|
|
21
|
+
export interface QueryNotFakedErrorOptions extends SpruceErrors.DataStores.QueryNotFaked, ISpruceErrorOptions {
|
|
22
|
+
code: 'QUERY_NOT_FAKED';
|
|
23
|
+
}
|
|
21
24
|
export interface NotImplementedErrorOptions extends SpruceErrors.DataStores.NotImplemented, ISpruceErrorOptions {
|
|
22
25
|
code: 'NOT_IMPLEMENTED';
|
|
23
26
|
}
|
|
@@ -30,6 +33,9 @@ export interface InvalidStoreNameErrorOptions extends SpruceErrors.DataStores.In
|
|
|
30
33
|
export interface InvalidStoreErrorOptions extends SpruceErrors.DataStores.InvalidStore, ISpruceErrorOptions {
|
|
31
34
|
code: 'INVALID_STORE';
|
|
32
35
|
}
|
|
36
|
+
export interface InvalidFakeQueryResponseErrorOptions extends SpruceErrors.DataStores.InvalidFakeQueryResponse, ISpruceErrorOptions {
|
|
37
|
+
code: 'INVALID_FAKE_QUERY_RESPONSE';
|
|
38
|
+
}
|
|
33
39
|
export interface InvalidDbConnectionStringErrorOptions extends SpruceErrors.DataStores.InvalidDbConnectionString, ISpruceErrorOptions {
|
|
34
40
|
code: 'INVALID_DB_CONNECTION_STRING';
|
|
35
41
|
}
|
|
@@ -60,5 +66,5 @@ export interface DuplicateKeyErrorOptions extends SpruceErrors.DataStores.Duplic
|
|
|
60
66
|
export interface DatabaseNotConnectedErrorOptions extends SpruceErrors.DataStores.DatabaseNotConnected, ISpruceErrorOptions {
|
|
61
67
|
code: 'DATABASE_NOT_CONNECTED';
|
|
62
68
|
}
|
|
63
|
-
type ErrorOptions = UnknownStoreErrorErrorOptions | UnknownErrorErrorOptions | UnknownDatabaseErrorErrorOptions | UnableToConnectToDbErrorOptions | ScrambleNotConfiguredErrorOptions | RecordNotFoundErrorOptions | 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;
|
|
64
70
|
export default ErrorOptions;
|
|
@@ -490,6 +490,7 @@ export default class MongoDatabase {
|
|
|
490
490
|
return __awaiter(this, void 0, void 0, function* () {
|
|
491
491
|
throw new SpruceError({
|
|
492
492
|
code: 'NOT_IMPLEMENTED',
|
|
493
|
+
friendlyMessage: `You cannot run a query using mongodb. Try a different database adapter!`,
|
|
493
494
|
});
|
|
494
495
|
});
|
|
495
496
|
}
|
|
@@ -4,6 +4,7 @@ import { QueryOptions } from '../types/query.types';
|
|
|
4
4
|
export default class NeDbDatabase extends AbstractMutexer implements Database {
|
|
5
5
|
private collections;
|
|
6
6
|
private _isConnected;
|
|
7
|
+
private fakedQueries;
|
|
7
8
|
generateId(): string;
|
|
8
9
|
connect(): Promise<void>;
|
|
9
10
|
close(): Promise<void>;
|
|
@@ -39,5 +40,8 @@ export default class NeDbDatabase extends AbstractMutexer implements Database {
|
|
|
39
40
|
createIndex(collection: string, fields: string[]): Promise<void>;
|
|
40
41
|
syncUniqueIndexes(collectionName: string, indexes: string[][]): Promise<void>;
|
|
41
42
|
syncIndexes(collectionName: string, indexes: string[][]): Promise<void>;
|
|
42
|
-
query<T>(): Promise<T>;
|
|
43
|
+
query<T>(query: string, params?: any[]): Promise<T[]>;
|
|
44
|
+
private assertValidFakedQueryResponse;
|
|
45
|
+
fakeQuery<T>(query: string, cb: FakeQueryHandler<T>): void;
|
|
43
46
|
}
|
|
47
|
+
export type FakeQueryHandler<T> = (params?: Record<string, any>) => Promise<T[]> | T[];
|
|
@@ -40,6 +40,7 @@ export default class NeDbDatabase extends AbstractMutexer {
|
|
|
40
40
|
super(...arguments);
|
|
41
41
|
this.collections = {};
|
|
42
42
|
this._isConnected = false;
|
|
43
|
+
this.fakedQueries = {};
|
|
43
44
|
}
|
|
44
45
|
generateId() {
|
|
45
46
|
return generateId();
|
|
@@ -508,11 +509,30 @@ export default class NeDbDatabase extends AbstractMutexer {
|
|
|
508
509
|
}
|
|
509
510
|
});
|
|
510
511
|
}
|
|
511
|
-
query() {
|
|
512
|
+
query(query, params) {
|
|
512
513
|
return __awaiter(this, void 0, void 0, function* () {
|
|
514
|
+
const cb = this.fakedQueries[query];
|
|
515
|
+
if (cb) {
|
|
516
|
+
const results = yield cb(params);
|
|
517
|
+
this.assertValidFakedQueryResponse(results, query);
|
|
518
|
+
return results;
|
|
519
|
+
}
|
|
513
520
|
throw new SpruceError({
|
|
514
|
-
code: '
|
|
521
|
+
code: 'QUERY_NOT_FAKED',
|
|
522
|
+
query,
|
|
515
523
|
});
|
|
516
524
|
});
|
|
517
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
|
+
}
|
|
535
|
+
fakeQuery(query, cb) {
|
|
536
|
+
this.fakedQueries[query] = cb;
|
|
537
|
+
}
|
|
518
538
|
}
|
|
@@ -97,6 +97,12 @@ If you are on a mac, using brew is recommended: https://brew.sh`;
|
|
|
97
97
|
case 'NOT_IMPLEMENTED':
|
|
98
98
|
message = 'A Not implemented just happened!';
|
|
99
99
|
break;
|
|
100
|
+
case 'QUERY_NOT_FAKED':
|
|
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.`;
|
|
105
|
+
break;
|
|
100
106
|
default:
|
|
101
107
|
message = super.friendlyMessage();
|
|
102
108
|
}
|
|
@@ -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
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { buildErrorSchema } from '@sprucelabs/schema';
|
|
2
|
+
export default buildErrorSchema({
|
|
3
|
+
id: 'queryNotFaked',
|
|
4
|
+
name: 'Query not faked',
|
|
5
|
+
fields: {
|
|
6
|
+
query: {
|
|
7
|
+
type: 'text',
|
|
8
|
+
isRequired: true,
|
|
9
|
+
},
|
|
10
|
+
params: {
|
|
11
|
+
type: 'raw',
|
|
12
|
+
options: { valueType: 'Record<string, any>' },
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
});
|
|
@@ -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): Promise<T>;
|
|
29
|
+
query<T>(query: string, params?: Record<string, any>): Promise<T[]>;
|
|
30
30
|
}
|
|
31
31
|
export interface DatabaseOptions {
|
|
32
32
|
dbName?: string;
|
|
@@ -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): Promise<T>;
|
|
29
|
+
query<T>(query: string, params?: Record<string, any>): Promise<T[]>;
|
|
30
30
|
}
|
|
31
31
|
export interface DatabaseOptions {
|
|
32
32
|
dbName?: string;
|