@omegup/msync 0.1.67 → 0.1.68

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.
package/index.js CHANGED
@@ -68,6 +68,45 @@ const map1 = (k, to) => {
68
68
  return { [k]: [k2, to] };
69
69
  };
70
70
  //#endregion
71
+ //#region .build/lib/field/field.js
72
+ var Field = class Field {
73
+ field;
74
+ raw;
75
+ concat;
76
+ has(p) {
77
+ return { raw: (f) => ({ [f.with(this).str()]: p.raw }) };
78
+ }
79
+ constructor(field, raw, concat) {
80
+ this.field = field;
81
+ this.raw = raw;
82
+ this.concat = concat;
83
+ }
84
+ static root = () => {
85
+ const concat = (f1, f2) => new Field([f1.field, f2.field].filter(id$1).join("."), f1.raw, f1.concat);
86
+ return new Field("", (s) => s ? `$${s}` : "$$ROOT", concat);
87
+ };
88
+ static ctx = () => (k) => {
89
+ const concat = (_, f2) => new Field(f2.field, f2.raw, f2.concat);
90
+ return new Field(k, (s) => `$$${s}`, concat);
91
+ };
92
+ of(k) {
93
+ return new Field([this.field, k].filter(id$1).join("."), this.raw, this.concat);
94
+ }
95
+ with(k) {
96
+ return k.concat(this, k);
97
+ }
98
+ str() {
99
+ return this.field;
100
+ }
101
+ exprRaw() {
102
+ return asExprRaw(this.raw(this.field));
103
+ }
104
+ expr() {
105
+ return asExpr({ raw: (f) => f.with(this).exprRaw() });
106
+ }
107
+ };
108
+ const { root, ctx } = Field;
109
+ //#endregion
71
110
  //#region .build/lib/utils/map-object.js
