@stonyx/orm 0.3.2-beta.75 → 0.3.2-beta.76

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.
@@ -33,6 +33,23 @@ function generateUlid() {
33
33
  }
34
34
  return id;
35
35
  }
36
+ /**
37
+ * Generates a monotonically unique numeric ID for DynamoDB tables with numeric keys.
38
+ * Uses timestamp-based generation with a sub-millisecond counter to ensure uniqueness.
39
+ */
40
+ let _numericIdCounter = 0;
41
+ let _numericIdLastMs = 0;
42
+ function generateNumericId() {
43
+ const now = Date.now();
44
+ if (now === _numericIdLastMs) {
45
+ _numericIdCounter++;
46
+ }
47
+ else {
48
+ _numericIdLastMs = now;
49
+ _numericIdCounter = 0;
50
+ }
51
+ return now * 1000 + _numericIdCounter;
52
+ }
36
53
  // ---------------------------------------------------------------------------
37
54
  // SDK Command factories (injectable for testing without real AWS SDK)
38
55
  // ---------------------------------------------------------------------------
@@ -309,10 +326,11 @@ export default class DynamoDBDB {
309
326
  return;
310
327
  const isPendingId = context.rawData?.__pendingSqlId === true;
311
328
  const tableName = sanitizeTableName(this.deps.getPluralName(modelName));
312
- // For numeric-ID models with a pending ID, generate a ULID
329
+ // For models with a pending ID, generate a unique replacement ID
313
330
  let finalId = record.id;
314
331
  if (isPendingId) {
315
- finalId = generateUlid();
332
+ const keyType = this.deps.getDynamoKeyType(schema.idType);
333
+ finalId = keyType === 'N' ? generateNumericId() : generateUlid();
316
334
  }
317
335
  const item = this._recordToItem(record, schema, context.rawData);
318
336
  item.id = finalId;
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "stonyx-async",
5
5
  "stonyx-module"
6
6
  ],
7
- "version": "0.3.2-beta.75",
7
+ "version": "0.3.2-beta.76",
8
8
  "description": "",
9
9
  "main": "dist/index.js",
10
10
  "type": "module",
@@ -50,6 +50,24 @@ function generateUlid(): string {
50
50
  return id;
51
51
  }
52
52
 
53
+ /**
54
+ * Generates a monotonically unique numeric ID for DynamoDB tables with numeric keys.
55
+ * Uses timestamp-based generation with a sub-millisecond counter to ensure uniqueness.
56
+ */
57
+ let _numericIdCounter = 0;
58
+ let _numericIdLastMs = 0;
59
+
60
+ function generateNumericId(): number {
61
+ const now = Date.now();
62
+ if (now === _numericIdLastMs) {
63
+ _numericIdCounter++;
64
+ } else {
65
+ _numericIdLastMs = now;
66
+ _numericIdCounter = 0;
67
+ }
68
+ return now * 1000 + _numericIdCounter;
69
+ }
70
+
53
71
  // ---------------------------------------------------------------------------
54
72
  // SDK Command factories (injectable for testing without real AWS SDK)
55
73
  // ---------------------------------------------------------------------------
@@ -459,10 +477,11 @@ export default class DynamoDBDB {
459
477
  const isPendingId = context.rawData?.__pendingSqlId === true;
460
478
  const tableName = sanitizeTableName(this.deps.getPluralName(modelName));
461
479
 
462
- // For numeric-ID models with a pending ID, generate a ULID
480
+ // For models with a pending ID, generate a unique replacement ID
463
481
  let finalId: unknown = record.id;
464
482
  if (isPendingId) {
465
- finalId = generateUlid();
483
+ const keyType = this.deps.getDynamoKeyType(schema.idType);
484
+ finalId = keyType === 'N' ? generateNumericId() : generateUlid();
466
485
  }
467
486
 
468
487
  const item = this._recordToItem(record, schema, context.rawData);