@use-stall/core 0.0.10 → 0.0.12

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.mts CHANGED
@@ -721,6 +721,7 @@ interface SyncQueueType {
721
721
  error?: string;
722
722
  retry_count: number;
723
723
  last_retry_at?: number;
724
+ priority: number;
724
725
  }
725
726
  interface SyncLogType {
726
727
  id: string;
@@ -819,6 +820,7 @@ declare const save_bulk_data: (props: {
819
820
  }) => Promise<void>;
820
821
  /**
821
822
  * Add a single item to the sync queue for later synchronization
823
+ * Automatically assigns priority: create=1, update=2, delete=3
822
824
  */
823
825
  declare const add_to_sync_queue: (props: {
824
826
  action: "create" | "update" | "delete";
@@ -828,7 +830,14 @@ declare const add_to_sync_queue: (props: {
828
830
  data: Record<string, any>;
829
831
  }) => Promise<SyncQueueType>;
830
832
  /**
831
- * Get pending sync queue items grouped by table for dependency-aware syncing
833
+ * Get pending sync queue items ordered by priority
834
+ * Returns items sorted by: priority (create first), then by timestamp
835
+ * Groups by table to maintain dependency order
836
+ *
837
+ * Execution order:
838
+ * 1. All CREATE operations per table (in timestamp order)
839
+ * 2. All UPDATE operations per table (in timestamp order)
840
+ * 3. All DELETE operations per table (in timestamp order)
832
841
  */
833
842
  declare const get_pending_sync_queue: () => Promise<Map<ConnectorModuleKey, SyncQueueType[]>>;
834
843
  /**
package/dist/index.d.ts CHANGED
@@ -721,6 +721,7 @@ interface SyncQueueType {
721
721
  error?: string;
722
722
  retry_count: number;
723
723
  last_retry_at?: number;
724
+ priority: number;
724
725
  }
725
726
  interface SyncLogType {
726
727
  id: string;
@@ -819,6 +820,7 @@ declare const save_bulk_data: (props: {
819
820
  }) => Promise<void>;
820
821
  /**
821
822
  * Add a single item to the sync queue for later synchronization
823
+ * Automatically assigns priority: create=1, update=2, delete=3
822
824
  */
823
825
  declare const add_to_sync_queue: (props: {
824
826
  action: "create" | "update" | "delete";
@@ -828,7 +830,14 @@ declare const add_to_sync_queue: (props: {
828
830
  data: Record<string, any>;
829
831
  }) => Promise<SyncQueueType>;
830
832
  /**
831
- * Get pending sync queue items grouped by table for dependency-aware syncing
833
+ * Get pending sync queue items ordered by priority
834
+ * Returns items sorted by: priority (create first), then by timestamp
835
+ * Groups by table to maintain dependency order
836
+ *
837
+ * Execution order:
838
+ * 1. All CREATE operations per table (in timestamp order)
839
+ * 2. All UPDATE operations per table (in timestamp order)
840
+ * 3. All DELETE operations per table (in timestamp order)
832
841
  */
833
842
  declare const get_pending_sync_queue: () => Promise<Map<ConnectorModuleKey, SyncQueueType[]>>;
834
843
  /**
package/dist/index.js CHANGED
@@ -68,7 +68,7 @@ var import_dexie = __toESM(require("dexie"));
68
68
  // src/db/schema.ts
69
69
  var schemas = {
70
70
  connector_cache: "id",
71
- sync_queue: "++id, status, timestamp",
71
+ sync_queue: "++id, status, timestamp, priority",
72
72
  sync_logs: "++id, sync_batch_id, timestamp",
73
73
  products: "++id",
74
74
  variants: "++id",
@@ -146,6 +146,18 @@ var generate_uuid = () => {
146
146
  var generate_offline_id = (table) => {
147
147
  return `stall_${table}_${generate_uuid()}_${Date.now()}`;
148
148
  };
149
+ var get_action_priority = (action) => {
150
+ switch (action) {
151
+ case "create":
152
+ return 1;
153
+ case "update":
154
+ return 2;
155
+ case "delete":
156
+ return 3;
157
+ default:
158
+ return 2;
159
+ }
160
+ };
149
161
  var save_bulk_data = async (props) => {
150
162
  await local_db.transaction("rw", props.table, async () => {
151
163
  await local_db?.[props.table].clear();
@@ -164,15 +176,25 @@ var add_to_sync_queue = async (props) => {
164
176
  data: props.data,
165
177
  timestamp: Date.now(),
166
178
  status: "pending",
167
- retry_count: 0
179
+ retry_count: 0,
180
+ priority: get_action_priority(props.action)
168
181
  };
169
182
  await local_db.sync_queue.add(queue_item);
170
183
  return queue_item;
171
184
  };
172
185
  var get_pending_sync_queue = async () => {
173
186
  const pending_items = await local_db.sync_queue.where("status").equals("pending").toArray();
187
+ const sorted_items = pending_items.sort((a, b) => {
188
+ if (a.table !== b.table) {
189
+ return a.table.localeCompare(b.table);
190
+ }
191
+ if (a.priority !== b.priority) {
192
+ return a.priority - b.priority;
193
+ }
194
+ return a.timestamp - b.timestamp;
195
+ });
174
196
  const grouped = /* @__PURE__ */ new Map();
175
- pending_items.forEach((item) => {
197
+ sorted_items.forEach((item) => {
176
198
  if (!grouped.has(item.table)) {
177
199
  grouped.set(item.table, []);
178
200
  }
@@ -3320,6 +3342,51 @@ var tax_rates = {
3320
3342
  };
3321
3343
 
3322
3344
  // src/services/locations.service.ts
3345
+ var build_location = (data, offline_id, now) => {
3346
+ return {
3347
+ id: offline_id,
3348
+ name: data.name || "",
3349
+ address: data.address || "",
3350
+ city: data.city || "",
3351
+ country: data.country || "",
3352
+ region: data.region || "",
3353
+ base_currency: data.base_currency || "",
3354
+ timezone: data.timezone || "",
3355
+ organization_id: data.organization_id || "",
3356
+ created_at: data.created_at || now,
3357
+ updated_at: data.updated_at || now,
3358
+ active: data.active ?? true,
3359
+ metadata: {
3360
+ ...data.metadata,
3361
+ stall_offline_id: offline_id,
3362
+ stall_offline_created_at: now,
3363
+ stall_offline_updated_at: now
3364
+ }
3365
+ };
3366
+ };
3367
+ var merge_location = (existing, updates, now) => {
3368
+ return {
3369
+ id: existing.id,
3370
+ name: updates.name ?? existing.name,
3371
+ address: updates.address ?? existing.address,
3372
+ city: updates.city ?? existing.city,
3373
+ country: updates.country ?? existing.country,
3374
+ region: updates.region ?? existing.region,
3375
+ base_currency: updates.base_currency ?? existing.base_currency,
3376
+ timezone: updates.timezone ?? existing.timezone,
3377
+ organization_id: updates.organization_id ?? existing.organization_id,
3378
+ created_at: existing.created_at,
3379
+ updated_at: now,
3380
+ active: updates.active ?? existing.active,
3381
+ metadata: {
3382
+ ...existing.metadata,
3383
+ ...updates.metadata,
3384
+ stall_offline_id: existing.metadata?.stall_offline_id,
3385
+ stall_offline_created_at: existing.metadata?.stall_offline_created_at,
3386
+ stall_offline_updated_at: now
3387
+ }
3388
+ };
3389
+ };
3323
3390
  var list15 = async (props) => {
3324
3391
  try {
3325
3392
  const { sdk, query } = props;
@@ -3358,18 +3425,7 @@ var create15 = async (props) => {
3358
3425
  const { sdk, data } = props;
3359
3426
  const offline_id = generate_offline_id("location");
3360
3427
  const now = (/* @__PURE__ */ new Date()).toISOString();
3361
- const local_location = {
3362
- ...data,
3363
- id: offline_id,
3364
- metadata: {
3365
- ...data.metadata,
3366
- stall_offline_id: offline_id
3367
- },
3368
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
3369
- stall_offline_id: offline_id,
3370
- stall_offline_created_at: now,
3371
- stall_offline_updated_at: now
3372
- };
3428
+ const local_location = build_location(data, offline_id, now);
3373
3429
  await local_db.locations.add(local_location);
3374
3430
  await add_to_sync_queue({
3375
3431
  action: "create",
@@ -3391,21 +3447,13 @@ var update15 = async (props) => {
3391
3447
  throw new Error(`Location with id ${id} not found locally`);
3392
3448
  }
3393
3449
  const now = (/* @__PURE__ */ new Date()).toISOString();
3394
- const updated_location = {
3395
- ...existing,
3396
- ...data,
3397
- id: existing.id,
3398
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
3399
- stall_offline_id: existing.stall_offline_id,
3400
- stall_offline_updated_at: now
3401
- };
3450
+ const updated_location = merge_location(existing, data, now);
3402
3451
  await local_db.locations.put(updated_location);
3403
3452
  await add_to_sync_queue({
3404
3453
  action: "update",
3405
3454
  table: "locations",
3406
3455
  document_id: id,
3407
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
3408
- stall_offline_id: existing.stall_offline_id,
3456
+ stall_offline_id: existing.metadata?.stall_offline_id || id,
3409
3457
  data: updated_location
3410
3458
  });
3411
3459
  return updated_location;
@@ -3424,8 +3472,7 @@ var _delete15 = async (props) => {
3424
3472
  action: "delete",
3425
3473
  table: "locations",
3426
3474
  document_id: id,
3427
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
3428
- stall_offline_id: existing.stall_offline_id,
3475
+ stall_offline_id: existing.metadata?.stall_offline_id || id,
3429
3476
  data: { id }
3430
3477
  });
3431
3478
  await local_db.locations.delete(id);
@@ -3441,18 +3488,7 @@ var bulk_create15 = async (props) => {
3441
3488
  const created_locations = [];
3442
3489
  for (const location of data) {
3443
3490
  const offline_id = generate_offline_id("location");
3444
- const local_location = {
3445
- ...location,
3446
- id: offline_id,
3447
- metadata: {
3448
- ...location.metadata,
3449
- stall_offline_id: offline_id
3450
- },
3451
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
3452
- stall_offline_id: offline_id,
3453
- stall_offline_created_at: now,
3454
- stall_offline_updated_at: now
3455
- };
3491
+ const local_location = build_location(location, offline_id, now);
3456
3492
  await local_db.locations.add(local_location);
3457
3493
  await add_to_sync_queue({
3458
3494
  action: "create",
@@ -3479,21 +3515,13 @@ var bulk_update15 = async (props) => {
3479
3515
  console.warn(`Location with id ${item.id} not found locally, skipping`);
3480
3516
  continue;
3481
3517
  }
3482
- const updated_location = {
3483
- ...existing,
3484
- ...item.data,
3485
- id: existing.id,
3486
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
3487
- stall_offline_id: existing.stall_offline_id,
3488
- stall_offline_updated_at: now
3489
- };
3518
+ const updated_location = merge_location(existing, item.data, now);
3490
3519
  await local_db.locations.put(updated_location);
3491
3520
  await add_to_sync_queue({
3492
3521
  action: "update",
3493
3522
  table: "locations",
3494
3523
  document_id: item.id,
3495
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
3496
- stall_offline_id: existing.stall_offline_id,
3524
+ stall_offline_id: existing.metadata?.stall_offline_id || item.id,
3497
3525
  data: updated_location
3498
3526
  });
3499
3527
  updated_locations.push(updated_location);
@@ -3516,8 +3544,7 @@ var bulk_delete15 = async (props) => {
3516
3544
  action: "delete",
3517
3545
  table: "locations",
3518
3546
  document_id: id,
3519
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
3520
- stall_offline_id: existing.stall_offline_id,
3547
+ stall_offline_id: existing.metadata?.stall_offline_id || id,
3521
3548
  data: { id }
3522
3549
  });
3523
3550
  await local_db.locations.delete(id);
package/dist/index.mjs CHANGED
@@ -4,7 +4,7 @@ import Dexie from "dexie";
4
4
  // src/db/schema.ts
5
5
  var schemas = {
6
6
  connector_cache: "id",
7
- sync_queue: "++id, status, timestamp",
7
+ sync_queue: "++id, status, timestamp, priority",
8
8
  sync_logs: "++id, sync_batch_id, timestamp",
9
9
  products: "++id",
10
10
  variants: "++id",
@@ -82,6 +82,18 @@ var generate_uuid = () => {
82
82
  var generate_offline_id = (table) => {
83
83
  return `stall_${table}_${generate_uuid()}_${Date.now()}`;
84
84
  };
85
+ var get_action_priority = (action) => {
86
+ switch (action) {
87
+ case "create":
88
+ return 1;
89
+ case "update":
90
+ return 2;
91
+ case "delete":
92
+ return 3;
93
+ default:
94
+ return 2;
95
+ }
96
+ };
85
97
  var save_bulk_data = async (props) => {
86
98
  await local_db.transaction("rw", props.table, async () => {
87
99
  await local_db?.[props.table].clear();
@@ -100,15 +112,25 @@ var add_to_sync_queue = async (props) => {
100
112
  data: props.data,
101
113
  timestamp: Date.now(),
102
114
  status: "pending",
103
- retry_count: 0
115
+ retry_count: 0,
116
+ priority: get_action_priority(props.action)
104
117
  };
105
118
  await local_db.sync_queue.add(queue_item);
106
119
  return queue_item;
107
120
  };
108
121
  var get_pending_sync_queue = async () => {
109
122
  const pending_items = await local_db.sync_queue.where("status").equals("pending").toArray();
123
+ const sorted_items = pending_items.sort((a, b) => {
124
+ if (a.table !== b.table) {
125
+ return a.table.localeCompare(b.table);
126
+ }
127
+ if (a.priority !== b.priority) {
128
+ return a.priority - b.priority;
129
+ }
130
+ return a.timestamp - b.timestamp;
131
+ });
110
132
  const grouped = /* @__PURE__ */ new Map();
111
- pending_items.forEach((item) => {
133
+ sorted_items.forEach((item) => {
112
134
  if (!grouped.has(item.table)) {
113
135
  grouped.set(item.table, []);
114
136
  }
@@ -3256,6 +3278,51 @@ var tax_rates = {
3256
3278
  };
3257
3279
 
3258
3280
  // src/services/locations.service.ts
3281
+ var build_location = (data, offline_id, now) => {
3282
+ return {
3283
+ id: offline_id,
3284
+ name: data.name || "",
3285
+ address: data.address || "",
3286
+ city: data.city || "",
3287
+ country: data.country || "",
3288
+ region: data.region || "",
3289
+ base_currency: data.base_currency || "",
3290
+ timezone: data.timezone || "",
3291
+ organization_id: data.organization_id || "",
3292
+ created_at: data.created_at || now,
3293
+ updated_at: data.updated_at || now,
3294
+ active: data.active ?? true,
3295
+ metadata: {
3296
+ ...data.metadata,
3297
+ stall_offline_id: offline_id,
3298
+ stall_offline_created_at: now,
3299
+ stall_offline_updated_at: now
3300
+ }
3301
+ };
3302
+ };
3303
+ var merge_location = (existing, updates, now) => {
3304
+ return {
3305
+ id: existing.id,
3306
+ name: updates.name ?? existing.name,
3307
+ address: updates.address ?? existing.address,
3308
+ city: updates.city ?? existing.city,
3309
+ country: updates.country ?? existing.country,
3310
+ region: updates.region ?? existing.region,
3311
+ base_currency: updates.base_currency ?? existing.base_currency,
3312
+ timezone: updates.timezone ?? existing.timezone,
3313
+ organization_id: updates.organization_id ?? existing.organization_id,
3314
+ created_at: existing.created_at,
3315
+ updated_at: now,
3316
+ active: updates.active ?? existing.active,
3317
+ metadata: {
3318
+ ...existing.metadata,
3319
+ ...updates.metadata,
3320
+ stall_offline_id: existing.metadata?.stall_offline_id,
3321
+ stall_offline_created_at: existing.metadata?.stall_offline_created_at,
3322
+ stall_offline_updated_at: now
3323
+ }
3324
+ };
3325
+ };
3259
3326
  var list15 = async (props) => {
3260
3327
  try {
3261
3328
  const { sdk, query } = props;
@@ -3294,18 +3361,7 @@ var create15 = async (props) => {
3294
3361
  const { sdk, data } = props;
3295
3362
  const offline_id = generate_offline_id("location");
3296
3363
  const now = (/* @__PURE__ */ new Date()).toISOString();
3297
- const local_location = {
3298
- ...data,
3299
- id: offline_id,
3300
- metadata: {
3301
- ...data.metadata,
3302
- stall_offline_id: offline_id
3303
- },
3304
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
3305
- stall_offline_id: offline_id,
3306
- stall_offline_created_at: now,
3307
- stall_offline_updated_at: now
3308
- };
3364
+ const local_location = build_location(data, offline_id, now);
3309
3365
  await local_db.locations.add(local_location);
3310
3366
  await add_to_sync_queue({
3311
3367
  action: "create",
@@ -3327,21 +3383,13 @@ var update15 = async (props) => {
3327
3383
  throw new Error(`Location with id ${id} not found locally`);
3328
3384
  }
3329
3385
  const now = (/* @__PURE__ */ new Date()).toISOString();
3330
- const updated_location = {
3331
- ...existing,
3332
- ...data,
3333
- id: existing.id,
3334
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
3335
- stall_offline_id: existing.stall_offline_id,
3336
- stall_offline_updated_at: now
3337
- };
3386
+ const updated_location = merge_location(existing, data, now);
3338
3387
  await local_db.locations.put(updated_location);
3339
3388
  await add_to_sync_queue({
3340
3389
  action: "update",
3341
3390
  table: "locations",
3342
3391
  document_id: id,
3343
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
3344
- stall_offline_id: existing.stall_offline_id,
3392
+ stall_offline_id: existing.metadata?.stall_offline_id || id,
3345
3393
  data: updated_location
3346
3394
  });
3347
3395
  return updated_location;
@@ -3360,8 +3408,7 @@ var _delete15 = async (props) => {
3360
3408
  action: "delete",
3361
3409
  table: "locations",
3362
3410
  document_id: id,
3363
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
3364
- stall_offline_id: existing.stall_offline_id,
3411
+ stall_offline_id: existing.metadata?.stall_offline_id || id,
3365
3412
  data: { id }
3366
3413
  });
3367
3414
  await local_db.locations.delete(id);
@@ -3377,18 +3424,7 @@ var bulk_create15 = async (props) => {
3377
3424
  const created_locations = [];
3378
3425
  for (const location of data) {
3379
3426
  const offline_id = generate_offline_id("location");
3380
- const local_location = {
3381
- ...location,
3382
- id: offline_id,
3383
- metadata: {
3384
- ...location.metadata,
3385
- stall_offline_id: offline_id
3386
- },
3387
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
3388
- stall_offline_id: offline_id,
3389
- stall_offline_created_at: now,
3390
- stall_offline_updated_at: now
3391
- };
3427
+ const local_location = build_location(location, offline_id, now);
3392
3428
  await local_db.locations.add(local_location);
3393
3429
  await add_to_sync_queue({
3394
3430
  action: "create",
@@ -3415,21 +3451,13 @@ var bulk_update15 = async (props) => {
3415
3451
  console.warn(`Location with id ${item.id} not found locally, skipping`);
3416
3452
  continue;
3417
3453
  }
3418
- const updated_location = {
3419
- ...existing,
3420
- ...item.data,
3421
- id: existing.id,
3422
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
3423
- stall_offline_id: existing.stall_offline_id,
3424
- stall_offline_updated_at: now
3425
- };
3454
+ const updated_location = merge_location(existing, item.data, now);
3426
3455
  await local_db.locations.put(updated_location);
3427
3456
  await add_to_sync_queue({
3428
3457
  action: "update",
3429
3458
  table: "locations",
3430
3459
  document_id: item.id,
3431
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
3432
- stall_offline_id: existing.stall_offline_id,
3460
+ stall_offline_id: existing.metadata?.stall_offline_id || item.id,
3433
3461
  data: updated_location
3434
3462
  });
3435
3463
  updated_locations.push(updated_location);
@@ -3452,8 +3480,7 @@ var bulk_delete15 = async (props) => {
3452
3480
  action: "delete",
3453
3481
  table: "locations",
3454
3482
  document_id: id,
3455
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
3456
- stall_offline_id: existing.stall_offline_id,
3483
+ stall_offline_id: existing.metadata?.stall_offline_id || id,
3457
3484
  data: { id }
3458
3485
  });
3459
3486
  await local_db.locations.delete(id);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@use-stall/core",
3
- "version": "0.0.10",
3
+ "version": "0.0.12",
4
4
  "author": "Stall",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",