@squidcloud/local-backend 1.0.46 → 1.0.48

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 (171) hide show
  1. package/dist/common/src/ai-assistant.context.d.ts +13 -13
  2. package/dist/common/src/ai-assistant.context.js +19 -19
  3. package/dist/common/src/ai-assistant.schemas.d.ts +207 -207
  4. package/dist/common/src/ai-assistant.schemas.js +196 -196
  5. package/dist/common/src/ai-assistant.types.d.ts +80 -80
  6. package/dist/common/src/ai-assistant.types.js +11 -11
  7. package/dist/common/src/api-call.context.d.ts +13 -13
  8. package/dist/common/src/api-call.context.js +16 -16
  9. package/dist/common/src/api.types.d.ts +23 -23
  10. package/dist/common/src/api.types.js +2 -2
  11. package/dist/common/src/application.schemas.d.ts +1168 -1168
  12. package/dist/common/src/application.schemas.js +741 -741
  13. package/dist/common/src/application.types.d.ts +228 -228
  14. package/dist/common/src/application.types.js +89 -89
  15. package/dist/common/src/backend-function.schemas.d.ts +3 -3
  16. package/dist/common/src/backend-function.schemas.js +12 -12
  17. package/dist/common/src/backend-function.types.d.ts +6 -6
  18. package/dist/common/src/backend-function.types.js +2 -2
  19. package/dist/common/src/backend-run.types.d.ts +54 -54
  20. package/dist/common/src/backend-run.types.js +73 -73
  21. package/dist/common/src/bundle-api.types.d.ts +71 -71
  22. package/dist/common/src/bundle-api.types.js +2 -2
  23. package/dist/common/src/bundle-data.types.d.ts +59 -59
  24. package/dist/common/src/bundle-data.types.js +2 -2
  25. package/dist/common/src/communication.types.d.ts +18 -18
  26. package/dist/common/src/communication.types.js +40 -40
  27. package/dist/common/src/context.types.d.ts +24 -24
  28. package/dist/common/src/context.types.js +2 -2
  29. package/dist/common/src/distributed-lock.context.d.ts +5 -5
  30. package/dist/common/src/distributed-lock.context.js +10 -10
  31. package/dist/common/src/document.types.d.ts +32 -32
  32. package/dist/common/src/document.types.js +50 -50
  33. package/dist/common/src/graphql.context.d.ts +14 -14
  34. package/dist/common/src/graphql.context.js +12 -12
  35. package/dist/common/src/graphql.types.d.ts +11 -11
  36. package/dist/common/src/graphql.types.js +34 -34
  37. package/dist/common/src/heartbeat.types.d.ts +4 -4
  38. package/dist/common/src/heartbeat.types.js +16 -16
  39. package/dist/common/src/http-status.enum.d.ts +50 -50
  40. package/dist/common/src/http-status.enum.js +54 -54
  41. package/dist/common/src/index.d.ts +53 -53
  42. package/dist/common/src/index.js +69 -69
  43. package/dist/common/src/integrations/ai_assistant.types.d.ts +30 -30
  44. package/dist/common/src/integrations/ai_assistant.types.js +2 -2
  45. package/dist/common/src/integrations/api.types.d.ts +80 -80
  46. package/dist/common/src/integrations/api.types.js +2 -2
  47. package/dist/common/src/integrations/auth.types.d.ts +39 -39
  48. package/dist/common/src/integrations/auth.types.js +2 -2
  49. package/dist/common/src/integrations/database.types.d.ts +236 -236
  50. package/dist/common/src/integrations/database.types.js +2 -2
  51. package/dist/common/src/integrations/index.d.ts +101 -101
  52. package/dist/common/src/integrations/index.js +105 -105
  53. package/dist/common/src/integrations/observability.types.d.ts +23 -23
  54. package/dist/common/src/integrations/observability.types.js +23 -23
  55. package/dist/common/src/logger.types.d.ts +21 -21
  56. package/dist/common/src/logger.types.js +11 -11
  57. package/dist/common/src/metrics.types.d.ts +73 -73
  58. package/dist/common/src/metrics.types.js +11 -11
  59. package/dist/common/src/mutation.context.d.ts +13 -13
  60. package/dist/common/src/mutation.context.js +26 -26
  61. package/dist/common/src/mutation.schemas.d.ts +5 -5
  62. package/dist/common/src/mutation.schemas.js +114 -114
  63. package/dist/common/src/mutation.types.d.ts +63 -63
  64. package/dist/common/src/mutation.types.js +142 -142
  65. package/dist/common/src/named-query.context.d.ts +4 -4
  66. package/dist/common/src/named-query.context.js +9 -9
  67. package/dist/common/src/named-query.schemas.d.ts +3 -3
  68. package/dist/common/src/named-query.schemas.js +13 -13
  69. package/dist/common/src/named-query.types.d.ts +8 -8
  70. package/dist/common/src/named-query.types.js +2 -2
  71. package/dist/common/src/query/base-query-builder.d.ts +63 -63
  72. package/dist/common/src/query/base-query-builder.js +294 -289
  73. package/dist/common/src/query/base-query-builder.js.map +1 -1
  74. package/dist/common/src/query/index.d.ts +2 -2
  75. package/dist/common/src/query/index.js +18 -18
  76. package/dist/common/src/query/query-context.d.ts +47 -47
  77. package/dist/common/src/query/query-context.js +159 -159
  78. package/dist/common/src/query.types.d.ts +74 -74
  79. package/dist/common/src/query.types.js +153 -153
  80. package/dist/common/src/regions.d.ts +21 -21
  81. package/dist/common/src/regions.js +46 -46
  82. package/dist/common/src/schema/schema.types.d.ts +30 -30
  83. package/dist/common/src/schema/schema.types.js +172 -172
  84. package/dist/common/src/secret.schemas.d.ts +6 -6
  85. package/dist/common/src/secret.schemas.js +32 -32
  86. package/dist/common/src/secret.types.d.ts +49 -49
  87. package/dist/common/src/secret.types.js +5 -5
  88. package/dist/common/src/security.types.d.ts +3 -3
  89. package/dist/common/src/security.types.js +2 -2
  90. package/dist/common/src/socket.schemas.d.ts +3 -3
  91. package/dist/common/src/socket.schemas.js +58 -58
  92. package/dist/common/src/socket.types.d.ts +108 -108
  93. package/dist/common/src/socket.types.js +2 -2
  94. package/dist/common/src/time-units.d.ts +5 -5
  95. package/dist/common/src/time-units.js +8 -8
  96. package/dist/common/src/trigger.types.d.ts +15 -15
  97. package/dist/common/src/trigger.types.js +2 -2
  98. package/dist/common/src/types.d.ts +21 -21
  99. package/dist/common/src/types.js +2 -2
  100. package/dist/common/src/utils/array.d.ts +8 -8
  101. package/dist/common/src/utils/array.js +51 -51
  102. package/dist/common/src/utils/assert.d.ts +8 -8
  103. package/dist/common/src/utils/assert.js +49 -49
  104. package/dist/common/src/utils/error.d.ts +4 -4
  105. package/dist/common/src/utils/error.js +10 -10
  106. package/dist/common/src/utils/http.d.ts +2 -2
  107. package/dist/common/src/utils/http.js +38 -38
  108. package/dist/common/src/utils/id.d.ts +2 -2
  109. package/dist/common/src/utils/id.js +21 -21
  110. package/dist/common/src/utils/lock.manager.d.ts +9 -9
  111. package/dist/common/src/utils/lock.manager.js +39 -39
  112. package/dist/common/src/utils/nullish.d.ts +1 -1
  113. package/dist/common/src/utils/nullish.js +7 -7
  114. package/dist/common/src/utils/object.d.ts +6 -6
  115. package/dist/common/src/utils/object.js +89 -89
  116. package/dist/common/src/utils/serialization.d.ts +6 -6
  117. package/dist/common/src/utils/serialization.js +77 -77
  118. package/dist/common/src/utils/transforms.d.ts +18 -18
  119. package/dist/common/src/utils/transforms.js +92 -92
  120. package/dist/common/src/utils/url.d.ts +1 -1
  121. package/dist/common/src/utils/url.js +15 -15
  122. package/dist/common/src/utils/validation.d.ts +17 -17
  123. package/dist/common/src/utils/validation.js +209 -209
  124. package/dist/common/src/webhook-response.d.ts +7 -7
  125. package/dist/common/src/webhook-response.js +7 -7
  126. package/dist/internal-common/src/async-queue.d.ts +4 -4
  127. package/dist/internal-common/src/async-queue.js +17 -17
  128. package/dist/internal-common/src/code-executor.types.d.ts +13 -13
  129. package/dist/internal-common/src/code-executor.types.js +2 -2
  130. package/dist/internal-common/src/environment/environment.types.d.ts +74 -74
  131. package/dist/internal-common/src/environment/environment.types.js +2 -2
  132. package/dist/internal-common/src/environment/index.d.ts +6 -6
  133. package/dist/internal-common/src/environment/index.js +27 -27
  134. package/dist/internal-common/src/environment/local.environment.d.ts +2 -2
  135. package/dist/internal-common/src/environment/local.environment.js +62 -62
  136. package/dist/internal-common/src/filters/custom-exception.filter.d.ts +6 -6
  137. package/dist/internal-common/src/filters/custom-exception.filter.js +44 -44
  138. package/dist/internal-common/src/index.d.ts +14 -14
  139. package/dist/internal-common/src/index.js +32 -32
  140. package/dist/internal-common/src/interceptors/body.interceptor.d.ts +5 -5
  141. package/dist/internal-common/src/interceptors/body.interceptor.js +24 -24
  142. package/dist/internal-common/src/kafka/kafka.manager.d.ts +22 -22
  143. package/dist/internal-common/src/kafka/kafka.manager.js +128 -128
  144. package/dist/internal-common/src/kafka/kafka.types.d.ts +17 -17
  145. package/dist/internal-common/src/kafka/kafka.types.js +2 -2
  146. package/dist/internal-common/src/kafka-observability-publisher.d.ts +11 -11
  147. package/dist/internal-common/src/kafka-observability-publisher.js +20 -20
  148. package/dist/internal-common/src/local-dev.types.d.ts +31 -31
  149. package/dist/internal-common/src/local-dev.types.js +2 -2
  150. package/dist/internal-common/src/observability-publisher.d.ts +9 -9
  151. package/dist/internal-common/src/observability-publisher.js +58 -58
  152. package/dist/internal-common/src/observability.types.d.ts +17 -17
  153. package/dist/internal-common/src/observability.types.js +2 -2
  154. package/dist/internal-common/src/shutdown.manager.d.ts +20 -20
  155. package/dist/internal-common/src/shutdown.manager.js +75 -75
  156. package/dist/internal-common/src/utils.d.ts +5 -5
  157. package/dist/internal-common/src/utils.js +29 -29
  158. package/dist/local-backend/src/index.d.ts +4 -4
  159. package/dist/local-backend/src/index.js +20 -20
  160. package/dist/local-backend/src/local-backend-socket.service.d.ts +18 -18
  161. package/dist/local-backend/src/local-backend-socket.service.js +108 -108
  162. package/dist/local-backend/src/local-backend.app.d.ts +2 -2
  163. package/dist/local-backend/src/local-backend.app.js +19 -19
  164. package/dist/local-backend/src/local-backend.controller.d.ts +8 -8
  165. package/dist/local-backend/src/local-backend.controller.js +49 -49
  166. package/dist/local-backend/src/local-backend.module.d.ts +8 -8
  167. package/dist/local-backend/src/local-backend.module.js +53 -53
  168. package/dist/local-backend/src/local-backend.service.d.ts +15 -15
  169. package/dist/local-backend/src/local-backend.service.js +165 -165
  170. package/dist/tsconfig.build.tsbuildinfo +1 -1
  171. package/package.json +1 -1
