@sprucelabs/data-stores 29.0.45 → 30.0.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.
@@ -1,11 +1,11 @@
1
1
  import { FullQueryOptions } from '../stores/AbstractStore';
2
- import { DataStorePlugin, DataStorePluginPrepareResponse, DataStorePluginWillCreateOneResponse, DataStorePluginWillFindResponse, DataStorePluginWillUpdateOneResponse } from '../types/stores.types';
2
+ import { DataStorePlugin, DataStorePluginPrepareResponse, DataStorePluginWillCreateOneResponse, DataStorePluginWillFindResponse, DataStorePluginWillUpdateResponse } from '../types/stores.types';
3
3
  export default class DatabaseFieldMapperPlugin implements DataStorePlugin {
4
4
  private mapper;
5
5
  constructor(map: Record<string, any>);
6
6
  getName(): string;
7
7
  willCreateOne(values: Record<string, any>): Promise<void | DataStorePluginWillCreateOneResponse>;
8
8
  prepareRecord(record: Record<string, any>): Promise<void | DataStorePluginPrepareResponse>;
9
- willUpdateOne(query: Record<string, any>, updates: Record<string, any>): Promise<void | DataStorePluginWillUpdateOneResponse>;
9
+ willUpdate(query: Record<string, any>, updates: Record<string, any>): Promise<void | DataStorePluginWillUpdateResponse>;
10
10
  willFind(query: Record<string, any>, options?: FullQueryOptions): Promise<void | DataStorePluginWillFindResponse>;
11
11
  }
@@ -31,7 +31,7 @@ export default class DatabaseFieldMapperPlugin {
31
31
  };
32
32
  });
33
33
  }
