dexie 4.0.9-alpha.1 → 4.0.10

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.js CHANGED
@@ -4,7 +4,7 @@
4
4
  *
5
5
  * By David Fahlander, david.fahlander@gmail.com
6
6
  *
7
- * Version 4.0.9-alpha.1, Fri Aug 23 2024
7
+ * Version 4.0.10, Fri Nov 15 2024
8
8
  *
9
9
  * https://dexie.org
10
10
  *
@@ -776,6 +776,8 @@
776
776
  }); });
777
777
  });
778
778
  });
779
+ if (NativePromise.withResolvers)
780
+ DexiePromise.withResolvers = NativePromise.withResolvers;
779
781
  }
780
782
  function executePromiseTask(promise, fn) {
781
783
  try {
@@ -1150,7 +1152,7 @@
1150
1152
  }
1151
1153
  }
1152
1154
 
1153
- var DEXIE_VERSION = '4.0.9-alpha.1';
1155
+ var DEXIE_VERSION = '4.0.10';
1154
1156
  var maxString = String.fromCharCode(65535);
1155
1157
  var minKey = -Infinity;
1156
1158
  var INVALID_KEY_ARGUMENT = "Invalid key provided. Keys must be of type string, number, Date or Array<string | number | Date>.";
@@ -1268,95 +1270,6 @@
1268
1270
  return new Uint8Array(a);
1269
1271
  }
1270
1272
 
