dexie 4.1.0-alpha.2 → 4.1.0-alpha.4

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/dist/dexie.d.ts CHANGED
@@ -4,7 +4,7 @@
4
4
  *
5
5
  * By David Fahlander, david.fahlander@gmail.com
6
6
  *
7
- * Version 4.1.0-alpha.2, Fri Oct 04 2024
7
+ * Version 4.1.0-alpha.4, Sun Oct 13 2024
8
8
  *
9
9
  * https://dexie.org
10
10
  *
@@ -746,7 +746,7 @@ export interface DBCoreMutateResponse {
746
746
  [operationNumber: number]: Error;
747
747
  };
748
748
  lastResult: any;
749
- results?: any[]; // Present on AddRequest and PutRequest.
749
+ results?: any[]; // Always present on responses to AddRequest and PutRequest.
750
750
  }
751
751
  export interface DBCoreAddRequest {
752
752
  type: "add";
package/dist/dexie.js CHANGED
@@ -4,7 +4,7 @@
4
4
  *
5
5
  * By David Fahlander, david.fahlander@gmail.com
6
6
  *
7
- * Version 4.1.0-alpha.2, Fri Oct 04 2024
7
+ * Version 4.1.0-alpha.4, Sun Oct 13 2024
8
8
  *
9
9
  * https://dexie.org
10
10
  *
@@ -1177,7 +1177,7 @@
1177
1177
  }
1178
1178
  }
1179
1179
 
1180
- var DEXIE_VERSION = '4.1.0-alpha.2';
1180
+ var DEXIE_VERSION = '4.1.0-alpha.4';
1181
1181
  var maxString = String.fromCharCode(65535);
1182
1182
  var minKey = -Infinity;
1183
1183
  var INVALID_KEY_ARGUMENT = "Invalid key provided. Keys must be of type string, number, Date or Array<string | number | Date>.";
@@ -1339,6 +1339,9 @@
1339
1339
  var pkKeyPath = table.schema.primKey.keyPath;
1340
1340
  var docCache = getDocCache(db);
