owservable 0.8.40 → 0.8.42

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.
Files changed (82) hide show
  1. package/lib/_enums/store.type.enum.d.ts +6 -6
  2. package/lib/_enums/store.type.enum.js +9 -9
  3. package/lib/_types/connection.manager.refresh.type.d.ts +9 -9
  4. package/lib/_types/connection.manager.refresh.type.js +2 -2
  5. package/lib/_types/cronjob.type.d.ts +7 -7
  6. package/lib/_types/cronjob.type.js +2 -2
  7. package/lib/_types/store.scope.type.d.ts +2 -2
  8. package/lib/_types/store.scope.type.js +2 -2
  9. package/lib/_types/store.subscription.config.type.d.ts +15 -15
  10. package/lib/_types/store.subscription.config.type.js +2 -2
  11. package/lib/_types/store.subscription.update.type.d.ts +9 -9
  12. package/lib/_types/store.subscription.update.type.js +2 -2
  13. package/lib/_types/subscription.methods.type.d.ts +6 -6
  14. package/lib/_types/subscription.methods.type.js +2 -2
  15. package/lib/_types/watcher.type.d.ts +6 -5
  16. package/lib/_types/watcher.type.js +2 -2
  17. package/lib/_types/worker.type.d.ts +5 -5
  18. package/lib/_types/worker.type.js +2 -2
  19. package/lib/auth/i.connection.manager.d.ts +9 -9
  20. package/lib/auth/i.connection.manager.js +2 -2
  21. package/lib/functions/action/add.action.cronjobs.d.ts +1 -1
  22. package/lib/functions/action/add.action.cronjobs.js +25 -25
  23. package/lib/functions/action/add.action.watchers.d.ts +1 -1
  24. package/lib/functions/action/add.action.watchers.js +22 -22
  25. package/lib/functions/action/add.action.workers.d.ts +1 -1
  26. package/lib/functions/action/add.action.workers.js +22 -22
  27. package/lib/functions/execute/execute.cronjob.d.ts +3 -3
  28. package/lib/functions/execute/execute.cronjob.js +12 -12
  29. package/lib/functions/execute/execute.on.files.recursively.d.ts +2 -2
  30. package/lib/functions/execute/execute.on.files.recursively.js +17 -17
  31. package/lib/functions/execute/execute.processes.in.folder.d.ts +2 -2
  32. package/lib/functions/execute/execute.processes.in.folder.js +10 -10
  33. package/lib/functions/execute/execute.watcher.d.ts +3 -3
  34. package/lib/functions/execute/execute.watcher.js +16 -11
  35. package/lib/functions/execute/execute.watcher.js.map +1 -1
  36. package/lib/functions/execute/execute.worker.d.ts +3 -3
  37. package/lib/functions/execute/execute.worker.js +11 -11
  38. package/lib/functions/initiate.cronjobs.d.ts +2 -2
  39. package/lib/functions/initiate.cronjobs.js +8 -8
  40. package/lib/functions/initiate.watchers.d.ts +2 -2
  41. package/lib/functions/initiate.watchers.js +8 -8
  42. package/lib/functions/initiate.workers.d.ts +2 -2
  43. package/lib/functions/initiate.workers.js +8 -8
  44. package/lib/middleware/data.middleware.map.d.ts +7 -7
  45. package/lib/middleware/data.middleware.map.js +18 -18
  46. package/lib/middleware/data.middleware.map.js.map +1 -1
  47. package/lib/mongodb/collections.models.map.d.ts +8 -8
  48. package/lib/mongodb/collections.models.map.js +22 -22
  49. package/lib/mongodb/collections.models.map.js.map +1 -1
  50. package/lib/mongodb/functions/index/add.both.indices.to.attributes.d.ts +3 -3
  51. package/lib/mongodb/functions/index/add.both.indices.to.attributes.js +8 -8
  52. package/lib/mongodb/functions/index/add.down.indices.to.attributes.d.ts +3 -3
  53. package/lib/mongodb/functions/index/add.down.indices.to.attributes.js +5 -5
  54. package/lib/mongodb/functions/index/add.index.to.attributes.d.ts +3 -3
  55. package/lib/mongodb/functions/index/add.index.to.attributes.js +11 -11
  56. package/lib/mongodb/functions/index/add.up.and.down.indices.to.attributes.d.ts +3 -3
  57. package/lib/mongodb/functions/index/add.up.and.down.indices.to.attributes.js +9 -9
  58. package/lib/mongodb/functions/index/add.up.indices.to.attributes.d.ts +3 -3
  59. package/lib/mongodb/functions/index/add.up.indices.to.attributes.js +5 -5
  60. package/lib/mongodb/functions/observable.database.d.ts +3 -3
  61. package/lib/mongodb/functions/observable.database.js +22 -22
  62. package/lib/mongodb/functions/observable.model.d.ts +4 -4
  63. package/lib/mongodb/functions/observable.model.js +41 -41
  64. package/lib/mongodb/functions/process.models.d.ts +2 -2
  65. package/lib/mongodb/functions/process.models.js +40 -40
  66. package/lib/mongodb/mongodb.connector.d.ts +7 -7
  67. package/lib/mongodb/mongodb.connector.js +51 -51
  68. package/lib/owservable.client.d.ts +23 -23
  69. package/lib/owservable.client.js +162 -162
  70. package/lib/owservable.d.ts +41 -41
  71. package/lib/owservable.js +63 -63
  72. package/lib/store/a.store.d.ts +37 -37
  73. package/lib/store/a.store.js +126 -126
  74. package/lib/store/collection.store.d.ts +8 -8
  75. package/lib/store/collection.store.js +111 -111
  76. package/lib/store/count.store.d.ts +7 -7
  77. package/lib/store/count.store.js +46 -46
  78. package/lib/store/document.store.d.ts +12 -12
  79. package/lib/store/document.store.js +134 -134
  80. package/lib/store/factories/store.factory.d.ts +4 -4
  81. package/lib/store/factories/store.factory.js +15 -15
  82. package/package.json +10 -10
