@anfenn/dync 1.0.26 → 1.0.27

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/README.md CHANGED
@@ -48,9 +48,9 @@ And see how Dync compares to the alternatives [below](#hasnt-this-already-been-d
48
48
  - Option 1: Map remote api CRUD urls to a local collection:
49
49
 
50
50
  ```ts
51
- const db = new Dync(
51
+ const db = new Dync({
52
52
  ...,
53
- {
53
+ sync: {
54
54
  // Only add an entry here for tables that should be synced
55
55
  // Pseudocode here, see examples for working code
56
56
  items: {
@@ -60,16 +60,15 @@ And see how Dync compares to the alternatives [below](#hasnt-this-already-been-d
60
60
  list: (since) => fetch(`/api/items?since=${since}`),
61
61
  },
62
62
  },
63
- ...,
64
- );
63
+ });
65
64
  ```
66
65
 
67
66
  - Option 2: Batch sync to remote /push & /pull endpoints:
68
67
 
69
68
  ```ts
70
- const db = new Dync(
69
+ const db = new Dync({
71
70
  ...,
72
- {
71
+ sync: {
73
72
  syncTables: ['items'], // Only add tables to this array that should be synced
74
73
  push: async (changes) => {
75
74
  const res = await fetch('/api/sync/push', {
@@ -80,15 +79,12 @@ And see how Dync compares to the alternatives [below](#hasnt-this-already-been-d
80
79
  return res.json();
81
80
  },
82
81
  pull: async (since) => {
83
- const params = new URLSearchParams(
84
- Object.entries(since).map(([table, date]) => [table, date.toISOString()])
85
- );
82
+ const params = new URLSearchParams(Object.entries(since).map(([table, date]) => [table, date.toISOString()]));
86
83
  const res = await fetch(`/api/sync/pull?${params}`);
87
84
  return res.json();
88
85
  },
89
86
  },
90
- ...,
91
- );
87
+ });
92
88
  ```
93
89
 
94
90
  See [examples/shared/api.ts](examples/shared/api.ts) for a fully documented example of these two options.
@@ -440,4 +440,4 @@ declare class DexieAdapter implements StorageAdapter {
440
440
  transaction<T>(mode: TransactionMode, tableNames: string[], callback: (context: StorageTransactionContext) => Promise<T>): Promise<T>;
441
441
  }
442
442
 
443
- export { DexieQueryContext as D, MemoryQueryContext as M, type StorageAdapter as S, type TableSchemaDefinition as T, SqliteQueryContext as a, type StorageTable as b, MemoryAdapter as c, SQLiteAdapter as d, DexieAdapter as e };
443
+ export { DexieQueryContext as D, MemoryQueryContext as M, SqliteQueryContext as S, type TableSchemaDefinition as T, type StorageTable as a, MemoryAdapter as b, SQLiteAdapter as c, type StorageAdapter as d, DexieAdapter as e };
@@ -440,4 +440,4 @@ declare class DexieAdapter implements StorageAdapter {
440
440
  transaction<T>(mode: TransactionMode, tableNames: string[], callback: (context: StorageTransactionContext) => Promise<T>): Promise<T>;
441
441
  }
442
442
 
443
- export { DexieQueryContext as D, MemoryQueryContext as M, type StorageAdapter as S, type TableSchemaDefinition as T, SqliteQueryContext as a, type StorageTable as b, MemoryAdapter as c, SQLiteAdapter as d, DexieAdapter as e };
443
+ export { DexieQueryContext as D, MemoryQueryContext as M, SqliteQueryContext as S, type TableSchemaDefinition as T, type StorageTable as a, MemoryAdapter as b, SQLiteAdapter as c, type StorageAdapter as d, DexieAdapter as e };
package/dist/dexie.d.cts CHANGED
@@ -1,3 +1,3 @@
1
- export { e as DexieAdapter, D as DexieQueryContext, S as StorageAdapter } from './dexie-DtgGGM68.cjs';
1
+ export { e as DexieAdapter, D as DexieQueryContext, d as StorageAdapter } from './dexie-T9m1mP1h.cjs';
2
2
  import 'dexie';
3
3
  import './types-CSbIAfu2.cjs';
package/dist/dexie.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- export { e as DexieAdapter, D as DexieQueryContext, S as StorageAdapter } from './dexie-B1FuZqDB.js';
1
+ export { e as DexieAdapter, D as DexieQueryContext, d as StorageAdapter } from './dexie-BFPA0JU2.js';
2
2
  import 'dexie';
3
3
  import './types-CSbIAfu2.js';
package/dist/index.cjs CHANGED
@@ -1317,13 +1317,35 @@ var DyncBase = class {
1317
1317
  mutationsDuringSync = false;
1318
1318
  state;
1319
1319
  name;
1320
- constructor(databaseName, syncApisOrBatchSync, storageAdapter, options) {
1321
- const isBatchMode = typeof syncApisOrBatchSync.push === "function";
1320
+ /**
1321
+ * Create a new Dync instance.
1322
+ *
1323
+ * @example Per-table sync mode
1324
+ * ```ts
1325
+ * const db = new Dync<Store>({
1326
+ * databaseName: 'my-app',
1327
+ * storageAdapter: new SQLiteAdapter(driver),
1328
+ * sync: { todos: todoSyncApi },
1329
+ * });
1330
+ * ```
1331
+ *
1332
+ * @example Batch sync mode
1333
+ * ```ts
1334
+ * const db = new Dync<Store>({
1335
+ * databaseName: 'my-app',
1336
+ * storageAdapter: new SQLiteAdapter(driver),
1337
+ * sync: { syncTables: ['todos'], push, pull },
1338
+ * });
1339
+ * ```
1340
+ */
1341
+ constructor(config) {
1342
+ const { databaseName, storageAdapter, sync: syncConfig, options } = config;
1343
+ const isBatchMode = typeof syncConfig.push === "function" && typeof syncConfig.pull === "function";
1322
1344
  if (isBatchMode) {
1323
- this.batchSync = syncApisOrBatchSync;
1345
+ this.batchSync = syncConfig;
1324
1346
  this.syncedTables = new Set(this.batchSync.syncTables);
1325
1347
  } else {
1326
- this.syncApis = syncApisOrBatchSync;
1348
+ this.syncApis = syncConfig;
1327
1349
  this.syncedTables = new Set(Object.keys(this.syncApis));
1328
1350
  }
1329
1351
  this.adapter = storageAdapter;
@@ -1387,6 +1409,17 @@ var DyncBase = class {
1387
1409
  storesDefined = true;
1388
1410
  self.adapter.defineSchema(versionNumber, fullSchema, schemaOptions);
1389
1411
  self.setupEnhancedTables(Object.keys(schema));
1412
+ for (const tableName of Object.keys(schema)) {
1413
+ if (!(tableName in self)) {
1414
+ Object.defineProperty(self, tableName, {
1415
+ get() {
1416
+ return self.table(tableName);
1417
+ },
1418
+ enumerable: true,
1419
+ configurable: false
1420
+ });
1421
+ }
1422
+ }
1390
1423
  return builder;
1391
1424
  },
1392
1425
  sqlite(configure) {
@@ -1725,8 +1758,7 @@ var DyncBase = class {
1725
1758
  onMutation: this.onMutation.bind(this)
1726
1759
  };
1727
1760
  };
1728
- var DyncConstructor = DyncBase;
1729
- var Dync = DyncConstructor;
1761
+ var Dync = DyncBase;
1730
1762
 
1731
1763
  // src/storage/memory/MemoryQueryContext.ts
1732
1764
  var MemoryQueryContext = class {