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

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.
@@ -104,6 +104,9 @@ export function createRecord(modelName, rawData = {}, userOptions = {}) {
104
104
  // Auto-persist to SQL — skip for DB loads (isDbRecord) and relationship resolution (_relationshipKey)
105
105
  const shouldPersist = orm?.sqlDb && !options.isDbRecord && !userOptions._relationshipKey && !options._skipAutoPersist;
106
106
  if (shouldPersist) {
107
+ // Capture ID before persist — SQL adapters re-key pending IDs to real DB IDs,
108
+ // but relationship registries were keyed with this original ID
109
+ const registryId = record.id;
107
110
  const response = { data: { id: record.id } };
108
111
  orm.sqlDb.persist('create', modelName, { rawData }, response)
109
112
  .catch((err) => {
@@ -117,7 +120,7 @@ export function createRecord(modelName, rawData = {}, userOptions = {}) {
117
120
  .finally(() => {
118
121
  // Evict non-memory records after persist to prevent unbounded heap growth (stonyx#81)
119
122
  if (store._memoryResolver && !store._memoryResolver(modelName)) {
120
- store.evictRecord(modelName, record.id);
123
+ store.evictRecord(modelName, record.id, registryId);
121
124
  }
122
125
  });
123
126
  }
package/dist/store.d.ts CHANGED
@@ -50,8 +50,12 @@ export default class Store {
50
50
  * Evict a record from the store with full relationship registry cleanup,
51
51
  * WITHOUT calling record.clean(). This preserves the caller's reference
52
52
  * to the returned record (used by memory:false post-persist eviction).
53
+ *
54
+ * @param registryId - The ID used when the record's relationships were
55
+ * registered. For SQL models with pending IDs, this is the original
56
+ * negative pending ID (before the adapter re-keyed to the real DB ID).
53
57
  */
54
- evictRecord(modelName: string, id: unknown): void;
58
+ evictRecord(modelName: string, id: unknown, registryId?: unknown): void;
55
59
  unloadRecord(model: string, id: unknown, options?: UnloadOptions): void;
56
60
  unloadAllRecords(model: string, options?: UnloadOptions): void;
57
61
  private _removeFromHasManyArrays;
package/dist/store.js CHANGED
@@ -131,8 +131,12 @@ export default class Store {
131
131
  * Evict a record from the store with full relationship registry cleanup,
132
132
  * WITHOUT calling record.clean(). This preserves the caller's reference
133
133
  * to the returned record (used by memory:false post-persist eviction).
134
+ *
135
+ * @param registryId - The ID used when the record's relationships were
136
+ * registered. For SQL models with pending IDs, this is the original
137
+ * negative pending ID (before the adapter re-keyed to the real DB ID).
134
138
  */
135
- evictRecord(modelName, id) {
139
+ evictRecord(modelName, id, registryId) {
136
140
  const modelStore = this.data.get(modelName);
137
141
  if (!modelStore)
138
142
  return;
@@ -142,9 +146,19 @@ export default class Store {
142
146
  if (!raw || !isStoreRecord(raw))
143
147
  return;
144
148
  const visited = new Set([`${modelName}:${id}`]);
149
+ // Remove from hasMany arrays and nullify belongsTo references using current ID
150
+ // (the adapter updates record.id, so value-based matches need the current ID)
145
151
  this._removeFromHasManyArrays(modelName, id, visited);
146
152
  this._nullifyBelongsToReferences(modelName, id, visited);
147
- this._cleanupRelationshipRegistries(modelName, id);
153
+ // Clean up relationship registry entries using the registry key
154
+ // (belongsTo/hasMany registries were keyed by the ID at registration time,
155
+ // which may differ from the current ID if SQL persist re-keyed the record)
156
+ const cleanupId = registryId ?? id;
157
+ this._cleanupRelationshipRegistries(modelName, cleanupId);
158
+ // If registryId differs from id, also clean with current id as safety net
159
+ if (registryId !== undefined && registryId !== id) {
160
+ this._cleanupRelationshipRegistries(modelName, id);
161
+ }
148
162
  modelStore.delete(id);
149
163
  }
150
164
  unloadRecord(model, id, options = {}) {
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "stonyx-async",
5
5
  "stonyx-module"
6
6
  ],
7
- "version": "0.3.2-beta.77",
7
+ "version": "0.3.2-beta.78",
8
8
  "description": "",
9
9
  "main": "dist/index.js",
10
10
  "type": "module",
@@ -139,6 +139,9 @@ export function createRecord(modelName: string, rawData: { [key: string]: unknow
139
139
  // Auto-persist to SQL — skip for DB loads (isDbRecord) and relationship resolution (_relationshipKey)
140
140
  const shouldPersist = orm?.sqlDb && !options.isDbRecord && !userOptions._relationshipKey && !options._skipAutoPersist;
141
141
  if (shouldPersist) {
142
+ // Capture ID before persist — SQL adapters re-key pending IDs to real DB IDs,
143
+ // but relationship registries were keyed with this original ID
144
+ const registryId = record.id;
142
145
  const response = { data: { id: record.id } };
143
146
  orm!.sqlDb!.persist('create', modelName, { rawData }, response)
144
147
  .catch((err: unknown) => {
@@ -152,7 +155,7 @@ export function createRecord(modelName: string, rawData: { [key: string]: unknow
152
155
  .finally(() => {
153
156
  // Evict non-memory records after persist to prevent unbounded heap growth (stonyx#81)
154
157
  if (store._memoryResolver && !store._memoryResolver(modelName)) {
155
- store.evictRecord(modelName, record.id);
158
+ store.evictRecord(modelName, record.id, registryId);
156
159
  }
157
160
  });
158
161
  }
package/src/store.ts CHANGED
@@ -197,8 +197,12 @@ export default class Store {
197
197
  * Evict a record from the store with full relationship registry cleanup,
198
198
  * WITHOUT calling record.clean(). This preserves the caller's reference
199
199
  * to the returned record (used by memory:false post-persist eviction).
200
+ *
201
+ * @param registryId - The ID used when the record's relationships were
202
+ * registered. For SQL models with pending IDs, this is the original
203
+ * negative pending ID (before the adapter re-keyed to the real DB ID).
200
204
  */
201
- evictRecord(modelName: string, id: unknown): void {
205
+ evictRecord(modelName: string, id: unknown, registryId?: unknown): void {
202
206
  const modelStore = this.data.get(modelName);
203
207
  if (!modelStore) return;
204
208
 
@@ -207,9 +211,22 @@ export default class Store {
207
211
  if (!raw || !isStoreRecord(raw)) return;
208
212
 
209
213
  const visited = new Set([`${modelName}:${id}`]);
214
+
215
+ // Remove from hasMany arrays and nullify belongsTo references using current ID
216
+ // (the adapter updates record.id, so value-based matches need the current ID)
210
217
  this._removeFromHasManyArrays(modelName, id, visited);
211
218
  this._nullifyBelongsToReferences(modelName, id, visited);
212
- this._cleanupRelationshipRegistries(modelName, id);
219
+
220
+ // Clean up relationship registry entries using the registry key
221
+ // (belongsTo/hasMany registries were keyed by the ID at registration time,
222
+ // which may differ from the current ID if SQL persist re-keyed the record)
223
+ const cleanupId = registryId ?? id;
224
+ this._cleanupRelationshipRegistries(modelName, cleanupId);
225
+
226
+ // If registryId differs from id, also clean with current id as safety net
227
+ if (registryId !== undefined && registryId !== id) {
228
+ this._cleanupRelationshipRegistries(modelName, id);
229
+ }
213
230
 
214
231
  modelStore.delete(id);
215
232
  }