@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.d.cts CHANGED
@@ -1,5 +1,5 @@
1
- import { D as DyncOptions, S as SyncStatus, a as SyncApi, T as TableMap } from './types-9I2fmDbU.cjs';
2
- export { A as AfterRemoteAddCallback, c as ApiFunctions, f as BatchFirstLoadResult, d as BatchPushPayload, e as BatchPushResult, B as BatchSync, C as ConflictResolutionStrategy, F as FirstLoadProgress, g as FirstLoadProgressCallback, h as MissingRemoteRecordDuringUpdateCallback, M as MissingRemoteRecordStrategy, i as MutationEvent, b as SyncAction, j as SyncOptions, k as SyncState, l as SyncedRecord } from './types-9I2fmDbU.cjs';
1
+ import { D as DyncOptions, S as SyncStatus, a as SyncApi, T as TableMap } from './types-DW42y281.cjs';
2
+ export { A as AfterRemoteAddCallback, c as ApiFunctions, f as BatchFirstLoadResult, d as BatchPushPayload, e as BatchPushResult, B as BatchSync, C as ConflictResolutionStrategy, F as FirstLoadProgress, g as FirstLoadProgressCallback, h as MissingRemoteRecordDuringUpdateCallback, M as MissingRemoteRecordStrategy, i as MutationEvent, b as SyncAction, j as SyncOptions, k as SyncState, l as SyncedRecord } from './types-DW42y281.cjs';
3
3
  import { T as TableSchemaDefinition, D as DexieQueryContext, S as SqliteQueryContext, M as MemoryQueryContext, a as StorageTable } from './dexie-T9m1mP1h.cjs';
4
4
  export { b as MemoryAdapter, c as SQLiteAdapter, d as StorageAdapter } from './dexie-T9m1mP1h.cjs';
5
5
  import { c as SQLiteVersionConfigurator } from './types-CSbIAfu2.cjs';
@@ -62,6 +62,7 @@ declare class DyncBase<_TStoreMap extends Record<string, any> = Record<string, a
62
62
  private withTransaction;
63
63
  private setupEnhancedTables;
64
64
  private injectSyncColumns;
65
+ private injectLocalIdColumn;
65
66
  private enhanceSyncTable;
66
67
  private syncOnce;
67
68
  private pullAll;
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { D as DyncOptions, S as SyncStatus, a as SyncApi, T as TableMap } from './types-DLFva7gq.js';
2
- export { A as AfterRemoteAddCallback, c as ApiFunctions, f as BatchFirstLoadResult, d as BatchPushPayload, e as BatchPushResult, B as BatchSync, C as ConflictResolutionStrategy, F as FirstLoadProgress, g as FirstLoadProgressCallback, h as MissingRemoteRecordDuringUpdateCallback, M as MissingRemoteRecordStrategy, i as MutationEvent, b as SyncAction, j as SyncOptions, k as SyncState, l as SyncedRecord } from './types-DLFva7gq.js';
1
+ import { D as DyncOptions, S as SyncStatus, a as SyncApi, T as TableMap } from './types-n8Zge2zF.js';
2
+ export { A as AfterRemoteAddCallback, c as ApiFunctions, f as BatchFirstLoadResult, d as BatchPushPayload, e as BatchPushResult, B as BatchSync, C as ConflictResolutionStrategy, F as FirstLoadProgress, g as FirstLoadProgressCallback, h as MissingRemoteRecordDuringUpdateCallback, M as MissingRemoteRecordStrategy, i as MutationEvent, b as SyncAction, j as SyncOptions, k as SyncState, l as SyncedRecord } from './types-n8Zge2zF.js';
3
3
  import { T as TableSchemaDefinition, D as DexieQueryContext, S as SqliteQueryContext, M as MemoryQueryContext, a as StorageTable } from './dexie-BFPA0JU2.js';
4
4
  export { b as MemoryAdapter, c as SQLiteAdapter, d as StorageAdapter } from './dexie-BFPA0JU2.js';
5
5
  import { c as SQLiteVersionConfigurator } from './types-CSbIAfu2.js';
@@ -62,6 +62,7 @@ declare class DyncBase<_TStoreMap extends Record<string, any> = Record<string, a
62
62
  private withTransaction;
63
63
  private setupEnhancedTables;
64
64
  private injectSyncColumns;
65
+ private injectLocalIdColumn;
65
66
  private enhanceSyncTable;
66
67
  private syncOnce;
67
68
  private pullAll;
