@sprucelabs/data-stores 25.8.1 → 25.10.0

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.
@@ -25,6 +25,7 @@ export * from './cursors/CursorPager';
25
25
  */
26
26
  export { default as databaseAssertUtil } from './tests/databaseAssertUtil';
27
27
  export { default as databaseAssert } from './tests/databaseAssertUtil';
28
+ export { default as storePluginAssert } from './tests/storePluginAssert';
28
29
  export { default as BatchCursorImpl } from './cursors/BatchCursor';
29
30
  export * from './cursors/BatchCursor';
30
31
  export { default as BatchArrayCursor } from './cursors/BatchArrayCursor';
@@ -25,6 +25,7 @@ export * from './cursors/CursorPager.js';
25
25
  */
26
26
  export { default as databaseAssertUtil } from './tests/databaseAssertUtil.js';
27
27
  export { default as databaseAssert } from './tests/databaseAssertUtil.js';
28
+ export { default as storePluginAssert } from './tests/storePluginAssert.js';
28
29
  export { default as BatchCursorImpl } from './cursors/BatchCursor.js';
29
30
  export * from './cursors/BatchCursor.js';
30
31
  export { default as BatchArrayCursor } from './cursors/BatchArrayCursor.js';
@@ -4,9 +4,7 @@ import AbstractMutexer from '../mutexers/AbstractMutexer';
4
4
  import { DataStorePlugin, Database } from '../types/database.types';
5
5
  import { QueryBuilder, QueryOptions } from '../types/query.types';
6
6
  import { PrepareOptions, PrepareResults, SaveOperations, DataStore } from '../types/stores.types';
7
- export default abstract class AbstractStore<FullSchema extends Schema, CreateSchema extends Schema = FullSchema, UpdateSchema extends Schema = CreateSchema, DatabaseSchema extends Schema = FullSchema, DatabaseRecord = SchemaValues<DatabaseSchema> & {
8
- id: string;
9
- }, QueryRecord = SchemaPartialValues<FullSchema>, FullRecord = SchemaValues<FullSchema>, CreateRecord = SchemaValues<CreateSchema>, UpdateRecord = SchemaValues<UpdateSchema> & SaveOperations> extends AbstractMutexer implements DataStore {
7
+ export default abstract class AbstractStore<FullSchema extends Schema, CreateSchema extends Schema = FullSchema, UpdateSchema extends Schema = CreateSchema, DatabaseSchema extends Schema = FullSchema, DatabaseRecord = SchemaValues<DatabaseSchema>, QueryRecord = SchemaPartialValues<FullSchema>, FullRecord = SchemaValues<FullSchema>, CreateRecord = SchemaValues<CreateSchema>, UpdateRecord = SchemaValues<UpdateSchema> & SaveOperations> extends AbstractMutexer implements DataStore {
10
8
  abstract readonly name: string;
11
9
  protected abstract collectionName: string;
12
10
  protected abstract createSchema: CreateSchema;
@@ -323,12 +323,16 @@ export default class AbstractStore extends AbstractMutexer {
323
323
  });
324
324
  }
325
325
  deleteOne(query) {
326
- var _a;
326
+ var _a, _b;
327
327
  return __awaiter(this, void 0, void 0, function* () {
328
+ let q = Object.assign({}, query);
328
329
  for (const plugin of this.plugins) {
329
- yield ((_a = plugin.willDeleteOne) === null || _a === void 0 ? void 0 : _a.call(plugin, query));
330
+ const { query } = (_b = (yield ((_a = plugin.willDeleteOne) === null || _a === void 0 ? void 0 : _a.call(plugin, q)))) !== null && _b !== void 0 ? _b : {};
331
+ if (query) {
332
+ q = query;
333
+ }
330
334
  }
331
- return yield this.db.deleteOne(this.collectionName, query);
335
+ return yield this.db.deleteOne(this.collectionName, q);
332
336
  });
333
337
  }
