@anfenn/dync 1.0.28 → 1.0.30

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/index.cjs CHANGED
@@ -371,12 +371,14 @@ function wrapWithMutationEmitter(table, tableName, emitMutation) {
371
371
  const rawBulkDelete = table.raw.bulkDelete;
372
372
  const rawClear = table.raw.clear;
373
373
  table.add = async (item) => {
374
- const result = await rawAdd(item);
374
+ const itemWithLocalId = { ...item, _localId: item._localId || createLocalId() };
375
+ const result = await rawAdd(itemWithLocalId);
375
376
  emitMutation({ type: "add", tableName, keys: [result] });
376
377
  return result;
377
378
  };
378
379
  table.put = async (item) => {
379
- const result = await rawPut(item);
380
+ const itemWithLocalId = { ...item, _localId: item._localId || createLocalId() };
381
+ const result = await rawPut(itemWithLocalId);
380
382
  emitMutation({ type: "update", tableName, keys: [result] });
381
383
  return result;
382
384
  };
@@ -392,14 +394,22 @@ function wrapWithMutationEmitter(table, tableName, emitMutation) {
392
394
  emitMutation({ type: "delete", tableName, keys: [key] });
393
395
  };
394
396
  table.bulkAdd = async (items) => {
395
- const result = await rawBulkAdd(items);
397
+ const itemsWithLocalIds = items.map((item) => ({
398
+ ...item,
399
+ _localId: item._localId || createLocalId()
400
+ }));
401
+ const result = await rawBulkAdd(itemsWithLocalIds);
396
402
  if (items.length > 0) {
397
403
  emitMutation({ type: "add", tableName });
398
404
  }
399
405
  return result;
400
406
  };
401
407
  table.bulkPut = async (items) => {
402
- const result = await rawBulkPut(items);
408
+ const itemsWithLocalIds = items.map((item) => ({
409
+ ...item,
410
+ _localId: item._localId || createLocalId()
411
+ }));
412
+ const result = await rawBulkPut(itemsWithLocalIds);
403
413
  if (items.length > 0) {
404
414
  emitMutation({ type: "update", tableName });
405
415
  }
@@ -1340,13 +1350,15 @@ var DyncBase = class {
1340
1350
  */
1341
1351
  constructor(config) {
1342
1352
  const { databaseName, storageAdapter, sync: syncConfig, options } = config;
1343
- const isBatchMode = typeof syncConfig.push === "function" && typeof syncConfig.pull === "function";
1344
- if (isBatchMode) {
1345
- this.batchSync = syncConfig;
1346
- this.syncedTables = new Set(this.batchSync.syncTables);
1347
- } else {
1348
- this.syncApis = syncConfig;
1349
- this.syncedTables = new Set(Object.keys(this.syncApis));
1353
+ if (syncConfig) {
1354
+ const isBatchMode = typeof syncConfig.push === "function" && typeof syncConfig.pull === "function";
1355
+ if (isBatchMode) {
1356
+ this.batchSync = syncConfig;
1357
+ this.syncedTables = new Set(this.batchSync.syncTables);
1358
+ } else {
1359
+ this.syncApis = syncConfig;
1360
+ this.syncedTables = new Set(Object.keys(this.syncApis));
1361
+ }
1350
1362
  }
1351
1363
  this.adapter = storageAdapter;
1352
1364
  this.name = databaseName;
@@ -1391,6 +1403,8 @@ var DyncBase = class {
1391
1403
  if (typeof tableSchema === "string") {
1392
1404
  if (isSyncTable) {
1393
1405
  fullSchema[tableName] = `${LOCAL_PK}, &${SERVER_PK}, ${tableSchema}, ${UPDATED_AT}`;
1406
+ } else {
1407
+ fullSchema[tableName] = `${LOCAL_PK}, ${tableSchema}`;
1394
1408
  }
1395
1409
  self.logger.debug(
1396
1410
  `[dync] Defining ${isSyncTable ? "" : "non-"}sync table '${tableName}' with primary key & indexes '${fullSchema[tableName]}'`
@@ -1398,6 +1412,8 @@ var DyncBase = class {
1398
1412
  } else {
1399
1413
  if (isSyncTable) {
1400
1414
  fullSchema[tableName] = self.injectSyncColumns(tableSchema);
1415
+ } else {
1416
+ fullSchema[tableName] = self.injectLocalIdColumn(tableSchema);
1401
1417
  }
1402
1418
  const schemaColumns = Object.keys(fullSchema[tableName].columns ?? {}).join(", ");
1403
1419
  const schemaIndexes = (fullSchema[tableName].indexes ?? []).map((idx) => idx.columns.join("+")).join(", ");
@@ -1544,6 +1560,20 @@ var DyncBase = class {
1544
1560
  indexes: injectedIndexes
1545
1561
  };
1546
1562
  }
1563
+ injectLocalIdColumn(schema) {
1564
+ const columns = schema.columns ?? {};
1565
+ if (columns[LOCAL_PK]) {
1566
+ throw new Error(`Column '${LOCAL_PK}' is auto-injected and cannot be defined manually.`);
1567
+ }
1568
+ const injectedColumns = {
1569
+ ...columns,
1570
+ [LOCAL_PK]: { type: "TEXT" }
1571
+ };
1572
+ return {
1573
+ ...schema,
1574
+ columns: injectedColumns
1575
+ };
1576
+ }
1547
1577
  enhanceSyncTable(table, tableName) {
1548
1578
  enhanceSyncTable({
1549
1579
  table,