@@ -1,290 +1,295 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Pagination = exports.BaseQueryBuilder = void 0;
4
- const rxjs_1 = require("rxjs");
5
- const assert_1 = require("../utils/assert");
6
- const object_1 = require("../utils/object");
7
- class BaseQueryBuilder {
8
- eq(fieldName, value) {
9
- return this.where(fieldName, '==', value);
10
- }
11
- neq(fieldName, value) {
12
- return this.where(fieldName, '!=', value);
13
- }
14
- in(fieldName, value) {
15
- return this.where(fieldName, 'in', value);
16
- }
17
- nin(fieldName, value) {
18
- return this.where(fieldName, 'not in', value);
19
- }
20
- gt(fieldName, value) {
21
- return this.where(fieldName, '>', value);
22
- }
23
- gte(fieldName, value) {
24
- return this.where(fieldName, '>=', value);
25
- }
26
- lt(fieldName, value) {
27
- return this.where(fieldName, '<', value);
28
- }
29
- lte(fieldName, value) {
30
- return this.where(fieldName, '<=', value);
31
- }
32
- like(fieldName, pattern, caseSensitive) {
33
- return this.where(fieldName, caseSensitive ? 'like_cs' : 'like', pattern);
34
- }
35
- notLike(fieldName, pattern, caseSensitive) {
36
- return this.where(fieldName, caseSensitive ? 'not like_cs' : 'not like', pattern);
37
- }
38
- }
39
- exports.BaseQueryBuilder = BaseQueryBuilder;
40
- class SnapshotEmitterWrapper {
41
- constructor(snapshotEmitter, subscribe, forward) {
42
- this.forward = forward;
43
- this.destroySubject = new rxjs_1.Subject();
44
- this.data = new rxjs_1.BehaviorSubject(undefined);
45
- snapshotEmitter
46
- .snapshots(subscribe)
47
- .pipe((0, rxjs_1.takeUntil)(this.destroySubject))
48
- .subscribe((data) => this.data.next(forward ? data : [...data].reverse()));
49
- this.destroySubject.subscribe(() => this.data.complete());
50
- }
51
- destroy() {
52
- this.destroySubject.next();
53
- this.destroySubject.complete();
54
- }
55
- }
56
- class Pagination {
57
- constructor(snapshotEmitter, options = {}) {
58
- this.isDestroyed = new rxjs_1.BehaviorSubject(false);
59
- this.destroyedObs = this.isDestroyed.pipe((0, rxjs_1.filter)(Boolean), (0, rxjs_1.take)(1));
60
- this._observeState = new rxjs_1.BehaviorSubject({
61
- data: [],
62
- hasNext: false,
63
- hasPrev: false,
64
- isLoading: true,
65
- });
66
- this.currentPageSnapshotEmitterWrapperSubject = new rxjs_1.BehaviorSubject(undefined);
67
- this.options = Object.assign({ pageSize: 100, subscribe: true }, options);
68
- this.templateSnapshotEmitter = snapshotEmitter.clone();
69
- if (this.options.subscribe) {
70
- this.firstPageSnapshotEmitterWrapper = new SnapshotEmitterWrapper(this.templateSnapshotEmitter.clone().limit(Math.min(this.options.pageSize * 3, 2000)), this.options.subscribe, true);
71
- this.firstPageDataAvailable = (0, rxjs_1.firstValueFrom)(this.firstPageSnapshotEmitterWrapper.data.pipe((0, rxjs_1.filter)(Boolean), (0, rxjs_1.raceWith)(this.isDestroyed)));
72
- this.firstPageSnapshotEmitterWrapper.data.pipe((0, rxjs_1.filter)(Boolean)).subscribe((data) => {
73
- if (!data.length) {
74
- this.resetToFirstPageData();
75
- return;
76
- }
77
- if (!this.currentPageSnapshotEmitterWrapperSubject.value) {
78
- this._observeState.next({
79
- data: data.slice(0, this.options.pageSize),
80
- hasNext: data.length > this.options.pageSize,
81
- hasPrev: false,
82
- isLoading: false,
83
- });
84
- }
85
- else if (!this._observeState.value.hasPrev &&
86
- this.templateSnapshotEmitter.extractData(data[0]) !==
87
- this.templateSnapshotEmitter.extractData(this._observeState.value.data[0])) {
88
- this._observeState.next(Object.assign(Object.assign({}, this._observeState.value), { hasPrev: true }));
89
- }
90
- });
91
- }
92
- else {
93
- const firstPageEmitter = this.templateSnapshotEmitter.clone().limit(this.options.pageSize + 1);
94
- this.currentPageSnapshotEmitterWrapperSubject.next(new SnapshotEmitterWrapper(firstPageEmitter, false, true));
95
- }
96
- this.currentPageSnapshotEmitterWrapperSubject
97
- .pipe((0, rxjs_1.takeUntil)(this.destroyedObs), (0, rxjs_1.startWith)(undefined), (0, rxjs_1.pairwise)())
98
- .subscribe(([before, after]) => {
99
- if (before)
100
- before.destroy();
101
- if (!after)
102
- return;
103
- after.data.pipe((0, rxjs_1.filter)(Boolean)).subscribe((data) => {
104
- var _a, _b, _c;
105
- if (after.forward) {
106
- let hasPrev = true;
107
- if (this.options.subscribe) {
108
- const firstPageData = (0, assert_1.truthy)((_a = this.firstPageSnapshotEmitterWrapper) === null || _a === void 0 ? void 0 : _a.data.value, 'NO_FIRST_PAGE_DATA');
109
- if (!firstPageData.length || !data.length) {
110
- this.resetToFirstPageData();
111
- return;
112
- }
113
- if (this.templateSnapshotEmitter.extractData(firstPageData[0]) ===
114
- this.templateSnapshotEmitter.extractData(data[0])) {
115
- this.resetToFirstPageData();
116
- return;
117
- }
118
- }
119
- else {
120
- hasPrev = this._observeState.value.hasNext;
121
- if (hasPrev && !data.length) {
122
- this.resetToFirstPageData();
123
- return;
124
- }
125
- }
126
- this._observeState.next({
127
- data: data.slice(0, this.options.pageSize),
128
- hasNext: data.length > this.options.pageSize,
129
- hasPrev,
130
- isLoading: false,
131
- });
132
- }
133
- else {
134
- let hasNext = true;
135
- if (this.options.subscribe) {
136
- if (data.length < this.options.pageSize) {
137
- this.resetToFirstPageData();
138
- return;
139
- }
140
- const firstPageData = (0, assert_1.truthy)((_b = this.firstPageSnapshotEmitterWrapper) === null || _b === void 0 ? void 0 : _b.data.value, 'NO_FIRST_PAGE_DATA');
141
- if (!firstPageData ||
142
- !data.length ||
143
- this.templateSnapshotEmitter.extractData(firstPageData[0]) ===
144
- this.templateSnapshotEmitter.extractData(data[0])) {
145
- this.resetToFirstPageData();
146
- return;
147
- }
148
- const lastPageData = (0, assert_1.truthy)((_c = this.lastPageSnapshotEmitterWrapper) === null || _c === void 0 ? void 0 : _c.data.value, 'NO_LAST_PAGE_DATA');
149
- if (this.templateSnapshotEmitter.extractData(lastPageData[lastPageData.length - 1]) ===
150
- this.templateSnapshotEmitter.extractData(data[data.length - 1])) {
151
- hasNext = false;
152
- }
153
- }
154
- const hasPrev = this.options.subscribe || data.length > this.options.pageSize;
155
- if (!data.length && this._observeState.value.hasPrev) {
156
- this.resetToFirstPageData();
157
- return;
158
- }
159
- this._observeState.next({
160
- data: data.slice(0, this.options.pageSize),
161
- hasNext: hasNext,
162
- hasPrev: hasPrev,
163
- isLoading: false,
164
- });
165
- }
166
- });
167
- });
168
- }
169
- async next() {
170
- if (this.isDestroyed.value)
171
- return this._observeState.value;
172
- await this.firstPageDataAvailable;
173
- if (this.isDestroyed.value)
174
- return this._observeState.value;
175
- const currentState = this._observeState.value;
176
- if (currentState.isLoading || !currentState.hasNext)
177
- return this._observeState.value;
178
- const currentData = currentState.data;
179
- const lastDoc = this.templateSnapshotEmitter.extractData((0, assert_1.truthy)(currentData[this.options.pageSize - 1], 'NO_LAST_ITEM'));
180
- const nextPageSnapshotEmitter = this.templateSnapshotEmitter
181
- .clone()
182
- .limit(Math.min(this.options.pageSize * 3, 2000))
183
- .addCompositeCondition(this.templateSnapshotEmitter.getSortOrders().map((sortOrder) => {
184
- return {
185
- fieldName: sortOrder.fieldName,
186
- operator: sortOrder.asc ? '>' : '<',
187
- value: (0, object_1.getInPath)(lastDoc, sortOrder.fieldName),
188
- };
189
- }));
190
- this._observeState.next(Object.assign(Object.assign({}, currentState), { isLoading: true }));
191
- this.currentPageSnapshotEmitterWrapperSubject.next(new SnapshotEmitterWrapper(nextPageSnapshotEmitter, this.options.subscribe, true));
192
- return await this.waitForData();
193
- }
194
- async prev() {
195
- if (this.isDestroyed.value)
196
- return this._observeState.value;
197
- await this.firstPageDataAvailable;
198
- if (this.options.subscribe) {
199
- this.createLastPageSnapshotEmitterWrapper();
200
- await this.lastPageDataAvailable;
201
- }
202
- if (this.isDestroyed.value)
203
- return this._observeState.value;
204
- const currentState = this._observeState.value;
205
- if (currentState.isLoading || !currentState.hasPrev)
206
- return this._observeState.value;
207
- const currentData = currentState.data;
208
- const firstDoc = this.templateSnapshotEmitter.extractData((0, assert_1.truthy)(currentData[0], 'NO_FIRST_ITEM'));
209
- const prevPageSnapshotEmitter = this.templateSnapshotEmitter
210
- .clone()
211
- .flipSortOrder()
212
- .limit(Math.min(this.options.pageSize * 3, 2000));
213
- prevPageSnapshotEmitter.addCompositeCondition(prevPageSnapshotEmitter.getSortOrders().map((sortOrder) => {
214
- return {
215
- fieldName: sortOrder.fieldName,
216
- operator: sortOrder.asc ? '>' : '<',
217
- value: (0, object_1.getInPath)(firstDoc, sortOrder.fieldName),
218
- };
219
- }));
220
- this._observeState.next(Object.assign(Object.assign({}, currentState), { isLoading: true }));
221
- this.currentPageSnapshotEmitterWrapperSubject.next(new SnapshotEmitterWrapper(prevPageSnapshotEmitter, this.options.subscribe, false));
222
- return await this.waitForData();
223
- }
224
- unsubscribe() {
225
- this.isDestroyed.next(true);
226
- this.isDestroyed.complete();
227
- if (this.firstPageSnapshotEmitterWrapper) {
228
- this.firstPageSnapshotEmitterWrapper.destroy();
229
- }
230
- if (this.currentPageSnapshotEmitterWrapperSubject.value) {
231
- this.currentPageSnapshotEmitterWrapperSubject.value.destroy();
232
- }
233
- if (this.lastPageSnapshotEmitterWrapper) {
234
- this.lastPageSnapshotEmitterWrapper.destroy();
235
- }
236
- this.currentPageSnapshotEmitterWrapperSubject.complete();
237
- this._observeState.complete();
238
- }
239
- observeState() {
240
- return this._observeState.asObservable();
241
- }
242
- async waitForData() {
243
- if (!this._observeState.value.isLoading)
244
- return this._observeState.value;
245
- await (0, rxjs_1.firstValueFrom)(this._observeState.pipe((0, rxjs_1.filter)((state) => !state.isLoading), (0, rxjs_1.take)(1), (0, rxjs_1.raceWith)(this.destroyedObs)));
246
- return this._observeState.value;
247
- }
248
- createLastPageSnapshotEmitterWrapper() {
249
- if (this.lastPageSnapshotEmitterWrapper)
250
- return;
251
- const lastPageEmitter = this.templateSnapshotEmitter
252
- .clone()
253
- .flipSortOrder()
254
- .limit(Math.min(this.options.pageSize * 3, 2000));
255
- this.lastPageSnapshotEmitterWrapper = new SnapshotEmitterWrapper(lastPageEmitter, this.options.subscribe, false);
256
- this.lastPageDataAvailable = (0, rxjs_1.firstValueFrom)(this.lastPageSnapshotEmitterWrapper.data.pipe((0, rxjs_1.filter)(Boolean), (0, rxjs_1.raceWith)(this.destroyedObs)));
257
- this.lastPageSnapshotEmitterWrapper.data.pipe((0, rxjs_1.filter)(Boolean)).subscribe((data) => {
258
- if (!this.currentPageSnapshotEmitterWrapperSubject) {
259
- return;
260
- }
261
- if (!data.length) {
262
- return;
263
- }
264
- if (!this._observeState.value.hasNext &&
265
- this.templateSnapshotEmitter.extractData(data[data.length - 1]) !==
266
- this.templateSnapshotEmitter.extractData(this._observeState.value.data[this._observeState.value.data.length - 1])) {
267
- this._observeState.next(Object.assign(Object.assign({}, this._observeState.value), { hasNext: true }));
268
- }
269
- });
270
- }
271
- resetToFirstPageData() {
272
- var _a, _b, _c, _d;
273
- this.currentPageSnapshotEmitterWrapperSubject.next(undefined);
274
- if (!this.options.subscribe) {
275
- const currentState = this._observeState.value;
276
- this._observeState.next(Object.assign(Object.assign({}, currentState), { hasNext: false, hasPrev: false, isLoading: true }));
277
- const firstPageEmitter = this.templateSnapshotEmitter.clone().limit(this.options.pageSize + 1);
278
- this.currentPageSnapshotEmitterWrapperSubject.next(new SnapshotEmitterWrapper(firstPageEmitter, false, true));
279
- return;
280
- }
281
- this._observeState.next({
282
- data: (_c = (_b = (_a = this.firstPageSnapshotEmitterWrapper) === null || _a === void 0 ? void 0 : _a.data.value) === null || _b === void 0 ? void 0 : _b.slice(0, this.options.pageSize)) !== null && _c !== void 0 ? _c : [],
283
- hasNext: (0, assert_1.truthy)((_d = this.firstPageSnapshotEmitterWrapper) === null || _d === void 0 ? void 0 : _d.data.value, 'NO_FIRST_PAGE_DATA').length > this.options.pageSize,
284
- hasPrev: false,
285
- isLoading: false,
286
- });
287
- }
288
- }
289
- exports.Pagination = Pagination;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Pagination = exports.BaseQueryBuilder = void 0;
4
+ const rxjs_1 = require("rxjs");
5
+ const assert_1 = require("../utils/assert");
6
+ const object_1 = require("../utils/object");
7
+ class BaseQueryBuilder {
8
+ eq(fieldName, value) {
9
+ return this.where(fieldName, '==', value);
10
+ }
11
+ neq(fieldName, value) {
12
+ return this.where(fieldName, '!=', value);
13
+ }
14
+ in(fieldName, value) {
15
+ return this.where(fieldName, 'in', value);
16
+ }
17
+ nin(fieldName, value) {
18
+ return this.where(fieldName, 'not in', value);
19
+ }
20
+ gt(fieldName, value) {
21
+ return this.where(fieldName, '>', value);
22
+ }
23
+ gte(fieldName, value) {
24
+ return this.where(fieldName, '>=', value);
25
+ }
26
+ lt(fieldName, value) {
27
+ return this.where(fieldName, '<', value);
28
+ }
29
+ lte(fieldName, value) {
30
+ return this.where(fieldName, '<=', value);
31
+ }
32
+ like(fieldName, pattern, caseSensitive) {
33
+ return this.where(fieldName, caseSensitive ? 'like_cs' : 'like', pattern);
34
+ }
35
+ notLike(fieldName, pattern, caseSensitive) {
36
+ return this.where(fieldName, caseSensitive ? 'not like_cs' : 'not like', pattern);
37
+ }
38
+ }
39
+ exports.BaseQueryBuilder = BaseQueryBuilder;
40
+ class SnapshotEmitterWrapper {
41
+ constructor(snapshotEmitter, options, forward) {
42
+ this.options = options;
43
+ this.forward = forward;
44
+ this.destroySubject = new rxjs_1.Subject();
45
+ this.data = new rxjs_1.BehaviorSubject(undefined);
46
+ snapshotEmitter
47
+ .snapshots(this.options.subscribe)
48
+ .pipe((0, rxjs_1.takeUntil)(this.destroySubject))
49
+ .subscribe((data) => this.data.next(forward ? data : this.reverse(data)));
50
+ this.destroySubject.subscribe(() => this.data.complete());
51
+ }
52
+ reverse(data) {
53
+ const slice = data.slice(0, this.options.pageSize).reverse();
54
+ return [...slice, ...data.slice(this.options.pageSize)];
55
+ }
56
+ destroy() {
57
+ this.destroySubject.next();
58
+ this.destroySubject.complete();
59
+ }
60
+ }
61
+ class Pagination {
62
+ constructor(snapshotEmitter, options = {}) {
63
+ this.isDestroyed = new rxjs_1.BehaviorSubject(false);
64
+ this.destroyedObs = this.isDestroyed.pipe((0, rxjs_1.filter)(Boolean), (0, rxjs_1.take)(1));
65
+ this._observeState = new rxjs_1.BehaviorSubject({
66
+ data: [],
67
+ hasNext: false,
68
+ hasPrev: false,
69
+ isLoading: true,
70
+ });
71
+ this.currentPageSnapshotEmitterWrapperSubject = new rxjs_1.BehaviorSubject(undefined);
72
+ this.options = Object.assign({ pageSize: 100, subscribe: true }, options);
73
+ this.templateSnapshotEmitter = snapshotEmitter.clone();
74
+ if (this.options.subscribe) {
75
+ this.firstPageSnapshotEmitterWrapper = new SnapshotEmitterWrapper(this.templateSnapshotEmitter.clone().limit(Math.min(this.options.pageSize * 3, 2000)), this.options, true);
76
+ this.firstPageDataAvailable = (0, rxjs_1.firstValueFrom)(this.firstPageSnapshotEmitterWrapper.data.pipe((0, rxjs_1.filter)(Boolean), (0, rxjs_1.raceWith)(this.isDestroyed)));
77
+ this.firstPageSnapshotEmitterWrapper.data.pipe((0, rxjs_1.filter)(Boolean)).subscribe((data) => {
78
+ if (!data.length) {
79
+ this.resetToFirstPageData();
80
+ return;
81
+ }
82
+ if (!this.currentPageSnapshotEmitterWrapperSubject.value) {
83
+ this._observeState.next({
84
+ data: data.slice(0, this.options.pageSize),
85
+ hasNext: data.length > this.options.pageSize,
86
+ hasPrev: false,
87
+ isLoading: false,
88
+ });
89
+ }
90
+ else if (!this._observeState.value.hasPrev &&
91
+ this.templateSnapshotEmitter.extractData(data[0]) !==
92
+ this.templateSnapshotEmitter.extractData(this._observeState.value.data[0])) {
93
+ this._observeState.next(Object.assign(Object.assign({}, this._observeState.value), { hasPrev: true }));
94
+ }
95
+ });
96
+ }
97
+ else {
98
+ const firstPageEmitter = this.templateSnapshotEmitter.clone().limit(this.options.pageSize + 1);
99
+ this.currentPageSnapshotEmitterWrapperSubject.next(new SnapshotEmitterWrapper(firstPageEmitter, this.options, true));
100
+ }
101
+ this.currentPageSnapshotEmitterWrapperSubject
102
+ .pipe((0, rxjs_1.takeUntil)(this.destroyedObs), (0, rxjs_1.startWith)(undefined), (0, rxjs_1.pairwise)())
103
+ .subscribe(([before, after]) => {
104
+ if (before)
105
+ before.destroy();
106
+ if (!after)
107
+ return;
108
+ after.data.pipe((0, rxjs_1.filter)(Boolean)).subscribe((data) => {
109
+ var _a, _b, _c;
110
+ if (after.forward) {
111
+ let hasPrev = true;
112
+ if (this.options.subscribe) {
113
+ const firstPageData = (0, assert_1.truthy)((_a = this.firstPageSnapshotEmitterWrapper) === null || _a === void 0 ? void 0 : _a.data.value, 'NO_FIRST_PAGE_DATA');
114
+ if (!firstPageData.length || !data.length) {
115
+ this.resetToFirstPageData();
116
+ return;
117
+ }
118
+ if (this.templateSnapshotEmitter.extractData(firstPageData[0]) ===
119
+ this.templateSnapshotEmitter.extractData(data[0])) {
120
+ this.resetToFirstPageData();
121
+ return;
122
+ }
123
+ }
124
+ else {
125
+ hasPrev = this._observeState.value.hasNext;
126
+ if (hasPrev && !data.length) {
127
+ this.resetToFirstPageData();
128
+ return;
129
+ }
130
+ }
131
+ this._observeState.next({
132
+ data: data.slice(0, this.options.pageSize),
133
+ hasNext: data.length > this.options.pageSize,
134
+ hasPrev,
135
+ isLoading: false,
136
+ });
137
+ }
138
+ else {
139
+ let hasNext = true;
140
+ if (this.options.subscribe) {
141
+ if (data.length < this.options.pageSize) {
142
+ this.resetToFirstPageData();
143
+ return;
144
+ }
145
+ const firstPageData = (0, assert_1.truthy)((_b = this.firstPageSnapshotEmitterWrapper) === null || _b === void 0 ? void 0 : _b.data.value, 'NO_FIRST_PAGE_DATA');
146
+ if (!firstPageData ||
147
+ !data.length ||
148
+ this.templateSnapshotEmitter.extractData(firstPageData[0]) ===
149
+ this.templateSnapshotEmitter.extractData(data[0])) {
150
+ this.resetToFirstPageData();
151
+ return;
152
+ }
153
+ const lastPageData = (0, assert_1.truthy)((_c = this.lastPageSnapshotEmitterWrapper) === null || _c === void 0 ? void 0 : _c.data.value, 'NO_LAST_PAGE_DATA');
154
+ if (this.templateSnapshotEmitter.extractData(lastPageData[lastPageData.length - 1]) ===
155
+ this.templateSnapshotEmitter.extractData(data[Math.min(data.length, this.options.pageSize) - 1])) {
156
+ hasNext = false;
157
+ }
158
+ }
159
+ const hasPrev = this.options.subscribe || data.length > this.options.pageSize;
160
+ if (!data.length && this._observeState.value.hasPrev) {
161
+ this.resetToFirstPageData();
162
+ return;
163
+ }
164
+ this._observeState.next({
165
+ data: data.slice(0, this.options.pageSize),
166
+ hasNext: hasNext,
167
+ hasPrev: hasPrev,
168
+ isLoading: false,
169
+ });
170
+ }
171
+ });
172
+ });
173
+ }
174
+ async next() {
175
+ if (this.isDestroyed.value)
176
+ return this._observeState.value;
177
+ await this.firstPageDataAvailable;
178
+ if (this.isDestroyed.value)
179
+ return this._observeState.value;
180
+ const currentState = this._observeState.value;
181
+ if (currentState.isLoading || !currentState.hasNext)
182
+ return this._observeState.value;
183
+ const currentData = currentState.data;
184
+ const lastDoc = this.templateSnapshotEmitter.extractData((0, assert_1.truthy)(currentData[this.options.pageSize - 1], 'NO_LAST_ITEM'));
185
+ const nextPageSnapshotEmitter = this.templateSnapshotEmitter
186
+ .clone()
187
+ .limit(Math.min(this.options.pageSize * 3, 2000))
188
+ .addCompositeCondition(this.templateSnapshotEmitter.getSortOrders().map((sortOrder) => {
189
+ return {
190
+ fieldName: sortOrder.fieldName,
191
+ operator: sortOrder.asc ? '>' : '<',
192
+ value: (0, object_1.getInPath)(lastDoc, sortOrder.fieldName),
193
+ };
194
+ }));
195
+ this._observeState.next(Object.assign(Object.assign({}, currentState), { isLoading: true }));
196
+ this.currentPageSnapshotEmitterWrapperSubject.next(new SnapshotEmitterWrapper(nextPageSnapshotEmitter, this.options, true));
197
+ return await this.waitForData();
198
+ }
199
+ async prev() {
200
+ if (this.isDestroyed.value)
201
+ return this._observeState.value;
202
+ await this.firstPageDataAvailable;
203
+ if (this.options.subscribe) {
204
+ this.createLastPageSnapshotEmitterWrapper();
205
+ await this.lastPageDataAvailable;
206
+ }
207
+ if (this.isDestroyed.value)
208
+ return this._observeState.value;
209
+ const currentState = this._observeState.value;
210
+ if (currentState.isLoading || !currentState.hasPrev)
211
+ return this._observeState.value;
212
+ const currentData = currentState.data;
213
+ const firstDoc = this.templateSnapshotEmitter.extractData((0, assert_1.truthy)(currentData[0], 'NO_FIRST_ITEM'));
214
+ const prevPageSnapshotEmitter = this.templateSnapshotEmitter
215
+ .clone()
216
+ .flipSortOrder()
217
+ .limit(Math.min(this.options.pageSize * 3, 2000));
218
+ prevPageSnapshotEmitter.addCompositeCondition(prevPageSnapshotEmitter.getSortOrders().map((sortOrder) => {
219
+ return {
220
+ fieldName: sortOrder.fieldName,
221
+ operator: sortOrder.asc ? '>' : '<',
222
+ value: (0, object_1.getInPath)(firstDoc, sortOrder.fieldName),
223
+ };
224
+ }));
225
+ this._observeState.next(Object.assign(Object.assign({}, currentState), { isLoading: true }));
226
+ this.currentPageSnapshotEmitterWrapperSubject.next(new SnapshotEmitterWrapper(prevPageSnapshotEmitter, this.options, false));
227
+ return await this.waitForData();
228
+ }
229
+ unsubscribe() {
230
+ this.isDestroyed.next(true);
231
+ this.isDestroyed.complete();
232
+ if (this.firstPageSnapshotEmitterWrapper) {
233
+ this.firstPageSnapshotEmitterWrapper.destroy();
234
+ }
235
+ if (this.currentPageSnapshotEmitterWrapperSubject.value) {
236
+ this.currentPageSnapshotEmitterWrapperSubject.value.destroy();
237
+ }
238
+ if (this.lastPageSnapshotEmitterWrapper) {
239
+ this.lastPageSnapshotEmitterWrapper.destroy();
240
+ }
241
+ this.currentPageSnapshotEmitterWrapperSubject.complete();
242
+ this._observeState.complete();
243
+ }
244
+ observeState() {
245
+ return this._observeState.asObservable();
246
+ }
247
+ async waitForData() {
248
+ if (!this._observeState.value.isLoading)
249
+ return this._observeState.value;
250
+ await (0, rxjs_1.firstValueFrom)(this._observeState.pipe((0, rxjs_1.filter)((state) => !state.isLoading), (0, rxjs_1.take)(1), (0, rxjs_1.raceWith)(this.destroyedObs)));
251
+ return this._observeState.value;
252
+ }
253
+ createLastPageSnapshotEmitterWrapper() {
254
+ if (this.lastPageSnapshotEmitterWrapper)
255
+ return;
256
+ const lastPageEmitter = this.templateSnapshotEmitter
257
+ .clone()
258
+ .flipSortOrder()
259
+ .limit(Math.min(this.options.pageSize * 3, 2000));
260
+ this.lastPageSnapshotEmitterWrapper = new SnapshotEmitterWrapper(lastPageEmitter, this.options, false);
261
+ this.lastPageDataAvailable = (0, rxjs_1.firstValueFrom)(this.lastPageSnapshotEmitterWrapper.data.pipe((0, rxjs_1.filter)(Boolean), (0, rxjs_1.raceWith)(this.destroyedObs)));
262
+ this.lastPageSnapshotEmitterWrapper.data.pipe((0, rxjs_1.filter)(Boolean)).subscribe((data) => {
263
+ if (!this.currentPageSnapshotEmitterWrapperSubject) {
264
+ return;
265
+ }
266
+ if (!data.length) {
267
+ return;
268
+ }
269
+ if (!this._observeState.value.hasNext &&
270
+ this.templateSnapshotEmitter.extractData(data[data.length - 1]) !==
271
+ this.templateSnapshotEmitter.extractData(this._observeState.value.data[this._observeState.value.data.length - 1])) {
272
+ this._observeState.next(Object.assign(Object.assign({}, this._observeState.value), { hasNext: true }));
273
+ }
274
+ });
275
+ }
276
+ resetToFirstPageData() {
277
+ var _a, _b, _c, _d;
278
+ this.currentPageSnapshotEmitterWrapperSubject.next(undefined);
279
+ if (!this.options.subscribe) {
280
+ const currentState = this._observeState.value;
281
+ this._observeState.next(Object.assign(Object.assign({}, currentState), { hasNext: false, hasPrev: false, isLoading: true }));
282
+ const firstPageEmitter = this.templateSnapshotEmitter.clone().limit(this.options.pageSize + 1);
283
+ this.currentPageSnapshotEmitterWrapperSubject.next(new SnapshotEmitterWrapper(firstPageEmitter, this.options, true));
284
+ return;
285
+ }
286
+ this._observeState.next({
287
+ data: (_c = (_b = (_a = this.firstPageSnapshotEmitterWrapper) === null || _a === void 0 ? void 0 : _a.data.value) === null || _b === void 0 ? void 0 : _b.slice(0, this.options.pageSize)) !== null && _c !== void 0 ? _c : [],
288
+ hasNext: (0, assert_1.truthy)((_d = this.firstPageSnapshotEmitterWrapper) === null || _d === void 0 ? void 0 : _d.data.value, 'NO_FIRST_PAGE_DATA').length > this.options.pageSize,
289
+ hasPrev: false,
290
+ isLoading: false,
291
+ });
292
+ }
293
+ }
294
+ exports.Pagination = Pagination;
290
295
  //# sourceMappingURL=base-query-builder.js.map