@stonyx/orm 0.3.2-alpha.14 → 0.3.2-alpha.16

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;
@@ -105,13 +105,22 @@ export function createRecord(modelName, rawData = {}, userOptions = {}) {
105
105
  const shouldPersist = orm?.sqlDb && !options.isDbRecord && !userOptions._relationshipKey && !options._skipAutoPersist;
106
106
  if (shouldPersist) {
107
107
  const response = { data: { id: record.id } };
108
- orm.sqlDb.persist('create', modelName, { rawData }, response).catch((err) => {
108
+ orm.sqlDb.persist('create', modelName, { rawData }, response)
109
+ .catch((err) => {
109
110
  orm.emitPersistError({
110
111
  operation: 'create',
111
112
  modelName,
112
113
  recordId: record.id,
113
114
  error: err instanceof Error ? err : new Error(String(err)),
114
115
  });
116
+ })
117
+ .finally(() => {
118
+ // Evict non-memory records after persist to prevent unbounded heap growth (stonyx#81)
119
+ if (store._memoryResolver && !store._memoryResolver(modelName)) {
120
+ const ms = store.get(modelName);
121
+ if (ms)
122
+ ms.delete(record.id);
123
+ }
115
124
  });
116
125
  }
117
126
  return record;
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "stonyx-async",
5
5
  "stonyx-module"
6
6
  ],
7
- "version": "0.3.2-alpha.14",
7
+ "version": "0.3.2-alpha.16",
8
8
  "description": "",
9
9
  "main": "dist/index.js",
10
10
  "type": "module",
@@ -61,9 +61,9 @@
61
61
  },
62
62
  "homepage": "https://github.com/abofs/stonyx-orm#readme",
63
63
  "dependencies": {
64
- "@stonyx/cron": "0.2.1-beta.66",
65
- "@stonyx/events": "0.1.1-beta.49",
66
- "stonyx": "0.2.3-beta.66"
64
+ "@stonyx/cron": "0.2.1-beta.70",
65
+ "@stonyx/events": "0.1.1-beta.51",
66
+ "stonyx": "0.2.3-beta.68"
67
67
  },
68
68
  "peerDependencies": {
69
69
  "@aws-sdk/client-dynamodb": "^3.0.0",
@@ -90,8 +90,8 @@
90
90
  }
91
91
  },
92
92
  "devDependencies": {
93
- "@stonyx/rest-server": "0.2.1-beta.67",
94
- "@stonyx/utils": "0.2.3-beta.24",
93
+ "@stonyx/rest-server": "0.2.1-beta.71",
94
+ "@stonyx/utils": "0.2.3-beta.25",
95
95
  "@types/node": "^25.6.0",
96
96
  "mysql2": "^3.20.0",
97
97
  "pg": "^8.20.0",
@@ -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);
@@ -140,14 +140,22 @@ export function createRecord(modelName: string, rawData: { [key: string]: unknow
140
140
  const shouldPersist = orm?.sqlDb && !options.isDbRecord && !userOptions._relationshipKey && !options._skipAutoPersist;
141
141
  if (shouldPersist) {
142
142
  const response = { data: { id: record.id } };
143
- orm!.sqlDb!.persist('create', modelName, { rawData }, response).catch((err: unknown) => {
144
- orm!.emitPersistError({
145
- operation: 'create',
146
- modelName,
147
- recordId: record.id,
148
- error: err instanceof Error ? err : new Error(String(err)),
143
+ orm!.sqlDb!.persist('create', modelName, { rawData }, response)
144
+ .catch((err: unknown) => {
145
+ orm!.emitPersistError({
146
+ operation: 'create',
147
+ modelName,
148
+ recordId: record.id,
149
+ error: err instanceof Error ? err : new Error(String(err)),
150
+ });
151
+ })
152
+ .finally(() => {
153
+ // Evict non-memory records after persist to prevent unbounded heap growth (stonyx#81)
154
+ if (store._memoryResolver && !store._memoryResolver(modelName)) {
155
+ const ms = store.get(modelName);
156
+ if (ms) ms.delete(record.id as number | string);
157
+ }
149
158
  });
150
- });
151
159
  }
152
160
 
153
161
  return record;