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
@@ -1,112 +1,112 @@
1
- 'use strict';
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- const sift_1 = require("sift");
13
- const _ = require("lodash");
14
- const a_store_1 = require("./a.store");
15
- const store_type_enum_1 = require("../_enums/store.type.enum");
16
- class CollectionStore extends a_store_1.default {
17
- constructor(model, target) {
18
- super(model, target);
19
- this._type = store_type_enum_1.default.COLLECTION;
20
- Object.setPrototypeOf(this, CollectionStore.prototype);
21
- }
22
- shouldReload(change) {
23
- if (this.isInitialSubscription(change))
24
- return true;
25
- const { operationType: type, updateDescription: description, fullDocument: document } = change;
26
- const test = (0, sift_1.default)(_.omit(this._query, ['createdAt', 'updatedAt']));
27
- switch (type) {
28
- case 'delete':
29
- case 'insert':
30
- return true;
31
- case 'replace':
32
- case 'update':
33
- if (!description)
34
- return true;
35
- if (!this.shouldConsiderFields())
36
- return test(document);
37
- const { updatedFields, removedFields } = description;
38
- const us = _.concat(removedFields, _.keys(updatedFields));
39
- const qs = _.keys(this._fields);
40
- return !_.isEmpty(_.intersection(qs, us));
41
- }
42
- return false;
43
- }
44
- load(change) {
45
- return __awaiter(this, void 0, void 0, function* () {
46
- if (_.isEmpty(this._config))
47
- return this.emitMany();
48
- if (!this.shouldReload(change))
49
- return;
50
- try {
51
- const { operationType: type, documentKey, fullDocument: document } = change;
52
- const key = _.get(documentKey, '_id', '').toString();
53
- if (document && this._incremental) {
54
- if ('delete' === type)
55
- return this.emitDelete(key);
56
- for (const populate of this._populates) {
57
- yield this._model.populate(document, populate);
58
- }
59
- if (_.isEmpty(this._virtuals))
60
- return this.emitMany({ data: document });
61
- const replacement = _.cloneDeep(_.omit(document.toJSON(), this._virtuals));
62
- for (const virtual of this._virtuals) {
63
- replacement[virtual] = yield Promise.resolve(document[virtual]);
64
- }
65
- return this.emitMany({ data: replacement });
66
- }
67
- else {
68
- let data = [];
69
- const total = yield this._model.countDocuments(this._query);
70
- if (total > 0) {
71
- data = yield this._model
72
- .find(this._query, this._fields, this._paging)
73
- .sort(this._sort)
74
- .setOptions({ allowDiskUse: true });
75
- for (const populate of this._populates) {
76
- yield this._model.populate(data, populate);
77
- }
78
- if (!_.isEmpty(this._virtuals)) {
79
- const replacements = [];
80
- for (const item of data) {
81
- const replacement = _.cloneDeep(_.omit(item.toJSON(), this._virtuals));
82
- for (const virtual of this._virtuals) {
83
- replacement[virtual] = yield Promise.resolve(item[virtual]);
84
- }
85
- replacements.push(replacement);
86
- }
87
- data = replacements;
88
- }
89
- }
90
- return this.emitMany({ total, data });
91
- }
92
- }
93
- catch (error) {
94
- this.emitError(error);
95
- }
96
- });
97
- }
98
- extractFromConfig() {
99
- super.extractFromConfig();
100
- const { incremental = false, page = 1, pageSize } = this._config;
101
- this._incremental = incremental;
102
- this._paging = {};
103
- if (pageSize) {
104
- this._paging = {
105
- skip: (page - 1) * pageSize,
106
- limit: pageSize
107
- };
108
- }
109
- }
110
- }
111
- exports.default = CollectionStore;
1
+ 'use strict';
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const sift_1 = require("sift");
13
+ const _ = require("lodash");
14
+ const a_store_1 = require("./a.store");
15
+ const store_type_enum_1 = require("../_enums/store.type.enum");
16
+ class CollectionStore extends a_store_1.default {
17
+ constructor(model, target) {
18
+ super(model, target);
19
+ this._type = store_type_enum_1.default.COLLECTION;
20
+ Object.setPrototypeOf(this, CollectionStore.prototype);
21
+ }
22
+ shouldReload(change) {
23
+ if (this.isInitialSubscription(change))
24
+ return true;
25
+ const { operationType: type, updateDescription: description, fullDocument: document } = change;
26
+ const test = (0, sift_1.default)(_.omit(this._query, ['createdAt', 'updatedAt']));
27
+ switch (type) {
28
+ case 'delete':
29
+ case 'insert':
30
+ return true;
31
+ case 'replace':
32
+ case 'update':
33
+ if (!description)
34
+ return true;
35
+ if (!this.shouldConsiderFields())
36
+ return test(document);
37
+ const { updatedFields, removedFields } = description;
38
+ const us = _.concat(removedFields, _.keys(updatedFields));
39
+ const qs = _.keys(this._fields);
40
+ return !_.isEmpty(_.intersection(qs, us));
41
+ }
42
+ return false;
43
+ }
44
+ load(change) {
45
+ return __awaiter(this, void 0, void 0, function* () {
46
+ if (_.isEmpty(this._config))
47
+ return this.emitMany();
48
+ if (!this.shouldReload(change))
49
+ return;
50
+ try {
51
+ const { operationType: type, documentKey, fullDocument: document } = change;
52
+ const key = _.get(documentKey, '_id', '').toString();
53
+ if (document && this._incremental) {
54
+ if ('delete' === type)
55
+ return this.emitDelete(key);
56
+ for (const populate of this._populates) {
57
+ yield this._model.populate(document, populate);
58
+ }
59
+ if (_.isEmpty(this._virtuals))
60
+ return this.emitMany({ data: document });
61
+ const replacement = _.cloneDeep(_.omit(document.toJSON(), this._virtuals));
62
+ for (const virtual of this._virtuals) {
63
+ replacement[virtual] = yield Promise.resolve(document[virtual]);
64
+ }
65
+ return this.emitMany({ data: replacement });
66
+ }
67
+ else {
68
+ let data = [];
69
+ const total = yield this._model.countDocuments(this._query);
70
+ if (total > 0) {
71
+ data = yield this._model
72
+ .find(this._query, this._fields, this._paging)
73
+ .sort(this._sort)
74
+ .setOptions({ allowDiskUse: true });
75
+ for (const populate of this._populates) {
76
+ yield this._model.populate(data, populate);
77
+ }
78
+ if (!_.isEmpty(this._virtuals)) {
79
+ const replacements = [];
80
+ for (const item of data) {
81
+ const replacement = _.cloneDeep(_.omit(item.toJSON(), this._virtuals));
82
+ for (const virtual of this._virtuals) {
83
+ replacement[virtual] = yield Promise.resolve(item[virtual]);
84
+ }
85
+ replacements.push(replacement);
86
+ }
87
+ data = replacements;
88
+ }
89
+ }
90
+ return this.emitMany({ total, data });
91
+ }
92
+ }
93
+ catch (error) {
94
+ this.emitError(error);
95
+ }
96
+ });
97
+ }
98
+ extractFromConfig() {
99
+ super.extractFromConfig();
100
+ const { incremental = false, page = 1, pageSize } = this._config;
101
+ this._incremental = incremental;
102
+ this._paging = {};
103
+ if (pageSize) {
104
+ this._paging = {
105
+ skip: (page - 1) * pageSize,
106
+ limit: pageSize
107
+ };
108
+ }
109
+ }
110
+ }
111
+ exports.default = CollectionStore;
112
112
  //# sourceMappingURL=collection.store.js.map
