@kidus.dev/flowdb 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/README.md +15 -0
  2. package/dist/core/blob/blob.d.ts +25 -0
  3. package/dist/core/blob/blob.d.ts.map +1 -0
  4. package/dist/core/blob/blob.js +57 -0
  5. package/dist/core/blob/blob_disk_driver.d.ts +27 -0
  6. package/dist/core/blob/blob_disk_driver.d.ts.map +1 -0
  7. package/dist/core/blob/blob_disk_driver.js +179 -0
  8. package/dist/core/blob/blob_storage.d.ts +90 -0
  9. package/dist/core/blob/blob_storage.d.ts.map +1 -0
  10. package/dist/core/blob/blob_storage.js +248 -0
  11. package/dist/core/collection/collection.d.ts +99 -0
  12. package/dist/core/collection/collection.d.ts.map +1 -0
  13. package/dist/core/collection/collection.js +226 -0
  14. package/dist/core/collection/document.d.ts +24 -0
  15. package/dist/core/collection/document.d.ts.map +1 -0
  16. package/dist/core/collection/document.js +86 -0
  17. package/dist/core/collection/driver/collection_file_driver.d.ts +20 -0
  18. package/dist/core/collection/driver/collection_file_driver.d.ts.map +1 -0
  19. package/dist/core/collection/driver/collection_file_driver.js +90 -0
  20. package/dist/core/collection/driver/collection_operations.d.ts +43 -0
  21. package/dist/core/collection/driver/collection_operations.d.ts.map +1 -0
  22. package/dist/core/collection/driver/collection_operations.js +278 -0
  23. package/dist/core/collection/query/compares.d.ts +90 -0
  24. package/dist/core/collection/query/compares.d.ts.map +1 -0
  25. package/dist/core/collection/query/compares.js +362 -0
  26. package/dist/core/collection/query/filtering.d.ts +14 -0
  27. package/dist/core/collection/query/filtering.d.ts.map +1 -0
  28. package/dist/core/collection/query/filtering.js +176 -0
  29. package/dist/core/collection/query/query_builder.d.ts +145 -0
  30. package/dist/core/collection/query/query_builder.d.ts.map +1 -0
  31. package/dist/core/collection/query/query_builder.js +196 -0
  32. package/dist/core/collection/query/query_executor.d.ts +87 -0
  33. package/dist/core/collection/query/query_executor.d.ts.map +1 -0
  34. package/dist/core/collection/query/query_executor.js +348 -0
  35. package/dist/core/collection/query/relation.d.ts +29 -0
  36. package/dist/core/collection/query/relation.d.ts.map +1 -0
  37. package/dist/core/collection/query/relation.js +40 -0
  38. package/dist/core/database/backup.d.ts +26 -0
  39. package/dist/core/database/backup.d.ts.map +1 -0
  40. package/dist/core/database/backup.js +114 -0
  41. package/dist/core/database/database.d.ts +57 -0
  42. package/dist/core/database/database.d.ts.map +1 -0
  43. package/dist/core/database/database.js +179 -0
  44. package/dist/core/database/database_file_driver.d.ts +27 -0
  45. package/dist/core/database/database_file_driver.d.ts.map +1 -0
  46. package/dist/core/database/database_file_driver.js +135 -0
  47. package/dist/core/store/store.d.ts +24 -0
  48. package/dist/core/store/store.d.ts.map +1 -0
  49. package/dist/core/store/store.js +54 -0
  50. package/dist/core/store/store_file_driver.d.ts +26 -0
  51. package/dist/core/store/store_file_driver.d.ts.map +1 -0
  52. package/dist/core/store/store_file_driver.js +87 -0
  53. package/dist/tools/encryptor.d.ts +17 -0
  54. package/dist/tools/encryptor.d.ts.map +1 -0
  55. package/dist/tools/encryptor.js +80 -0
  56. package/dist/tools/format.d.ts +10 -0
  57. package/dist/tools/format.d.ts.map +1 -0
  58. package/dist/tools/format.js +40 -0
  59. package/dist/tools/index.d.ts +7 -0
  60. package/dist/tools/index.d.ts.map +1 -0
  61. package/dist/tools/index.js +6 -0
  62. package/dist/tools/json.d.ts +19 -0
  63. package/dist/tools/json.d.ts.map +1 -0
  64. package/dist/tools/json.js +232 -0
  65. package/dist/tools/performance.d.ts +20 -0
  66. package/dist/tools/performance.d.ts.map +1 -0
  67. package/dist/tools/performance.js +53 -0
  68. package/dist/tools/print_colored.d.ts +47 -0
  69. package/dist/tools/print_colored.d.ts.map +1 -0
  70. package/dist/tools/print_colored.js +119 -0
  71. package/dist/tools/random.d.ts +46 -0
  72. package/dist/tools/random.d.ts.map +1 -0
  73. package/dist/tools/random.js +214 -0
  74. package/package.json +40 -0
