@omegup/msync 0.1.8 → 0.1.9
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.d.ts +33 -23
- package/index.esm.js +63 -75
- package/index.js +63 -74
- package/lib/aggregate/$merge.d.ts +17 -9
- package/lib/aggregate/mongo-stages.d.ts +1 -1
- package/lib/aggregate/out.d.ts +1 -1
- package/lib/expression/array.d.ts +1 -1
- package/lib/types/stream.d.ts +6 -4
- package/lib/utils/index.d.ts +1 -0
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -106,13 +106,15 @@ type Stream<out Q extends O, in out T extends Q, in out F extends HKT<[O, O]>, i
|
|
|
106
106
|
type TS = {
|
|
107
107
|
readonly touchedAt: Timestamp;
|
|
108
108
|
};
|
|
109
|
-
type
|
|
109
|
+
type IsDeleted = {
|
|
110
110
|
readonly deletedAt: Timestamp;
|
|
111
|
-
}
|
|
111
|
+
};
|
|
112
|
+
type DDel = IsDeleted & ID & TS;
|
|
112
113
|
type Del = O<DDel>;
|
|
113
|
-
type
|
|
114
|
+
type DeletedAt = {
|
|
114
115
|
readonly deletedAt?: Timestamp | null | undefined;
|
|
115
|
-
}
|
|
116
|
+
};
|
|
117
|
+
type D = O<DeletedAt & ID>;
|
|
116
118
|
type Model = D & TS;
|
|
117
119
|
type BA = 'before' | 'after';
|
|
118
120
|
type PreDelta<T, K extends BA = BA, E = unknown> = Rec<K, T> & E;
|
|
@@ -431,7 +433,7 @@ type MergeArgs<T extends O, Out extends O, Ctx, In extends O> = {
|
|
|
431
433
|
stages: true;
|
|
432
434
|
into: RWCollection<In, Out>;
|
|
433
435
|
whenMatched: RawStages<unknown, Out, In, {
|
|
434
|
-
new:
|
|
436
|
+
new: T;
|
|
435
437
|
}>;
|
|
436
438
|
} | {
|
|
437
439
|
stages: 'ctx';
|
|
@@ -503,11 +505,19 @@ type ND$1 = {
|
|
|
503
505
|
readonly deletedAt?: null;
|
|
504
506
|
};
|
|
505
507
|
type SafeE$1<E> = Omit<E, `$${string}` | keyof ID>;
|
|
506
|
-
declare const
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
508
|
+
declare const getWhenMatched: <Out extends Model, P extends Model, K extends keyof IsDeleted>(whenNotMatched: "discard" | "fail" | "insert") => RawStages<O, Out, Out | Replace<Out, P>, RORec<"new", Replace<P, RORec<K, Timestamp>>>>;
|
|
509
|
+
type MergeCollection<V extends O, Out extends Model> = {
|
|
510
|
+
coll: RWCollection<Out | Replace<Out, Patch<V>> | Replace<Patch<V>, IsDeleted>, Out>;
|
|
511
|
+
whenNotMatched: 'discard';
|
|
512
|
+
} | {
|
|
513
|
+
coll: RWCollection<Out | Replace<Out, Patch<V>>, Out>;
|
|
514
|
+
whenNotMatched: 'fail';
|
|
515
|
+
};
|
|
516
|
+
declare const $mergeId: <V extends O>() => <SourcePart extends doc, Out extends Model, E = unknown, EEE extends RORec<string, rawItem> = {}>(out: MergeCollection<V, Out>, keys: ExprsExact<TakeDoc<V, unknown>, SourcePart>, id: Expr<string, OutInputE<TakeDoc<V>, E, null>>, ext: Exact<Omit<SafeE$1<EEE>, keyof (ND$1 & TS)>, IdHKT>) => StreamRunnerParam<OutInputE<SourcePart, E>, "out">;
|
|
517
|
+
declare const $simpleMergePart: <V extends O>() => <Source extends doc, Out extends Model, EEE extends RORec<string, rawItem>>(out: MergeCollection<V, Out>, keys: ExprsExact<TakeDoc<V, unknown>, Source>, ext: Exact<Omit<SafeE$1<EEE>, keyof (ND$1 & TS)>, IdHKT>) => StreamRunnerParam<Source, "out">;
|
|
518
|
+
declare const $simpleMerge: <V extends O>() => <Source extends doc, Out extends Model>(out: RWCollection<Out | Replace<Out, Patch<V>> | Replace<Patch<V>, IsDeleted>, Out>, keys: ExprsExact<TakeDoc<V, unknown>, Source>, whenNotMatched?: "fail" | "discard") => StreamRunnerParam<Source, "out">;
|
|
519
|
+
declare const $mergePart: <V extends O>() => <Out extends Model, SourcePart extends doc, EEE extends RORec<string, rawItem>>(out: RWCollection<Out | Replace<Out, Patch<V>>, Out>, keys: ExprsExact<TakeDoc<V, unknown>, SourcePart>, ext: Exact<Omit<SafeE$1<EEE>, keyof (ND$1 & TS)>, IdHKT>) => StreamRunnerParam<Delta<SourcePart>, "out">;
|
|
520
|
+
declare const $merge: <V extends O>() => <Out extends Model, SourcePart extends doc>(out: RWCollection<Out | Replace<Out, Patch<V>>, Out>, keys: ExprsExact<TakeDoc<V, unknown>, SourcePart>) => StreamRunnerParam<Delta<SourcePart>, "out">;
|
|
511
521
|
|
|
512
522
|
type ND = {
|
|
513
523
|
readonly deletedAt?: null;
|
|
@@ -519,6 +529,17 @@ declare const $simpleInsert: <T extends doc>(out: RWCollection<Merge<T, {}>>) =>
|
|
|
519
529
|
declare const $insertPart: <T extends doc, EEE extends RORec<string, rawItem>>(out: RWCollection<Merge<T, EEE>>, ext: Exact<Omit<SafeE<EEE>, keyof (ND & TS)>, IdHKT>) => StreamRunnerParam<Delta<T>, "out">;
|
|
520
530
|
declare const $insert: <T extends doc>(out: RWCollection<Merge<T, {}>>) => StreamRunnerParam<Delta<T>, "out">;
|
|
521
531
|
|
|
532
|
+
declare const log: (...args: unknown[]) => void;
|
|
533
|
+
|
|
534
|
+
declare const createIndex: (collection: {
|
|
535
|
+
readonly createIndex: Collection["createIndex"];
|
|
536
|
+
}, indexSpec: IndexSpecification, options?: CreateIndexesOptions) => Promise<void>;
|
|
537
|
+
|
|
538
|
+
declare const noop: () => void;
|
|
539
|
+
declare const map1: <K extends string, Im>(k: AsLiteral<K>, to: Im) => { readonly [P in K]: [P, Im]; } & {
|
|
540
|
+
readonly [_: string]: [K, Im];
|
|
541
|
+
};
|
|
542
|
+
|
|
522
543
|
declare const $group_: <V extends O>() => <ID, T extends O, C = unknown>(id: Expr<ID, T, C>, args: Accumulators<T, V, C>) => RawStages<O, T, Rec<"_id", ID> & V, C, 1>;
|
|
523
544
|
|
|
524
545
|
type DeltaPipe<Q extends O, T extends Q, F extends HKT<O2>, G extends HKT<O3>> = {
|
|
@@ -608,7 +629,7 @@ declare const first: <T, D, C>(expr: Expr<Arr<T>, D, C>) => Expr<T | null, D, C>
|
|
|
608
629
|
declare const firstSure: <T, D, C>(expr: Expr<Arr<T>, D, C>) => Expr<T, D, C>;
|
|
609
630
|
declare const last: <T, D, C>(expr: Expr<Arr<T>, D, C>) => Expr<T | null, D, C>;
|
|
610
631
|
type NullToOBJ<N extends null> = N extends null ? O : N;
|
|
611
|
-
declare const mergeObjects: <T1, T2, D, C = unknown, N extends null = never>(
|
|
632
|
+
declare const mergeObjects: <T1, T2, D, C = unknown, N extends null = never>(...exprs: readonly [Expr<T1 | N, D, C>, ...[...Expr<T1 | T2 | N, D, C>[], Expr<T2, D, C>]]) => Expr<(T1 | NullToOBJ<N>) & T2, D, C>;
|
|
612
633
|
declare const anyElementTrue: <D, C = unknown>(expr: Expr<Arr<boolean>, D, C>) => Expr<boolean, D, C>;
|
|
613
634
|
type Reduce<T, V> = RORec<'value', V> & RORec<'this', T>;
|
|
614
635
|
declare const $reduce: <T, V, D, C>(input: Expr<Arr<T>, D, C>, initialValue: Expr<V, D, C>, inExpr: Expr<V, D, C & Reduce<T, V>>) => Expr<V, D, C>;
|
|
@@ -706,19 +727,8 @@ declare const $and: Combiner;
|
|
|
706
727
|
declare const $nor: Combiner;
|
|
707
728
|
declare const $or: Combiner;
|
|
708
729
|
|
|
709
|
-
declare const log: (...args: unknown[]) => void;
|
|
710
|
-
|
|
711
|
-
declare const createIndex: (collection: {
|
|
712
|
-
readonly createIndex: Collection["createIndex"];
|
|
713
|
-
}, indexSpec: IndexSpecification, options?: CreateIndexesOptions) => Promise<void>;
|
|
714
|
-
|
|
715
|
-
declare const noop: () => void;
|
|
716
|
-
declare const map1: <K extends string, Im>(k: AsLiteral<K>, to: Im) => { readonly [P in K]: [P, Im]; } & {
|
|
717
|
-
readonly [_: string]: [K, Im];
|
|
718
|
-
};
|
|
719
|
-
|
|
720
730
|
declare const enablePreAndPostImages: <T extends doc>(coll: Collection<T>) => Promise<Document>;
|
|
721
731
|
declare const prepare: (testName?: string) => Promise<MongoClient$1>;
|
|
722
732
|
declare const makeCol: <T extends ID>(docs: readonly OptionalUnlessRequiredId<T>[], database: Db, name?: string) => Promise<Collection<T>>;
|
|
723
733
|
|
|
724
|
-
export { $accumulator, $and, $countDict, $entries, $eq, $exists, $expr, $getField, $group, $groupId, $groupMerge, $group_, $gt, $gtTs, $gte, $gteTs, $ifNull, $in, $insert, $insertPart, $insertX, $keys, $let, $lookup, $lt, $lte, $map, $map0, $map1, $match, $matchDelta, $merge, $merge2, $mergeId, $mergePart, $merge_, $ne, $nin, $nor, $or, $outerLookup, $pushDict, $rand, $reduce, $replaceWith, $set, $simpleInsert, $simpleMerge, $simpleMergePart, $sum, $type, $unwind, $unwindDelta, type Accumulators, type Arr, type AsLiteral, type Delta, type DeltaAccumulator, type DeltaAccumulators, Expr, type ExprHKT, type Exprs, type ExprsExact, type ExprsExactHKT, type ExprsPart, Field, type ID, type Loose, Machine, type Merge, type MergeArgs, type MergeInto, type MergeMapOArgs, type Model, type MongoTypeNames, type N, type NoRaw, type NullToOBJ, type O, type OPick, type OPickD, type RONoRaw, type RORec, type RawStages, type Rec, type Replace, type SnapshotStreamExecutionResult, type StrKey, type Strict, type TS, Type, type WriteonlyCollection, add, and, anyElementTrue, array, ceil, comp, concat, concatArray, createIndex, ctx, current, dateAdd, dateDiff, dateLt, datePart, dayAndMonthPart, divide, type doc, enablePreAndPostImages, eq, eqTyped, except, exprMapVal, field, fieldF, fieldM, filter, filterDefined, first, firstSure, floor, from, func, gt, gte, inArray, isArray, ite, type jsonPrim, last, log, lt, lte, makeCol, map1, mapVal, max, maxDate, mergeExact, mergeExact0, mergeExpr, mergeObjects, minDate, monthPart, multiply, ne, nil, noop, not, type notArr, notNull, now, or, pair, prepare, rand, range, regex, root, set, setField, single, size, slice, sortArray, staging, startOf, str, sub, subtract, to, toInt, val, weekPart, wrap, year };
|
|
734
|
+
export { $accumulator, $and, $countDict, $entries, $eq, $exists, $expr, $getField, $group, $groupId, $groupMerge, $group_, $gt, $gtTs, $gte, $gteTs, $ifNull, $in, $insert, $insertPart, $insertX, $keys, $let, $lookup, $lt, $lte, $map, $map0, $map1, $match, $matchDelta, $merge, $merge2, $mergeId, $mergePart, $merge_, $ne, $nin, $nor, $or, $outerLookup, $pushDict, $rand, $reduce, $replaceWith, $set, $simpleInsert, $simpleMerge, $simpleMergePart, $sum, $type, $unwind, $unwindDelta, type Accumulators, type Arr, type AsLiteral, type Delta, type DeltaAccumulator, type DeltaAccumulators, type ExactKeys, Expr, type ExprHKT, type Exprs, type ExprsExact, type ExprsExactHKT, type ExprsPart, Field, type ID, type Loose, Machine, type Merge, type MergeArgs, type MergeInto, type MergeMapOArgs, type Model, type MongoTypeNames, type N, type NoRaw, type NullToOBJ, type O, type OPick, type OPickD, type Patch, type RONoRaw, type RORec, type RawStages, type Rec, type Replace, type SnapshotStreamExecutionResult, type StrKey, type Strict, type TS, Type, type WriteonlyCollection, add, and, anyElementTrue, array, ceil, comp, concat, concatArray, createIndex, ctx, current, dateAdd, dateDiff, dateLt, datePart, dayAndMonthPart, divide, type doc, enablePreAndPostImages, eq, eqTyped, except, exprMapVal, field, fieldF, fieldM, filter, filterDefined, first, firstSure, floor, from, func, getWhenMatched, gt, gte, inArray, isArray, ite, type jsonPrim, last, log, lt, lte, makeCol, map1, mapVal, max, maxDate, mergeExact, mergeExact0, mergeExpr, mergeObjects, minDate, monthPart, multiply, ne, nil, noop, not, type notArr, notNull, now, or, pair, prepare, rand, range, regex, root, set, setField, single, size, slice, sortArray, staging, startOf, str, sub, subtract, to, toInt, val, weekPart, wrap, year };
|
package/index.esm.js
CHANGED
|
@@ -1365,84 +1365,88 @@ const $replaceWith = (expr) => ({
|
|
|
1365
1365
|
lin: $replaceWith_(expr),
|
|
1366
1366
|
});
|
|
1367
1367
|
|
|
1368
|
-
const
|
|
1368
|
+
const getWhenMatched = (whenNotMatched) => {
|
|
1369
|
+
const setDeleted = whenNotMatched === 'discard';
|
|
1370
|
+
const orNull = (e) => (setDeleted ? $ifNull(e, nil) : e);
|
|
1371
|
+
const preMergeOld = setDeleted
|
|
1372
|
+
? mergeObjects(root().of('old').expr(), field({
|
|
1373
|
+
deletedAt: ['deletedAt', orNull(root().of('old').of('deletedAt').expr())],
|
|
1374
|
+
touchedAt: ['touchedAt', root().of('merged').of('touchedAt').expr()],
|
|
1375
|
+
}))
|
|
1376
|
+
: root().of('old').expr();
|
|
1377
|
+
const same = eq(preMergeOld)(root().of('merged').expr());
|
|
1378
|
+
const newOrOld = whenNotMatched ===
|
|
1379
|
+
'insert'
|
|
1380
|
+
? ctx()('new')
|
|
1381
|
+
: root();
|
|
1382
|
+
const merged = mergeObjects(root().expr(), ctx()('new').expr(), field({
|
|
1383
|
+
deletedAt: ['deletedAt', orNull(newOrOld.of('deletedAt').expr())],
|
|
1384
|
+
}));
|
|
1385
|
+
return link()
|
|
1386
|
+
.with($replaceWith_(field({
|
|
1387
|
+
old: ['old', root().expr()],
|
|
1388
|
+
merged: ['merged', merged],
|
|
1389
|
+
})))
|
|
1390
|
+
.with($replaceWith_(ite(same, root().of('old').expr(), root().of('merged').expr()))).stages;
|
|
1391
|
+
};
|
|
1392
|
+
const $mergeX = (out, keys, f, map, ext) => {
|
|
1369
1393
|
const patch = mapExact(keys, v => sub(v, f));
|
|
1370
1394
|
const filter = mapExactToObject(ext, v => ({ $eq: v }));
|
|
1371
|
-
const setDeleted = whenNotMatched === 'discard';
|
|
1395
|
+
const setDeleted = out.whenNotMatched === 'discard';
|
|
1372
1396
|
const replacer = map(field(omitPick().backward(spread(patch, {
|
|
1373
1397
|
_id: ['_id', f.of('_id').expr()],
|
|
1374
1398
|
touchedAt: ['touchedAt', current],
|
|
1375
|
-
deletedAt: [setDeleted ? 'deletedAt' : 'touchedAt', current],
|
|
1376
1399
|
}))));
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
.with($
|
|
1400
|
+
const sss = setDeleted
|
|
1401
|
+
? link()
|
|
1402
|
+
.with($set_(set()({
|
|
1403
|
+
deletedAt: ['deletedAt', to(current)],
|
|
1404
|
+
})))
|
|
1380
1405
|
.with($merge_({
|
|
1381
|
-
into: out,
|
|
1406
|
+
into: out.coll,
|
|
1407
|
+
on: root().of('_id'),
|
|
1408
|
+
whenNotMatched: 'insert',
|
|
1409
|
+
stages: true,
|
|
1410
|
+
whenMatched: getWhenMatched(out.whenNotMatched),
|
|
1411
|
+
})).stages
|
|
1412
|
+
: link().with($merge_({
|
|
1413
|
+
into: out.coll,
|
|
1382
1414
|
on: root().of('_id'),
|
|
1383
|
-
whenNotMatched:
|
|
1415
|
+
whenNotMatched: 'fail',
|
|
1384
1416
|
stages: true,
|
|
1385
|
-
whenMatched:
|
|
1417
|
+
whenMatched: getWhenMatched(out.whenNotMatched),
|
|
1418
|
+
})).stages;
|
|
1419
|
+
const teardown = (coll) => ({
|
|
1420
|
+
collection: coll,
|
|
1421
|
+
method: 'updateMany',
|
|
1422
|
+
params: [
|
|
1423
|
+
filter,
|
|
1424
|
+
[
|
|
1386
1425
|
{
|
|
1387
|
-
$
|
|
1388
|
-
old: '$$ROOT',
|
|
1389
|
-
merged: {
|
|
1390
|
-
$mergeObjects: [
|
|
1391
|
-
'$$ROOT',
|
|
1392
|
-
'$$new',
|
|
1393
|
-
...(setDeleted ? [{ deletedAt: { $ifNull: ['$deletedAt', null] } }] : []),
|
|
1394
|
-
],
|
|
1395
|
-
},
|
|
1396
|
-
},
|
|
1426
|
+
$unset: Object.keys(mapExactToObject(keys, () => 1)),
|
|
1397
1427
|
},
|
|
1398
|
-
{
|
|
1399
|
-
$replaceWith: {
|
|
1400
|
-
$cond: {
|
|
1401
|
-
if: {
|
|
1402
|
-
$eq: [
|
|
1403
|
-
{
|
|
1404
|
-
$mergeObjects: [
|
|
1405
|
-
'$old',
|
|
1406
|
-
{ deletedAt: { $ifNull: ['$old.deletedAt', null] } },
|
|
1407
|
-
],
|
|
1408
|
-
},
|
|
1409
|
-
{ $mergeObjects: ['$merged', { touchedAt: '$old.touchedAt' }] },
|
|
1410
|
-
],
|
|
1411
|
-
},
|
|
1412
|
-
then: '$old',
|
|
1413
|
-
else: '$merged',
|
|
1414
|
-
},
|
|
1415
|
-
},
|
|
1416
|
-
},
|
|
1417
|
-
]),
|
|
1418
|
-
})).stages,
|
|
1419
|
-
teardown: c => c({
|
|
1420
|
-
collection: out,
|
|
1421
|
-
method: 'updateMany',
|
|
1422
|
-
params: [
|
|
1423
|
-
filter,
|
|
1424
|
-
[
|
|
1425
|
-
{
|
|
1426
|
-
$unset: Object.keys(mapExactToObject(keys, () => 1)),
|
|
1427
|
-
},
|
|
1428
|
-
],
|
|
1429
1428
|
],
|
|
1430
|
-
|
|
1429
|
+
],
|
|
1430
|
+
});
|
|
1431
|
+
return {
|
|
1432
|
+
raw: (first) => link().with($replaceWith_(replacer)).with(sss)
|
|
1433
|
+
.stages,
|
|
1434
|
+
teardown: c => (setDeleted ? c(teardown(out.coll)) : c(teardown(out.coll))),
|
|
1431
1435
|
};
|
|
1432
1436
|
};
|
|
1433
|
-
const $mergeId = () => (out, keys, id, ext
|
|
1437
|
+
const $mergeId = () => (out, keys, id, ext) => {
|
|
1434
1438
|
const omRORec = omitRORec();
|
|
1435
1439
|
return $mergeX(out, keys, root().of('after'), or => {
|
|
1436
1440
|
return ite(eqTyped(root().of('after').expr(), nil), field(omRORec.backward(spread(mapExact(keys, () => nil), {
|
|
1437
1441
|
_id: ['_id', id],
|
|
1438
1442
|
touchedAt: ['touchedAt', current],
|
|
1439
1443
|
}))), or);
|
|
1440
|
-
},
|
|
1444
|
+
}, ext);
|
|
1441
1445
|
};
|
|
1442
|
-
const $simpleMergePart = () => (out, keys,
|
|
1443
|
-
const $simpleMerge = () => (out, keys, whenNotMatched = 'fail') => $mergeX(out, keys, root(), id$1,
|
|
1444
|
-
const $mergePart = () => (out, keys, ext
|
|
1445
|
-
const $merge = () => (out, keys
|
|
1446
|
+
const $simpleMergePart = () => (out, keys, ext) => $mergeX(out, keys, root(), id$1, ext);
|
|
1447
|
+
const $simpleMerge = () => (out, keys, whenNotMatched = 'fail') => $mergeX({ coll: out, whenNotMatched }, keys, root(), id$1, {});
|
|
1448
|
+
const $mergePart = () => (out, keys, ext) => $mergeId()({ coll: out, whenNotMatched: 'fail' }, keys, assertNotNull$1(root().of('before').of('_id').expr()), ext);
|
|
1449
|
+
const $merge = () => (out, keys) => $mergePart()(out, keys, {});
|
|
1446
1450
|
const assertNotNull$1 = (expr) => expr;
|
|
1447
1451
|
|
|
1448
1452
|
const $insertX = (out, expr, map, ext, extExpr) => {
|
|
@@ -1467,23 +1471,7 @@ const $insertX = (out, expr, map, ext, extExpr) => {
|
|
|
1467
1471
|
into: out,
|
|
1468
1472
|
on: root().of('_id'),
|
|
1469
1473
|
stages: true,
|
|
1470
|
-
whenMatched:
|
|
1471
|
-
{ $replaceWith: { old: '$$ROOT', merged: { $mergeObjects: ['$$ROOT', '$$new'] } } },
|
|
1472
|
-
{
|
|
1473
|
-
$replaceWith: {
|
|
1474
|
-
$cond: {
|
|
1475
|
-
if: {
|
|
1476
|
-
$eq: [
|
|
1477
|
-
'$old',
|
|
1478
|
-
{ $mergeObjects: ['$merged', { touchedAt: '$old.touchedAt' }] },
|
|
1479
|
-
],
|
|
1480
|
-
},
|
|
1481
|
-
then: '$old',
|
|
1482
|
-
else: '$merged',
|
|
1483
|
-
},
|
|
1484
|
-
},
|
|
1485
|
-
},
|
|
1486
|
-
]),
|
|
1474
|
+
whenMatched: getWhenMatched('insert'),
|
|
1487
1475
|
whenNotMatched: 'insert',
|
|
1488
1476
|
})).stages;
|
|
1489
1477
|
},
|
|
@@ -2007,4 +1995,4 @@ const makeCol = async (docs, database, name) => {
|
|
|
2007
1995
|
}
|
|
2008
1996
|
};
|
|
2009
1997
|
|
|
2010
|
-
export { $accumulator, $and, $countDict, $entries, $eq, $exists, $expr, $getField, $group, $groupId, $groupMerge, $group_, $gt, $gtTs, $gte, $gteTs, $ifNull, $in, $insert, $insertPart, $insertX, $keys, $let, $lookup, $lt, $lte, $map, $map0, $map1, $match, $matchDelta, $merge, $merge2, $mergeId, $mergePart, $merge_, $ne, $nin, $nor, $or, $outerLookup, $pushDict, $rand, $reduce, $replaceWith, $set, $simpleInsert, $simpleMerge, $simpleMergePart, $sum, $type, $unwind, $unwindDelta, Field, Machine, add, and, anyElementTrue, array, ceil, comp, concat$1 as concat, concatArray, createIndex, ctx, current, dateAdd, dateDiff, dateLt, datePart, dayAndMonthPart, divide, enablePreAndPostImages, eq, eqTyped, except, exprMapVal, field, fieldF, fieldM, filter, filterDefined, first$1 as first, firstSure, floor, from, func, gt, gte, inArray, isArray, ite, last, log, lt, lte, makeCol, map1, mapVal, max, maxDate, mergeExact, mergeExact0, mergeExpr, mergeObjects, minDate, monthPart, multiply, ne, nil, noop, not, notNull, now, or, pair, prepare, rand, range, regex, root, set, setField, single, size, slice, sortArray, staging, startOf, str, sub, subtract, to, toInt, val, weekPart, wrap, year };
|
|
1998
|
+
export { $accumulator, $and, $countDict, $entries, $eq, $exists, $expr, $getField, $group, $groupId, $groupMerge, $group_, $gt, $gtTs, $gte, $gteTs, $ifNull, $in, $insert, $insertPart, $insertX, $keys, $let, $lookup, $lt, $lte, $map, $map0, $map1, $match, $matchDelta, $merge, $merge2, $mergeId, $mergePart, $merge_, $ne, $nin, $nor, $or, $outerLookup, $pushDict, $rand, $reduce, $replaceWith, $set, $simpleInsert, $simpleMerge, $simpleMergePart, $sum, $type, $unwind, $unwindDelta, Field, Machine, add, and, anyElementTrue, array, ceil, comp, concat$1 as concat, concatArray, createIndex, ctx, current, dateAdd, dateDiff, dateLt, datePart, dayAndMonthPart, divide, enablePreAndPostImages, eq, eqTyped, except, exprMapVal, field, fieldF, fieldM, filter, filterDefined, first$1 as first, firstSure, floor, from, func, getWhenMatched, gt, gte, inArray, isArray, ite, last, log, lt, lte, makeCol, map1, mapVal, max, maxDate, mergeExact, mergeExact0, mergeExpr, mergeObjects, minDate, monthPart, multiply, ne, nil, noop, not, notNull, now, or, pair, prepare, rand, range, regex, root, set, setField, single, size, slice, sortArray, staging, startOf, str, sub, subtract, to, toInt, val, weekPart, wrap, year };
|
package/index.js
CHANGED
|
@@ -1367,84 +1367,88 @@ const $replaceWith = (expr) => ({
|
|
|
1367
1367
|
lin: $replaceWith_(expr),
|
|
1368
1368
|
});
|
|
1369
1369
|
|
|
1370
|
-
const
|
|
1370
|
+
const getWhenMatched = (whenNotMatched) => {
|
|
1371
|
+
const setDeleted = whenNotMatched === 'discard';
|
|
1372
|
+
const orNull = (e) => (setDeleted ? $ifNull(e, nil) : e);
|
|
1373
|
+
const preMergeOld = setDeleted
|
|
1374
|
+
? mergeObjects(root().of('old').expr(), field({
|
|
1375
|
+
deletedAt: ['deletedAt', orNull(root().of('old').of('deletedAt').expr())],
|
|
1376
|
+
touchedAt: ['touchedAt', root().of('merged').of('touchedAt').expr()],
|
|
1377
|
+
}))
|
|
1378
|
+
: root().of('old').expr();
|
|
1379
|
+
const same = eq(preMergeOld)(root().of('merged').expr());
|
|
1380
|
+
const newOrOld = whenNotMatched ===
|
|
1381
|
+
'insert'
|
|
1382
|
+
? ctx()('new')
|
|
1383
|
+
: root();
|
|
1384
|
+
const merged = mergeObjects(root().expr(), ctx()('new').expr(), field({
|
|
1385
|
+
deletedAt: ['deletedAt', orNull(newOrOld.of('deletedAt').expr())],
|
|
1386
|
+
}));
|
|
1387
|
+
return link()
|
|
1388
|
+
.with($replaceWith_(field({
|
|
1389
|
+
old: ['old', root().expr()],
|
|
1390
|
+
merged: ['merged', merged],
|
|
1391
|
+
})))
|
|
1392
|
+
.with($replaceWith_(ite(same, root().of('old').expr(), root().of('merged').expr()))).stages;
|
|
1393
|
+
};
|
|
1394
|
+
const $mergeX = (out, keys, f, map, ext) => {
|
|
1371
1395
|
const patch = mapExact(keys, v => sub(v, f));
|
|
1372
1396
|
const filter = mapExactToObject(ext, v => ({ $eq: v }));
|
|
1373
|
-
const setDeleted = whenNotMatched === 'discard';
|
|
1397
|
+
const setDeleted = out.whenNotMatched === 'discard';
|
|
1374
1398
|
const replacer = map(field(omitPick().backward(spread(patch, {
|
|
1375
1399
|
_id: ['_id', f.of('_id').expr()],
|
|
1376
1400
|
touchedAt: ['touchedAt', current],
|
|
1377
|
-
deletedAt: [setDeleted ? 'deletedAt' : 'touchedAt', current],
|
|
1378
1401
|
}))));
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
.with($
|
|
1402
|
+
const sss = setDeleted
|
|
1403
|
+
? link()
|
|
1404
|
+
.with($set_(set()({
|
|
1405
|
+
deletedAt: ['deletedAt', to(current)],
|
|
1406
|
+
})))
|
|
1382
1407
|
.with($merge_({
|
|
1383
|
-
into: out,
|
|
1408
|
+
into: out.coll,
|
|
1409
|
+
on: root().of('_id'),
|
|
1410
|
+
whenNotMatched: 'insert',
|
|
1411
|
+
stages: true,
|
|
1412
|
+
whenMatched: getWhenMatched(out.whenNotMatched),
|
|
1413
|
+
})).stages
|
|
1414
|
+
: link().with($merge_({
|
|
1415
|
+
into: out.coll,
|
|
1384
1416
|
on: root().of('_id'),
|
|
1385
|
-
whenNotMatched:
|
|
1417
|
+
whenNotMatched: 'fail',
|
|
1386
1418
|
stages: true,
|
|
1387
|
-
whenMatched:
|
|
1419
|
+
whenMatched: getWhenMatched(out.whenNotMatched),
|
|
1420
|
+
})).stages;
|
|
1421
|
+
const teardown = (coll) => ({
|
|
1422
|
+
collection: coll,
|
|
1423
|
+
method: 'updateMany',
|
|
1424
|
+
params: [
|
|
1425
|
+
filter,
|
|
1426
|
+
[
|
|
1388
1427
|
{
|
|
1389
|
-
$
|
|
1390
|
-
old: '$$ROOT',
|
|
1391
|
-
merged: {
|
|
1392
|
-
$mergeObjects: [
|
|
1393
|
-
'$$ROOT',
|
|
1394
|
-
'$$new',
|
|
1395
|
-
...(setDeleted ? [{ deletedAt: { $ifNull: ['$deletedAt', null] } }] : []),
|
|
1396
|
-
],
|
|
1397
|
-
},
|
|
1398
|
-
},
|
|
1428
|
+
$unset: Object.keys(mapExactToObject(keys, () => 1)),
|
|
1399
1429
|
},
|
|
1400
|
-
{
|
|
1401
|
-
$replaceWith: {
|
|
1402
|
-
$cond: {
|
|
1403
|
-
if: {
|
|
1404
|
-
$eq: [
|
|
1405
|
-
{
|
|
1406
|
-
$mergeObjects: [
|
|
1407
|
-
'$old',
|
|
1408
|
-
{ deletedAt: { $ifNull: ['$old.deletedAt', null] } },
|
|
1409
|
-
],
|
|
1410
|
-
},
|
|
1411
|
-
{ $mergeObjects: ['$merged', { touchedAt: '$old.touchedAt' }] },
|
|
1412
|
-
],
|
|
1413
|
-
},
|
|
1414
|
-
then: '$old',
|
|
1415
|
-
else: '$merged',
|
|
1416
|
-
},
|
|
1417
|
-
},
|
|
1418
|
-
},
|
|
1419
|
-
]),
|
|
1420
|
-
})).stages,
|
|
1421
|
-
teardown: c => c({
|
|
1422
|
-
collection: out,
|
|
1423
|
-
method: 'updateMany',
|
|
1424
|
-
params: [
|
|
1425
|
-
filter,
|
|
1426
|
-
[
|
|
1427
|
-
{
|
|
1428
|
-
$unset: Object.keys(mapExactToObject(keys, () => 1)),
|
|
1429
|
-
},
|
|
1430
|
-
],
|
|
1431
1430
|
],
|
|
1432
|
-
|
|
1431
|
+
],
|
|
1432
|
+
});
|
|
1433
|
+
return {
|
|
1434
|
+
raw: (first) => link().with($replaceWith_(replacer)).with(sss)
|
|
1435
|
+
.stages,
|
|
1436
|
+
teardown: c => (setDeleted ? c(teardown(out.coll)) : c(teardown(out.coll))),
|
|
1433
1437
|
};
|
|
1434
1438
|
};
|
|
1435
|
-
const $mergeId = () => (out, keys, id, ext
|
|
1439
|
+
const $mergeId = () => (out, keys, id, ext) => {
|
|
1436
1440
|
const omRORec = omitRORec();
|
|
1437
1441
|
return $mergeX(out, keys, root().of('after'), or => {
|
|
1438
1442
|
return ite(eqTyped(root().of('after').expr(), nil), field(omRORec.backward(spread(mapExact(keys, () => nil), {
|
|
1439
1443
|
_id: ['_id', id],
|
|
1440
1444
|
touchedAt: ['touchedAt', current],
|
|
1441
1445
|
}))), or);
|
|
1442
|
-
},
|
|
1446
|
+
}, ext);
|
|
1443
1447
|
};
|
|
1444
|
-
const $simpleMergePart = () => (out, keys,
|
|
1445
|
-
const $simpleMerge = () => (out, keys, whenNotMatched = 'fail') => $mergeX(out, keys, root(), id$1,
|
|
1446
|
-
const $mergePart = () => (out, keys, ext
|
|
1447
|
-
const $merge = () => (out, keys
|
|
1448
|
+
const $simpleMergePart = () => (out, keys, ext) => $mergeX(out, keys, root(), id$1, ext);
|
|
1449
|
+
const $simpleMerge = () => (out, keys, whenNotMatched = 'fail') => $mergeX({ coll: out, whenNotMatched }, keys, root(), id$1, {});
|
|
1450
|
+
const $mergePart = () => (out, keys, ext) => $mergeId()({ coll: out, whenNotMatched: 'fail' }, keys, assertNotNull$1(root().of('before').of('_id').expr()), ext);
|
|
1451
|
+
const $merge = () => (out, keys) => $mergePart()(out, keys, {});
|
|
1448
1452
|
const assertNotNull$1 = (expr) => expr;
|
|
1449
1453
|
|
|
1450
1454
|
const $insertX = (out, expr, map, ext, extExpr) => {
|
|
@@ -1469,23 +1473,7 @@ const $insertX = (out, expr, map, ext, extExpr) => {
|
|
|
1469
1473
|
into: out,
|
|
1470
1474
|
on: root().of('_id'),
|
|
1471
1475
|
stages: true,
|
|
1472
|
-
whenMatched:
|
|
1473
|
-
{ $replaceWith: { old: '$$ROOT', merged: { $mergeObjects: ['$$ROOT', '$$new'] } } },
|
|
1474
|
-
{
|
|
1475
|
-
$replaceWith: {
|
|
1476
|
-
$cond: {
|
|
1477
|
-
if: {
|
|
1478
|
-
$eq: [
|
|
1479
|
-
'$old',
|
|
1480
|
-
{ $mergeObjects: ['$merged', { touchedAt: '$old.touchedAt' }] },
|
|
1481
|
-
],
|
|
1482
|
-
},
|
|
1483
|
-
then: '$old',
|
|
1484
|
-
else: '$merged',
|
|
1485
|
-
},
|
|
1486
|
-
},
|
|
1487
|
-
},
|
|
1488
|
-
]),
|
|
1476
|
+
whenMatched: getWhenMatched('insert'),
|
|
1489
1477
|
whenNotMatched: 'insert',
|
|
1490
1478
|
})).stages;
|
|
1491
1479
|
},
|
|
@@ -2096,6 +2084,7 @@ exports.firstSure = firstSure;
|
|
|
2096
2084
|
exports.floor = floor;
|
|
2097
2085
|
exports.from = from;
|
|
2098
2086
|
exports.func = func;
|
|
2087
|
+
exports.getWhenMatched = getWhenMatched;
|
|
2099
2088
|
exports.gt = gt;
|
|
2100
2089
|
exports.gte = gte;
|
|
2101
2090
|
exports.inArray = inArray;
|
|
@@ -1,19 +1,27 @@
|
|
|
1
|
-
import type { IdHKT, OPick, RWCollection } from '../../types';
|
|
2
|
-
import type { ID, N, O,
|
|
1
|
+
import type { IdHKT, OPick, RWCollection, Timestamp } from '../../types';
|
|
2
|
+
import type { doc, ID, N, O, rawItem, Rec, Replace, RORec, StrKey } from '../../types/json';
|
|
3
3
|
import { type ExprsExact } from '../expression/concat';
|
|
4
|
-
import type { Delta, Expr, StreamRunnerParam, TS } from '../types';
|
|
4
|
+
import type { Delta, Expr, IsDeleted, Model, RawStages, StreamRunnerParam, TS } from '../types';
|
|
5
5
|
import { type Exact } from '../utils/map-object';
|
|
6
6
|
type OutInputE<T, E, A = T | null> = ID & Rec<'after', A> & E;
|
|
7
7
|
type Allowed<K extends string> = Exclude<K, keyof (TS & ID)>;
|
|
8
|
-
type Patch<V, KK extends StrKey<V> = StrKey<V>> = ((OPick<V, Allowed<KK>> & ID) | (Rec<Allowed<KK>, N> & ID)) & TS;
|
|
8
|
+
export type Patch<V, KK extends StrKey<V> = StrKey<V>> = ((OPick<V, Allowed<KK>> & ID) | (Rec<Allowed<KK>, N> & ID)) & TS;
|
|
9
9
|
type TakeDoc<V, E = ID, KK extends StrKey<V> = StrKey<V>> = OPick<V, Allowed<KK>> & E;
|
|
10
10
|
type ND = {
|
|
11
11
|
readonly deletedAt?: null;
|
|
12
12
|
};
|
|
13
13
|
type SafeE<E> = Omit<E, `$${string}` | keyof ID>;
|
|
14
|
-
export declare const
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
14
|
+
export declare const getWhenMatched: <Out extends Model, P extends Model, K extends keyof IsDeleted>(whenNotMatched: "discard" | "fail" | "insert") => RawStages<O, Out, Out | Replace<Out, P>, RORec<"new", Replace<P, RORec<K, Timestamp>>>>;
|
|
15
|
+
type MergeCollection<V extends O, Out extends Model> = {
|
|
16
|
+
coll: RWCollection<Out | Replace<Out, Patch<V>> | Replace<Patch<V>, IsDeleted>, Out>;
|
|
17
|
+
whenNotMatched: 'discard';
|
|
18
|
+
} | {
|
|
19
|
+
coll: RWCollection<Out | Replace<Out, Patch<V>>, Out>;
|
|
20
|
+
whenNotMatched: 'fail';
|
|
21
|
+
};
|
|
22
|
+
export declare const $mergeId: <V extends O>() => <SourcePart extends doc, Out extends Model, E = unknown, EEE extends RORec<string, rawItem> = {}>(out: MergeCollection<V, Out>, keys: ExprsExact<TakeDoc<V, unknown>, SourcePart>, id: Expr<string, OutInputE<TakeDoc<V>, E, null>>, ext: Exact<Omit<SafeE<EEE>, keyof (ND & TS)>, IdHKT>) => StreamRunnerParam<OutInputE<SourcePart, E>, "out">;
|
|
23
|
+
export declare const $simpleMergePart: <V extends O>() => <Source extends doc, Out extends Model, EEE extends RORec<string, rawItem>>(out: MergeCollection<V, Out>, keys: ExprsExact<TakeDoc<V, unknown>, Source>, ext: Exact<Omit<SafeE<EEE>, keyof (ND & TS)>, IdHKT>) => StreamRunnerParam<Source, "out">;
|
|
24
|
+
export declare const $simpleMerge: <V extends O>() => <Source extends doc, Out extends Model>(out: RWCollection<Out | Replace<Out, Patch<V>> | Replace<Patch<V>, IsDeleted>, Out>, keys: ExprsExact<TakeDoc<V, unknown>, Source>, whenNotMatched?: "fail" | "discard") => StreamRunnerParam<Source, "out">;
|
|
25
|
+
export declare const $mergePart: <V extends O>() => <Out extends Model, SourcePart extends doc, EEE extends RORec<string, rawItem>>(out: RWCollection<Out | Replace<Out, Patch<V>>, Out>, keys: ExprsExact<TakeDoc<V, unknown>, SourcePart>, ext: Exact<Omit<SafeE<EEE>, keyof (ND & TS)>, IdHKT>) => StreamRunnerParam<Delta<SourcePart>, "out">;
|
|
26
|
+
export declare const $merge: <V extends O>() => <Out extends Model, SourcePart extends doc>(out: RWCollection<Out | Replace<Out, Patch<V>>, Out>, keys: ExprsExact<TakeDoc<V, unknown>, SourcePart>) => StreamRunnerParam<Delta<SourcePart>, "out">;
|
|
19
27
|
export {};
|
|
@@ -9,5 +9,5 @@ export declare const $replaceWith_: <T extends O, V extends O, C = unknown>(expr
|
|
|
9
9
|
export declare const $unwind_: <T extends O, K extends s, U, Null extends null = never>(k: K, includeNull?: Null) => RawStages<O<T>, T & Rec<K, Arr<U>>, T & Rec<K, Null | U>>;
|
|
10
10
|
export declare const $group_: <V extends O>() => <ID, T extends O, C = unknown>(id: Expr<ID, T, C>, args: Accumulators<T, V, C>) => RawStages<O, T, Rec<"_id", ID> & V, C, 1>;
|
|
11
11
|
export declare const $documents_: <T extends O, C>(docs: Expr<Arr<T>, unknown, C>) => RawStages<unknown, null, T, C, 1>;
|
|
12
|
-
export declare const $project_: <T extends O, K extends import("../..").StrKey<T>>(projection: import("../utils
|
|
12
|
+
export declare const $project_: <T extends O, K extends import("../..").StrKey<T>>(projection: import("../utils").ExactKeys<K>) => RawStages<T, T, T, unknown, 1>;
|
|
13
13
|
export declare const $simpleLookup_: <T extends O, U extends O, R, K extends s, Ctx, C = unknown, S = string>(args: LookupArgs<T, U, R, K, Ctx, C, S>) => RawStages<O<T>, T, T & Rec<K, Arr<U>>, C, 1>;
|
package/lib/aggregate/out.d.ts
CHANGED
|
@@ -21,7 +21,7 @@ export declare const first: <T, D, C>(expr: Expr<Arr<T>, D, C>) => Expr<T | null
|
|
|
21
21
|
export declare const firstSure: <T, D, C>(expr: Expr<Arr<T>, D, C>) => Expr<T, D, C>;
|
|
22
22
|
export declare const last: <T, D, C>(expr: Expr<Arr<T>, D, C>) => Expr<T | null, D, C>;
|
|
23
23
|
export type NullToOBJ<N extends null> = N extends null ? O : N;
|
|
24
|
-
export declare const mergeObjects: <T1, T2, D, C = unknown, N extends null = never>(
|
|
24
|
+
export declare const mergeObjects: <T1, T2, D, C = unknown, N extends null = never>(...exprs: readonly [Expr<T1 | N, D, C>, ...[...Expr<T1 | T2 | N, D, C>[], Expr<T2, D, C>]]) => Expr<(T1 | NullToOBJ<N>) & T2, D, C>;
|
|
25
25
|
export declare const anyElementTrue: <D, C = unknown>(expr: Expr<Arr<boolean>, D, C>) => Expr<boolean, D, C>;
|
|
26
26
|
type Reduce<T, V> = RORec<'value', V> & RORec<'this', T>;
|
|
27
27
|
export declare const $reduce: <T, V, D, C>(input: Expr<Arr<T>, D, C>, initialValue: Expr<V, D, C>, inExpr: Expr<V, D, C & Reduce<T, V>>) => Expr<V, D, C>;
|
package/lib/types/stream.d.ts
CHANGED
|
@@ -46,13 +46,15 @@ export type Stream<out Q extends O, in out T extends Q, in out F extends HKT<[O,
|
|
|
46
46
|
export type TS = {
|
|
47
47
|
readonly touchedAt: Timestamp;
|
|
48
48
|
};
|
|
49
|
-
export type
|
|
49
|
+
export type IsDeleted = {
|
|
50
50
|
readonly deletedAt: Timestamp;
|
|
51
|
-
}
|
|
51
|
+
};
|
|
52
|
+
export type DDel = IsDeleted & ID & TS;
|
|
52
53
|
export type Del = O<DDel>;
|
|
53
|
-
export type
|
|
54
|
+
export type DeletedAt = {
|
|
54
55
|
readonly deletedAt?: Timestamp | null | undefined;
|
|
55
|
-
}
|
|
56
|
+
};
|
|
57
|
+
export type D = O<DeletedAt & ID>;
|
|
56
58
|
export type Model = D & TS;
|
|
57
59
|
export type OutInput<T, A = T | null> = ID & Rec<'after', A>;
|
|
58
60
|
export type SimpleStream<in out Q extends O, out T extends Q> = <Q2 extends O, Result extends Q2>(input: LinStages<Q2 | T, T, Result>) => SimpleStreamExecutionResult<Q | Q2, Result>;
|
package/lib/utils/index.d.ts
CHANGED