@@ -1,7 +1,7 @@
1
- import { Model } from 'mongoose';
2
- import AStore from './a.store';
3
- export default class CountStore extends AStore {
4
- constructor(model: Model<any>, target: string);
5
- protected shouldReload(change: any): boolean;
6
- protected load(change: any): Promise<void>;
7
- }
1
+ import { Model } from 'mongoose';
2
+ import AStore from './a.store';
3
+ export default class CountStore extends AStore {
4
+ constructor(model: Model<any>, target: string);
5
+ protected shouldReload(change: any): boolean;
6
+ protected load(change: any): Promise<void>;
7
+ }
@@ -1,47 +1,47 @@
1
- 'use strict';
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- const lodash_1 = require("lodash");
13
- const a_store_1 = require("./a.store");
14
- const store_type_enum_1 = require("../_enums/store.type.enum");
15
- class CountStore extends a_store_1.default {
16
- constructor(model, target) {
17
- super(model, target);
18
- this._type = store_type_enum_1.default.COUNT;
19
- Object.setPrototypeOf(this, CountStore.prototype);
20
- }
21
- shouldReload(change) {
22
- if (this.isInitialSubscription(change))
23
- return true;
24
- const { operationType: type } = change;
25
- switch (type) {
26
- case 'delete':
27
- case 'insert':
28
- return true;
29
- case 'replace':
30
- case 'update':
31
- default:
32
- return false;
33
- }
34
- }
35
- load(change) {
36
- return __awaiter(this, void 0, void 0, function* () {
37
- if ((0, lodash_1.isEmpty)(this._config))
38
- return this.emitOne();
39
- if (!this.shouldReload(change))
40
- return;
41
- const count = yield this._model.countDocuments(this._query);
42
- this.emitOne(count);
43
- });
44
- }
45
- }
46
- exports.default = CountStore;
1
+ 'use strict';
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const lodash_1 = require("lodash");
13
+ const a_store_1 = require("./a.store");
14
+ const store_type_enum_1 = require("../_enums/store.type.enum");
15
+ class CountStore extends a_store_1.default {
16
+ constructor(model, target) {
17
+ super(model, target);
18
+ this._type = store_type_enum_1.default.COUNT;
19
+ Object.setPrototypeOf(this, CountStore.prototype);
20
+ }
21
+ shouldReload(change) {
22
+ if (this.isInitialSubscription(change))
23
+ return true;
24
+ const { operationType: type } = change;
25
+ switch (type) {
26
+ case 'delete':
27
+ case 'insert':
28
+ return true;
29
+ case 'replace':
30
+ case 'update':
31
+ default:
32
+ return false;
33
+ }
34
+ }
35
+ load(change) {
36
+ return __awaiter(this, void 0, void 0, function* () {
37
+ if ((0, lodash_1.isEmpty)(this._config))
38
+ return this.emitOne();
39
+ if (!this.shouldReload(change))
40
+ return;
41
+ const count = yield this._model.countDocuments(this._query);
42
+ this.emitOne(count);
43
+ });
44
+ }
45
+ }
46
+ exports.default = CountStore;
47
47
  //# sourceMappingURL=count.store.js.map