@@ -0,0 +1,362 @@
1
+ // TypeScript version of compares.dart logic
2
+ export const compareMap = {
3
+ gt: (value) => new Gt(value),
4
+ gte: (value) => new Gte(value),
5
+ lt: (value) => new Lt(value),
6
+ lte: (value) => new Lte(value),
7
+ eq: (value) => new Eq(value),
8
+ neq: (value) => new Neq(value),
9
+ startsWith: (value) => new StartsWith(value),
10
+ endsWith: (value) => new EndsWith(value),
11
+ contains: (value) => new Contains(value),
12
+ doesNotContain: (value) => new DoesNotContain(value),
13
+ regexMatch: (value) => new RegexMatch(value),
14
+ between: (value) => new Between(value),
15
+ betweenStartInclusive: (value) => new Between(value, true, false),
16
+ betweenEndInclusive: (value) => new Between(value, false, true),
17
+ betweenBothInclusive: (value) => new Between(value, true, true),
18
+ isNull: () => new IsNull(),
19
+ isNotNull: () => new IsNotNull(),
20
+ isEmpty: () => new Empty(),
21
+ isNotEmpty: () => new NotEmpty(),
22
+ filter: (func) => new Filter(func),
23
+ };
24
+ export class Filter {
25
+ predicate;
26
+ constructor(predicate) {
27
+ this.predicate = predicate;
28
+ }
29
+ compare(other) {
30
+ try {
31
+ return this.predicate(other);
32
+ }
33
+ catch {
34
+ return false;
35
+ }
36
+ }
37
+ }
38
+ export class Gt {
39
+ value;
40
+ constructor(value) {
41
+ this.value = value;
42
+ }
43
+ compare(other) {
44
+ try {
45
+ if ((typeof other === "number" && typeof this.value === "number") ||
46
+ (typeof other === "string" && typeof this.value === "string")) {
47
+ return other > this.value;
48
+ }
49
+ if (other instanceof Date && this.value instanceof Date) {
50
+ return other.getTime() > this.value.getTime();
51
+ }
52
+ if (other != null && typeof other.compareTo === "function") {
53
+ return other.compareTo(this.value) > 0;
54
+ }
55
+ }
56
+ catch { }
57
+ return false;
58
+ }
59
+ }
60
+ export class Gte {
61
+ value;
62
+ constructor(value) {
63
+ this.value = value;
64
+ }
65
+ compare(other) {
66
+ try {
67
+ if ((typeof other === "number" && typeof this.value === "number") ||
68
+ (typeof other === "string" && typeof this.value === "string")) {
69
+ return other >= this.value;
70
+ }
71
+ if (other instanceof Date && this.value instanceof Date) {
72
+ return other.getTime() >= this.value.getTime();
73
+ }
74
+ if (other != null && typeof other.compareTo === "function") {
75
+ return other.compareTo(this.value) >= 0;
76
+ }
77
+ }
78
+ catch { }
79
+ return false;
80
+ }
81
+ }
82
+ export class Lt {
83
+ value;
84
+ constructor(value) {
85
+ this.value = value;
86
+ }
87
+ compare(other) {
88
+ try {
89
+ if ((typeof other === "number" && typeof this.value === "number") ||
90
+ (typeof other === "string" && typeof this.value === "string")) {
91
+ return other < this.value;
92
+ }
93
+ if (other instanceof Date && this.value instanceof Date) {
94
+ return other.getTime() < this.value.getTime();
95
+ }
96
+ if (other != null && typeof other.compareTo === "function") {
97
+ return other.compareTo(this.value) < 0;
98
+ }
99
+ }
100
+ catch { }
101
+ return false;
102
+ }
103
+ }
104
+ export class Lte {
105
+ value;
106
+ constructor(value) {
107
+ this.value = value;
108
+ }
109
+ compare(other) {
110
+ try {
111
+ if ((typeof other === "number" && typeof this.value === "number") ||
112
+ (typeof other === "string" && typeof this.value === "string")) {
113
+ return other <= this.value;
114
+ }
115
+ if (other instanceof Date && this.value instanceof Date) {
116
+ return other.getTime() <= this.value.getTime();
117
+ }
118
+ if (other != null && typeof other.compareTo === "function") {
119
+ return other.compareTo(this.value) <= 0;
120
+ }
121
+ }
122
+ catch { }
123
+ return false;
124
+ }
125
+ }
126
+ export class Eq {
127
+ value;
128
+ constructor(value) {
129
+ this.value = value;
130
+ }
131
+ compare(other) {
132
+ if (other instanceof Date && this.value instanceof Date) {
133
+ return other.getTime() === this.value.getTime();
134
+ }
135
+ return other === this.value;
136
+ }
137
+ }
138
+ export class Neq {
139
+ value;
140
+ constructor(value) {
141
+ this.value = value;
142
+ }
143
+ compare(other) {
144
+ if (other instanceof Date && this.value instanceof Date) {
145
+ return other.getTime() !== this.value.getTime();
146
+ }
147
+ return other !== this.value;
148
+ }
149
+ }
150
+ export class Contains {
151
+ value;
152
+ constructor(value) {
153
+ this.value = value;
154
+ }
155
+ compare(other) {
156
+ if (Array.isArray(other)) {
157
+ return other.includes(this.value);
158
+ }
159
+ if (typeof other === "string" && typeof this.value === "string") {
160
+ return other.includes(this.value);
161
+ }
162
+ if (other instanceof Set) {
163
+ return other.has(this.value);
164
+ }
165
+ if (typeof other === "object" &&
166
+ other !== null &&
167
+ !(other instanceof Set) &&
168
+ !Array.isArray(other)) {
169
+ return Object.prototype.hasOwnProperty.call(other, this.value);
170
+ }
171
+ return false;
172
+ }
173
+ }
174
+ export class DoesNotContain {
175
+ value;
176
+ constructor(value) {
177
+ this.value = value;
178
+ }
179
+ compare(other) {
180
+ if (Array.isArray(other)) {
181
+ return !other.includes(this.value);
182
+ }
183
+ if (typeof other === "string" && typeof this.value === "string") {
184
+ return !other.includes(this.value);
185
+ }
186
+ if (other instanceof Set) {
187
+ return !other.has(this.value);
188
+ }
189
+ if (typeof other === "object" &&
190
+ other !== null &&
191
+ !(other instanceof Set) &&
192
+ !Array.isArray(other)) {
193
+ return !Object.prototype.hasOwnProperty.call(other, this.value);
194
+ }
195
+ return false;
196
+ }
197
+ }
198
+ export class StartsWith {
199
+ value;
200
+ constructor(value) {
201
+ this.value = value;
202
+ }
203
+ compare(other) {
204
+ if (typeof other === "string" && typeof this.value === "string") {
205
+ return other.startsWith(this.value);
206
+ }
207
+ if (Array.isArray(other) && other.length > 0) {
208
+ return other[0] === this.value;
209
+ }
210
+ if (other && typeof other[Symbol.iterator] === "function") {
211
+ const iter = Array.from(other);
212
+ return iter.length > 0 && iter[0] === this.value;
213
+ }
214
+ return false;
215
+ }
216
+ }
217
+ export class EndsWith {
218
+ value;
219
+ constructor(value) {
220
+ this.value = value;
221
+ }
222
+ compare(other) {
223
+ if (typeof other === "string" && typeof this.value === "string") {
224
+ return other.endsWith(this.value);
225
+ }
226
+ if (Array.isArray(other) && other.length > 0) {
227
+ return other[other.length - 1] === this.value;
228
+ }
229
+ if (other && typeof other[Symbol.iterator] === "function") {
230
+ const iter = Array.from(other);
231
+ return iter.length > 0 && iter[iter.length - 1] === this.value;
232
+ }
233
+ return false;
234
+ }
235
+ }
236
+ export class RegexMatch {
237
+ pattern;
238
+ caseSensitive;
239
+ multiLine;
240
+ constructor(pattern, caseSensitive = true, multiLine = false) {
241
+ this.pattern = pattern;
242
+ this.caseSensitive = caseSensitive;
243
+ this.multiLine = multiLine;
244
+ }
245
+ compare(value) {
246
+ if (typeof value !== "string")
247
+ return false;
248
+ try {
249
+ const flags = (this.caseSensitive ? "" : "i") + (this.multiLine ? "m" : "");
250
+ return new RegExp(this.pattern, flags).test(value);
251
+ }
252
+ catch {
253
+ return false;
254
+ }
255
+ }
256
+ }
257
+ export class Between {
258
+ range;
259
+ includeLower;
260
+ includeUpper;
261
+ constructor(range, includeLower = false, includeUpper = false) {
262
+ this.range = range;
263
+ this.includeLower = includeLower;
264
+ this.includeUpper = includeUpper;
265
+ // If a tuple/array is not passed, try to parse from string/array.
266
+ if (!Array.isArray(this.range) && typeof this.range === "string") {
267
+ const parsed = JSON.parse(this.range);
268
+ if (Array.isArray(parsed) && parsed.length === 2) {
269
+ this.range = [parsed[0], parsed[1]];
270
+ }
271
+ else {
272
+ this.range = [undefined, undefined];
273
+ }
274
+ }
275
+ // If the range is a date, convert it to a number
276
+ if (this.range[0] instanceof Date && this.range[1] instanceof Date) {
277
+ this.range[0] = this.range[0].getTime();
278
+ this.range[1] = this.range[1].getTime();
279
+ if (this.includeLower) {
280
+ this.range[0] = this.range[0] - 1;
281
+ }
282
+ if (this.includeUpper) {
283
+ this.range[1] = this.range[1] + 1;
284
+ }
285
+ }
286
+ }
287
+ compare(value) {
288
+ try {
289
+ const v = value;
290
+ const low = this.range[0];
291
+ const high = this.range[1];
292
+ if ((typeof v === "number" &&
293
+ typeof low === "number" &&
294
+ typeof high === "number") ||
295
+ (typeof v === "string" &&
296
+ typeof low === "string" &&
297
+ typeof high === "string")) {
298
+ if (low > high)
299
+ return false;
300
+ const lowComp = v < low ? -1 : v > low ? 1 : 0;
301
+ const highComp = v < high ? -1 : v > high ? 1 : 0;
302
+ const lowOk = this.includeLower ? lowComp >= 0 : lowComp > 0;
303
+ const highOk = this.includeUpper ? highComp <= 0 : highComp < 0;
304
+ return lowOk && highOk;
305
+ }
306
+ if (v instanceof Date && low instanceof Date && high instanceof Date) {
307
+ return v.getTime() >= low.getTime() && v.getTime() <= high.getTime();
308
+ }
309
+ if (v != null && typeof v.compareTo === "function") {
310
+ return v.compareTo(low) >= 0 && v.compareTo(high) <= 0;
311
+ }
312
+ }
313
+ catch { }
314
+ return false;
315
+ }
316
+ }
317
+ export class IsNull {
318
+ compare(value) {
319
+ return value == null;
320
+ }
321
+ }
322
+ export class IsNotNull {
323
+ compare(value) {
324
+ return value != null;
325
+ }
326
+ }
327
+ export class Empty {
328
+ compare(value) {
329
+ if (value == null)
330
+ return false;
331
+ if (typeof value === "string" || Array.isArray(value)) {
332
+ return value.length === 0;
333
+ }
334
+ if (value instanceof Set || value instanceof Map) {
335
+ return value.size === 0;
336
+ }
337
+ if (typeof value === "object") {
338
+ return Object.keys(value).length === 0;
339
+ }
340
+ return false;
341
+ }
342
+ }
343
+ export class NotEmpty {
344
+ compare(value) {
345
+ if (value == null)
346
+ return false;
347
+ if (typeof value === "string" || Array.isArray(value)) {
348
+ return value.length > 0;
349
+ }
350
+ if (value instanceof Set || value instanceof Map) {
351
+ return value.size > 0;
352
+ }
353
+ if (typeof value === "object") {
354
+ return Object.keys(value).length > 0;
355
+ }
356
+ return false;
357
+ }
358
+ }
359
+ // Factory for creating Compare from key/value
360
+ export function compareFromJson(key, value) {
361
+ return compareMap[key]?.(value) ?? new Filter(() => true);
362
+ }
@@ -0,0 +1,14 @@
1
+ import Document from "../document";
2
+ import Relation from "./relation";
3
+ import { type SortOrder } from "./compares";
4
+ export declare class Filtering {
5
+ static matchedDocuments(documents: Document[], conditions: Array<{
6
+ [key: string]: any;
7
+ }>): Document[];
8
+ static sortedDocuments(documents: Document[], orderBys: Record<string, SortOrder>): Document[];
9
+ static skipAndTakeDocuments(documents: Document[], skip?: number | null, take?: number | null): Document[];
10
+ static pickAndOmitDocuments(documents: Document[], pick: Set<string>, omit: Set<string>): Document[];
11
+ static includedDocumentsSync(documents: Document[], includes: Record<string, Relation>): Document[];
12
+ static includedDocuments(documents: Iterable<Document>, includes: Record<string, Relation>): Promise<Document[]>;
13
+ }
14
+ //# sourceMappingURL=filtering.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"filtering.d.ts","sourceRoot":"","sources":["../../../../src/core/collection/query/filtering.ts"],"names":[],"mappings":"AAEA,OAAO,QAAQ,MAAM,aAAa,CAAC;AACnC,OAAO,QAAQ,MAAM,YAAY,CAAC;AAClC,OAAO,EAAc,KAAK,SAAS,EAAE,MAAM,YAAY,CAAC;AAExD,qBAAa,SAAS;IACpB,MAAM,CAAC,gBAAgB,CACrB,SAAS,EAAE,QAAQ,EAAE,EACrB,UAAU,EAAE,KAAK,CAAC;QAAE,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;KAAE,CAAC,GACxC,QAAQ,EAAE;IAsCb,MAAM,CAAC,eAAe,CACpB,SAAS,EAAE,QAAQ,EAAE,EACrB,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,GAClC,QAAQ,EAAE;IA6Cb,MAAM,CAAC,oBAAoB,CACzB,SAAS,EAAE,QAAQ,EAAE,EACrB,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,EACpB,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,GACnB,QAAQ,EAAE;IAOb,MAAM,CAAC,oBAAoB,CACzB,SAAS,EAAE,QAAQ,EAAE,EACrB,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,EACjB,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,GAChB,QAAQ,EAAE;IA8Bb,MAAM,CAAC,qBAAqB,CAC1B,SAAS,EAAE,QAAQ,EAAE,EACrB,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,GACjC,QAAQ,EAAE;WAiCA,iBAAiB,CAC5B,SAAS,EAAE,QAAQ,CAAC,QAAQ,CAAC,EAC7B,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,GACjC,OAAO,CAAC,QAAQ,EAAE,CAAC;CAgCvB"}
@@ -0,0 +1,176 @@
1
+ // Ported from Filtering.dart
2
+ import Document from "../document";
3
+ import Relation from "./relation";
4
+ import { compareMap } from "./compares";
5
+ export class Filtering {
6
+ static matchedDocuments(documents, conditions) {
7
+ const documentsPassed = [];
8
+ for (const doc of documents) {
9
+ const results = [];
10
+ for (const condition of conditions) {
11
+ const field = condition["field"];
12
+ const type = condition["type"];
13
+ const value = doc.data[field];
14
+ const comparers = Object.entries(condition).filter(([key, val]) => key !== "field" && key !== "type" && val != null);
15
+ for (const [compKey, compValue] of comparers) {
16
+ const compareClass = compareMap[compKey];
17
+ if (!compareClass)
18
+ continue;
19
+ const passed = compareClass(compValue).compare(value);
20
+ results.push({ passed, type });
21
+ }
22
+ }
23
+ const hasOrTrue = results.some((res) => res.type === "or" && res.passed === true);
24
+ const allAndTrue = results
25
+ .filter((res) => res.type === "and")
26
+ .every((res) => res.passed === true);
27
+ if (hasOrTrue || allAndTrue)
28
+ documentsPassed.push(doc);
29
+ }
30
+ return documentsPassed;
31
+ }
32
+ static sortedDocuments(documents, orderBys) {
33
+ const result = [...documents];
34
+ result.sort((a, b) => {
35
+ let cmp = 0;
36
+ for (const [key, order] of Object.entries(orderBys)) {
37
+ const av = a.data[key];
38
+ const bv = b.data[key];
39
+ if (av == null && bv != null) {
40
+ cmp = 1;
41
+ }
42
+ else if (av != null && bv == null) {
43
+ cmp = -1;
44
+ }
45
+ else if (av == null && bv == null) {
46
+ cmp = 0;
47
+ }
48
+ else if ((typeof av === "number" || typeof av === "string") &&
49
+ (typeof bv === "number" || typeof bv === "string")) {
50
+ cmp =
51
+ av > bv ? 1 : av < bv ? -1 : 0;
52
+ }
53
+ else if (av instanceof Date && bv instanceof Date) {
54
+ cmp =
55
+ av.getTime() > bv.getTime()
56
+ ? 1
57
+ : av.getTime() < bv.getTime()
58
+ ? -1
59
+ : 0;
60
+ }
61
+ else if (av != null &&
62
+ typeof av.compareTo === "function" &&
63
+ bv != null &&
64
+ typeof bv.compareTo === "function") {
65
+ cmp = av.compareTo(bv) > 0 ? 1 : av.compareTo(bv) < 0 ? -1 : 0;
66
+ }
67
+ else {
68
+ cmp = 0;
69
+ }
70
+ if (order === "desc")
71
+ cmp = -cmp;
72
+ if (cmp !== 0)
73
+ break;
74
+ }
75
+ return cmp;
76
+ });
77
+ return result;
78
+ }
79
+ static skipAndTakeDocuments(documents, skip, take) {
80
+ let result = [...documents];
81
+ if (skip != null)
82
+ result = documents.slice(skip);
83
+ if (take != null)
84
+ result = result.slice(0, take);
85
+ return result;
86
+ }
87
+ static pickAndOmitDocuments(documents, pick, omit) {
88
+ if (pick.size > 0 && omit.size > 0) {
89
+ throw new Error("Pick and Omit cannot be used together");
90
+ }
91
+ if (pick.size > 0) {
92
+ return Array.from(documents).map((doc) => {
93
+ const filteredData = {};
94
+ for (const k of pick) {
95
+ if (doc.data.hasOwnProperty(k)) {
96
+ filteredData[k] = doc.data[k];
97
+ }
98
+ }
99
+ // Assume Document.fromJson equivalent is Document.fromJson in JS/TS
100
+ return Document.fromJson(filteredData);
101
+ });
102
+ }
103
+ if (omit.size > 0) {
104
+ return Array.from(documents).map((doc) => {
105
+ const filteredData = {};
106
+ for (const [k, v] of Object.entries(doc.data)) {
107
+ if (!omit.has(k)) {
108
+ filteredData[k] = v;
109
+ }
110
+ }
111
+ return Document.fromJson(filteredData);
112
+ });
113
+ }
114
+ return documents;
115
+ }
116
+ static includedDocumentsSync(documents, includes) {
117
+ for (const document of documents) {
118
+ for (const relation of Object.values(includes)) {
119
+ const name = relation.name;
120
+ const localKey = relation.localKey;
121
+ const foreignCollection = relation.foreignCollection;
122
+ const foreignKey = relation.foreignKey;
123
+ const relationType = relation.type;
124
+ const collection = foreignCollection;
125
+ const localKeyValue = document.data[localKey];
126
+ if (localKeyValue == null)
127
+ continue;
128
+ if (relationType === "oneToOne") {
129
+ // getFirstSync must be implemented on collection
130
+ const includedRecord = collection
131
+ .where(foreignKey, { eq: localKeyValue })
132
+ .getFirstSync();
133
+ document.data[name] = includedRecord;
134
+ }
135
+ else if (relationType === "oneToMany" ||
136
+ relationType === "manyToMany") {
137
+ const includedDocuments = collection
138
+ .where(foreignKey, { eq: localKeyValue })
139
+ .getSync();
140
+ document.data[name] = includedDocuments;
141
+ }
142
+ }
143
+ }
144
+ return documents;
145
+ }
146
+ static async includedDocuments(documents, includes) {
147
+ const docs = Array.isArray(documents) ? documents : Array.from(documents);
148
+ for (const document of docs) {
149
+ for (const relation of Object.values(includes)) {
150
+ const name = relation.name;
151
+ const localKey = relation.localKey;
152
+ const foreignCollection = relation.foreignCollection;
153
+ const foreignKey = relation.foreignKey;
154
+ const relationType = relation.type;
155
+ const collection = foreignCollection;
156
+ const localKeyValue = document.data[localKey];
157
+ if (localKeyValue == null)
158
+ continue;
159
+ if (relationType === "oneToOne") {
160
+ const includedRecord = await collection
161
+ .where(foreignKey, { eq: localKeyValue })
162
+ .getFirst();
163
+ document.data[name] = includedRecord;
164
+ }
165
+ else if (relationType === "oneToMany" ||
166
+ relationType === "manyToMany") {
167
+ const includedDocuments = await collection
168
+ .where(foreignKey, { eq: localKeyValue })
169
+ .get();
170
+ document.data[name] = includedDocuments;
171
+ }
172
+ }
173
+ }
174
+ return docs;
175
+ }
176
+ }
@@ -0,0 +1,145 @@
1
+ import Collection from "../collection";
2
+ import Document from "../document";
3
+ import CollectionOperations from "../driver/collection_operations";
4
+ import QueryExecutor from "./query_executor";
5
+ type BetweenArg = [any, any];
6
+ export default class QueryBuilder {
7
+ private readonly _executor;
8
+ constructor({ debug, safe, collection, collectionOperations, }: {
9
+ debug?: boolean;
10
+ safe?: boolean;
11
+ collection: Collection;
12
+ collectionOperations: CollectionOperations;
13
+ });
14
+ get executor(): QueryExecutor;
15
+ where(field: string, opts?: {
16
+ eq?: any;
17
+ neq?: any;
18
+ gt?: any;
19
+ gte?: any;
20
+ lt?: any;
21
+ lte?: any;
22
+ contains?: any;
23
+ doesNotContain?: any;
24
+ startsWith?: string;
25
+ endsWith?: string;
26
+ regexMatch?: string;
27
+ between?: BetweenArg;
28
+ betweenStartInclusive?: BetweenArg;
29
+ betweenEndInclusive?: BetweenArg;
30
+ betweenBothInclusive?: BetweenArg;
31
+ isNull?: boolean;
32
+ isNotNull?: boolean;
33
+ isEmpty?: boolean;
34
+ isNotEmpty?: boolean;
35
+ filter?: (value: any) => boolean;
36
+ }): this;
37
+ and(field: string, opts?: {
38
+ eq?: any;
39
+ neq?: any;
40
+ gt?: any;
41
+ gte?: any;
42
+ lt?: any;
43
+ lte?: any;
44
+ contains?: any;
45
+ doesNotContain?: any;
46
+ startsWith?: string;
47
+ endsWith?: string;
48
+ regexMatch?: string;
49
+ between?: BetweenArg;
50
+ betweenStartInclusive?: BetweenArg;
51
+ betweenEndInclusive?: BetweenArg;
52
+ betweenBothInclusive?: BetweenArg;
53
+ isNull?: boolean;
54
+ isNotNull?: boolean;
55
+ isEmpty?: boolean;
56
+ isNotEmpty?: boolean;
57
+ filter?: (value: any) => boolean;
58
+ }): this;
59
+ or(field: string, opts?: {
60
+ eq?: any;
61
+ neq?: any;
62
+ gt?: any;
63
+ gte?: any;
64
+ lt?: any;
65
+ lte?: any;
66
+ contains?: any;
67
+ doesNotContain?: any;
68
+ startsWith?: string;
69
+ endsWith?: string;
70
+ regexMatch?: string;
71
+ between?: BetweenArg;
72
+ betweenStartInclusive?: BetweenArg;
73
+ betweenEndInclusive?: BetweenArg;
74
+ betweenBothInclusive?: BetweenArg;
75
+ isNull?: boolean;
76
+ isNotNull?: boolean;
77
+ isEmpty?: boolean;
78
+ isNotEmpty?: boolean;
79
+ filter?: (value: any) => boolean;
80
+ }): this;
81
+ pick(field: string): this;
82
+ pickMany(fields: string[]): this;
83
+ omit(field: string): this;
84
+ omitMany(fields: string[]): this;
85
+ skip(skip: number): this;
86
+ limit(limit: number): this;
87
+ take(take: number): this;
88
+ skipAndTake({ skip, take }?: {
89
+ skip?: number;
90
+ take?: number;
91
+ }): this;
92
+ orderBy(field: string, { desc }?: {
93
+ desc?: boolean;
94
+ }): this;
95
+ include(name: string): this;
96
+ includeMany(names: string[]): this;
97
+ get(): Promise<Document[]>;
98
+ getSync(): Document[];
99
+ getRandom(): Promise<Document | undefined>;
100
+ getRandomSync(): Document | undefined;
101
+ getShuffled(): Promise<Document[]>;
102
+ getShuffledSync(): Document[];
103
+ getRandomMany(count?: number): Promise<Document[]>;
104
+ getRandomManySync(count?: number): Document[];
105
+ getFirst(): Promise<Document | undefined>;
106
+ getFirstSync(): Document | undefined;
107
+ getLast(): Promise<Document | undefined>;
108
+ getLastSync(): Document | undefined;
109
+ getAt(index: number): Promise<Document | undefined>;
110
+ getAtSync(index: number): Document | undefined;
111
+ count(): Promise<number>;
112
+ countSync(): number;
113
+ update(data: Record<string, any>): Promise<Document | undefined>;
114
+ updateSync(data: Record<string, any>): Document | undefined;
115
+ updateFrom(update: (data: Record<string, any>) => Record<string, any>): Promise<Document | undefined>;
116
+ updateFromSync(update: (data: Record<string, any>) => Record<string, any>): Document | undefined;
117
+ updateMany(data: Record<string, any>, { count }?: {
118
+ count?: number;
119
+ }): Promise<Document[]>;
120
+ updateManySync(data: Record<string, any>, { count }?: {
121
+ count?: number;
122
+ }): Document[];
123
+ updateManyFrom(update: (data: Record<string, any>) => Record<string, any>, { count }?: {
124
+ count?: number;
125
+ }): Promise<Document[]>;
126
+ updateManyFromSync(update: (data: Record<string, any>) => Record<string, any>, { count }?: {
127
+ count?: number;
128
+ }): Document[];
129
+ delete(): Promise<Document | undefined>;
130
+ deleteSync(): Document | undefined;
131
+ deleteMany(): Promise<Document[]>;
132
+ deleteManySync(): Document[];
133
+ upsert({ update, insert, }: {
134
+ update: Record<string, any>;
135
+ insert: Record<string, any>;
136
+ }): Promise<Document | undefined>;
137
+ upsertSync({ update, insert, }: {
138
+ update: Record<string, any>;
139
+ insert: Record<string, any>;
140
+ }): Document | undefined;
141
+ addIfAbsent(data: Record<string, any>): Promise<Document | undefined>;
142
+ addIfAbsentSync(data: Record<string, any>): Document | undefined;
143
+ }
144
+ export {};
145
+ //# sourceMappingURL=query_builder.d.ts.map