@sprucelabs/data-stores 26.3.0 → 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.
@@ -0,0 +1,3 @@
1
+ import { SpruceErrors } from '../errors.types';
2
+ declare const invalidFakeQueryResponseSchema: SpruceErrors.DataStores.InvalidFakeQueryResponseSchema;
3
+ export default invalidFakeQueryResponseSchema;
@@ -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?: Record<string, any>): Promise<T>;
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
- return cb(params);
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
  }
@@ -103,7 +103,10 @@ 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.stores.fakeQuery(...).`;
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();
@@ -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?: Record<string, any>): Promise<T>;
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
- return cb(params);
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
  }
@@ -98,7 +98,10 @@ 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.stores.fakeQuery(...).`;
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();
@@ -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
+ });
@@ -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;
@@ -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;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "26.3.0",
6
+ "version": "26.3.1",
7
7
  "files": [
8
8
  "build/**/*",
9
9
  "!build/__tests__",