34
- willUpdateOne(query, updates) {
34
+ willUpdate(query, updates) {
35
35
  return __awaiter(this, void 0, void 0, function* () {
36
36
  return {
37
37
  query: this.mapper.mapTo(query),
@@ -52,7 +52,7 @@ export default abstract class AbstractStore<FullSchema extends Schema, CreateSch
52
52
  updateOne<IncludePrivateFields extends boolean = false, CreateEntityInstances extends boolean = false, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>, PF extends SchemaPublicFieldNames<FullSchema> = SchemaPublicFieldNames<FullSchema>>(query: QueryBuilder<QueryRecord>, updates: ValuesWithPaths<UpdateRecord>, options?: PrepareOptions<IncludePrivateFields, FullSchema, F>): Promise<Response<FullSchema, CreateEntityInstances, IncludePrivateFields, PF, F>>;
53
53
  update(query: QueryBuilder<QueryRecord>, updates: ValuesWithPaths<UpdateRecord>): Promise<number>;
54
54
  private findOneAndUpdate;
55
- private handleWillUpdateOnePlugins;
55
+ private handleWillUpdatePlugins;
56
56
  private pluckOperations;
57
57
  scramble(id: string): Promise<SchemaValues<FullSchema, false, false, false, Extract<keyof FullSchema["fields"], string>, SchemaPublicFieldNames<FullSchema>>>;
58
58
  private isScrambled;
@@ -315,7 +315,8 @@ export default class AbstractStore extends AbstractMutexer {
315
315
  }
316
316
  update(query, updates) {
317
317
  return __awaiter(this, void 0, void 0, function* () {
318
- return this.db.update(this.collectionName, query, updates);
318
+ const { updates: pUpdates, query: pQuery } = yield this.handleWillUpdatePlugins(query, updates);
319
+ return this.db.update(this.collectionName, pQuery, pUpdates);
319
320
  });
320
321
  }
321
322
  findOneAndUpdate(query_1, updates_1, notFoundHandler_1) {
@@ -329,9 +330,9 @@ export default class AbstractStore extends AbstractMutexer {
329
330
  //@ts-ignore
330
331
  validateSchemaValues(this.updateSchema, initialUpdates);
331
332
  }
332
- const { query: qPlugins, shouldUpdate, updates: qUpdates, } = yield this.handleWillUpdateOnePlugins(q, initialUpdates);
333
- q = qPlugins;
334
- initialUpdates = qUpdates;
333
+ const { query: pQuery, shouldUpdate, updates: pUpdates, } = yield this.handleWillUpdatePlugins(q, initialUpdates);
334
+ q = pQuery;
335
+ initialUpdates = pUpdates;
335
336
  let current = yield this._findOne(q, Object.assign(Object.assign({}, options), { shouldIncludePrivateFields: true }), { shouldTriggerWillQuery: false });
336
337
  if (!current) {
337
338
  current = yield notFoundHandler();
@@ -376,14 +377,14 @@ export default class AbstractStore extends AbstractMutexer {
376
377
  }
377
378
  });
378
379
  }
379
- handleWillUpdateOnePlugins(query, updates) {
380
+ handleWillUpdatePlugins(query, updates) {
380
381
  return __awaiter(this, void 0, void 0, function* () {
381
382
  var _a;
382
383
  let shouldUpdate = true;
383
384
  let resolvedQuery = query;
384
385
  let resolvedUpdates = updates;
385
386
  for (const plugin of this.plugins) {
386
- const results = yield ((_a = plugin.willUpdateOne) === null || _a === void 0 ? void 0 : _a.call(plugin, query, updates));
387
+ const results = yield ((_a = plugin.willUpdate) === null || _a === void 0 ? void 0 : _a.call(plugin, query, updates));
387
388
  if ((results === null || results === void 0 ? void 0 : results.shouldUpdate) === false) {
388
389
  shouldUpdate = false;
389
390
  }
@@ -2,14 +2,14 @@ import AbstractSpruceTest from '@sprucelabs/test-utils';
2
2
  import DatabaseFixture, { DatabaseFixtureOptions } from '../fixtures/DatabaseFixture';
3
3
  import { Database } from '../types/database.types';
4
4
  export default class AbstractDatabaseTest extends AbstractSpruceTest {
5
- protected static db: Database;
6
- protected static shouldUseInMemoryDatabase: boolean;
7
- protected static DB_NAME: string;
8
- protected static beforeEach(): Promise<void>;
9
- protected static afterEach(): Promise<void>;
10
- protected static DatabaseFixture(options?: DatabaseFixtureOptions): Promise<DatabaseFixture>;
11
- protected static connectToDatabase(): Promise<Database>;
12
- protected static DatabaseConnection(): Promise<{
5
+ protected db: Database;
6
+ protected shouldUseInMemoryDatabase: boolean;
7
+ protected DB_NAME: string;
8
+ protected beforeEach(): Promise<void>;
9
+ protected afterEach(): Promise<void>;
10
+ protected DatabaseFixture(options?: DatabaseFixtureOptions): Promise<DatabaseFixture>;
11
+ protected connectToDatabase(): Promise<Database>;
12
+ protected DatabaseConnection(): Promise<{
13
13
  dbFixture: DatabaseFixture;
14
14
  db: Database;
15
15
  }>;
@@ -10,8 +10,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  import { SchemaRegistry } from '@sprucelabs/schema';
11
11
  import AbstractSpruceTest from '@sprucelabs/test-utils';
12
12
  import DatabaseFixture from '../fixtures/DatabaseFixture.js';
13
- class AbstractDatabaseTest extends AbstractSpruceTest {
14
- static beforeEach() {
13
+ export default class AbstractDatabaseTest extends AbstractSpruceTest {
14
+ constructor() {
15
+ super(...arguments);
16
+ this.shouldUseInMemoryDatabase = true;
17
+ }
18
+ beforeEach() {
15
19
  const _super = Object.create(null, {
16
20
  beforeEach: { get: () => super.beforeEach }
17
21
  });
@@ -20,7 +24,7 @@ class AbstractDatabaseTest extends AbstractSpruceTest {
20
24
  SchemaRegistry.getInstance().forgetAllSchemas();
21
25
  });
22
26
  }
23
- static afterEach() {
27
+ afterEach() {
24
28
  const _super = Object.create(null, {
25
29
  afterEach: { get: () => super.afterEach }
26
30
  });
@@ -33,13 +37,13 @@ class AbstractDatabaseTest extends AbstractSpruceTest {
33
37
  this.DB_NAME = undefined;
34
38
  });
35
39
  }
36
- static DatabaseFixture(options) {
40
+ DatabaseFixture(options) {
37
41
  return __awaiter(this, void 0, void 0, function* () {
38
42
  const d = new DatabaseFixture(Object.assign({}, options));
39
43
  return d;
40
44
  });
41
45
  }
42
- static connectToDatabase() {
46
+ connectToDatabase() {
43
47
  return __awaiter(this, void 0, void 0, function* () {
44
48
  if (!this.db) {
45
49
  const { dbFixture, db } = yield this.DatabaseConnection();
@@ -51,7 +55,7 @@ class AbstractDatabaseTest extends AbstractSpruceTest {
51
55
  return this.db;
52
56
  });
53
57
  }
54
- static DatabaseConnection() {
58
+ DatabaseConnection() {
55
59
  return __awaiter(this, void 0, void 0, function* () {
56
60
  const dbFixture = yield this.DatabaseFixture();
57
61
  const db = yield dbFixture.connectToDatabase();
@@ -59,5 +63,3 @@ class AbstractDatabaseTest extends AbstractSpruceTest {
59
63
  });
60
64
  }
61
65
  }
62
- AbstractDatabaseTest.shouldUseInMemoryDatabase = true;
63
- export default AbstractDatabaseTest;
@@ -35,7 +35,7 @@ export type StoreOptions<Name extends StoreName> = Name extends keyof StoreOptio
35
35
  export interface DataStorePlugin {
36
36
  willCreateOne?: (values: Record<string, any>) => Promise<void | DataStorePluginWillCreateOneResponse>;
37
37
  didCreateOne?: (record: Record<string, any>) => Promise<void | DataStorePluginDidCreateOneResponse>;
38
- willUpdateOne?: (query: Record<string, any>, updates: Record<string, any>) => Promise<void | DataStorePluginWillUpdateOneResponse>;
38
+ willUpdate?: (query: Record<string, any>, updates: Record<string, any>) => Promise<void | DataStorePluginWillUpdateResponse>;
39
39
  willDeleteOne?: (query: Record<string, any>) => Promise<void | DataStorePluginWillDeleteOneResponse>;
40
40
  didFindOne?: (query: Record<string, any>, record: Record<string, any>) => Promise<void | DataStorePluginDidFindOneResponse>;
41
41
  getName(): string;
@@ -52,7 +52,7 @@ export interface DataStorePluginWillCreateOneResponse {
52
52
  valuesToMixinBeforeCreate?: Record<string, any>;
53
53
  newValues?: Record<string, any>;
54
54
  }
55
- export interface DataStorePluginWillUpdateOneResponse {
55
+ export interface DataStorePluginWillUpdateResponse {
56
56
  query?: Record<string, any>;
57
57
  shouldUpdate?: boolean;
58
58
  newUpdates?: Record<string, any>;
@@ -1,11 +1,11 @@
1
1
  import { FullQueryOptions } from '../stores/AbstractStore';
2
- import { DataStorePlugin, DataStorePluginPrepareResponse, DataStorePluginWillCreateOneResponse, DataStorePluginWillFindResponse, DataStorePluginWillUpdateOneResponse } from '../types/stores.types';
2
+ import { DataStorePlugin, DataStorePluginPrepareResponse, DataStorePluginWillCreateOneResponse, DataStorePluginWillFindResponse, DataStorePluginWillUpdateResponse } from '../types/stores.types';
3
3
  export default class DatabaseFieldMapperPlugin implements DataStorePlugin {
4
4
  private mapper;
5
5
  constructor(map: Record<string, any>);
6
6
  getName(): string;
7
7
  willCreateOne(values: Record<string, any>): Promise<void | DataStorePluginWillCreateOneResponse>;
8
8
  prepareRecord(record: Record<string, any>): Promise<void | DataStorePluginPrepareResponse>;
9
- willUpdateOne(query: Record<string, any>, updates: Record<string, any>): Promise<void | DataStorePluginWillUpdateOneResponse>;
9
+ willUpdate(query: Record<string, any>, updates: Record<string, any>): Promise<void | DataStorePluginWillUpdateResponse>;
10
10
  willFind(query: Record<string, any>, options?: FullQueryOptions): Promise<void | DataStorePluginWillFindResponse>;
11
11
  }
@@ -20,7 +20,7 @@ class DatabaseFieldMapperPlugin {
20
20
  }),
21
21
  };
22
22
  }
23
- async willUpdateOne(query, updates) {
23
+ async willUpdate(query, updates) {
24
24
  return {
25
25
  query: this.mapper.mapTo(query),
26
26
  newUpdates: this.mapper.mapTo(updates),
@@ -52,7 +52,7 @@ export default abstract class AbstractStore<FullSchema extends Schema, CreateSch
52
52
  updateOne<IncludePrivateFields extends boolean = false, CreateEntityInstances extends boolean = false, F extends SchemaFieldNames<FullSchema> = SchemaFieldNames<FullSchema>, PF extends SchemaPublicFieldNames<FullSchema> = SchemaPublicFieldNames<FullSchema>>(query: QueryBuilder<QueryRecord>, updates: ValuesWithPaths<UpdateRecord>, options?: PrepareOptions<IncludePrivateFields, FullSchema, F>): Promise<Response<FullSchema, CreateEntityInstances, IncludePrivateFields, PF, F>>;
53
53
  update(query: QueryBuilder<QueryRecord>, updates: ValuesWithPaths<UpdateRecord>): Promise<number>;
54
54
  private findOneAndUpdate;
55
- private handleWillUpdateOnePlugins;
55
+ private handleWillUpdatePlugins;
56
56
  private pluckOperations;
57
57
  scramble(id: string): Promise<SchemaValues<FullSchema, false, false, false, Extract<keyof FullSchema["fields"], string>, SchemaPublicFieldNames<FullSchema>>>;
58
58
  private isScrambled;
@@ -313,7 +313,8 @@ class AbstractStore extends AbstractMutexer_1.default {
313
313
  return this.findOneAndUpdate(query, updates, notFoundHandler, options);
314
314
  }
315
315
  async update(query, updates) {
316
- return this.db.update(this.collectionName, query, updates);
316
+ const { updates: pUpdates, query: pQuery } = await this.handleWillUpdatePlugins(query, updates);
317
+ return this.db.update(this.collectionName, pQuery, pUpdates);
317
318
  }
318
319
  async findOneAndUpdate(query, updates, notFoundHandler, options = {}) {
319
320
  var _a, _b, _c;
@@ -325,9 +326,9 @@ class AbstractStore extends AbstractMutexer_1.default {
325
326
  //@ts-ignore
326
327
  (0, schema_1.validateSchemaValues)(this.updateSchema, initialUpdates);
327
328
  }
328
- const { query: qPlugins, shouldUpdate, updates: qUpdates, } = await this.handleWillUpdateOnePlugins(q, initialUpdates);
329
- q = qPlugins;
330
- initialUpdates = qUpdates;
329
+ const { query: pQuery, shouldUpdate, updates: pUpdates, } = await this.handleWillUpdatePlugins(q, initialUpdates);
330
+ q = pQuery;
331
+ initialUpdates = pUpdates;
331
332
  let current = await this._findOne(q, Object.assign(Object.assign({}, options), { shouldIncludePrivateFields: true }), { shouldTriggerWillQuery: false });
332
333
  if (!current) {
333
334
  current = await notFoundHandler();
@@ -371,13 +372,13 @@ class AbstractStore extends AbstractMutexer_1.default {
371
372
  throw coded[0];
372
373
  }
373
374
  }
374
- async handleWillUpdateOnePlugins(query, updates) {
375
+ async handleWillUpdatePlugins(query, updates) {
375
376
  var _a;
376
377
  let shouldUpdate = true;
377
378
  let resolvedQuery = query;
378
379
  let resolvedUpdates = updates;
379
380
  for (const plugin of this.plugins) {
380
- const results = await ((_a = plugin.willUpdateOne) === null || _a === void 0 ? void 0 : _a.call(plugin, query, updates));
381
+ const results = await ((_a = plugin.willUpdate) === null || _a === void 0 ? void 0 : _a.call(plugin, query, updates));
381
382
  if ((results === null || results === void 0 ? void 0 : results.shouldUpdate) === false) {
382
383
  shouldUpdate = false;
383
384
  }
@@ -2,14 +2,14 @@ import AbstractSpruceTest from '@sprucelabs/test-utils';
2
2
  import DatabaseFixture, { DatabaseFixtureOptions } from '../fixtures/DatabaseFixture';
3
3
  import { Database } from '../types/database.types';
4
4
  export default class AbstractDatabaseTest extends AbstractSpruceTest {
5
- protected static db: Database;
6
- protected static shouldUseInMemoryDatabase: boolean;
7
- protected static DB_NAME: string;
8
- protected static beforeEach(): Promise<void>;
9
- protected static afterEach(): Promise<void>;
10
- protected static DatabaseFixture(options?: DatabaseFixtureOptions): Promise<DatabaseFixture>;
11
- protected static connectToDatabase(): Promise<Database>;
12
- protected static DatabaseConnection(): Promise<{
5
+ protected db: Database;
6
+ protected shouldUseInMemoryDatabase: boolean;
7
+ protected DB_NAME: string;
8
+ protected beforeEach(): Promise<void>;
9
+ protected afterEach(): Promise<void>;
10
+ protected DatabaseFixture(options?: DatabaseFixtureOptions): Promise<DatabaseFixture>;
11
+ protected connectToDatabase(): Promise<Database>;
12
+ protected DatabaseConnection(): Promise<{
13
13
  dbFixture: DatabaseFixture;
14
14
  db: Database;
15
15
  }>;
@@ -7,11 +7,15 @@ const schema_1 = require("@sprucelabs/schema");
7
7
  const test_utils_1 = __importDefault(require("@sprucelabs/test-utils"));
8
8
  const DatabaseFixture_1 = __importDefault(require("../fixtures/DatabaseFixture"));
9
9
  class AbstractDatabaseTest extends test_utils_1.default {
10
- static async beforeEach() {
10
+ constructor() {
11
+ super(...arguments);
12
+ this.shouldUseInMemoryDatabase = true;
13
+ }
14
+ async beforeEach() {
11
15
  await super.beforeEach();
12
16
  schema_1.SchemaRegistry.getInstance().forgetAllSchemas();
13
17
  }
14
- static async afterEach() {
18
+ async afterEach() {
15
19
  await super.afterEach();
16
20
  await DatabaseFixture_1.default.destroy();
17
21
  //@ts-ignore
@@ -19,11 +23,11 @@ class AbstractDatabaseTest extends test_utils_1.default {
19
23
  //@ts-ignore
20
24
  this.DB_NAME = undefined;
21
25
  }
22
- static async DatabaseFixture(options) {
26
+ async DatabaseFixture(options) {
23
27
  const d = new DatabaseFixture_1.default(Object.assign({}, options));
24
28
  return d;
25
29
  }
26
- static async connectToDatabase() {
30
+ async connectToDatabase() {
27
31
  if (!this.db) {
28
32
  const { dbFixture, db } = await this.DatabaseConnection();
29
33
  this.DB_NAME = this.shouldUseInMemoryDatabase
@@ -33,11 +37,10 @@ class AbstractDatabaseTest extends test_utils_1.default {
33
37
  }
34
38
  return this.db;
35
39
  }
36
- static async DatabaseConnection() {
40
+ async DatabaseConnection() {
37
41
  const dbFixture = await this.DatabaseFixture();
38
42
  const db = await dbFixture.connectToDatabase();
39
43
  return { dbFixture, db };
40
44
  }
41
45
  }
42
- AbstractDatabaseTest.shouldUseInMemoryDatabase = true;
43
46
  exports.default = AbstractDatabaseTest;
@@ -35,7 +35,7 @@ export type StoreOptions<Name extends StoreName> = Name extends keyof StoreOptio
35
35
  export interface DataStorePlugin {
36
36
  willCreateOne?: (values: Record<string, any>) => Promise<void | DataStorePluginWillCreateOneResponse>;
37
37
  didCreateOne?: (record: Record<string, any>) => Promise<void | DataStorePluginDidCreateOneResponse>;
38
- willUpdateOne?: (query: Record<string, any>, updates: Record<string, any>) => Promise<void | DataStorePluginWillUpdateOneResponse>;
38
+ willUpdate?: (query: Record<string, any>, updates: Record<string, any>) => Promise<void | DataStorePluginWillUpdateResponse>;
39
39
  willDeleteOne?: (query: Record<string, any>) => Promise<void | DataStorePluginWillDeleteOneResponse>;
40
40
  didFindOne?: (query: Record<string, any>, record: Record<string, any>) => Promise<void | DataStorePluginDidFindOneResponse>;
41
41
  getName(): string;
@@ -52,7 +52,7 @@ export interface DataStorePluginWillCreateOneResponse {
52
52
  valuesToMixinBeforeCreate?: Record<string, any>;
53
53
  newValues?: Record<string, any>;
54
54
  }
55
- export interface DataStorePluginWillUpdateOneResponse {
55
+ export interface DataStorePluginWillUpdateResponse {
56
56
  query?: Record<string, any>;
57
57
  shouldUpdate?: boolean;
58
58
  newUpdates?: Record<string, any>;
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "29.0.45",
6
+ "version": "30.0.1",
7
7
  "files": [
8
8
  "build/**/*",
9
9
  "!build/__tests__",