334
338
  delete(query) {
@@ -0,0 +1,6 @@
1
+ import { Schema } from '@sprucelabs/schema';
2
+ import AbstractStore from '../stores/AbstractStore';
3
+ declare const storePluginAssert: {
4
+ storeHasPlugin: (store: AbstractStore<Schema>, pluginName: string) => import("..").DataStorePlugin;
5
+ };
6
+ export default storePluginAssert;
@@ -0,0 +1,21 @@
1
+ import { assertOptions } from '@sprucelabs/schema';
2
+ import { assert } from '@sprucelabs/test-utils';
3
+ const storePluginAssert = {
4
+ storeHasPlugin: (store, pluginName) => {
5
+ assertOptions({
6
+ store,
7
+ pluginName,
8
+ }, ['store', 'pluginName']);
9
+ //@ts-ignore
10
+ const { plugins } = store;
11
+ if (!(plugins === null || plugins === void 0 ? void 0 : plugins.length)) {
12
+ assert.fail(`The store you passed has no plugins. Add one to 'protected plugins: DataStorePlugin[] = [...]' or add it in the constructor of your store with 'this.plugins[...]'`);
13
+ }
14
+ const plugin = plugins === null || plugins === void 0 ? void 0 : plugins.find((p) => p.getName() === pluginName);
15
+ if ((plugin === null || plugin === void 0 ? void 0 : plugin.getName()) !== pluginName) {
16
+ assert.fail(`I could not find the plugin '${pluginName}' in the store you passed. Make sure you added it to 'protected plugins: DataStorePlugin[] = [...]' or added it in the constructor of your store with 'this.plugins[...]'`);
17
+ }
18
+ return plugin;
19
+ },
20
+ };
21
+ export default storePluginAssert;
@@ -42,7 +42,7 @@ export type TestConnect = (connectionString?: string, dbName?: string) => Promis
42
42
  export interface DataStorePlugin {
43
43
  willCreateOne?: (values: Record<string, any>) => Promise<void | DataStorePluginHookResponse>;
44
44
  willUpdateOne?: (query: Record<string, any>, updates: Record<string, any>) => Promise<void | DataStorePluginWillUpdateOneResponse>;
45
- willDeleteOne?: (query: Record<string, any>) => Promise<void>;
45
+ willDeleteOne?: (query: Record<string, any>) => Promise<void | DataStorePluginWillDeleteOneResponse>;
46
46
  getName(): string;
47
47
  }
48
48
  export interface DataStorePluginHookResponse {
@@ -51,3 +51,6 @@ export interface DataStorePluginHookResponse {
51
51
  export interface DataStorePluginWillUpdateOneResponse {
52
52
  query?: Record<string, any>;
53
53
  }
54
+ export interface DataStorePluginWillDeleteOneResponse {
55
+ query?: Record<string, any>;
56
+ }
package/build/index.d.ts CHANGED
@@ -25,6 +25,7 @@ export * from './cursors/CursorPager';
25
25
  */
26
26
  export { default as databaseAssertUtil } from './tests/databaseAssertUtil';
27
27
  export { default as databaseAssert } from './tests/databaseAssertUtil';
28
+ export { default as storePluginAssert } from './tests/storePluginAssert';
28
29
  export { default as BatchCursorImpl } from './cursors/BatchCursor';
29
30
  export * from './cursors/BatchCursor';
30
31
  export { default as BatchArrayCursor } from './cursors/BatchArrayCursor';
package/build/index.js CHANGED
@@ -17,7 +17,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
17
17
  return (mod && mod.__esModule) ? mod : { "default": mod };
18
18
  };
19
19
  Object.defineProperty(exports, "__esModule", { value: true });
20
- exports.BatchArrayCursor = exports.BatchCursorImpl = exports.databaseAssert = exports.databaseAssertUtil = exports.CursorPagerFaker = exports.CursorPager = exports.generateId = exports.DataStoresError = exports.mongoUtil = exports.NeDbDatabase = exports.MongoDatabase = exports.StoreLoader = exports.DatabaseFactory = exports.StoreFactory = exports.AbstractStore = exports.DatabaseFixture = exports.AbstractDatabaseTest = void 0;
20
+ exports.BatchArrayCursor = exports.BatchCursorImpl = exports.storePluginAssert = exports.databaseAssert = exports.databaseAssertUtil = exports.CursorPagerFaker = exports.CursorPager = exports.generateId = exports.DataStoresError = exports.mongoUtil = exports.NeDbDatabase = exports.MongoDatabase = exports.StoreLoader = exports.DatabaseFactory = exports.StoreFactory = exports.AbstractStore = exports.DatabaseFixture = exports.AbstractDatabaseTest = void 0;
21
21
  var AbstractDatabaseTest_1 = require("./tests/AbstractDatabaseTest");
22
22
  Object.defineProperty(exports, "AbstractDatabaseTest", { enumerable: true, get: function () { return __importDefault(AbstractDatabaseTest_1).default; } });
23
23
  var DatabaseFixture_1 = require("./fixtures/DatabaseFixture");
@@ -60,6 +60,8 @@ var databaseAssertUtil_1 = require("./tests/databaseAssertUtil");
60
60
  Object.defineProperty(exports, "databaseAssertUtil", { enumerable: true, get: function () { return __importDefault(databaseAssertUtil_1).default; } });
61
61
  var databaseAssertUtil_2 = require("./tests/databaseAssertUtil");
62
62
  Object.defineProperty(exports, "databaseAssert", { enumerable: true, get: function () { return __importDefault(databaseAssertUtil_2).default; } });
63
+ var storePluginAssert_1 = require("./tests/storePluginAssert");
64
+ Object.defineProperty(exports, "storePluginAssert", { enumerable: true, get: function () { return __importDefault(storePluginAssert_1).default; } });
63
65
  var BatchCursor_1 = require("./cursors/BatchCursor");
64
66
  Object.defineProperty(exports, "BatchCursorImpl", { enumerable: true, get: function () { return __importDefault(BatchCursor_1).default; } });
65
67
  __exportStar(require("./cursors/BatchCursor"), exports);
@@ -4,9 +4,7 @@ import AbstractMutexer from '../mutexers/AbstractMutexer';
4
4
  import { DataStorePlugin, Database } from '../types/database.types';
5
5
  import { QueryBuilder, QueryOptions } from '../types/query.types';
6
6
  import { PrepareOptions, PrepareResults, SaveOperations, DataStore } from '../types/stores.types';
7
- export default abstract class AbstractStore<FullSchema extends Schema, CreateSchema extends Schema = FullSchema, UpdateSchema extends Schema = CreateSchema, DatabaseSchema extends Schema = FullSchema, DatabaseRecord = SchemaValues<DatabaseSchema> & {
8
- id: string;
9
- }, QueryRecord = SchemaPartialValues<FullSchema>, FullRecord = SchemaValues<FullSchema>, CreateRecord = SchemaValues<CreateSchema>, UpdateRecord = SchemaValues<UpdateSchema> & SaveOperations> extends AbstractMutexer implements DataStore {
7
+ export default abstract class AbstractStore<FullSchema extends Schema, CreateSchema extends Schema = FullSchema, UpdateSchema extends Schema = CreateSchema, DatabaseSchema extends Schema = FullSchema, DatabaseRecord = SchemaValues<DatabaseSchema>, QueryRecord = SchemaPartialValues<FullSchema>, FullRecord = SchemaValues<FullSchema>, CreateRecord = SchemaValues<CreateSchema>, UpdateRecord = SchemaValues<UpdateSchema> & SaveOperations> extends AbstractMutexer implements DataStore {
10
8
  abstract readonly name: string;
11
9
  protected abstract collectionName: string;
12
10
  protected abstract createSchema: CreateSchema;
@@ -316,11 +316,15 @@ class AbstractStore extends AbstractMutexer_1.default {
316
316
  return cleanedValues;
317
317
  }
318
318
  async deleteOne(query) {
319
- var _a;
319
+ var _a, _b;
320
+ let q = Object.assign({}, query);
320
321
  for (const plugin of this.plugins) {
321
- await ((_a = plugin.willDeleteOne) === null || _a === void 0 ? void 0 : _a.call(plugin, query));
322
+ const { query } = (_b = (await ((_a = plugin.willDeleteOne) === null || _a === void 0 ? void 0 : _a.call(plugin, q)))) !== null && _b !== void 0 ? _b : {};
323
+ if (query) {
324
+ q = query;
325
+ }
322
326
  }
323
- return await this.db.deleteOne(this.collectionName, query);
327
+ return await this.db.deleteOne(this.collectionName, q);
324
328
  }
325
329
  async delete(query) {
326
330
  return await this.db.delete(this.collectionName, query);
@@ -0,0 +1,6 @@
1
+ import { Schema } from '@sprucelabs/schema';
2
+ import AbstractStore from '../stores/AbstractStore';
3
+ declare const storePluginAssert: {
4
+ storeHasPlugin: (store: AbstractStore<Schema>, pluginName: string) => import("..").DataStorePlugin;
5
+ };
6
+ export default storePluginAssert;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const schema_1 = require("@sprucelabs/schema");
4
+ const test_utils_1 = require("@sprucelabs/test-utils");
5
+ const storePluginAssert = {
6
+ storeHasPlugin: (store, pluginName) => {
7
+ (0, schema_1.assertOptions)({
8
+ store,
9
+ pluginName,
10
+ }, ['store', 'pluginName']);
11
+ //@ts-ignore
12
+ const { plugins } = store;
13
+ if (!(plugins === null || plugins === void 0 ? void 0 : plugins.length)) {
14
+ test_utils_1.assert.fail(`The store you passed has no plugins. Add one to 'protected plugins: DataStorePlugin[] = [...]' or add it in the constructor of your store with 'this.plugins[...]'`);
15
+ }
16
+ const plugin = plugins === null || plugins === void 0 ? void 0 : plugins.find((p) => p.getName() === pluginName);
17
+ if ((plugin === null || plugin === void 0 ? void 0 : plugin.getName()) !== pluginName) {
18
+ test_utils_1.assert.fail(`I could not find the plugin '${pluginName}' in the store you passed. Make sure you added it to 'protected plugins: DataStorePlugin[] = [...]' or added it in the constructor of your store with 'this.plugins[...]'`);
19
+ }
20
+ return plugin;
21
+ },
22
+ };
23
+ exports.default = storePluginAssert;
@@ -42,7 +42,7 @@ export type TestConnect = (connectionString?: string, dbName?: string) => Promis
42
42
  export interface DataStorePlugin {
43
43
  willCreateOne?: (values: Record<string, any>) => Promise<void | DataStorePluginHookResponse>;
44
44
  willUpdateOne?: (query: Record<string, any>, updates: Record<string, any>) => Promise<void | DataStorePluginWillUpdateOneResponse>;
45
- willDeleteOne?: (query: Record<string, any>) => Promise<void>;
45
+ willDeleteOne?: (query: Record<string, any>) => Promise<void | DataStorePluginWillDeleteOneResponse>;
46
46
  getName(): string;
47
47
  }
48
48
  export interface DataStorePluginHookResponse {
@@ -51,3 +51,6 @@ export interface DataStorePluginHookResponse {
51
51
  export interface DataStorePluginWillUpdateOneResponse {
52
52
  query?: Record<string, any>;
53
53
  }
54
+ export interface DataStorePluginWillDeleteOneResponse {
55
+ query?: Record<string, any>;
56
+ }
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "25.8.1",
6
+ "version": "25.10.0",
7
7
  "files": [
8
8
  "build/**/*",
9
9
  "!build/__tests__",