package/dist/index.js CHANGED
@@ -341,12 +341,14 @@ function wrapWithMutationEmitter(table, tableName, emitMutation) {
341
341
  const rawBulkDelete = table.raw.bulkDelete;
342
342
  const rawClear = table.raw.clear;
343
343
  table.add = async (item) => {
344
- const result = await rawAdd(item);
344
+ const itemWithLocalId = { ...item, _localId: item._localId || createLocalId() };
345
+ const result = await rawAdd(itemWithLocalId);
345
346
  emitMutation({ type: "add", tableName, keys: [result] });
346
347
  return result;
347
348
  };
348
349
  table.put = async (item) => {
349
- const result = await rawPut(item);
350
+ const itemWithLocalId = { ...item, _localId: item._localId || createLocalId() };
351
+ const result = await rawPut(itemWithLocalId);
350
352
  emitMutation({ type: "update", tableName, keys: [result] });
351
353
  return result;
352
354
  };
@@ -362,14 +364,22 @@ function wrapWithMutationEmitter(table, tableName, emitMutation) {
362
364
  emitMutation({ type: "delete", tableName, keys: [key] });
363
365
  };
364
366
  table.bulkAdd = async (items) => {
365
- const result = await rawBulkAdd(items);
367
+ const itemsWithLocalIds = items.map((item) => ({
368
+ ...item,
369
+ _localId: item._localId || createLocalId()
370
+ }));
371
+ const result = await rawBulkAdd(itemsWithLocalIds);
366
372
  if (items.length > 0) {
367
373
  emitMutation({ type: "add", tableName });
368
374
  }
369
375
  return result;
370
376
  };
371
377
  table.bulkPut = async (items) => {
372
- const result = await rawBulkPut(items);
378
+ const itemsWithLocalIds = items.map((item) => ({
379
+ ...item,
380
+ _localId: item._localId || createLocalId()
381
+ }));
382
+ const result = await rawBulkPut(itemsWithLocalIds);
373
383
  if (items.length > 0) {
374
384
  emitMutation({ type: "update", tableName });
375
385
  }
@@ -1310,13 +1320,15 @@ var DyncBase = class {
1310
1320
  */
1311
1321
  constructor(config) {
1312
1322
  const { databaseName, storageAdapter, sync: syncConfig, options } = config;
1313
- const isBatchMode = typeof syncConfig.push === "function" && typeof syncConfig.pull === "function";
1314
- if (isBatchMode) {
1315
- this.batchSync = syncConfig;
1316
- this.syncedTables = new Set(this.batchSync.syncTables);
1317
- } else {
1318
- this.syncApis = syncConfig;
1319
- this.syncedTables = new Set(Object.keys(this.syncApis));
1323
+ if (syncConfig) {
1324
+ const isBatchMode = typeof syncConfig.push === "function" && typeof syncConfig.pull === "function";
1325
+ if (isBatchMode) {
1326
+ this.batchSync = syncConfig;
1327
+ this.syncedTables = new Set(this.batchSync.syncTables);
1328
+ } else {
1329
+ this.syncApis = syncConfig;
1330
+ this.syncedTables = new Set(Object.keys(this.syncApis));
1331
+ }
1320
1332
  }
1321
1333
  this.adapter = storageAdapter;
1322
1334
  this.name = databaseName;
@@ -1361,6 +1373,8 @@ var DyncBase = class {
1361
1373
  if (typeof tableSchema === "string") {
1362
1374
  if (isSyncTable) {
1363
1375
  fullSchema[tableName] = `${LOCAL_PK}, &${SERVER_PK}, ${tableSchema}, ${UPDATED_AT}`;
1376
+ } else {
1377
+ fullSchema[tableName] = `${LOCAL_PK}, ${tableSchema}`;
1364
1378
  }
1365
1379
  self.logger.debug(
1366
1380
  `[dync] Defining ${isSyncTable ? "" : "non-"}sync table '${tableName}' with primary key & indexes '${fullSchema[tableName]}'`
@@ -1368,6 +1382,8 @@ var DyncBase = class {
1368
1382
  } else {
1369
1383
  if (isSyncTable) {
1370
1384
  fullSchema[tableName] = self.injectSyncColumns(tableSchema);
1385
+ } else {
1386
+ fullSchema[tableName] = self.injectLocalIdColumn(tableSchema);
1371
1387
  }
1372
1388
  const schemaColumns = Object.keys(fullSchema[tableName].columns ?? {}).join(", ");
1373
1389
  const schemaIndexes = (fullSchema[tableName].indexes ?? []).map((idx) => idx.columns.join("+")).join(", ");
@@ -1514,6 +1530,20 @@ var DyncBase = class {
1514
1530
  indexes: injectedIndexes
1515
1531
  };
1516
1532
  }
1533
+ injectLocalIdColumn(schema) {
1534
+ const columns = schema.columns ?? {};
1535
+ if (columns[LOCAL_PK]) {
1536
+ throw new Error(`Column '${LOCAL_PK}' is auto-injected and cannot be defined manually.`);
1537
+ }
1538
+ const injectedColumns = {
1539
+ ...columns,
1540
+ [LOCAL_PK]: { type: "TEXT" }
1541
+ };
1542
+ return {
1543
+ ...schema,
1544
+ columns: injectedColumns
1545
+ };
1546
+ }
1517
1547
  enhanceSyncTable(table, tableName) {
1518
1548
  enhanceSyncTable({
1519
1549
  table,