@omegup/msync 0.0.89 → 0.0.91

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 CHANGED
@@ -427,7 +427,9 @@ type MergeArgs<T extends O, Out extends O, Ctx, In extends O> = {
427
427
  })) | {
428
428
  stages: true;
429
429
  into: RWCollection<In, Out>;
430
- whenMatched: RawStages<unknown, Out, In>;
430
+ whenMatched: RawStages<unknown, Out, In, {
431
+ new: In;
432
+ }>;
431
433
  } | {
432
434
  stages: 'ctx';
433
435
  vars: ExprsExact<Ctx, T>;
package/index.esm.js CHANGED
@@ -1094,7 +1094,7 @@ const $unwind = (k, dict, middle) => {
1094
1094
 
1095
1095
  const $lookupDelta = ({ field1, field2 }, { coll, exec, input }, k1, k2, k, includeNull1, includeNull2) => {
1096
1096
  const omit = omitRORec();
1097
- const hash = crypto$1.createHash('md5').update(canonicalize(input)).digest('base64url');
1097
+ const hash = crypto$1.createHash('md5').update(coll.collectionName + canonicalize(input) + canonicalize(exec)).digest('base64url');
1098
1098
  const ss = (f) => to($ifNull(root().of(f).of(k1).with(field1).expr(), val(hash)));
1099
1099
  return link()
1100
1100
  .with($replaceWithDelta(field(map1(k1, root().expr()))))
@@ -1378,7 +1378,21 @@ const $mergeX = (out, keys, f, map) => ({
1378
1378
  into: out,
1379
1379
  on: root().of('_id'),
1380
1380
  whenNotMatched: 'fail',
1381
- whenMatched: 'merge',
1381
+ stages: true,
1382
+ whenMatched: asStages([
1383
+ { $replaceWith: { old: '$$ROOT', merged: { $mergeObjects: ['$$ROOT', '$$new'] } } },
1384
+ {
1385
+ $replaceWith: {
1386
+ $cond: {
1387
+ if: {
1388
+ $eq: ['$old', { $mergeObjects: ['$merged', { touchedAt: '$old.touchedAt' }] }],
1389
+ },
1390
+ then: '$old',
1391
+ else: '$merged',
1392
+ },
1393
+ },
1394
+ },
1395
+ ]),
1382
1396
  })).stages;
1383
1397
  },
1384
1398
  teardown: c => c({
@@ -1428,7 +1442,24 @@ const $insertX = (out, expr, map, ext, extExpr) => {
1428
1442
  .with($merge_({
1429
1443
  into: out,
1430
1444
  on: root().of('_id'),
1431
- whenMatched: 'merge',
1445
+ stages: true,
1446
+ whenMatched: asStages([
1447
+ { $replaceWith: { old: '$$ROOT', merged: { $mergeObjects: ['$$ROOT', '$$new'] } } },
1448
+ {
1449
+ $replaceWith: {
1450
+ $cond: {
1451
+ if: {
1452
+ $eq: [
1453
+ '$old',
1454
+ { $mergeObjects: ['$merged', { touchedAt: '$old.touchedAt' }] },
1455
+ ],
1456
+ },
1457
+ then: '$old',
1458
+ else: '$merged',
1459
+ },
1460
+ },
1461
+ },
1462
+ ]),
1432
1463
  whenNotMatched: 'insert',
1433
1464
  })).stages;
1434
1465
  },
@@ -1515,7 +1546,14 @@ const makeWatchStream = (db, { collection, projection: p, hardMatch: m }, startA
1515
1546
  pipeline.push({
1516
1547
  $match: {
1517
1548
  $or: [
1518
- { $expr: { $ne: ['$fullDocument', '$fullDocumentBeforeChange'] } },
1549
+ {
1550
+ $expr: {
1551
+ $ne: [
1552
+ { $mergeObjects: ['$fullDocument', { touchedAt: null }] },
1553
+ { $mergeObjects: ['$fullDocumentBeforeChange', { touchedAt: null }] },
1554
+ ],
1555
+ },
1556
+ },
1519
1557
  Object.fromEntries(changeKeys.map(k => [k, null])),
1520
1558
  ],
1521
1559
  },
@@ -1525,9 +1563,7 @@ const makeWatchStream = (db, { collection, projection: p, hardMatch: m }, startA
1525
1563
  _id: 1,
1526
1564
  },
1527
1565
  });
1528
- const stream = db
1529
- .collection(collection.collectionName)
1530
- .watch(pipeline, {
1566
+ const stream = db.collection(collection.collectionName).watch(pipeline, {
1531
1567
  fullDocument: 'required',
1532
1568
  fullDocumentBeforeChange: 'required',
1533
1569
  startAtOperationTime: startAt,
@@ -1663,6 +1699,7 @@ const executes$2 = (view, input, streamName, skip = false, after) => {
1663
1699
  log('teardown done', `db['${snapshotCollection.collectionName}'].drop()`, ...out);
1664
1700
  };
1665
1701
  if (!same) {
1702
+ log('not same, new data', data);
1666
1703
  await handleTeardown(exists ?? { data });
1667
1704
  }
1668
1705
  await after?.();
package/index.js CHANGED
@@ -1096,7 +1096,7 @@ const $unwind = (k, dict, middle) => {
1096
1096
 
1097
1097
  const $lookupDelta = ({ field1, field2 }, { coll, exec, input }, k1, k2, k, includeNull1, includeNull2) => {
1098
1098
  const omit = omitRORec();
1099
- const hash = crypto$1.createHash('md5').update(jsonCanonicalize.canonicalize(input)).digest('base64url');
1099
+ const hash = crypto$1.createHash('md5').update(coll.collectionName + jsonCanonicalize.canonicalize(input) + jsonCanonicalize.canonicalize(exec)).digest('base64url');
1100
1100
  const ss = (f) => to($ifNull(root().of(f).of(k1).with(field1).expr(), val(hash)));
1101
1101
  return link()
1102
1102
  .with($replaceWithDelta(field(map1(k1, root().expr()))))
@@ -1380,7 +1380,21 @@ const $mergeX = (out, keys, f, map) => ({
1380
1380
  into: out,
1381
1381
  on: root().of('_id'),
1382
1382
  whenNotMatched: 'fail',
1383
- whenMatched: 'merge',
1383
+ stages: true,
1384
+ whenMatched: asStages([
1385
+ { $replaceWith: { old: '$$ROOT', merged: { $mergeObjects: ['$$ROOT', '$$new'] } } },
1386
+ {
1387
+ $replaceWith: {
1388
+ $cond: {
1389
+ if: {
1390
+ $eq: ['$old', { $mergeObjects: ['$merged', { touchedAt: '$old.touchedAt' }] }],
1391
+ },
1392
+ then: '$old',
1393
+ else: '$merged',
1394
+ },
1395
+ },
1396
+ },
1397
+ ]),
1384
1398
  })).stages;
1385
1399
  },
1386
1400
  teardown: c => c({
@@ -1430,7 +1444,24 @@ const $insertX = (out, expr, map, ext, extExpr) => {
1430
1444
  .with($merge_({
1431
1445
  into: out,
1432
1446
  on: root().of('_id'),
1433
- whenMatched: 'merge',
1447
+ stages: true,
1448
+ whenMatched: asStages([
1449
+ { $replaceWith: { old: '$$ROOT', merged: { $mergeObjects: ['$$ROOT', '$$new'] } } },
1450
+ {
1451
+ $replaceWith: {
1452
+ $cond: {
1453
+ if: {
1454
+ $eq: [
1455
+ '$old',
1456
+ { $mergeObjects: ['$merged', { touchedAt: '$old.touchedAt' }] },
1457
+ ],
1458
+ },
1459
+ then: '$old',
1460
+ else: '$merged',
1461
+ },
1462
+ },
1463
+ },
1464
+ ]),
1434
1465
  whenNotMatched: 'insert',
1435
1466
  })).stages;
1436
1467
  },
@@ -1517,7 +1548,14 @@ const makeWatchStream = (db, { collection, projection: p, hardMatch: m }, startA
1517
1548
  pipeline.push({
1518
1549
  $match: {
1519
1550
  $or: [
1520
- { $expr: { $ne: ['$fullDocument', '$fullDocumentBeforeChange'] } },
1551
+ {
1552
+ $expr: {
1553
+ $ne: [
1554
+ { $mergeObjects: ['$fullDocument', { touchedAt: null }] },
1555
+ { $mergeObjects: ['$fullDocumentBeforeChange', { touchedAt: null }] },
1556
+ ],
1557
+ },
1558
+ },
1521
1559
  Object.fromEntries(changeKeys.map(k => [k, null])),
1522
1560
  ],
1523
1561
  },
@@ -1527,9 +1565,7 @@ const makeWatchStream = (db, { collection, projection: p, hardMatch: m }, startA
1527
1565
  _id: 1,
1528
1566
  },
1529
1567
  });
1530
- const stream = db
1531
- .collection(collection.collectionName)
1532
- .watch(pipeline, {
1568
+ const stream = db.collection(collection.collectionName).watch(pipeline, {
1533
1569
  fullDocument: 'required',
1534
1570
  fullDocumentBeforeChange: 'required',
1535
1571
  startAtOperationTime: startAt,
@@ -1665,6 +1701,7 @@ const executes$2 = (view, input, streamName, skip = false, after) => {
1665
1701
  log('teardown done', `db['${snapshotCollection.collectionName}'].drop()`, ...out);
1666
1702
  };
1667
1703
  if (!same) {
1704
+ log('not same, new data', data);
1668
1705
  await handleTeardown(exists ?? { data });
1669
1706
  }
1670
1707
  await after?.();
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "module": "index.esm.js",
4
4
  "typings": "index.d.ts",
5
5
  "name": "@omegup/msync",
6
- "version": "0.0.89",
6
+ "version": "0.0.91",
7
7
  "dependencies": {
8
8
  "dayjs": "^1.11.9",
9
9
  "dotenv": "^16.3.1",