package/lib/owservable.js CHANGED
@@ -1,64 +1,64 @@
1
- 'use strict';
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.storeFactory = exports.CollectionStore = exports.DocumentStore = exports.CountStore = exports.AStore = exports.addUpAndDownIndicesToAttributes = exports.addBothIndicesToAttributes = exports.addDownIndicesToAttributes = exports.addUpIndicesToAttributes = exports.addIndexToAttributes = exports.observableDatabase = exports.observableModel = exports.processModels = exports.CollectionsModelsMap = exports.MongoDBConnector = exports.DataMiddlewareMap = exports.executeOnFilesRecursively = exports.executeProcessesInFolder = exports.executeWorker = exports.executeWatcher = exports.executeCronjob = exports.addActionWorkers = exports.addActionWatchers = exports.addActionCronjobs = exports.initiateWorkers = exports.initiateWatchers = exports.initiateCronjobs = exports.EStoreType = exports.OwservableClient = void 0;
4
- const owservable_client_1 = require("./owservable.client");
5
- exports.OwservableClient = owservable_client_1.default;
6
- const store_type_enum_1 = require("./_enums/store.type.enum");
7
- exports.EStoreType = store_type_enum_1.default;
8
- const initiate_workers_1 = require("./functions/initiate.workers");
9
- exports.initiateWorkers = initiate_workers_1.default;
10
- const initiate_cronjobs_1 = require("./functions/initiate.cronjobs");
11
- exports.initiateCronjobs = initiate_cronjobs_1.default;
12
- const initiate_watchers_1 = require("./functions/initiate.watchers");
13
- exports.initiateWatchers = initiate_watchers_1.default;
14
- const add_action_cronjobs_1 = require("./functions/action/add.action.cronjobs");
15
- exports.addActionCronjobs = add_action_cronjobs_1.default;
16
- const add_action_watchers_1 = require("./functions/action/add.action.watchers");
17
- exports.addActionWatchers = add_action_watchers_1.default;
18
- const add_action_workers_1 = require("./functions/action/add.action.workers");
19
- exports.addActionWorkers = add_action_workers_1.default;
20
- const execute_cronjob_1 = require("./functions/execute/execute.cronjob");
21
- exports.executeCronjob = execute_cronjob_1.default;
22
- const execute_watcher_1 = require("./functions/execute/execute.watcher");
23
- exports.executeWatcher = execute_watcher_1.default;
24
- const execute_worker_1 = require("./functions/execute/execute.worker");
25
- exports.executeWorker = execute_worker_1.default;
26
- const execute_processes_in_folder_1 = require("./functions/execute/execute.processes.in.folder");
27
- exports.executeProcessesInFolder = execute_processes_in_folder_1.default;
28
- const execute_on_files_recursively_1 = require("./functions/execute/execute.on.files.recursively");
29
- exports.executeOnFilesRecursively = execute_on_files_recursively_1.default;
30
- const data_middleware_map_1 = require("./middleware/data.middleware.map");
31
- exports.DataMiddlewareMap = data_middleware_map_1.default;
32
- const mongodb_connector_1 = require("./mongodb/mongodb.connector");
33
- exports.MongoDBConnector = mongodb_connector_1.default;
34
- const collections_models_map_1 = require("./mongodb/collections.models.map");
35
- exports.CollectionsModelsMap = collections_models_map_1.default;
36
- const process_models_1 = require("./mongodb/functions/process.models");
37
- exports.processModels = process_models_1.default;
38
- const observable_model_1 = require("./mongodb/functions/observable.model");
39
- exports.observableModel = observable_model_1.default;
40
- const observable_database_1 = require("./mongodb/functions/observable.database");
41
- exports.observableDatabase = observable_database_1.default;
42
- const add_index_to_attributes_1 = require("./mongodb/functions/index/add.index.to.attributes");
43
- exports.addIndexToAttributes = add_index_to_attributes_1.default;
44
- const add_up_indices_to_attributes_1 = require("./mongodb/functions/index/add.up.indices.to.attributes");
45
- exports.addUpIndicesToAttributes = add_up_indices_to_attributes_1.default;
46
- const add_down_indices_to_attributes_1 = require("./mongodb/functions/index/add.down.indices.to.attributes");
47
- exports.addDownIndicesToAttributes = add_down_indices_to_attributes_1.default;
48
- const add_both_indices_to_attributes_1 = require("./mongodb/functions/index/add.both.indices.to.attributes");
49
- exports.addBothIndicesToAttributes = add_both_indices_to_attributes_1.default;
50
- const add_up_and_down_indices_to_attributes_1 = require("./mongodb/functions/index/add.up.and.down.indices.to.attributes");
51
- exports.addUpAndDownIndicesToAttributes = add_up_and_down_indices_to_attributes_1.default;
52
- const a_store_1 = require("./store/a.store");
53
- exports.AStore = a_store_1.default;
54
- const count_store_1 = require("./store/count.store");
55
- exports.CountStore = count_store_1.default;
56
- const document_store_1 = require("./store/document.store");
57
- exports.DocumentStore = document_store_1.default;
58
- const collection_store_1 = require("./store/collection.store");
59
- exports.CollectionStore = collection_store_1.default;
60
- const store_factory_1 = require("./store/factories/store.factory");
61
- exports.storeFactory = store_factory_1.default;
62
- const Owservable = {};
63
- exports.default = Owservable;
1
+ 'use strict';
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.storeFactory = exports.CollectionStore = exports.DocumentStore = exports.CountStore = exports.AStore = exports.addUpAndDownIndicesToAttributes = exports.addBothIndicesToAttributes = exports.addDownIndicesToAttributes = exports.addUpIndicesToAttributes = exports.addIndexToAttributes = exports.observableDatabase = exports.observableModel = exports.processModels = exports.CollectionsModelsMap = exports.MongoDBConnector = exports.DataMiddlewareMap = exports.executeOnFilesRecursively = exports.executeProcessesInFolder = exports.executeWorker = exports.executeWatcher = exports.executeCronjob = exports.addActionWorkers = exports.addActionWatchers = exports.addActionCronjobs = exports.initiateWorkers = exports.initiateWatchers = exports.initiateCronjobs = exports.EStoreType = exports.OwservableClient = void 0;
4
+ const owservable_client_1 = require("./owservable.client");
5
+ exports.OwservableClient = owservable_client_1.default;
6
+ const store_type_enum_1 = require("./_enums/store.type.enum");
7
+ exports.EStoreType = store_type_enum_1.default;
8
+ const initiate_workers_1 = require("./functions/initiate.workers");
9
+ exports.initiateWorkers = initiate_workers_1.default;
10
+ const initiate_cronjobs_1 = require("./functions/initiate.cronjobs");
11
+ exports.initiateCronjobs = initiate_cronjobs_1.default;
12
+ const initiate_watchers_1 = require("./functions/initiate.watchers");
13
+ exports.initiateWatchers = initiate_watchers_1.default;
14
+ const add_action_cronjobs_1 = require("./functions/action/add.action.cronjobs");
15
+ exports.addActionCronjobs = add_action_cronjobs_1.default;
16
+ const add_action_watchers_1 = require("./functions/action/add.action.watchers");
17
+ exports.addActionWatchers = add_action_watchers_1.default;
18
+ const add_action_workers_1 = require("./functions/action/add.action.workers");
19
+ exports.addActionWorkers = add_action_workers_1.default;
20
+ const execute_cronjob_1 = require("./functions/execute/execute.cronjob");
21
+ exports.executeCronjob = execute_cronjob_1.default;
22
+ const execute_watcher_1 = require("./functions/execute/execute.watcher");
23
+ exports.executeWatcher = execute_watcher_1.default;
24
+ const execute_worker_1 = require("./functions/execute/execute.worker");
25
+ exports.executeWorker = execute_worker_1.default;
26
+ const execute_processes_in_folder_1 = require("./functions/execute/execute.processes.in.folder");
27
+ exports.executeProcessesInFolder = execute_processes_in_folder_1.default;
28
+ const execute_on_files_recursively_1 = require("./functions/execute/execute.on.files.recursively");
29
+ exports.executeOnFilesRecursively = execute_on_files_recursively_1.default;
30
+ const data_middleware_map_1 = require("./middleware/data.middleware.map");
31
+ exports.DataMiddlewareMap = data_middleware_map_1.default;
32
+ const mongodb_connector_1 = require("./mongodb/mongodb.connector");
33
+ exports.MongoDBConnector = mongodb_connector_1.default;
34
+ const collections_models_map_1 = require("./mongodb/collections.models.map");
35
+ exports.CollectionsModelsMap = collections_models_map_1.default;
36
+ const process_models_1 = require("./mongodb/functions/process.models");
37
+ exports.processModels = process_models_1.default;
38
+ const observable_model_1 = require("./mongodb/functions/observable.model");
39
+ exports.observableModel = observable_model_1.default;
40
+ const observable_database_1 = require("./mongodb/functions/observable.database");
41
+ exports.observableDatabase = observable_database_1.default;
42
+ const add_index_to_attributes_1 = require("./mongodb/functions/index/add.index.to.attributes");
43
+ exports.addIndexToAttributes = add_index_to_attributes_1.default;
44
+ const add_up_indices_to_attributes_1 = require("./mongodb/functions/index/add.up.indices.to.attributes");
45
+ exports.addUpIndicesToAttributes = add_up_indices_to_attributes_1.default;
46
+ const add_down_indices_to_attributes_1 = require("./mongodb/functions/index/add.down.indices.to.attributes");
47
+ exports.addDownIndicesToAttributes = add_down_indices_to_attributes_1.default;
48
+ const add_both_indices_to_attributes_1 = require("./mongodb/functions/index/add.both.indices.to.attributes");
49
+ exports.addBothIndicesToAttributes = add_both_indices_to_attributes_1.default;
50
+ const add_up_and_down_indices_to_attributes_1 = require("./mongodb/functions/index/add.up.and.down.indices.to.attributes");
51
+ exports.addUpAndDownIndicesToAttributes = add_up_and_down_indices_to_attributes_1.default;
52
+ const a_store_1 = require("./store/a.store");
53
+ exports.AStore = a_store_1.default;
54
+ const count_store_1 = require("./store/count.store");
55
+ exports.CountStore = count_store_1.default;
56
+ const document_store_1 = require("./store/document.store");
57
+ exports.DocumentStore = document_store_1.default;
58
+ const collection_store_1 = require("./store/collection.store");
59
+ exports.CollectionStore = collection_store_1.default;
60
+ const store_factory_1 = require("./store/factories/store.factory");
61
+ exports.storeFactory = store_factory_1.default;
62
+ const Owservable = {};
63
+ exports.default = Owservable;
64
64
  //# sourceMappingURL=owservable.js.map