1341
1341
  return {
1342
+ set: function () {
1343
+ throw new TypeError("Y.Doc properties are read-only");
1344
+ },
1342
1345
  get: function () {
1343
1346
  var id = getByKeyPath(this, pkKeyPath);
1344
1347
  var doc = docCache.find(table.name, id, prop);
@@ -1535,13 +1538,14 @@
1535
1538
  }
1536
1539
  if (this.schema.yProps) {
1537
1540
  var Y_1 = getYLibrary(db);
1538
- constructor = (function (_super) {
1539
- __extends(class_2, _super);
1540
- function class_2() {
1541
+ var DBObjectWithYProps = (function (_super) {
1542
+ __extends(DBObjectWithYProps, _super);
1543
+ function DBObjectWithYProps() {
1541
1544
  return _super !== null && _super.apply(this, arguments) || this;
1542
1545
  }
1543
- return class_2;
1546
+ return DBObjectWithYProps;
1544
1547
  }(constructor));
1548
+ constructor = DBObjectWithYProps;
1545
1549
  this.schema.yProps.forEach(function (_a) {
1546
1550
  var prop = _a.prop, updatesTable = _a.updatesTable;
1547
1551
  Object.defineProperty(constructor.prototype, prop, createYDocProperty(db, Y_1, _this, prop, updatesTable));
@@ -3847,6 +3851,87 @@
3847
3851
  });
3848
3852
  }
3849
3853
 
3854
+ var EMPTY_ARRAY = [];
3855
+ function createYjsMiddleware(dbSchema, Y) {
3856
+ return {
3857
+ stack: 'dbcore',
3858
+ name: 'yjsMiddleware',
3859
+ level: 50,
3860
+ create: function (downCore) {
3861
+ return (__assign(__assign({}, downCore), { table: function (tableName) {
3862
+ var downTable = downCore.table(tableName);
3863
+ var dbTableSchema = dbSchema[tableName];
3864
+ var yProps = dbTableSchema.yProps;
3865
+ if (!yProps || yProps.length === 0)
3866
+ return downTable;
3867
+ var tableMiddleware = __assign(__assign({}, downTable), { mutate: function (req) {
3868
+ if (req.type !== 'add' && req.type !== 'put')
3869
+ return downTable.mutate(req);
3870
+ var reqClone = req;
3871
+ var updateSources = yProps
3872
+ .map(function (p) { return ({
3873
+ p: p,
3874
+ entries: req.values.flatMap(function (value, iter) {
3875
+ if (!value || typeof value !== 'object')
3876
+ throw new TypeError("Table ".concat(tableName, " (with Y-properties) must only contain objects"));
3877
+ if (!hasOwn(value, p.prop))
3878
+ return [];
3879
+ if (reqClone === req)
3880
+ reqClone = __assign(__assign({}, req), { values: req.values.map(function (v) { return (__assign({}, v)); }) });
3881
+ delete reqClone.values[iter][p.prop];
3882
+ var doc = value[p.prop];
3883
+ if (doc === null)
3884
+ throw new TypeError("Cannot set Y property to null");
3885
+ if (doc === undefined)
3886
+ return EMPTY_ARRAY;
3887
+ if (typeof doc !== 'object' || !('whenLoaded' in doc)) {
3888
+ throw new TypeError("Y properties can only be inited with an Y.Doc instance or undefined to create an empty Y.Doc");
3889
+ }
3890
+ if (req.type === 'put')
3891
+ throw new TypeError("Setting ".concat(tableName, ".").concat(p.prop, " (declared as ").concat(p.prop, ":Y) is only allowed when inserting new objects using db.").concat(tableName, ".add(), not put() or update()."));
3892
+ if (Y.encodeStateVector(doc).length === 1) {
3893
+ return EMPTY_ARRAY;
3894
+ }
3895
+ return {
3896
+ iter: iter,
3897
+ u: Y.encodeStateAsUpdateV2(doc),
3898
+ };
3899
+ }),
3900
+ }); })
3901
+ .filter(function (_a) {
3902
+ var entries = _a.entries;
3903
+ return entries.length > 0;
3904
+ });
3905
+ if (req === reqClone)
3906
+ return downTable.mutate(req);
3907
+ return downTable.mutate(reqClone).then(function (res) {
3908
+ if (updateSources.length === 0)
3909
+ return res;
3910
+ return Promise.all(updateSources.map(function (_a) {
3911
+ var p = _a.p, entries = _a.entries;
3912
+ var updatesTable = downCore.table(p.updatesTable);
3913
+ var updatesToInsert = entries.map(function (_a) {
3914
+ var iter = _a.iter, u = _a.u;
3915
+ return ({
3916
+ k: res.results[iter],
3917
+ u: u,
3918
+ f: 1,
3919
+ });
3920
+ });
3921
+ return updatesTable.mutate({
3922
+ type: 'add',
3923
+ values: updatesToInsert,
3924
+ trans: req.trans,
3925
+ });
3926
+ })).then(function () { return res; });
3927
+ });
3928
+ } });
3929
+ return tableMiddleware;
3930
+ } }));
3931
+ },
3932
+ };
3933
+ }
3934
+
3850
3935
  var Version = (function () {
3851
3936
  function Version() {
3852
3937
  }
@@ -3911,6 +3996,12 @@
3911
3996
  db.table(tableName).mapToClass(dbschema[tableName].mappedClass || Object);
3912
3997
  }
3913
3998
  });
3999
+ if (Object.values(dbschema).some(function (table) { return table.yProps; })) {
4000
+ db.use(createYjsMiddleware(dbschema, getYLibrary(db)));
4001
+ }
4002
+ else {
4003
+ db.unuse({ stack: 'dbcore', name: 'yjsMiddleware' });
4004
+ }
3914
4005
  return this;
3915
4006
  };
3916
4007
  Version.prototype.upgrade = function (upgradeFunction) {
@@ -5611,20 +5702,22 @@
5611
5702
  if (!stamp)
5612
5703
  return;
5613
5704
  var lastCompressedUpdate = stamp.lastCompressed;
5614
- var unsyncedFrom = Math.min.apply(Math, __spreadArray([], __read(syncers.map(function (s) { return Math.min(s.unsentFrom || Infinity, s.receivedUntil != null ? s.receivedUntil + 1 : Infinity); })), false));
5705
+ var unsyncedFrom = Math.min.apply(Math, __spreadArray([], __read(syncers.map(function (s) {
5706
+ return Math.min(s.unsentFrom || Infinity, s.receivedUntil != null ? s.receivedUntil + 1 : Infinity);
5707
+ })), false));
5615
5708
  return updTbl
5616
5709
  .where('i')
5617
5710
  .between(lastCompressedUpdate, Infinity, false)
5618
5711
  .toArray(function (addedUpdates) {
5619
5712
  var e_3, _a;
5620
- if (addedUpdates.length <= 1)
5713
+ if (addedUpdates.length === 0)
5621
5714
  return;
5622
5715
  var docsToCompress = [];
5623
- var lastUpdateToCompress = lastCompressedUpdate + 1;
5716
+ var lastUpdateToCompress = lastCompressedUpdate;
5624
5717
  var _loop_2 = function (j) {
5625
5718
  var updateRow = addedUpdates[j];
5626
5719
  var i = updateRow.i, f = updateRow.f, k = updateRow.k;
5627
- if (i >= unsyncedFrom && (f & 0x01))
5720
+ if (i >= unsyncedFrom && f & 0x01)
5628
5721
  return "break";
5629
5722
  var entry = docsToCompress.find(function (entry) { return cmp(entry.docId, k) === 0; });
5630
5723
  if (entry)
@@ -5638,9 +5731,13 @@
5638
5731
  if (state_1 === "break")
5639
5732
  break;
5640
5733
  }
5734
+ if (lastUpdateToCompress === lastCompressedUpdate)
5735
+ return;
5641
5736
  var p = Promise.resolve();
5642
5737
  var _loop_3 = function (docId, updates) {
5643
- p = p.then(function () { return compressUpdatesForDoc(db, updatesTable, docId, updates); });
5738
+ p = p.then(function () {
5739
+ return compressUpdatesForDoc(db, updatesTable, docId, updates);
5740
+ });
5644
5741
  };
5645
5742
  try {
5646
5743
  for (var docsToCompress_1 = __values(docsToCompress), docsToCompress_1_1 = docsToCompress_1.next(); !docsToCompress_1_1.done; docsToCompress_1_1 = docsToCompress_1.next()) {
@@ -5671,7 +5768,7 @@
5671
5768
  return db.transaction('rw', updatesTable, function (tx) {
5672
5769
  var updTbl = tx.table(updatesTable);
5673
5770
  return updTbl.where({ k: parentId }).first(function (mainUpdate) {
5674
- var updates = [mainUpdate].concat(addedUpdatesToCompress);
5771
+ var updates = [mainUpdate].concat(addedUpdatesToCompress.filter(function (u) { return u.i !== mainUpdate.i; }));
5675
5772
  var Y = getYLibrary(db);
5676
5773
  var doc = new Y.Doc({ gc: true });
5677
5774
  updates.forEach(function (update) {