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

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;
@@ -117,9 +117,7 @@ export function createRecord(modelName, rawData = {}, userOptions = {}) {
117
117
  .finally(() => {
118
118
  // Evict non-memory records after persist to prevent unbounded heap growth (stonyx#81)
119
119
  if (store._memoryResolver && !store._memoryResolver(modelName)) {
120
- const ms = store.get(modelName);
121
- if (ms)
122
- ms.delete(record.id);
120
+ store.evictRecord(modelName, record.id);
123
121
  }
124
122
  });
125
123
  }
package/dist/store.d.ts CHANGED
@@ -46,6 +46,12 @@ export default class Store {
46
46
  private _isMemoryModel;
47
47
  set(key: string, value: Map<number | string, unknown>): void;
48
48
  remove(key: string, id?: number | string): void;
49
+ /**
50
+ * Evict a record from the store with full relationship registry cleanup,
51
+ * WITHOUT calling record.clean(). This preserves the caller's reference
52
+ * to the returned record (used by memory:false post-persist eviction).
53
+ */
54
+ evictRecord(modelName: string, id: unknown): void;
49
55
  unloadRecord(model: string, id: unknown, options?: UnloadOptions): void;
50
56
  unloadAllRecords(model: string, options?: UnloadOptions): void;
51
57
  private _removeFromHasManyArrays;
package/dist/store.js CHANGED
@@ -127,6 +127,26 @@ export default class Store {
127
127
  return this.unloadRecord(key, id);
128
128
  this.unloadAllRecords(key);
129
129
  }
130
+ /**
131
+ * Evict a record from the store with full relationship registry cleanup,
132
+ * WITHOUT calling record.clean(). This preserves the caller's reference
133
+ * to the returned record (used by memory:false post-persist eviction).
134
+ */
135
+ evictRecord(modelName, id) {
136
+ const modelStore = this.data.get(modelName);
137
+ if (!modelStore)
138
+ return;
139
+ if (typeof id !== 'string' && typeof id !== 'number')
140
+ return;
141
+ const raw = modelStore.get(id);
142
+ if (!raw || !isStoreRecord(raw))
143
+ return;
144
+ const visited = new Set([`${modelName}:${id}`]);
145
+ this._removeFromHasManyArrays(modelName, id, visited);
146
+ this._nullifyBelongsToReferences(modelName, id, visited);
147
+ this._cleanupRelationshipRegistries(modelName, id);
148
+ modelStore.delete(id);
149
+ }
130
150
  unloadRecord(model, id, options = {}) {
131
151
  const modelStore = this.data.get(model);
132
152
  if (!modelStore) {
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.77",
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);
@@ -152,8 +152,7 @@ export function createRecord(modelName: string, rawData: { [key: string]: unknow
152
152
  .finally(() => {
153
153
  // Evict non-memory records after persist to prevent unbounded heap growth (stonyx#81)
154
154
  if (store._memoryResolver && !store._memoryResolver(modelName)) {
155
- const ms = store.get(modelName);
156
- if (ms) ms.delete(record.id as number | string);
155
+ store.evictRecord(modelName, record.id);
157
156
  }
158
157
  });
159
158
  }
package/src/store.ts CHANGED
@@ -193,6 +193,27 @@ export default class Store {
193
193
  this.unloadAllRecords(key);
194
194
  }
195
195
 
196
+ /**
197
+ * Evict a record from the store with full relationship registry cleanup,
198
+ * WITHOUT calling record.clean(). This preserves the caller's reference
199
+ * to the returned record (used by memory:false post-persist eviction).
200
+ */
201
+ evictRecord(modelName: string, id: unknown): void {
202
+ const modelStore = this.data.get(modelName);
203
+ if (!modelStore) return;
204
+
205
+ if (typeof id !== 'string' && typeof id !== 'number') return;
206
+ const raw = modelStore.get(id);
207
+ if (!raw || !isStoreRecord(raw)) return;
208
+
209
+ const visited = new Set([`${modelName}:${id}`]);
210
+ this._removeFromHasManyArrays(modelName, id, visited);
211
+ this._nullifyBelongsToReferences(modelName, id, visited);
212
+ this._cleanupRelationshipRegistries(modelName, id);
213
+
214
+ modelStore.delete(id);
215
+ }
216
+
196
217
  unloadRecord(model: string, id: unknown, options: UnloadOptions = {}): void {
197
218
  const modelStore = this.data.get(model);
198
219