1271
- function getDocCache(db) {
1272
- var _a;
1273
- var _b;
1274
- return (_a = (_b = db._novip)['_docCache']) !== null && _a !== void 0 ? _a : (_b['_docCache'] = {
1275
- cache: {},
1276
- get size() {
1277
- return Object.keys(this.cache).length;
1278
- },
1279
- find: function (table, primaryKey, ydocProp) {
1280
- var cacheKey = getYDocCacheKey(table, primaryKey, ydocProp);
1281
- var docRef = this.cache[cacheKey];
1282
- return docRef ? docRef.deref() : undefined;
1283
- },
1284
- add: function (doc) {
1285
- var _a = doc.meta, parentTable = _a.parentTable, parentId = _a.parentId, parentProp = _a.parentProp;
1286
- if (!parentTable || !parentProp || parentId == null)
1287
- throw new Error("Missing Dexie-related metadata in Y.Doc");
1288
- var cacheKey = getYDocCacheKey(parentTable, parentId, parentProp);
1289
- this.cache[cacheKey] = new WeakRef(doc);
1290
- docRegistry.register(doc, { cache: this.cache, key: cacheKey });
1291
- },
1292
- delete: function (doc) {
1293
- docRegistry.unregister(doc);
1294
- delete this.cache[getYDocCacheKey(doc.meta.parentTable, doc.meta.parentId, doc.meta.parentProp)];
1295
- },
1296
- });
1297
- }
1298
- var docRegistry = new FinalizationRegistry(function (_a) {
1299
- var cache = _a.cache, key = _a.key;
1300
- delete cache[key];
1301
- });
1302
- var destroyed = new WeakSet();
1303
- function throwIfDestroyed(doc) {
1304
- if (destroyed.has(doc))
1305
- throw new Error('Y.Doc has been destroyed');
1306
- }
1307
- function getYDocCacheKey(table, primaryKey, ydocProp) {
1308
- return "".concat(table, "[").concat(primaryKey, "].").concat(ydocProp);
1309
- }
1310
-
1311
- function createYDocProperty(db, Y, table, prop, updatesTable) {
1312
- var pkKeyPath = table.schema.primKey.keyPath;
1313
- var docCache = getDocCache(db);
1314
- return {
1315
- get: function () {
1316
- var id = getByKeyPath(this, pkKeyPath);
1317
- var doc = docCache.find(table.name, id, prop);
1318
- if (doc)
1319
- return doc;
1320
- doc = new Y.Doc({
1321
- meta: {
1322
- db: db,
1323
- updatesTable: updatesTable,
1324
- parentProp: prop,
1325
- parentTable: table.name,
1326
- parentId: id
1327
- },
1328
- });
1329
- docCache.add(doc);
1330
- doc.on('destroy', function () {
1331
- destroyed.add(doc);
1332
- docCache.delete(doc);
1333
- });
1334
- return doc;
1335
- },
1336
- };
1337
- }
1338
-
1339
- function builtInDeletionTrigger(table, keys, res) {
1340
- var yProps = table.schema.yProps;
1341
- if (!yProps)
1342
- return res;
1343
- if (keys && res.numFailures > 0)
1344
- keys = keys.filter(function (_, i) { return !res.failures[i]; });
1345
- return Promise.all(yProps.map(function (_a) {
1346
- var updatesTable = _a.updatesTable;
1347
- return keys
1348
- ? table.db.table(updatesTable).where('k').anyOf(keys).delete()
1349
- : table.db.table(updatesTable).clear();
1350
- })).then(function () { return res; });
1351
- }
1352
-
1353
- function getYLibrary(db) {
1354
- var Y = db._options.Y;
1355
- if (!Y)
1356
- throw new exceptions.MissingAPI('Y library not supplied to Dexie constructor');
1357
- return Y;
1358
- }
1359
-
1360
1273
  var Table = (function () {
1361
1274
  function Table() {
1362
1275
  }
@@ -1432,9 +1345,8 @@
1432
1345
  console.warn("The query ".concat(JSON.stringify(indexOrCrit), " on ").concat(this.name, " would benefit from a ") +
1433
1346
  "compound index [".concat(keyPaths.join('+'), "]"));
1434
1347
  var idxByName = this.schema.idxByName;
1435
- var idb = this.db._deps.indexedDB;
1436
1348
  function equals(a, b) {
1437
- return idb.cmp(a, b) === 0;
1349
+ return cmp(a, b) === 0;
1438
1350
  }
1439
1351
  var _a = keyPaths.reduce(function (_a, keyPath) {
1440
1352
  var prevIndex = _a[0], prevFilterFn = _a[1];
@@ -1488,7 +1400,6 @@
1488
1400
  return this.toCollection().reverse();
1489
1401
  };
1490
1402
  Table.prototype.mapToClass = function (constructor) {
1491
- var _this = this;
1492
1403
  var _a = this, db = _a.db, tableName = _a.name;
1493
1404
  this.schema.mappedClass = constructor;
1494
1405
  if (constructor.prototype instanceof Entity) {
@@ -1506,20 +1417,6 @@
1506
1417
  return class_1;
1507
1418
  }(constructor));
1508
1419
  }
1509
- if (this.schema.yProps) {
1510
- var Y_1 = getYLibrary(db);
1511
- constructor = (function (_super) {
1512
- __extends(class_2, _super);
1513
- function class_2() {
1514
- return _super !== null && _super.apply(this, arguments) || this;
1515
- }
1516
- return class_2;
1517
- }(constructor));
1518
- this.schema.yProps.forEach(function (_a) {
1519
- var prop = _a.prop, updatesTable = _a.updatesTable;
1520
- Object.defineProperty(constructor.prototype, prop, createYDocProperty(db, Y_1, _this, prop, updatesTable));
1521
- });
1522
- }
1523
1420
  var inheritedProps = new Set();
1524
1421
  for (var proto = constructor.prototype; proto; proto = getProto(proto)) {
1525
1422
  Object.getOwnPropertyNames(proto).forEach(function (propName) { return inheritedProps.add(propName); });
@@ -1601,14 +1498,12 @@
1601
1498
  };
1602
1499
  Table.prototype.delete = function (key) {
1603
1500
  var _this = this;
1604
- return this._trans('readwrite', function (trans) { return _this.core.mutate({ trans: trans, type: 'delete', keys: [key] })
1605
- .then(function (res) { return builtInDeletionTrigger(_this, [key], res); })
1606
- .then(function (res) { return res.numFailures ? DexiePromise.reject(res.failures[0]) : undefined; }); });
1501
+ return this._trans('readwrite', function (trans) { return _this.core.mutate({ trans: trans, type: 'delete', keys: [key] }); })
1502
+ .then(function (res) { return res.numFailures ? DexiePromise.reject(res.failures[0]) : undefined; });
1607
1503
  };
1608
1504
  Table.prototype.clear = function () {
1609
1505
  var _this = this;
1610
- return this._trans('readwrite', function (trans) { return _this.core.mutate({ trans: trans, type: 'deleteRange', range: AnyRange })
1611
- .then(function (res) { return builtInDeletionTrigger(_this, null, res); }); })
1506
+ return this._trans('readwrite', function (trans) { return _this.core.mutate({ trans: trans, type: 'deleteRange', range: AnyRange }); })
1612
1507
  .then(function (res) { return res.numFailures ? DexiePromise.reject(res.failures[0]) : undefined; });
1613
1508
  };
1614
1509
  Table.prototype.bulkGet = function (keys) {
@@ -1735,8 +1630,7 @@
1735
1630
  var _this = this;
1736
1631
  var numKeys = keys.length;
1737
1632
  return this._trans('readwrite', function (trans) {
1738
- return _this.core.mutate({ trans: trans, type: 'delete', keys: keys })
1739
- .then(function (res) { return builtInDeletionTrigger(_this, keys, res); });
1633
+ return _this.core.mutate({ trans: trans, type: 'delete', keys: keys });
1740
1634
  }).then(function (_a) {
1741
1635
  var numFailures = _a.numFailures, lastResult = _a.lastResult, failures = _a.failures;
1742
1636
  if (numFailures === 0)
@@ -2028,7 +1922,7 @@
2028
1922
  var order = this._ctx.dir === "next" ? 1 : -1;
2029
1923
  function sorter(a, b) {
2030
1924
  var aVal = getval(a, lastIndex), bVal = getval(b, lastIndex);
2031
- return aVal < bVal ? -order : aVal > bVal ? order : 0;
1925
+ return cmp(aVal, bVal) * order;
2032
1926
  }
2033
1927
  return this.toArray(function (a) {
2034
1928
  return a.sort(sorter);
@@ -2248,7 +2142,16 @@
2248
2142
  }
2249
2143
  var coreTable = ctx.table.core;
2250
2144
  var _a = coreTable.schema.primaryKey, outbound = _a.outbound, extractKey = _a.extractKey;
2251
- var limit = _this.db._options.modifyChunkSize || 200;
2145
+ var limit = 200;
2146
+ var modifyChunkSize = _this.db._options.modifyChunkSize;
2147
+ if (modifyChunkSize) {
2148
+ if (typeof modifyChunkSize == 'object') {
2149
+ limit = modifyChunkSize[coreTable.name] || modifyChunkSize['*'] || 200;
2150
+ }
2151
+ else {
2152
+ limit = modifyChunkSize;
2153
+ }
2154
+ }
2252
2155
  var totalFailures = [];
2253
2156
  var successCount = 0;
2254
2157
  var failedKeys = [];
@@ -2260,48 +2163,45 @@
2260
2163
  totalFailures.push(failures[pos]);
2261
2164
  }
2262
2165
  };
2263
- var isUnconditionalDelete = changes === deleteCallback;
2264
2166
  return _this.clone().primaryKeys().then(function (keys) {
2265
2167
  var criteria = isPlainKeyRange(ctx) &&
2266
2168
  ctx.limit === Infinity &&
2267
- (typeof changes !== 'function' || isUnconditionalDelete) && {
2169
+ (typeof changes !== 'function' || changes === deleteCallback) && {
2268
2170
  index: ctx.index,
2269
2171
  range: ctx.range
2270
2172
  };
2271
2173
  var nextChunk = function (offset) {
2272
2174
  var count = Math.min(limit, keys.length - offset);
2273
- var keysInChunk = keys.slice(offset, offset + count);
2274
- return (isUnconditionalDelete ? Promise.resolve([]) : coreTable.getMany({
2175
+ return coreTable.getMany({
2275
2176
  trans: trans,
2276
- keys: keysInChunk,
2177
+ keys: keys.slice(offset, offset + count),
2277
2178
  cache: "immutable"
2278
- })).then(function (values) {
2179
+ }).then(function (values) {
2279
2180
  var addValues = [];
2280
2181
  var putValues = [];
2281
2182
  var putKeys = outbound ? [] : null;
2282
- var deleteKeys = isUnconditionalDelete ? keysInChunk : [];
2283
- if (!isUnconditionalDelete)
2284
- for (var i = 0; i < count; ++i) {
2285
- var origValue = values[i];
2286
- var ctx_1 = {
2287
- value: deepClone(origValue),
2288
- primKey: keys[offset + i]
2289
- };
2290
- if (modifyer.call(ctx_1, ctx_1.value, ctx_1) !== false) {
2291
- if (ctx_1.value == null) {
2292
- deleteKeys.push(keys[offset + i]);
2293
- }
2294
- else if (!outbound && cmp(extractKey(origValue), extractKey(ctx_1.value)) !== 0) {
2295
- deleteKeys.push(keys[offset + i]);
2296
- addValues.push(ctx_1.value);
2297
- }
2298
- else {
2299
- putValues.push(ctx_1.value);
2300
- if (outbound)
2301
- putKeys.push(keys[offset + i]);
2302
- }
2183
+ var deleteKeys = [];
2184
+ for (var i = 0; i < count; ++i) {
2185
+ var origValue = values[i];
2186
+ var ctx_1 = {
2187
+ value: deepClone(origValue),
2188
+ primKey: keys[offset + i]
2189
+ };
2190
+ if (modifyer.call(ctx_1, ctx_1.value, ctx_1) !== false) {
2191
+ if (ctx_1.value == null) {
2192
+ deleteKeys.push(keys[offset + i]);
2193
+ }
2194
+ else if (!outbound && cmp(extractKey(origValue), extractKey(ctx_1.value)) !== 0) {
2195
+ deleteKeys.push(keys[offset + i]);
2196
+ addValues.push(ctx_1.value);
2197
+ }
2198
+ else {
2199
+ putValues.push(ctx_1.value);
2200
+ if (outbound)
2201
+ putKeys.push(keys[offset + i]);
2303
2202
  }
2304
2203
  }
2204
+ }
2305
2205
  return Promise.resolve(addValues.length > 0 &&
2306
2206
  coreTable.mutate({ trans: trans, type: 'add', values: addValues })
2307
2207
  .then(function (res) {
@@ -2319,15 +2219,14 @@
2319
2219
  changeSpec: typeof changes !== 'function'
2320
2220
  && changes,
2321
2221
  isAdditionalChunk: offset > 0
2322
- }).then(function (res) { return applyMutateResult(putValues.length, res); }); }).then(function () { return (deleteKeys.length > 0 || (criteria && isUnconditionalDelete)) &&
2222
+ }).then(function (res) { return applyMutateResult(putValues.length, res); }); }).then(function () { return (deleteKeys.length > 0 || (criteria && changes === deleteCallback)) &&
2323
2223
  coreTable.mutate({
2324
2224
  trans: trans,
2325
2225
  type: 'delete',
2326
2226
  keys: deleteKeys,
2327
2227
  criteria: criteria,
2328
2228
  isAdditionalChunk: offset > 0
2329
- }).then(function (res) { return builtInDeletionTrigger(ctx.table, deleteKeys, res); })
2330
- .then(function (res) { return applyMutateResult(deleteKeys.length, res); }); }).then(function () {
2229
+ }).then(function (res) { return applyMutateResult(deleteKeys.length, res); }); }).then(function () {
2331
2230
  return keys.length > offset + count && nextChunk(offset + limit);
2332
2231
  });
2333
2232
  });
@@ -2343,7 +2242,6 @@
2343
2242
  Collection.prototype.delete = function () {
2344
2243
  var ctx = this._ctx, range = ctx.range;
2345
2244
  if (isPlainKeyRange(ctx) &&
2346
- !ctx.table.schema.yProps &&
2347
2245
  (ctx.isPrimKey || range.type === 3 ))
2348
2246
  {
2349
2247
  return this._write(function (trans) {
@@ -2352,7 +2250,7 @@
2352
2250
  return ctx.table.core.count({ trans: trans, query: { index: primaryKey, range: coreRange } }).then(function (count) {
2353
2251
  return ctx.table.core.mutate({ trans: trans, type: 'deleteRange', range: coreRange })
2354
2252
  .then(function (_a) {
2355
- var failures = _a.failures, numFailures = _a.numFailures;
2253
+ var failures = _a.failures; _a.lastResult; _a.results; var numFailures = _a.numFailures;
2356
2254
  if (numFailures)
2357
2255
  throw new ModifyError("Could not delete some values", Object.keys(failures).map(function (pos) { return failures[pos]; }), count - numFailures);
2358
2256
  return count - numFailures;
@@ -2954,13 +2852,6 @@
2954
2852
  function createTransactionConstructor(db) {
2955
2853
  return makeClassConstructor(Transaction.prototype, function Transaction(mode, storeNames, dbschema, chromeTransactionDurability, parent) {
2956
2854
  var _this = this;
2957
- if (mode !== 'readonly')
2958
- storeNames.forEach(function (storeName) {
2959
- var _a;
2960
- var yProps = (_a = dbschema[storeName]) === null || _a === void 0 ? void 0 : _a.yProps;
2961
- if (yProps)
2962
- storeNames = storeNames.concat(yProps.map(function (p) { return p.updatesTable; }));
2963
- });
2964
2855
  this.db = db;
2965
2856
  this.mode = mode;
2966
2857
  this.storeNames = storeNames;
@@ -2996,7 +2887,7 @@
2996
2887
  });
2997
2888
  }
2998
2889
 
2999
- function createIndexSpec(name, keyPath, unique, multi, auto, compound, isPrimKey, type) {
2890
+ function createIndexSpec(name, keyPath, unique, multi, auto, compound, isPrimKey) {
3000
2891
  return {
3001
2892
  name: name,
3002
2893
  keyPath: keyPath,
@@ -3004,8 +2895,7 @@
3004
2895
  multi: multi,
3005
2896
  auto: auto,
3006
2897
  compound: compound,
3007
- src: (unique && !isPrimKey ? '&' : '') + (multi ? '*' : '') + (auto ? "++" : "") + nameFromKeyPath(keyPath),
3008
- type: type
2898
+ src: (unique && !isPrimKey ? '&' : '') + (multi ? '*' : '') + (auto ? "++" : "") + nameFromKeyPath(keyPath)
3009
2899
  };
3010
2900
  }
3011
2901
  function nameFromKeyPath(keyPath) {
@@ -3014,17 +2904,13 @@
3014
2904
  keyPath ? ('[' + [].join.call(keyPath, '+') + ']') : "";
3015
2905
  }
3016
2906
 
3017
- function createTableSchema(name, primKey, indexes, yProps) {
2907
+ function createTableSchema(name, primKey, indexes) {
3018
2908
  return {
3019
2909
  name: name,
3020
2910
  primKey: primKey,
3021
2911
  indexes: indexes,
3022
2912
  mappedClass: null,
3023
- yProps: yProps === null || yProps === void 0 ? void 0 : yProps.map(function (prop) { return ({
3024
- prop: prop,
3025
- updatesTable: "$".concat(name, ".").concat(prop, "_updates"),
3026
- }); }),
3027
- idxByName: arrayToObject(indexes, function (index) { return [index.name, index]; }),
2913
+ idxByName: arrayToObject(indexes, function (index) { return [index.name, index]; })
3028
2914
  };
3029
2915
  }
3030
2916
 
@@ -3768,15 +3654,10 @@
3768
3654
  }
3769
3655
  function parseIndexSyntax(primKeyAndIndexes) {
3770
3656
  return primKeyAndIndexes.split(',').map(function (index, indexNum) {
3771
- var _a;
3772
- var typeSplit = index.split(':');
3773
- var type = (_a = typeSplit[1]) === null || _a === void 0 ? void 0 : _a.trim();
3774
- index = typeSplit[0].trim();
3775
- if (type && type !== 'Y')
3776
- throw new exceptions.Schema("Unsupported type '".concat(type, "'"));
3657
+ index = index.trim();
3777
3658
  var name = index.replace(/([&*]|\+\+)/g, "");
3778
3659
  var keyPath = /^\[/.test(name) ? name.match(/^\[(.*)\]$/)[1].split('+') : name;
3779
- return createIndexSpec(name, keyPath || null, /\&/.test(index), /\*/.test(index), /\+\+/.test(index), isArray(keyPath), indexNum === 0, type);
3660
+ return createIndexSpec(name, keyPath || null, /\&/.test(index), /\*/.test(index), /\+\+/.test(index), isArray(keyPath), indexNum === 0);
3780
3661
  });
3781
3662
  }
3782
3663
 
@@ -3784,57 +3665,40 @@
3784
3665
  function Version() {
3785
3666
  }
3786
3667
  Version.prototype._parseStoresSpec = function (stores, outSchema) {
3787
- var _this = this;
3788
3668
  keys(stores).forEach(function (tableName) {
3789
- var _a;
3790
3669
  if (stores[tableName] !== null) {
3791
3670
  var indexes = parseIndexSyntax(stores[tableName]);
3792
- var yProps = indexes.filter(function (idx) { return idx.type === 'Y'; }).map(function (idx) { return idx.name; });
3793
- indexes = indexes.filter(function (idx) { return idx.type !== 'Y'; });
3794
3671
  var primKey = indexes.shift();
3795
- if (!primKey) {
3796
- throw new exceptions.Schema('Invalid schema for table ' + tableName + ': ' + stores[tableName]);
3797
- }
3798
3672
  primKey.unique = true;
3799
3673
  if (primKey.multi)
3800
- throw new exceptions.Schema('Primary key cannot be multiEntry*');
3674
+ throw new exceptions.Schema("Primary key cannot be multi-valued");
3801
3675
  indexes.forEach(function (idx) {
3802
3676
  if (idx.auto)
3803
- throw new exceptions.Schema('Only primary key can be marked as autoIncrement (++)');
3677
+ throw new exceptions.Schema("Only primary key can be marked as autoIncrement (++)");
3804
3678
  if (!idx.keyPath)
3805
- throw new exceptions.Schema('Index must have a name and cannot be an empty string');
3679
+ throw new exceptions.Schema("Index must have a name and cannot be an empty string");
3806
3680
  });
3807
- var tblSchema = createTableSchema(tableName, primKey, indexes, yProps.length ? yProps : undefined);
3808
- outSchema[tableName] = tblSchema;
3809
- for (var _i = 0, _b = tblSchema.yProps || []; _i < _b.length; _i++) {
3810
- var yProp = _b[_i];
3811
- _this._parseStoresSpec((_a = {}, _a[yProp.updatesTable] = '++i,k', _a), outSchema);
3812
- }
3681
+ outSchema[tableName] = createTableSchema(tableName, primKey, indexes);
3813
3682
  }
3814
3683
  });
3815
3684
  };
3816
3685
  Version.prototype.stores = function (stores) {
3817
3686
  var db = this.db;
3818
- this._cfg.storesSource = this._cfg.storesSource
3819
- ? extend(this._cfg.storesSource, stores)
3820
- : stores;
3687
+ this._cfg.storesSource = this._cfg.storesSource ?
3688
+ extend(this._cfg.storesSource, stores) :
3689
+ stores;
3821
3690
  var versions = db._versions;
3822
3691
  var storesSpec = {};
3823
3692
  var dbschema = {};
3824
3693
  versions.forEach(function (version) {
3825
3694
  extend(storesSpec, version._cfg.storesSource);
3826
- dbschema = version._cfg.dbschema = {};
3695
+ dbschema = (version._cfg.dbschema = {});
3827
3696
  version._parseStoresSpec(storesSpec, dbschema);
3828
3697
  });
3829
3698
  db._dbSchema = dbschema;
3830
3699
  removeTablesApi(db, [db._allTables, db, db.Transaction.prototype]);
3831
3700
  setApiOnPlace(db, [db._allTables, db, db.Transaction.prototype, this._cfg.tables], keys(dbschema), dbschema);
3832
3701
  db._storeNames = keys(dbschema);
3833
- db._storeNames.forEach(function (tableName) {
3834
- if (dbschema[tableName].yProps) {
3835
- db.table(tableName).mapToClass(dbschema[tableName].mappedClass || Object);
3836
- }
3837
- });
3838
3702
  return this;
3839
3703
  };
3840
3704
  Version.prototype.upgrade = function (upgradeFunction) {
@@ -4775,6 +4639,7 @@
4775
4639
  var extractKey = primaryKey.extractKey, outbound = primaryKey.outbound;
4776
4640
  var indexesWithAutoIncPK = primaryKey.autoIncrement && indexes.filter(function (index) { return index.compound && index.keyPath.includes(primaryKey.keyPath); });
4777
4641
  var tableClone = __assign(__assign({}, table), { mutate: function (req) {
4642
+ var _a, _b;
4778
4643
  var trans = req.trans;
4779
4644
  var mutatedParts = req.mutatedParts || (req.mutatedParts = {});
4780
4645
  var getRangeSet = function (indexName) {
@@ -4785,13 +4650,13 @@
4785
4650
  var pkRangeSet = getRangeSet("");
4786
4651
  var delsRangeSet = getRangeSet(":dels");
4787
4652
  var type = req.type;
4788
- var _a = req.type === "deleteRange"
4653
+ var _c = req.type === "deleteRange"
4789
4654
  ? [req.range]
4790
4655
  : req.type === "delete"
4791
4656
  ? [req.keys]
4792
4657
  : req.values.length < 50
4793
4658
  ? [getEffectiveKeys(primaryKey, req).filter(function (id) { return id; }), req.values]
4794
- : [], keys = _a[0], newObjs = _a[1];
4659
+ : [], keys = _c[0], newObjs = _c[1];
4795
4660
  var oldCache = req.trans["_cache"];
4796
4661
  if (isArray(keys)) {
4797
4662
  pkRangeSet.addKeys(keys);
@@ -4804,7 +4669,10 @@
4804
4669
  }
4805
4670
  }
4806
4671
  else if (keys) {
4807
- var range = { from: keys.lower, to: keys.upper };
4672
+ var range = {
4673
+ from: (_a = keys.lower) !== null && _a !== void 0 ? _a : core.MIN_KEY,
4674
+ to: (_b = keys.upper) !== null && _b !== void 0 ? _b : core.MAX_KEY
4675
+ };
4808
4676
  delsRangeSet.add(range);
4809
4677
  pkRangeSet.add(range);
4810
4678
  }
@@ -4820,7 +4688,9 @@
4820
4688
  indexesWithAutoIncPK.forEach(function (idx) {
4821
4689
  var idxVals = req.values.map(function (v) { return idx.extractKey(v); });
4822
4690
  var pkPos = idx.keyPath.findIndex(function (prop) { return prop === primaryKey.keyPath; });
4823
- res.results.forEach(function (pk) { return idxVals[pkPos] = pk; });
4691
+ for (var i = 0, len = res.results.length; i < len; ++i) {
4692
+ idxVals[i][pkPos] = res.results[i];
4693
+ }
4824
4694
  getRangeSet(idx.name).addKeys(idxVals);
4825
4695
  });
4826
4696
  }
@@ -5028,12 +4898,27 @@
5028
4898
  }
5029
4899
  }
5030
4900
  switch (op.type) {
5031
- case 'add':
4901
+ case 'add': {
4902
+ var existingKeys_1 = new RangeSet().addKeys(req.values ? result.map(function (v) { return extractPrimKey(v); }) : result);
5032
4903
  modifedResult = result.concat(req.values
5033
- ? includedValues
5034
- : includedValues.map(function (v) { return extractPrimKey(v); }));
4904
+ ? includedValues.filter(function (v) {
4905
+ var key = extractPrimKey(v);
4906
+ if (existingKeys_1.hasKey(key))
4907
+ return false;
4908
+ existingKeys_1.addKey(key);
4909
+ return true;
4910
+ })
4911
+ : includedValues
4912
+ .map(function (v) { return extractPrimKey(v); })
4913
+ .filter(function (k) {
4914
+ if (existingKeys_1.hasKey(k))
4915
+ return false;
4916
+ existingKeys_1.addKey(k);
4917
+ return true;
4918
+ }));
5035
4919
  break;
5036
- case 'put':
4920
+ }
4921
+ case 'put': {
5037
4922
  var keySet_1 = new RangeSet().addKeys(op.values.map(function (v) { return extractPrimKey(v); }));
5038
4923
  modifedResult = result
5039
4924
  .filter(
@@ -5043,9 +4928,12 @@
5043
4928
  ? includedValues
5044
4929
  : includedValues.map(function (v) { return extractPrimKey(v); }));
5045
4930
  break;
4931
+ }
5046
4932
  case 'delete':
5047
4933
  var keysToDelete_1 = new RangeSet().addKeys(op.keys);
5048
- modifedResult = result.filter(function (item) { return !keysToDelete_1.hasKey(req.values ? extractPrimKey(item) : item); });
4934
+ modifedResult = result.filter(function (item) {
4935
+ return !keysToDelete_1.hasKey(req.values ? extractPrimKey(item) : item);
4936
+ });
5049
4937
  break;
5050
4938
  case 'deleteRange':
5051
4939
  var range_1 = op.range;
@@ -5259,7 +5147,8 @@
5259
5147
  var trans = PSD.trans;
5260
5148
  if (primKey.outbound ||
5261
5149
  trans.db._options.cache === 'disabled' ||
5262
- trans.explicit
5150
+ trans.explicit ||
5151
+ trans.idbtrans.mode !== 'readwrite'
5263
5152
  ) {
5264
5153
  return downTable.mutate(req);
5265
5154
  }
@@ -5271,6 +5160,8 @@
5271
5160
  promise.then(function (res) {
5272
5161
  var reqWithResolvedKeys = __assign(__assign({}, req), { values: req.values.map(function (value, i) {
5273
5162
  var _a;
5163
+ if (res.failures[i])
5164
+ return value;
5274
5165
  var valueWithKey = ((_a = primKey.keyPath) === null || _a === void 0 ? void 0 : _a.includes('.'))
5275
5166
  ? deepClone(value)
5276
5167
  : __assign({}, value);
@@ -5381,140 +5272,6 @@
5381
5272
  });
5382
5273
  }
5383
5274
 
5384
- function compressYDocs(db, interval) {
5385
- var p = Promise.resolve();
5386
- for (var _i = 0, _a = db.tables; _i < _a.length; _i++) {
5387
- var table = _a[_i];
5388
- var _loop_1 = function (yProp) {
5389
- p = p.then(function () { return compressYDocsTable(db, yProp, interval); });
5390
- };
5391
- for (var _b = 0, _c = table.schema.yProps || []; _b < _c.length; _b++) {
5392
- var yProp = _c[_b];
5393
- _loop_1(yProp);
5394
- }
5395
- }
5396
- return p;
5397
- }
5398
- function compressYDocsTable(db, _a, skipIfRunnedSince
5399
- ) {
5400
- var updatesTable = _a.updatesTable;
5401
- var updTbl = db.table(updatesTable);
5402
- return Promise.all([
5403
- updTbl
5404
- .where('i')
5405
- .startsWith('')
5406
- .toArray(),
5407
- db.transaction('rw', updatesTable, function () {
5408
- return updTbl.get(0).then(function (lastCompressed) {
5409
- if (lastCompressed &&
5410
- skipIfRunnedSince &&
5411
- lastCompressed.lastRun.getTime() > Date.now() - skipIfRunnedSince) {
5412
- return null;
5413
- }
5414
- lastCompressed = lastCompressed || { i: 0, lastCompressed: 0 };
5415
- return updTbl
5416
- .put(__assign(__assign({}, lastCompressed), { lastRun: new Date() }))
5417
- .then(function () { return lastCompressed; });
5418
- });
5419
- }),
5420
- ]).then(function (_a) {
5421
- var syncers = _a[0], stamp = _a[1];
5422
- if (!stamp)
5423
- return;
5424
- var lastCompressedUpdate = stamp.lastCompressed;
5425
- var unsentFrom = Math.min.apply(Math, syncers.map(function (s) { return s.unsentFrom || Infinity; }));
5426
- return updTbl
5427
- .where('i')
5428
- .between(lastCompressedUpdate, Infinity, false)
5429
- .toArray(function (addedUpdates) {
5430
- if (addedUpdates.length <= 1)
5431
- return;
5432
- var docsToCompress = [];
5433
- var lastUpdateToCompress = lastCompressedUpdate + 1;
5434
- var _loop_2 = function (j) {
5435
- var updateRow = addedUpdates[j];
5436
- var i = updateRow.i, f = updateRow.f, k = updateRow.k;
5437
- if (i >= unsentFrom && (f & 0x01))
5438
- return "break";
5439
- var entry = docsToCompress.find(function (entry) { return cmp(entry.docId, k) === 0; });
5440
- if (entry)
5441
- entry.updates.push(updateRow);
5442
- else
5443
- docsToCompress.push({ docId: k, updates: [updateRow] });
5444
- lastUpdateToCompress = i;
5445
- };
5446
- for (var j = 0; j < addedUpdates.length; ++j) {
5447
- var state_1 = _loop_2(j);
5448
- if (state_1 === "break")
5449
- break;
5450
- }
5451
- var p = Promise.resolve();
5452
- var _loop_3 = function (docId, updates) {
5453
- p = p.then(function () { return compressUpdatesForDoc(db, updatesTable, docId, updates); });
5454
- };
5455
- for (var _i = 0, docsToCompress_1 = docsToCompress; _i < docsToCompress_1.length; _i++) {
5456
- var _a = docsToCompress_1[_i], docId = _a.docId, updates = _a.updates;
5457
- _loop_3(docId, updates);
5458
- }
5459
- return p.then(function () {
5460
- return db.transaction('rw', updTbl, function () {
5461
- return updTbl.get(0).then(function (current) {
5462
- return updTbl.put(__assign(__assign({}, current), { lastCompressed: Math.max(lastUpdateToCompress, (current === null || current === void 0 ? void 0 : current.lastCompressed) || 0) }));
5463
- });
5464
- });
5465
- });
5466
- });
5467
- });
5468
- }
5469
- function compressUpdatesForDoc(db, updatesTable, parentId, addedUpdatesToCompress) {
5470
- if (addedUpdatesToCompress.length < 1)
5471
- throw new Error('Invalid input');
5472
- return db.transaction('rw', updatesTable, function (tx) {
5473
- var updTbl = tx.table(updatesTable);
5474
- return updTbl.where({ k: parentId }).first(function (mainUpdate) {
5475
- var updates = [mainUpdate].concat(addedUpdatesToCompress);
5476
- var Y = getYLibrary(db);
5477
- var doc = new Y.Doc({ gc: true });
5478
- updates.forEach(function (update) {
5479
- Y.applyUpdateV2(doc, update.u);
5480
- });
5481
- var compressedUpdate = Y.encodeStateAsUpdateV2(doc);
5482
- var lastUpdate = updates.pop();
5483
- return updTbl
5484
- .put({
5485
- i: lastUpdate.i,
5486
- k: parentId,
5487
- u: compressedUpdate,
5488
- })
5489
- .then(function () { return updTbl.bulkDelete(updates.map(function (update) { return update.i; })); });
5490
- });
5491
- });
5492
- }
5493
-
5494
- var INTERVAL = 10000;
5495
- function periodicGC(db) {
5496
- var timer = null;
5497
- db.on('ready', function (db) {
5498
- if (db.tables.some(function (tbl) { return tbl.schema.yProps; })) {
5499
- var gc_1 = function () {
5500
- if (!db.isOpen())
5501
- return;
5502
- compressYDocs(db, INTERVAL).catch(function (err) {
5503
- console.debug('Error during periodic GC', err);
5504
- }).then(function () {
5505
- timer = setTimeout(gc_1, INTERVAL);
5506
- });
5507
- };
5508
- timer = setTimeout(gc_1, INTERVAL);
5509
- }
5510
- }, true);
5511
- db.on('close', function () {
5512
- if (timer)
5513
- clearTimeout(timer);
5514
- timer = null;
5515
- });
5516
- }
5517
-
5518
5275
  var Dexie$1 = (function () {
5519
5276
  function Dexie(name, options) {
5520
5277
  var _this = this;
@@ -5556,18 +5313,7 @@
5556
5313
  });
5557
5314
  this._state = state;
5558
5315
  this.name = name;
5559
- this.on = Events(this, "populate", "blocked", "versionchange", "close", "y", { ready: [promisableChain, nop] });
5560
- this.once = function (event, callback) {
5561
- var fn = function () {
5562
- var args = [];
5563
- for (var _i = 0; _i < arguments.length; _i++) {
5564
- args[_i] = arguments[_i];
5565
- }
5566
- _this.on(event).unsubscribe(fn);
5567
- callback.apply(_this, args);
5568
- };
5569
- return _this.on(event, fn);
5570
- };
5316
+ this.on = Events(this, "populate", "blocked", "versionchange", "close", { ready: [promisableChain, nop] });
5571
5317
  this.on.ready.subscribe = override(this.on.ready.subscribe, function (subscribe) {
5572
5318
  return function (subscriber, bSticky) {
5573
5319
  Dexie.vip(function () {
@@ -5646,8 +5392,6 @@
5646
5392
  }
5647
5393
  });
5648
5394
  this.vip = vipDB;
5649
- if (options === null || options === void 0 ? void 0 : options.Y)
5650
- periodicGC(this);
5651
5395
  addons.forEach(function (addon) { return addon(_this); });
5652
5396
  }
5653
5397
  Dexie.prototype.version = function (versionNumber) {
@@ -5710,7 +5454,6 @@
5710
5454
  function () { return dexieOpen(_this); });
5711
5455
  };
5712
5456
  Dexie.prototype._close = function () {
5713
- this.on.close.fire(new CustomEvent('close'));
5714
5457
  var state = this._state;
5715
5458
  var idx = connections.indexOf(this);
5716
5459
  if (idx >= 0)
@@ -5868,9 +5611,6 @@
5868
5611
  }
5869
5612
  return this._allTables[tableName];
5870
5613
  };
5871
- Dexie.prototype.gc = function () {
5872
- return compressYDocs(this);
5873
- };
5874
5614
  return Dexie;
5875
5615
  }());
5876
5616
 
@@ -6171,116 +5911,6 @@
6171
5911
  return new PropModification({ replacePrefix: [a, b] });
6172
5912
  }
6173
5913
 
6174
- function observeYDocUpdates(provider, doc, db, parentTableName, updatesTableName, parentId, Y) {
6175
- var lastUpdateId = 0;
6176
- var initial = true;
6177
- var subscription = liveQuery(function () {
6178
- throwIfDestroyed(doc);
6179
- var updatesTable = db.table(updatesTableName);
6180
- return Promise.all([
6181
- (lastUpdateId > 0
6182
- ? updatesTable
6183
- .where('i')
6184
- .between(lastUpdateId, Infinity, false)
6185
- .toArray()
6186
- .then(function (updates) {
6187
- return updates.filter(function (update) { return cmp(update.k, parentId) === 0; });
6188
- })
6189
- : updatesTable.where({ k: parentId }).toArray()).then(function (updates) {
6190
- if (updates.length > 0)
6191
- lastUpdateId = updates[updates.length - 1].i;
6192
- return updates;
6193
- }),
6194
- db.table(parentTableName).where(':id').equals(parentId).toArray(),
6195
- ]);
6196
- }).subscribe(function (_a) {
6197
- var updates = _a[0], parentRow = _a[1];
6198
- if (parentRow.length === 0) {
6199
- doc.destroy();
6200
- return;
6201
- }
6202
- throwIfDestroyed(doc);
6203
- if (updates.length > 0) {
6204
- Y.transact(doc, function () {
6205
- updates.forEach(function (update) {
6206
- Y.applyUpdateV2(doc, update.u);
6207
- });
6208
- }, provider, false);
6209
- }
6210
- if (initial) {
6211
- initial = false;
6212
- provider.on('load').fire(provider);
6213
- doc.emit('load', [doc]);
6214
- }
6215
- }, function (error) {
6216
- provider.on('error').fire(error);
6217
- });
6218
- var onUpdate = function (update, origin) {
6219
- if (origin === provider)
6220
- return;
6221
- db.table(updatesTableName)
6222
- .add({
6223
- k: parentId,
6224
- u: update,
6225
- f: 1,
6226
- })
6227
- .then(function (i) {
6228
- if (i === lastUpdateId - 1)
6229
- ++lastUpdateId;
6230
- })
6231
- .catch(function (error) {
6232
- provider.on('error').fire(error);
6233
- });
6234
- };
6235
- var stopObserving = function () {
6236
- subscription.unsubscribe();
6237
- doc.off('updateV2', onUpdate);
6238
- doc.off('destroy', stopObserving);
6239
- };
6240
- doc.on('updateV2', onUpdate);
6241
- doc.on('destroy', stopObserving);
6242
- return stopObserving;
6243
- }
6244
-
6245
- function DexieYProvider(doc) {
6246
- var _a = (doc || {}).meta, db = _a.db, parentTable = _a.parentTable, parentId = _a.parentId, updatesTable = _a.updatesTable;
6247
- if (!db || !parentTable || !updatesTable)
6248
- throw new Error('Y.Doc not generated by Dexie');
6249
- if (!db.table(parentTable) || !db.table(updatesTable)) {
6250
- throw new Error("Table ".concat(parentTable, " or ").concat(updatesTable, " not found in db"));
6251
- }
6252
- throwIfDestroyed(doc);
6253
- var Y = getYLibrary(db);
6254
- function createEvents() {
6255
- return Events(null, "load", "sync", "error");
6256
- }
6257
- var on = createEvents();
6258
- var provider = {
6259
- destroyed: false,
6260
- doc: doc,
6261
- on: on,
6262
- off: function (name, f) { var _a; (_a = on[name]) === null || _a === void 0 ? void 0 : _a.unsubscribe(f); },
6263
- whenLoaded: new Promise(function (resolve, reject) {
6264
- on('load', resolve);
6265
- on('error', reject);
6266
- }),
6267
- whenSynced: new Promise(function (resolve, reject) {
6268
- on('sync', resolve);
6269
- on('error', reject);
6270
- }),
6271
- destroy: function () {
6272
- this.destroyed = true;
6273
- stopObserving();
6274
- on = this.on = createEvents();
6275
- }
6276
- };
6277
- var stopObserving = observeYDocUpdates(provider, doc, db, parentTable, updatesTable, parentId, Y);
6278
- doc.on('destroy', provider.destroy.bind(provider));
6279
- db.on.y.fire(provider, Y);
6280
- return provider;
6281
- }
6282
- DexieYProvider.getDocCache = getDocCache;
6283
-
6284
5914
  DexiePromise.rejectionMapper = mapError;
6285
5915
  setDebug(debug);
6286
5916
 
@@ -6298,8 +5928,7 @@
6298
5928
  'default': Dexie$1,
6299
5929
  RangeSet: RangeSet,
6300
5930
  mergeRanges: mergeRanges,
6301
- rangesOverlap: rangesOverlap,
6302
- DexieYProvider: DexieYProvider
5931
+ rangesOverlap: rangesOverlap
6303
5932
  });
6304
5933
 
6305
5934
  __assign(Dexie$1, namedExports, { default: Dexie$1 });