@@ -1,12 +1,12 @@
1
- import AStore from './a.store';
2
- export default class DocumentStore extends AStore {
3
- constructor(model: any, target: string);
4
- protected extractFromConfig(): void;
5
- restartSubscription(): void;
6
- protected shouldReload(change: any): boolean;
7
- protected load(change: any): Promise<void>;
8
- private _pipeFilter;
9
- private _loadDocumentById;
10
- private _loadSortedFirstDocument;
11
- private _loadDocument;
12
- }
1
+ import AStore from './a.store';
2
+ export default class DocumentStore extends AStore {
3
+ constructor(model: any, target: string);
4
+ protected extractFromConfig(): void;
5
+ restartSubscription(): void;
6
+ protected shouldReload(change: any): boolean;
7
+ protected load(change: any): Promise<void>;
8
+ private _pipeFilter;
9
+ private _loadDocumentById;
10
+ private _loadSortedFirstDocument;
11
+ private _loadDocument;
12
+ }
@@ -1,135 +1,135 @@
1
- 'use strict';
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- const sift_1 = require("sift");
13
- const _ = require("lodash");
14
- const rxjs_1 = require("rxjs");
15
- const operators_1 = require("rxjs/operators");
16
- const a_store_1 = require("./a.store");
17
- const store_type_enum_1 = require("../_enums/store.type.enum");
18
- const observable_model_1 = require("../mongodb/functions/observable.model");
19
- const _getIdFromQuery = (query) => (_.isString(query) ? query : _.get(query, '_id', '').toString());
20
- class DocumentStore extends a_store_1.default {
21
- constructor(model, target) {
22
- super(model, target);
23
- this._type = store_type_enum_1.default.DOCUMENT;
24
- Object.setPrototypeOf(this, DocumentStore.prototype);
25
- }
26
- extractFromConfig() {
27
- super.extractFromConfig();
28
- const { skip = 0 } = this._config;
29
- this._paging = skip ? {} : { skip, limit: 1 };
30
- }
31
- restartSubscription() {
32
- this.subscription = (0, observable_model_1.default)(this.model)
33
- .pipe((0, operators_1.throttleTime)(this._delay, rxjs_1.asyncScheduler, { leading: true, trailing: true }))
34
- .pipe((0, operators_1.filter)((change) => this._pipeFilter(change)))
35
- .subscribe({
36
- next: (change) => this.load(change)
37
- });
38
- }
39
- shouldReload(change) {
40
- if (this.isInitialSubscription(change))
41
- return true;
42
- const id = _getIdFromQuery(this._query);
43
- const { operationType: type, documentKey, updateDescription: description } = change;
44
- const key = _.get(documentKey, '_id', '').toString();
45
- switch (type) {
46
- case 'delete':
47
- return true;
48
- case 'insert':
49
- return !id;
50
- case 'replace':
51
- case 'update':
52
- if (id && id === key)
53
- return true;
54
- if (!description)
55
- return true;
56
- if (!this.shouldConsiderFields())
57
- return true;
58
- const { updatedFields, removedFields } = description;
59
- const us = _.concat(removedFields, _.keys(updatedFields));
60
- const qs = _.keys(this._fields);
61
- return !_.isEmpty(_.intersection(qs, us));
62
- }
63
- return false;
64
- }
65
- load(change) {
66
- return __awaiter(this, void 0, void 0, function* () {
67
- if (_.isEmpty(this._config))
68
- return this.emitOne();
69
- if (!this.shouldReload(change))
70
- return;
71
- const id = _getIdFromQuery(this._query);
72
- const { operationType: type, documentKey } = change;
73
- const key = _.get(documentKey, '_id', '').toString();
74
- if (type === 'delete' && id === key)
75
- return this.emitDelete(key);
76
- try {
77
- let data;
78
- if (!_.isEmpty(this._sort))
79
- data = yield this._loadSortedFirstDocument();
80
- else
81
- data = id ? yield this._loadDocumentById(id) : yield this._loadDocument();
82
- if (!data)
83
- return this.emitOne();
84
- for (const populate of this._populates) {
85
- if (data === null || data === void 0 ? void 0 : data.populate)
86
- yield data.populate(populate).execPopulate();
87
- }
88
- if (_.isEmpty(this._virtuals))
89
- return this.emitOne(data.toJSON());
90
- const jsonData = _.cloneDeep(_.omit(data.toJSON(), this._virtuals));
91
- for (const virtual of this._virtuals) {
92
- jsonData[virtual] = yield Promise.resolve(data[virtual]);
93
- }
94
- this.emitOne(jsonData);
95
- }
96
- catch (error) {
97
- this.emitError(error);
98
- }
99
- });
100
- }
101
- _pipeFilter(change) {
102
- if (!_.isEmpty(this._sort))
103
- return true;
104
- const { operationType: type } = change;
105
- if ('delete' === type)
106
- return true;
107
- const { documentKey, fullDocument: document } = change;
108
- const key = _.get(documentKey, '_id', '').toString();
109
- if (key === _getIdFromQuery(this._query))
110
- return true;
111
- const test = (0, sift_1.default)(_.omit(this._query, ['createdAt', 'updatedAt']));
112
- return test(document);
113
- }
114
- _loadDocumentById(id) {
115
- return __awaiter(this, void 0, void 0, function* () {
116
- return this._model.findById(id, this._fields);
117
- });
118
- }
119
- _loadSortedFirstDocument() {
120
- return __awaiter(this, void 0, void 0, function* () {
121
- const docs = yield this._model
122
- .find(this._query, this._fields, this._paging)
123
- .sort(this._sort)
124
- .setOptions({ allowDiskUse: true });
125
- return _.first(docs);
126
- });
127
- }
128
- _loadDocument() {
129
- return __awaiter(this, void 0, void 0, function* () {
130
- return this._model.findOne(this._query, this._fields);
131
- });
132
- }
133
- }
134
- exports.default = DocumentStore;
1
+ 'use strict';
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const sift_1 = require("sift");
13
+ const _ = require("lodash");
14
+ const rxjs_1 = require("rxjs");
15
+ const operators_1 = require("rxjs/operators");
16
+ const a_store_1 = require("./a.store");
17
+ const store_type_enum_1 = require("../_enums/store.type.enum");
18
+ const observable_model_1 = require("../mongodb/functions/observable.model");
19
+ const _getIdFromQuery = (query) => (_.isString(query) ? query : _.get(query, '_id', '').toString());
20
+ class DocumentStore extends a_store_1.default {
21
+ constructor(model, target) {
22
+ super(model, target);
23
+ this._type = store_type_enum_1.default.DOCUMENT;
24
+ Object.setPrototypeOf(this, DocumentStore.prototype);
25
+ }
26
+ extractFromConfig() {
27
+ super.extractFromConfig();
28
+ const { skip = 0 } = this._config;
29
+ this._paging = skip ? {} : { skip, limit: 1 };
30
+ }
31
+ restartSubscription() {
32
+ this.subscription = (0, observable_model_1.default)(this.model)
33
+ .pipe((0, operators_1.throttleTime)(this._delay, rxjs_1.asyncScheduler, { leading: true, trailing: true }))
34
+ .pipe((0, operators_1.filter)((change) => this._pipeFilter(change)))
35
+ .subscribe({
36
+ next: (change) => this.load(change)
37
+ });
38
+ }
39
+ shouldReload(change) {
40
+ if (this.isInitialSubscription(change))
41
+ return true;
42
+ const id = _getIdFromQuery(this._query);
43
+ const { operationType: type, documentKey, updateDescription: description } = change;
44
+ const key = _.get(documentKey, '_id', '').toString();
45
+ switch (type) {
46
+ case 'delete':
47
+ return true;
48
+ case 'insert':
49
+ return !id;
50
+ case 'replace':
51
+ case 'update':
52
+ if (id && id === key)
53
+ return true;
54
+ if (!description)
55
+ return true;
56
+ if (!this.shouldConsiderFields())
57
+ return true;
58
+ const { updatedFields, removedFields } = description;
59
+ const us = _.concat(removedFields, _.keys(updatedFields));
60
+ const qs = _.keys(this._fields);
61
+ return !_.isEmpty(_.intersection(qs, us));
62
+ }
63
+ return false;
64
+ }
65
+ load(change) {
66
+ return __awaiter(this, void 0, void 0, function* () {
67
+ if (_.isEmpty(this._config))
68
+ return this.emitOne();
69
+ if (!this.shouldReload(change))
70
+ return;
71
+ const id = _getIdFromQuery(this._query);
72
+ const { operationType: type, documentKey } = change;
73
+ const key = _.get(documentKey, '_id', '').toString();
74
+ if (type === 'delete' && id === key)
75
+ return this.emitDelete(key);
76
+ try {
77
+ let data;
78
+ if (!_.isEmpty(this._sort))
79
+ data = yield this._loadSortedFirstDocument();
80
+ else
81
+ data = id ? yield this._loadDocumentById(id) : yield this._loadDocument();
82
+ if (!data)
83
+ return this.emitOne();
84
+ for (const populate of this._populates) {
85
+ if (data === null || data === void 0 ? void 0 : data.populate)
86
+ yield data.populate(populate).execPopulate();
87
+ }
88
+ if (_.isEmpty(this._virtuals))
89
+ return this.emitOne(data.toJSON());
90
+ const jsonData = _.cloneDeep(_.omit(data.toJSON(), this._virtuals));
91
+ for (const virtual of this._virtuals) {
92
+ jsonData[virtual] = yield Promise.resolve(data[virtual]);
93
+ }
94
+ this.emitOne(jsonData);
95
+ }
96
+ catch (error) {
97
+ this.emitError(error);
98
+ }
99
+ });
100
+ }
101
+ _pipeFilter(change) {
102
+ if (!_.isEmpty(this._sort))
103
+ return true;
104
+ const { operationType: type } = change;
105
+ if ('delete' === type)
106
+ return true;
107
+ const { documentKey, fullDocument: document } = change;
108
+ const key = _.get(documentKey, '_id', '').toString();
109
+ if (key === _getIdFromQuery(this._query))
110
+ return true;
111
+ const test = (0, sift_1.default)(_.omit(this._query, ['createdAt', 'updatedAt']));
112
+ return test(document);
113
+ }
114
+ _loadDocumentById(id) {
115
+ return __awaiter(this, void 0, void 0, function* () {
116
+ return this._model.findById(id, this._fields);
117
+ });
118
+ }
119
+ _loadSortedFirstDocument() {
120
+ return __awaiter(this, void 0, void 0, function* () {
121
+ const docs = yield this._model
122
+ .find(this._query, this._fields, this._paging)
123
+ .sort(this._sort)
124
+ .setOptions({ allowDiskUse: true });
125
+ return _.first(docs);
126
+ });
127
+ }
128
+ _loadDocument() {
129
+ return __awaiter(this, void 0, void 0, function* () {
130
+ return this._model.findOne(this._query, this._fields);
131
+ });
132
+ }
133
+ }
134
+ exports.default = DocumentStore;
135
135
  //# sourceMappingURL=document.store.js.map
@@ -1,4 +1,4 @@
1
- import AStore from '../a.store';
2
- import StoreScopeType from '../../_types/store.scope.type';
3
- declare const storeFactory: (scope: StoreScopeType, observe: string, target: string) => AStore;
4
- export default storeFactory;
1
+ import AStore from '../a.store';
2
+ import StoreScopeType from '../../_types/store.scope.type';
3
+ declare const storeFactory: (scope: StoreScopeType, observe: string, target: string) => AStore;
4
+ export default storeFactory;