@@ -1,37 +1,37 @@
1
- import { Model } from 'mongoose';
2
- import { Subject, Subscription } from 'rxjs';
3
- import EStoreType from '../_enums/store.type.enum';
4
- import StoreSubscriptionConfigType from '../_types/store.subscription.config.type';
5
- export default abstract class AStore extends Subject<any> {
6
- protected _model: Model<any>;
7
- protected _target: string;
8
- protected _type: EStoreType;
9
- protected _config: StoreSubscriptionConfigType;
10
- protected _incremental: boolean;
11
- protected _subscriptionId: string;
12
- protected _query: any;
13
- protected _sort: any;
14
- protected _fields: any;
15
- protected _paging: any;
16
- protected _populates: any[];
17
- protected _virtuals: any[];
18
- protected _delay: number;
19
- protected _subscription: Subscription;
20
- protected constructor(model: Model<any>, target: string);
21
- destroy(): void;
22
- restartSubscription(): void;
23
- protected isInitialSubscription(change: any): boolean;
24
- protected abstract shouldReload(change: any): boolean;
25
- protected abstract load(change: any): Promise<void>;
26
- protected extractFromConfig(): void;
27
- protected set subscription(subscription: Subscription);
28
- protected get model(): Model<any>;
29
- protected emitOne(update?: any): void;
30
- protected emitMany(update?: any): void;
31
- protected emitDelete(deleted: any): void;
32
- protected emitError(error: any): void;
33
- protected shouldConsiderFields(): boolean;
34
- set config(config: StoreSubscriptionConfigType);
35
- get target(): string;
36
- private _isValidConfig;
37
- }
1
+ import { Model } from 'mongoose';
2
+ import { Subject, Subscription } from 'rxjs';
3
+ import EStoreType from '../_enums/store.type.enum';
4
+ import StoreSubscriptionConfigType from '../_types/store.subscription.config.type';
5
+ export default abstract class AStore extends Subject<any> {
6
+ protected _model: Model<any>;
7
+ protected _target: string;
8
+ protected _type: EStoreType;
9
+ protected _config: StoreSubscriptionConfigType;
10
+ protected _incremental: boolean;
11
+ protected _subscriptionId: string;
12
+ protected _query: any;
13
+ protected _sort: any;
14
+ protected _fields: any;
15
+ protected _paging: any;
16
+ protected _populates: any[];
17
+ protected _virtuals: any[];
18
+ protected _delay: number;
19
+ protected _subscription: Subscription;
20
+ protected constructor(model: Model<any>, target: string);
21
+ destroy(): void;
22
+ restartSubscription(): void;
23
+ protected isInitialSubscription(change: any): boolean;
24
+ protected abstract shouldReload(change: any): boolean;
25
+ protected abstract load(change: any): Promise<void>;
26
+ protected extractFromConfig(): void;
27
+ protected set subscription(subscription: Subscription);
28
+ protected get model(): Model<any>;
29
+ protected emitOne(update?: any): void;
30
+ protected emitMany(update?: any): void;
31
+ protected emitDelete(deleted: any): void;
32
+ protected emitError(error: any): void;
33
+ protected shouldConsiderFields(): boolean;
34
+ set config(config: StoreSubscriptionConfigType);
35
+ get target(): string;
36
+ private _isValidConfig;
37
+ }
@@ -1,127 +1,127 @@
1
- 'use strict';
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const node_crypto_1 = require("node:crypto");
4
- const rxjs_1 = require("rxjs");
5
- const operators_1 = require("rxjs/operators");
6
- const jsondiffpatch = require("jsondiffpatch");
7
- const lodash_1 = require("lodash");
8
- const observable_model_1 = require("../mongodb/functions/observable.model");
9
- const diffPatcher = jsondiffpatch.create({
10
- propertyFilter: (name) => name !== 'subscriptionId'
11
- });
12
- const _baseMessage = (target, incremental = false) => ({
13
- type: incremental ? 'increment' : 'update',
14
- target,
15
- payload: {}
16
- });
17
- class AStore extends rxjs_1.Subject {
18
- constructor(model, target) {
19
- super();
20
- this._incremental = false;
21
- this._model = model;
22
- this._target = target;
23
- this._query = {};
24
- this._sort = {};
25
- this._fields = {};
26
- this._paging = {};
27
- this._populates = [];
28
- this._virtuals = [];
29
- this._delay = 50;
30
- }
31
- destroy() {
32
- var _a;
33
- (_a = this._subscription) === null || _a === void 0 ? void 0 : _a.unsubscribe();
34
- delete this._subscription;
35
- }
36
- restartSubscription() {
37
- this.subscription = (0, observable_model_1.default)(this.model)
38
- .pipe((0, operators_1.throttleTime)(this._delay, rxjs_1.asyncScheduler, { leading: true, trailing: true }))
39
- .subscribe({
40
- next: (change) => this.load(change)
41
- });
42
- }
43
- isInitialSubscription(change) {
44
- return (0, lodash_1.isEmpty)(change);
45
- }
46
- extractFromConfig() {
47
- const { subscriptionId = (0, node_crypto_1.randomUUID)(), query = {}, sort = {}, fields = {}, populates = [], virtuals = [], delay = 50 } = this._config;
48
- this._subscriptionId = subscriptionId;
49
- this._query = query;
50
- this._sort = sort;
51
- this._populates = populates;
52
- this._virtuals = virtuals;
53
- this._delay = delay;
54
- if ((0, lodash_1.isArray)(fields)) {
55
- this._fields = {};
56
- (0, lodash_1.each)(fields, (field) => (0, lodash_1.set)(this._fields, field, 1));
57
- }
58
- else {
59
- this._fields = fields;
60
- }
61
- }
62
- set subscription(subscription) {
63
- this.destroy();
64
- this._subscription = subscription;
65
- this.load({}).then(() => null);
66
- }
67
- get model() {
68
- return this._model;
69
- }
70
- emitOne(update = {}) {
71
- const message = _baseMessage(this._target, this._incremental);
72
- (0, lodash_1.set)(message.payload, this._target, update);
73
- this.next(message);
74
- }
75
- emitMany(update = { total: 0, data: [] }) {
76
- const { total, data } = update;
77
- const message = _baseMessage(this._target, this._incremental);
78
- (0, lodash_1.set)(message.payload, this._target, data);
79
- if (!this._incremental)
80
- (0, lodash_1.set)(message.payload, '_' + this._target + 'Count', total);
81
- this.next(Object.assign({ subscriptionId: this._subscriptionId }, message));
82
- }
83
- emitDelete(deleted) {
84
- this.next({
85
- subscriptionId: this._subscriptionId,
86
- type: 'delete',
87
- target: this._target,
88
- payload: deleted
89
- });
90
- }
91
- emitError(error) {
92
- this.next({
93
- subscriptionId: this._subscriptionId,
94
- type: 'error',
95
- error,
96
- target: this._target,
97
- query: this._query,
98
- sort: this._sort,
99
- fields: this._fields,
100
- paging: this._paging,
101
- populates: this._populates,
102
- virtuals: this._virtuals
103
- });
104
- }
105
- shouldConsiderFields() {
106
- return !(0, lodash_1.isEmpty)(this._fields) && !(0, lodash_1.includes)((0, lodash_1.values)(this._fields), 0);
107
- }
108
- set config(config) {
109
- if (!this._isValidConfig(config))
110
- return;
111
- this._config = (0, lodash_1.cloneDeep)(config);
112
- config.subscriptionId = config.subscriptionId || (0, node_crypto_1.randomUUID)();
113
- this.extractFromConfig();
114
- this.restartSubscription();
115
- }
116
- get target() {
117
- return this._target;
118
- }
119
- _isValidConfig(config) {
120
- if (!config)
121
- return false;
122
- const diff = diffPatcher.diff(this._config, config);
123
- return !(0, lodash_1.isEmpty)(diff);
124
- }
125
- }
126
- exports.default = AStore;
1
+ 'use strict';
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const node_crypto_1 = require("node:crypto");
4
+ const rxjs_1 = require("rxjs");
5
+ const operators_1 = require("rxjs/operators");
6
+ const jsondiffpatch = require("jsondiffpatch");
7
+ const lodash_1 = require("lodash");
8
+ const observable_model_1 = require("../mongodb/functions/observable.model");
9
+ const diffPatcher = jsondiffpatch.create({
10
+ propertyFilter: (name) => name !== 'subscriptionId'
11
+ });
12
+ const _baseMessage = (target, incremental = false) => ({
13
+ type: incremental ? 'increment' : 'update',
14
+ target,
15
+ payload: {}
16
+ });
17
+ class AStore extends rxjs_1.Subject {
18
+ constructor(model, target) {
19
+ super();
20
+ this._incremental = false;
21
+ this._model = model;
22
+ this._target = target;
23
+ this._query = {};
24
+ this._sort = {};
25
+ this._fields = {};
26
+ this._paging = {};
27
+ this._populates = [];
28
+ this._virtuals = [];
29
+ this._delay = 50;
30
+ }
31
+ destroy() {
32
+ var _a;
33
+ (_a = this._subscription) === null || _a === void 0 ? void 0 : _a.unsubscribe();
34
+ delete this._subscription;
35
+ }
36
+ restartSubscription() {
37
+ this.subscription = (0, observable_model_1.default)(this.model)
38
+ .pipe((0, operators_1.throttleTime)(this._delay, rxjs_1.asyncScheduler, { leading: true, trailing: true }))
39
+ .subscribe({
40
+ next: (change) => this.load(change)
41
+ });
42
+ }
43
+ isInitialSubscription(change) {
44
+ return (0, lodash_1.isEmpty)(change);
45
+ }
46
+ extractFromConfig() {
47
+ const { subscriptionId = (0, node_crypto_1.randomUUID)(), query = {}, sort = {}, fields = {}, populates = [], virtuals = [], delay = 50 } = this._config;
48
+ this._subscriptionId = subscriptionId;
49
+ this._query = query;
50
+ this._sort = sort;
51
+ this._populates = populates;
52
+ this._virtuals = virtuals;
53
+ this._delay = delay;
54
+ if ((0, lodash_1.isArray)(fields)) {
55
+ this._fields = {};
56
+ (0, lodash_1.each)(fields, (field) => (0, lodash_1.set)(this._fields, field, 1));
57
+ }
58
+ else {
59
+ this._fields = fields;
60
+ }
61
+ }
62
+ set subscription(subscription) {
63
+ this.destroy();
64
+ this._subscription = subscription;
65
+ this.load({}).then(() => null);
66
+ }
67
+ get model() {
68
+ return this._model;
69
+ }
70
+ emitOne(update = {}) {
71
+ const message = _baseMessage(this._target, this._incremental);
72
+ (0, lodash_1.set)(message.payload, this._target, update);
73
+ this.next(message);
74
+ }
75
+ emitMany(update = { total: 0, data: [] }) {
76
+ const { total, data } = update;
77
+ const message = _baseMessage(this._target, this._incremental);
78
+ (0, lodash_1.set)(message.payload, this._target, data);
79
+ if (!this._incremental)
80
+ (0, lodash_1.set)(message.payload, '_' + this._target + 'Count', total);
81
+ this.next(Object.assign({ subscriptionId: this._subscriptionId }, message));
82
+ }
83
+ emitDelete(deleted) {
84
+ this.next({
85
+ subscriptionId: this._subscriptionId,
86
+ type: 'delete',
87
+ target: this._target,
88
+ payload: deleted
89
+ });
90
+ }
91
+ emitError(error) {
92
+ this.next({
93
+ subscriptionId: this._subscriptionId,
94
+ type: 'error',
95
+ error,
96
+ target: this._target,
97
+ query: this._query,
98
+ sort: this._sort,
99
+ fields: this._fields,
100
+ paging: this._paging,
101
+ populates: this._populates,
102
+ virtuals: this._virtuals
103
+ });
104
+ }
105
+ shouldConsiderFields() {
106
+ return !(0, lodash_1.isEmpty)(this._fields) && !(0, lodash_1.includes)((0, lodash_1.values)(this._fields), 0);
107
+ }
108
+ set config(config) {
109
+ if (!this._isValidConfig(config))
110
+ return;
111
+ this._config = (0, lodash_1.cloneDeep)(config);
112
+ config.subscriptionId = config.subscriptionId || (0, node_crypto_1.randomUUID)();
113
+ this.extractFromConfig();
114
+ this.restartSubscription();
115
+ }
116
+ get target() {
117
+ return this._target;
118
+ }
119
+ _isValidConfig(config) {
120
+ if (!config)
121
+ return false;
122
+ const diff = diffPatcher.diff(this._config, config);
123
+ return !(0, lodash_1.isEmpty)(diff);
124
+ }
125
+ }
126
+ exports.default = AStore;
127
127
  //# sourceMappingURL=a.store.js.map
@@ -1,8 +1,8 @@
1
- import { Model } from 'mongoose';
2
- import AStore from './a.store';
3
- export default class CollectionStore extends AStore {
4
- constructor(model: Model<any>, target: string);
5
- protected shouldReload(change: any): boolean;
6
- protected load(change: any): Promise<void>;
7
- protected extractFromConfig(): void;
8
- }
1
+ import { Model } from 'mongoose';
2
+ import AStore from './a.store';
3
+ export default class CollectionStore extends AStore {
4
+ constructor(model: Model<any>, target: string);
5
+ protected shouldReload(change: any): boolean;
6
+ protected load(change: any): Promise<void>;
7
+ protected extractFromConfig(): void;
8
+ }