72
111
  const mapExactToObject0 = (x, f) => {
73
112
  const filter = (x) => x.filter((x) => x[0] === x[1][0]);
@@ -88,9 +127,115 @@ const spread0 = (a, b) => ({
88
127
  ...mapExact0(b, id$1)
89
128
  });
90
129
  //#endregion
130
+ //#region .build/lib/expression/concat.js
131
+ const concat = (...expr) => asExpr({ raw: (f) => asExprRaw({ $concat: expr.map((e) => e.raw(f).get()) }) });
132
+ const $substr = (expr, start, length) => asExpr({ raw: (f) => asExprRaw({ $substrCP: [
133
+ expr.raw(f).get(),
134
+ start.raw(f).get(),
135
+ length.raw(f).get()
136
+ ] }) });
137
+ const padLeft = (expr, pad) => asExpr({ raw: (f) => asExprRaw({ $let: {
138
+ vars: { x: { $concat: [{ $literal: pad }, expr.raw(f).get()] } },
139
+ in: { $substrCP: [
140
+ "$$x",
141
+ { $subtract: [{ $strLenCP: "$$x" }, pad.length] },
142
+ pad.length
143
+ ] }
144
+ } }) });
145
+ const regex = (expr, regex, options) => asExpr({ raw: (f) => asExprRaw({ $regexMatch: {
146
+ input: expr.raw(f).get(),
147
+ regex: regex.raw(f).get(),
148
+ options: options?.raw(f).get()
149
+ } }) });
150
+ const str = (expr) => asExpr({ raw: (f) => asExprRaw({ $toString: expr.raw(f).get() }) });
151
+ const toInt = (expr) => asExpr({ raw: (f) => asExprRaw({ $toInt: expr.raw(f).get() }) });
152
+ const fieldM = (expr, m) => asExpr({ raw: (f) => asExprRaw(Object.fromEntries(Object.entries(m).flatMap(([dom, ref]) => expr[ref] ? [[dom, expr[ref].raw(f).get()]] : []))) });
153
+ const mergeExact = (...[exprsExact1, exprsExact2]) => spread(exprsExact1, exprsExact2);
154
+ const mergeExact0 = (...[exprsExact1, exprsExact2]) => spread0(exprsExact1, exprsExact2);
155
+ const mergeExpr = (...exprs) => mergeExact(...exprs);
156
+ const fieldF = () => (exprs) => Object.keys(exprs).length ? asExpr({ raw: (f) => asExprRaw(mapExactToObject(exprs, (e) => e.raw(f).get())) }) : asExpr(val(safeNarrow()({})));
157
+ const field = fieldF();
158
+ //#endregion
159
+ //#region .build/lib/expression/array.js
160
+ const size = (expr) => asExpr({ raw: (f) => asExprRaw({ $size: expr.raw(f).get() }) });
161
+ const filterDefined = (expr) => filter({
162
+ expr,
163
+ as: "x",
164
+ cond: ctx()("x").expr()
165
+ });
166
+ const filter = ({ as, cond, expr, limit }) => asExpr({ raw: (f) => asExprRaw({ $filter: {
167
+ input: expr.raw(f).get(),
168
+ as,
169
+ cond: cond.raw(f).get(),
170
+ ...limit ? { limit: limit.raw(f).get() } : {}
171
+ } }) });
172
+ const sortArray = ({ sortBy, expr, order }) => asExpr({ raw: (f) => asExprRaw({ $sortArray: {
173
+ input: expr.raw(f).get(),
174
+ sortBy: { [sortBy]: order ?? -1 }
175
+ } }) });
176
+ const isArray = (expr) => asBoolExpr({ raw: (f) => asExprRaw({ $isArray: expr.raw(f).get() }) });
177
+ const inArray = (item, expr) => asExpr({ raw: (f) => asExprRaw({ $in: [item.raw(f).get(), expr.raw(f).get()] }) });
178
+ const array = (...exprs) => asExpr({ raw: (f) => asExprRaw(exprs.map((x) => x.raw(f).get())) });
179
+ const concatArray = (...exprs) => asExpr({ raw: (f) => asExprRaw({ $concatArrays: exprs.map((x) => x.raw(f).get()) }) });
180
+ const $setUnion = (...exprs) => asExpr({ raw: (f) => asExprRaw({ $setUnion: exprs.map((x) => x.raw(f).get()) }) });
181
+ const first = (expr) => asExpr({ raw: (f) => asExprRaw({ $first: expr.raw(f).get() }) });
182
+ const firstSure = first;
183
+ const last = (expr) => asExpr({ raw: (f) => asExprRaw({ $last: expr.raw(f).get() }) });
184
+ const mergeObjects = (...exprs) => asExpr({ raw: (f) => asExprRaw({ $mergeObjects: exprs.map((x) => x.raw(f).get()) }) });
185
+ const $objectToArray = (expr) => asExpr({ raw: (f) => asExprRaw({ $objectToArray: expr.raw(f).get() }) });
186
+ const anyElementTrue = (expr) => asExpr({ raw: (f) => asExprRaw({ $anyElementTrue: expr.raw(f).get() }) });
187
+ const $reduce = (input, initialValue, inExpr) => asExpr({ raw: (f) => asExprRaw({ $reduce: {
188
+ input: input.raw(f).get(),
189
+ initialValue: initialValue.raw(f).get(),
190
+ in: inExpr.raw(f).get()
191
+ } }) });
192
+ const indexOfArray = (array, item) => asExpr({ raw: (f) => asExprRaw({ $indexOfArray: [array.raw(f).get(), item.raw(f).get()] }) });
193
+ const slice = (array, start, end) => asExpr({ raw: (f) => asExprRaw({ $slice: [
194
+ array.raw(f).get(),
195
+ start.raw(f).get(),
196
+ end.raw(f).get()
197
+ ] }) });
198
+ const except = (a, b) => {
199
+ const value = ctx()("value");
200
+ const out = value.of("out").expr();
201
+ const except = value.of("except").expr();
202
+ const curr = ctx()("this").expr();
203
+ const indexInExcept = ctx()("indexInExcept").expr();
204
+ return $let({ res: ["res", $reduce(a, field({
205
+ out: ["out", array()],
206
+ except: ["except", b]
207
+ }), $let({ indexInExcept: ["indexInExcept", indexOfArray(except, curr)] }, ite(gte(indexInExcept, val(0)), field({
208
+ out: ["out", out],
209
+ except: ["except", concatArray(ite(eq(indexInExcept)(val(0)), array(), slice(except, val(0), indexInExcept)), slice(except, add(indexInExcept, val(1)), size(except)))]
210
+ }), field({
211
+ out: ["out", concatArray(out, array(curr))],
212
+ except: ["except", except]
213
+ }))))] }, ctx()("res").of("out").expr());
214
+ };
215
+ //#endregion
216
+ //#region .build/lib/expression/range.js
217
+ const range = (start, end, step) => asExpr({ raw: (f) => asExprRaw({ $range: [
218
+ start.raw(f).get(),
219
+ end.raw(f).get(),
220
+ step?.raw(f).get() ?? 1
221
+ ] }) });
222
+ const $map0 = ({ as, expr, input }) => asExpr({ raw: (f) => asExprRaw({ $map: {
223
+ input: input.raw(f).get(),
224
+ as,
225
+ in: expr.raw(f).get()
226
+ } }) });
227
+ const $map1 = (ex, map) => $map0({
228
+ input: ex,
229
+ as: "item",
230
+ expr: map(ctx()("item"))
231
+ });
232
+ const $map = (ex, map) => $map1(ex, (i) => map(i.expr()));
233
+ //#endregion
91
234
  //#region .build/lib/expression/val.js
92
235
  const val = (val) => asExpr({ raw: () => asExprRaw(val && typeof val === "object" && !(val instanceof Date) || typeof val === "string" && val[0] === "$" ? { $literal: val } : val) });
93
236
  const current = asExpr({ raw: () => asExprRaw("$$CLUSTER_TIME") });
237
+ const $rand = asExpr({ raw: () => asExprRaw({ $rand: {} }) });
238
+ const rand = $reduce(range(val(0), val(17)), val(""), concat(ctx()("value").expr(), $substr(val("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"), floor(multiply($rand, val(62))), val(1))));
94
239
  const $let = (vars, inExpr) => asExpr({ raw: (f) => asExprRaw({ $let: {
95
240
  vars: mapExactToObject(vars, (v) => v.raw(f).get()),
96
241
  in: inExpr.raw(f).get()
@@ -105,11 +250,6 @@ const func = (f, ...args) => asExpr({ raw: (field) => asExprRaw({ $function: {
105
250
  args: args.map((x) => x.raw(field).get()),
106
251
  lang: "js"
107
252
  } }) });
108
- const rand = function() {
109
- const s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
110
- return Array(17).fill(0).map(() => s[Math.floor(Math.random() * 62)]).join("");
111
- };
112
- const $rand = func(rand);
113
253
  //#endregion
114
254
  //#region .build/lib/expression/logic.js
115
255
  const ite = ((cond, then, orelse) => asExpr({ raw: (f) => asExprRaw({ $cond: {
@@ -140,45 +280,6 @@ const setField = ({ field, input, value }) => asExpr({ raw: (f) => asExprRaw({ $
140
280
  value: value.raw(f).get()
141
281
  } }) });
142
282
  //#endregion
143
- //#region .build/lib/field/field.js
144
- var Field = class Field {
145
- field;
146
- raw;
147
- concat;
148
- has(p) {
149
- return { raw: (f) => ({ [f.with(this).str()]: p.raw }) };
150
- }
151
- constructor(field, raw, concat) {
152
- this.field = field;
153
- this.raw = raw;
154
- this.concat = concat;
155
- }
156
- static root = () => {
157
- const concat = (f1, f2) => new Field([f1.field, f2.field].filter(id$1).join("."), f1.raw, f1.concat);
158
- return new Field("", (s) => s ? `$${s}` : "$$ROOT", concat);
159
- };
160
- static ctx = () => (k) => {
161
- const concat = (_, f2) => new Field(f2.field, f2.raw, f2.concat);
162
- return new Field(k, (s) => `$$${s}`, concat);
163
- };
164
- of(k) {
165
- return new Field([this.field, k].filter(id$1).join("."), this.raw, this.concat);
166
- }
167
- with(k) {
168
- return k.concat(this, k);
169
- }
170
- str() {
171
- return this.field;
172
- }
173
- exprRaw() {
174
- return asExprRaw(this.raw(this.field));
175
- }
176
- expr() {
177
- return asExpr({ raw: (f) => f.with(this).exprRaw() });
178
- }
179
- };
180
- const { root, ctx } = Field;
181
- //#endregion
182
283
  //#region .build/lib/accumulators/index.js
183
284
  const asAccumulator = (x) => x;
184
285
  const $sum_ = (expr) => ({ raw: (f) => asAccumulator({ $sum: expr.raw(f).get() }) });
@@ -320,90 +421,6 @@ const concatTStages = ({ coll, exec, input }, stages) => ({
320
421
  exec: concatStages(exec, stages)
321
422
  });
322
423
  //#endregion
323
- //#region .build/lib/expression/concat.js
324
- const concat = (...expr) => asExpr({ raw: (f) => asExprRaw({ $concat: expr.map((e) => e.raw(f).get()) }) });
325
- const $substr = (expr, start, length) => asExpr({ raw: (f) => asExprRaw({ $substrCP: [
326
- expr.raw(f).get(),
327
- start.raw(f).get(),
328
- length.raw(f).get()
329
- ] }) });
330
- const padLeft = (expr, pad) => asExpr({ raw: (f) => asExprRaw({ $let: {
331
- vars: { x: { $concat: [{ $literal: pad }, expr.raw(f).get()] } },
332
- in: { $substrCP: [
333
- "$$x",
334
- { $subtract: [{ $strLenCP: "$$x" }, pad.length] },
335
- pad.length
336
- ] }
337
- } }) });
338
- const regex = (expr, regex, options) => asExpr({ raw: (f) => asExprRaw({ $regexMatch: {
339
- input: expr.raw(f).get(),
340
- regex: regex.raw(f).get(),
341
- options: options?.raw(f).get()
342
- } }) });
343
- const str = (expr) => asExpr({ raw: (f) => asExprRaw({ $toString: expr.raw(f).get() }) });
344
- const toInt = (expr) => asExpr({ raw: (f) => asExprRaw({ $toInt: expr.raw(f).get() }) });
345
- const fieldM = (expr, m) => asExpr({ raw: (f) => asExprRaw(Object.fromEntries(Object.entries(m).flatMap(([dom, ref]) => expr[ref] ? [[dom, expr[ref].raw(f).get()]] : []))) });
346
- const mergeExact = (...[exprsExact1, exprsExact2]) => spread(exprsExact1, exprsExact2);
347
- const mergeExact0 = (...[exprsExact1, exprsExact2]) => spread0(exprsExact1, exprsExact2);
348
- const mergeExpr = (...exprs) => mergeExact(...exprs);
349
- const fieldF = () => (exprs) => Object.keys(exprs).length ? asExpr({ raw: (f) => asExprRaw(mapExactToObject(exprs, (e) => e.raw(f).get())) }) : asExpr(val(safeNarrow()({})));
350
- const field = fieldF();
351
- //#endregion
352
- //#region .build/lib/expression/array.js
353
- const size = (expr) => asExpr({ raw: (f) => asExprRaw({ $size: expr.raw(f).get() }) });
354
- const filterDefined = (expr) => filter({
355
- expr,
356
- as: "x",
357
- cond: ctx()("x").expr()
358
- });
359
- const filter = ({ as, cond, expr, limit }) => asExpr({ raw: (f) => asExprRaw({ $filter: {
360
- input: expr.raw(f).get(),
361
- as,
362
- cond: cond.raw(f).get(),
363
- ...limit ? { limit: limit.raw(f).get() } : {}
364
- } }) });
365
- const sortArray = ({ sortBy, expr, order }) => asExpr({ raw: (f) => asExprRaw({ $sortArray: {
366
- input: expr.raw(f).get(),
367
- sortBy: { [sortBy]: order ?? -1 }
368
- } }) });
369
- const isArray = (expr) => asBoolExpr({ raw: (f) => asExprRaw({ $isArray: expr.raw(f).get() }) });
370
- const inArray = (item, expr) => asExpr({ raw: (f) => asExprRaw({ $in: [item.raw(f).get(), expr.raw(f).get()] }) });
371
- const array = (...exprs) => asExpr({ raw: (f) => asExprRaw(exprs.map((x) => x.raw(f).get())) });
372
- const concatArray = (...exprs) => asExpr({ raw: (f) => asExprRaw({ $concatArrays: exprs.map((x) => x.raw(f).get()) }) });
373
- const first = (expr) => asExpr({ raw: (f) => asExprRaw({ $first: expr.raw(f).get() }) });
374
- const firstSure = first;
375
- const last = (expr) => asExpr({ raw: (f) => asExprRaw({ $last: expr.raw(f).get() }) });
376
- const mergeObjects = (...exprs) => asExpr({ raw: (f) => asExprRaw({ $mergeObjects: exprs.map((x) => x.raw(f).get()) }) });
377
- const anyElementTrue = (expr) => asExpr({ raw: (f) => asExprRaw({ $anyElementTrue: expr.raw(f).get() }) });
378
- const $reduce = (input, initialValue, inExpr) => asExpr({ raw: (f) => asExprRaw({ $reduce: {
379
- input: input.raw(f).get(),
380
- initialValue: initialValue.raw(f).get(),
381
- in: inExpr.raw(f).get()
382
- } }) });
383
- const indexOfArray = (array, item) => asExpr({ raw: (f) => asExprRaw({ $indexOfArray: [array.raw(f).get(), item.raw(f).get()] }) });
384
- const slice = (array, start, end) => asExpr({ raw: (f) => asExprRaw({ $slice: [
385
- array.raw(f).get(),
386
- start.raw(f).get(),
387
- end.raw(f).get()
388
- ] }) });
389
- const except = (a, b) => {
390
- const value = ctx()("value");
391
- const out = value.of("out").expr();
392
- const except = value.of("except").expr();
393
- const curr = ctx()("this").expr();
394
- const indexInExcept = ctx()("indexInExcept").expr();
395
- return $let({ res: ["res", $reduce(a, field({
396
- out: ["out", array()],
397
- except: ["except", b]
398
- }), $let({ indexInExcept: ["indexInExcept", indexOfArray(except, curr)] }, ite(gte(indexInExcept, val(0)), field({
399
- out: ["out", out],
400
- except: ["except", concatArray(ite(eq(indexInExcept)(val(0)), array(), slice(except, val(0), indexInExcept)), slice(except, add(indexInExcept, val(1)), size(except)))]
401
- }), field({
402
- out: ["out", concatArray(out, array(curr))],
403
- except: ["except", except]
404
- }))))] }, ctx()("res").of("out").expr());
405
- };
406
- //#endregion
407
424
  //#region .build/lib/aggregate/raws.js
408
425
  const $match1 = (query) => (f) => asStages(query ? [{ $match: query.raw(f()) }] : []);
409
426
  const $set1 = (updater) => (f) => asStages([{ $set: Object.fromEntries(updater.raw(f()).map(([k, v]) => [f().of(k.slice(1)).str(), v])) }]);
@@ -596,24 +613,6 @@ const mapToPlainDateTime = (date, timezone) => asExpr({ raw: (f) => asExprRaw({
596
613
  timezone: timezone.raw(f).get()
597
614
  } } }) });
598
615
  //#endregion
599
- //#region .build/lib/expression/range.js
600
- const range = (start, end, step) => asExpr({ raw: (f) => asExprRaw({ $range: [
601
- start.raw(f).get(),
602
- end.raw(f).get(),
603
- step?.raw(f).get() ?? 1
604
- ] }) });
605
- const $map0 = ({ as, expr, input }) => asExpr({ raw: (f) => asExprRaw({ $map: {
606
- input: input.raw(f).get(),
607
- as,
608
- in: expr.raw(f).get()
609
- } }) });
610
- const $map1 = (ex, map) => $map0({
611
- input: ex,
612
- as: "item",
613
- expr: map(ctx()("item"))
614
- });
615
- const $map = (ex, map) => $map1(ex, (i) => map(i.expr()));
616
- //#endregion
617
616
  //#region .build/lib/update/updater.js
618
617
  const subUpdater = (a, f) => ({ raw: (g) => a.raw(g.with(f)) });
619
618
  const rawSet = (fields) => (f) => {
@@ -735,7 +734,7 @@ const subMerge = (args, out, gid, extra, idPrefix, first) => {
735
734
  const gidPath = root().of(gid).expr();
736
735
  const mapId = (k, v) => map1(k, v);
737
736
  const F1 = {
738
- _id: ["_id", to(idPrefix ? concat(val(idPrefix), $rand) : $rand)],
737
+ _id: ["_id", to(idPrefix ? concat(val(idPrefix), rand) : rand)],
739
738
  touchedAt: ["touchedAt", to(current)]
740
739
  };
741
740
  const F2 = mapId(gid, to(gidPath));
@@ -2183,6 +2182,7 @@ exports.$merge_ = $merge_;
2183
2182
  exports.$ne = $ne;
2184
2183
  exports.$nin = $nin;
2185
2184
  exports.$nor = $nor;
2185
+ exports.$objectToArray = $objectToArray;
2186
2186
  exports.$or = $or;
2187
2187
  exports.$outerLookup = $outerLookup;
2188
2188
  exports.$pushDict = $pushDict;
@@ -2190,6 +2190,7 @@ exports.$rand = $rand;
2190
2190
  exports.$reduce = $reduce;
2191
2191
  exports.$replaceWith = $replaceWith;
2192
2192
  exports.$set = $set;
2193
+ exports.$setUnion = $setUnion;
2193
2194
  exports.$simpleInsert = $simpleInsert;
2194
2195
  exports.$simpleMerge = $simpleMerge;
2195
2196
  exports.$simpleMergePart = $simpleMergePart;