dexie-cloud-addon 4.1.0-beta.42 → 4.1.0-beta.44

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.
@@ -8,7 +8,7 @@
8
8
  *
9
9
  * ==========================================================================
10
10
  *
11
- * Version 4.1.0-beta.42, Tue Feb 04 2025
11
+ * Version 4.1.0-beta.44, Fri Feb 14 2025
12
12
  *
13
13
  * https://dexie.org
14
14
  *
@@ -3680,8 +3680,6 @@
3680
3680
  */
3681
3681
  function setCurrentUser(db, user) {
3682
3682
  return __awaiter(this, void 0, void 0, function* () {
3683
- if (user.userId === db.cloud.currentUserId)
3684
- return; // Already this user.
3685
3683
  const $logins = db.table('$logins');
3686
3684
  yield db.transaction('rw', $logins, (tx) => __awaiter(this, void 0, void 0, function* () {
3687
3685
  const existingLogins = yield $logins.toArray();
@@ -4994,7 +4992,7 @@
4994
4992
  .toArray();
4995
4993
  }
4996
4994
 
4997
- function $Y$1(db) {
4995
+ function $Y(db) {
4998
4996
  const $Y = db.dx._options.Y;
4999
4997
  if (!$Y)
5000
4998
  throw new Error('Y library not supplied to Dexie constructor');
@@ -5024,7 +5022,7 @@
5024
5022
  for (const table of tablesToSync) {
5025
5023
  if (table.schema.yProps) {
5026
5024
  for (const yProp of table.schema.yProps) {
5027
- const Y = $Y$1(db); // This is how we retrieve the user-provided Y library
5025
+ const Y = $Y(db); // This is how we retrieve the user-provided Y library
5028
5026
  const yTable = db.table(yProp.updatesTable); // the updates-table for this combo of table+propName
5029
5027
  const syncState = (yield yTable.get(DEXIE_CLOUD_SYNCER_ID));
5030
5028
  // unsentFrom = the `i` value of updates that aren't yet sent to server (or at least not acked by the server yet)
@@ -6161,8 +6159,14 @@
6161
6159
  }
6162
6160
  return Object.assign(Object.assign({}, table), { mutate: (req) => {
6163
6161
  var _a, _b;
6164
- // @ts-ignore
6165
- if (req.trans.disableChangeTracking) {
6162
+ const idbtrans = req.trans;
6163
+ if (idbtrans.mode === 'versionchange') {
6164
+ // Tell all the other middlewares to skip bothering. We're in versionchange mode.
6165
+ // dexie-cloud is not initialized yet.
6166
+ idbtrans.disableChangeTracking = true;
6167
+ idbtrans.disableAccessControl = true;
6168
+ }
6169
+ if (idbtrans.disableChangeTracking) {
6166
6170
  // Disable ID policy checks and ID generation
6167
6171
  return table.mutate(req);
6168
6172
  }
@@ -6219,17 +6223,13 @@
6219
6223
  return Object.assign(Object.assign({}, core), { table: (tableName) => {
6220
6224
  const table = core.table(tableName);
6221
6225
  return Object.assign(Object.assign({}, table), { mutate: (req) => {
6222
- var _a, _b, _c, _d;
6223
- // @ts-ignore
6224
- if (req.trans.disableChangeTracking) {
6226
+ var _a, _b, _c, _d, _e, _f;
6227
+ const trans = req.trans;
6228
+ if (trans.disableChangeTracking) {
6225
6229
  return table.mutate(req);
6226
6230
  }
6227
- const trans = req.trans;
6228
- if ((_b = (_a = db.cloud.schema) === null || _a === void 0 ? void 0 : _a[tableName]) === null || _b === void 0 ? void 0 : _b.markedForSync) {
6229
- if (trans.mode === 'versionchange') {
6230
- // Don't mutate tables marked for sync in versionchange transactions.
6231
- return Promise.reject(new Dexie.UpgradeError(`Dexie Cloud Addon: Cannot upgrade or populate synced table "${tableName}". See https://dexie.org/cloud/docs/best-practices`));
6232
- }
6231
+ const currentUserId = (_b = (_a = trans.currentUser) === null || _a === void 0 ? void 0 : _a.userId) !== null && _b !== void 0 ? _b : UNAUTHORIZED_USER.userId;
6232
+ if ((_d = (_c = db.cloud.schema) === null || _c === void 0 ? void 0 : _c[tableName]) === null || _d === void 0 ? void 0 : _d.markedForSync) {
6233
6233
  if (req.type === 'add' || req.type === 'put') {
6234
6234
  if (tableName === 'members') {
6235
6235
  for (const member of req.values) {
@@ -6253,12 +6253,12 @@
6253
6253
  // and expect them to be returned. That scenario must work also when db.cloud.currentUserId === 'unauthorized'.
6254
6254
  for (const obj of req.values) {
6255
6255
  if (!obj.owner) {
6256
- obj.owner = trans.currentUser.userId;
6256
+ obj.owner = currentUserId;
6257
6257
  }
6258
6258
  if (!obj.realmId) {
6259
- obj.realmId = trans.currentUser.userId;
6259
+ obj.realmId = currentUserId;
6260
6260
  }
6261
- const key = (_d = (_c = table.schema.primaryKey).extractKey) === null || _d === void 0 ? void 0 : _d.call(_c, obj);
6261
+ const key = (_f = (_e = table.schema.primaryKey).extractKey) === null || _f === void 0 ? void 0 : _f.call(_e, obj);
6262
6262
  if (typeof key === 'string' && key[0] === '#') {
6263
6263
  // Add $ts prop for put operations and
6264
6264
  // disable update operations as well as consistent
@@ -6354,16 +6354,14 @@
6354
6354
  name: 'MutationTrackingMiddleware',
6355
6355
  level: 1,
6356
6356
  create: (core) => {
6357
+ const allTableNames = new Set(core.schema.tables.map((t) => t.name));
6357
6358
  const ordinaryTables = core.schema.tables.filter((t) => !/^\$/.test(t.name));
6358
- let mutTableMap;
6359
- try {
6360
- mutTableMap = new Map(ordinaryTables.map((tbl) => [
6361
- tbl.name,
6362
- core.table(`$${tbl.name}_mutations`),
6363
- ]));
6364
- }
6365
- catch (_a) {
6366
- throwVersionIncrementNeeded();
6359
+ const mutTableMap = new Map();
6360
+ for (const tbl of ordinaryTables) {
6361
+ const mutationTableName = `$${tbl.name}_mutations`;
6362
+ if (allTableNames.has(mutationTableName)) {
6363
+ mutTableMap.set(tbl.name, core.table(mutationTableName));
6364
+ }
6367
6365
  }
6368
6366
  return Object.assign(Object.assign({}, core), { transaction: (tables, mode) => {
6369
6367
  let tx;
@@ -6441,6 +6439,11 @@
6441
6439
  }
6442
6440
  const { schema } = table;
6443
6441
  const mutsTable = mutTableMap.get(tableName);
6442
+ if (!mutsTable) {
6443
+ // We cannot track mutations on this table because there is no mutations table for it.
6444
+ // This might happen in upgraders that executes before cloud schema is applied.
6445
+ return table;
6446
+ }
6444
6447
  return guardedTable(Object.assign(Object.assign({}, table), { mutate: (req) => {
6445
6448
  var _a, _b, _c;
6446
6449
  const trans = req.trans;
@@ -8139,7 +8142,7 @@
8139
8142
  if (provider.destroyed || currentFlowId !== myFlow || !connected)
8140
8143
  return;
8141
8144
  if (serverUpdatesSinceLastSync.length > 0) {
8142
- const Y = $Y$1(db); // Get the Yjs library from Dexie constructor options
8145
+ const Y = $Y(db); // Get the Yjs library from Dexie constructor options
8143
8146
  const mergedUpdate = Y.mergeUpdatesV2(serverUpdatesSinceLastSync.map((update) => update.u));
8144
8147
  const stateVector = Y.encodeStateVectorFromUpdateV2(mergedUpdate);
8145
8148
  docOpenMsg.sv = stateVector;
@@ -8159,8 +8162,68 @@
8159
8162
  }
8160
8163
 
8161
8164
  const ydocTriggers = {};
8162
- const docIsAlreadyHooked = new WeakSet();
8163
8165
  const middlewares = new WeakMap();
8166
+ const txRunner = TriggerRunner("tx");
8167
+ const unloadRunner = TriggerRunner("unload");
8168
+ function TriggerRunner(name) {
8169
+ let triggerExecPromise = null;
8170
+ let triggerScheduled = false;
8171
+ let registry = new Map();
8172
+ function execute(registryCopy) {
8173
+ return __awaiter(this, void 0, void 0, function* () {
8174
+ for (const { db, parentId, triggers, parentTable, prop, } of registryCopy.values()) {
8175
+ const yDoc = Dexie.DexieYProvider.getOrCreateDocument(db, parentTable, prop, parentId);
8176
+ try {
8177
+ Dexie.DexieYProvider.load(yDoc); // If doc is open, this would just be a ++refount
8178
+ yield yDoc.whenLoaded; // If doc is loaded, this would resolve immediately
8179
+ for (const trigger of triggers) {
8180
+ yield trigger(yDoc, parentId);
8181
+ }
8182
+ }
8183
+ catch (error) {
8184
+ console.error(`Error in YDocTrigger ${error}`);
8185
+ }
8186
+ finally {
8187
+ Dexie.DexieYProvider.release(yDoc);
8188
+ }
8189
+ }
8190
+ });
8191
+ }
8192
+ return {
8193
+ name,
8194
+ run() {
8195
+ return __awaiter(this, void 0, void 0, function* () {
8196
+ if (!triggerScheduled && registry.size > 0) {
8197
+ triggerScheduled = true;
8198
+ if (triggerExecPromise)
8199
+ yield triggerExecPromise.catch(() => { });
8200
+ setTimeout(() => {
8201
+ // setTimeout() is to escape from Promise.PSD zones and never run within liveQueries or transaction scopes
8202
+ triggerScheduled = false;
8203
+ const registryCopy = registry;
8204
+ registry = new Map();
8205
+ triggerExecPromise = execute(registryCopy).finally(() => (triggerExecPromise = null));
8206
+ }, 0);
8207
+ }
8208
+ });
8209
+ },
8210
+ enqueue(db, parentTable, parentId, prop, trigger) {
8211
+ const key = `${db.name}:${parentTable}:${parentId}:${prop}`;
8212
+ let entry = registry.get(key);
8213
+ if (!entry) {
8214
+ entry = {
8215
+ db,
8216
+ parentTable,
8217
+ parentId,
8218
+ prop,
8219
+ triggers: new Set(),
8220
+ };
8221
+ registry.set(key, entry);
8222
+ }
8223
+ entry.triggers.add(trigger);
8224
+ },
8225
+ };
8226
+ }
8164
8227
  const createMiddleware = (db) => ({
8165
8228
  stack: 'dbcore',
8166
8229
  level: 10,
@@ -8168,15 +8231,20 @@
8168
8231
  create: (down) => {
8169
8232
  return Object.assign(Object.assign({}, down), { transaction: (stores, mode, options) => {
8170
8233
  const idbtrans = down.transaction(stores, mode, options);
8234
+ if (mode === 'readonly')
8235
+ return idbtrans;
8236
+ if (!stores.some((store) => ydocTriggers[store]))
8237
+ return idbtrans;
8171
8238
  idbtrans.addEventListener('complete', onTransactionCommitted);
8172
8239
  return idbtrans;
8173
- }, table: (tblName) => {
8174
- const coreTable = down.table(tblName);
8175
- const triggerSpec = ydocTriggers[tblName];
8240
+ }, table: (updatesTable) => {
8241
+ const coreTable = down.table(updatesTable);
8242
+ const triggerSpec = ydocTriggers[updatesTable];
8176
8243
  if (!triggerSpec)
8177
8244
  return coreTable;
8178
8245
  const { trigger, parentTable, prop } = triggerSpec;
8179
8246
  return Object.assign(Object.assign({}, coreTable), { mutate(req) {
8247
+ var _a;
8180
8248
  switch (req.type) {
8181
8249
  case 'add': {
8182
8250
  for (const yUpdateRow of req.values) {
@@ -8184,15 +8252,10 @@
8184
8252
  continue; // A syncer or garbage collection state does not point to a key
8185
8253
  const primaryKey = yUpdateRow.k;
8186
8254
  const doc = Dexie.DexieYProvider.getDocCache(db).find(parentTable, primaryKey, prop);
8187
- if (doc) {
8188
- if (!docIsAlreadyHooked.has(doc)) {
8189
- hookToDoc(doc, primaryKey, trigger);
8190
- docIsAlreadyHooked.add(doc);
8191
- }
8192
- }
8193
- else {
8194
- enqueueTrigger(db, tblName, primaryKey, trigger);
8195
- }
8255
+ const runner = doc && ((_a = Dexie.DexieYProvider.for(doc)) === null || _a === void 0 ? void 0 : _a.refCount)
8256
+ ? unloadRunner // Document is open. Wait with trigger until it's closed.
8257
+ : txRunner; // Document is closed. Run trigger immediately after transaction commits.
8258
+ runner.enqueue(db, parentTable, primaryKey, prop, trigger);
8196
8259
  }
8197
8260
  break;
8198
8261
  }
@@ -8215,7 +8278,7 @@
8215
8278
  keySet.addKey(k);
8216
8279
  }
8217
8280
  for (const interval of keySet) {
8218
- enqueueTrigger(db, tblName, interval.from, trigger);
8281
+ txRunner.enqueue(db, parentTable, interval.from, prop, trigger);
8219
8282
  }
8220
8283
  });
8221
8284
  }
@@ -8226,74 +8289,11 @@
8226
8289
  } });
8227
8290
  },
8228
8291
  });
8229
- let triggerExecPromise = null;
8230
- let triggerScheduled = false;
8231
- let scheduledTriggers = [];
8232
- function $Y(db) {
8233
- const $Y = db._options.Y;
8234
- if (!$Y)
8235
- throw new Error('Y library not supplied to Dexie constructor');
8236
- return $Y;
8237
- }
8238
- function executeTriggers(triggersToRun) {
8239
- return __awaiter(this, void 0, void 0, function* () {
8240
- for (const { db, parentId, trigger, updatesTable } of triggersToRun) {
8241
- // Load entire document into an Y.Doc instance:
8242
- const updates = yield db
8243
- .table(updatesTable)
8244
- .where({ k: parentId })
8245
- .toArray();
8246
- const Y = $Y(db);
8247
- const yDoc = new Y.Doc();
8248
- for (const update of updates) {
8249
- Y.applyUpdateV2(yDoc, update.u);
8250
- }
8251
- try {
8252
- yield trigger(yDoc, parentId);
8253
- }
8254
- catch (error) {
8255
- console.error(`Error in YDocTrigger ${error}`);
8256
- }
8257
- }
8258
- });
8259
- }
8260
- function enqueueTrigger(db, updatesTable, parentId, trigger) {
8261
- scheduledTriggers.push({
8262
- db,
8263
- updatesTable,
8264
- parentId,
8265
- trigger,
8266
- });
8267
- }
8268
8292
  function onTransactionCommitted() {
8269
- return __awaiter(this, void 0, void 0, function* () {
8270
- if (!triggerScheduled && scheduledTriggers.length > 0) {
8271
- triggerScheduled = true;
8272
- if (triggerExecPromise)
8273
- yield triggerExecPromise.catch(() => { });
8274
- setTimeout(() => {
8275
- // setTimeout() is to escape from Promise.PSD zones and never run within liveQueries or transaction scopes
8276
- triggerScheduled = false;
8277
- const triggersToRun = scheduledTriggers;
8278
- scheduledTriggers = [];
8279
- triggerExecPromise = executeTriggers(triggersToRun).finally(() => (triggerExecPromise = null));
8280
- }, 0);
8281
- }
8282
- });
8293
+ txRunner.run();
8283
8294
  }
8284
- function hookToDoc(doc, parentId, trigger) {
8285
- // From now on, keep listening to doc updates and execute the trigger when it happens there instead
8286
- doc.on('updateV2', (update, origin) => {
8287
- //Dexie.ignoreTransaction(()=>{
8288
- trigger(doc, parentId);
8289
- //});
8290
- });
8291
- /*
8292
- NOT NEEDED because DexieYProvider's docCache will also listen to destroy and remove it from its cache:
8293
- doc.on('destroy', ()=>{
8294
- docIsAlreadyHooked.delete(doc);
8295
- })
8296
- */
8295
+ function beforeProviderUnload() {
8296
+ unloadRunner.run();
8297
8297
  }
8298
8298
  function defineYDocTrigger(table, prop, trigger) {
8299
8299
  var _a, _b;
@@ -8312,6 +8312,9 @@
8312
8312
  middlewares.set(db, mw);
8313
8313
  }
8314
8314
  db.use(mw);
8315
+ {
8316
+ Dexie.DexieYProvider.on('beforeunload', beforeProviderUnload);
8317
+ }
8315
8318
  }
8316
8319
 
8317
8320
  const DEFAULT_OPTIONS = {
@@ -8354,7 +8357,7 @@
8354
8357
  const syncComplete = new rxjs.Subject();
8355
8358
  dexie.cloud = {
8356
8359
  // @ts-ignore
8357
- version: "4.1.0-beta.42",
8360
+ version: "4.1.0-beta.44",
8358
8361
  options: Object.assign({}, DEFAULT_OPTIONS),
8359
8362
  schema: null,
8360
8363
  get currentUserId() {
@@ -8672,7 +8675,7 @@
8672
8675
  }
8673
8676
  }
8674
8677
  // @ts-ignore
8675
- dexieCloud.version = "4.1.0-beta.42";
8678
+ dexieCloud.version = "4.1.0-beta.44";
8676
8679
  Dexie.Cloud = dexieCloud;
8677
8680
 
8678
8681
  exports.default = dexieCloud;