@stonyx/orm 0.3.2-beta.4 → 0.3.2-beta.6

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.js CHANGED
@@ -33,7 +33,7 @@ export { beforeHook, afterHook, clearHook, clearAllHooks } from './hooks.js'; //
33
33
  // store.findAll(model) -- async, all records
34
34
  // store.query(model, conditions) -- async, always hits SQL
35
35
  //
36
- // Programmatic CRUD (memory + SQL persistence):
37
- // Orm.create(model, data) -- async, createRecord + sqlDb.persist
38
- // Orm.update(model, id, data) -- async, updateRecord + sqlDb.persist
39
- // Orm.remove(model, id) -- async, sqlDb.persist + store.remove
36
+ // Data-layer auto-persist (memory + SQL persistence):
37
+ // createRecord(model, data) -- sync, auto-persists to SQL (fire-and-forget)
38
+ // updateRecord(record, data) -- sync, auto-persists to SQL (fire-and-forget)
39
+ // store.remove(model, id) -- sync, auto-persists delete to SQL (fire-and-forget)
package/dist/main.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import Store from './store.js';
2
- import type { OrmRecord } from './types/orm-types.js';
3
2
  interface OrmOptions {
4
3
  dbType?: string;
5
4
  }
@@ -40,21 +39,6 @@ export default class Orm {
40
39
  serializerClass: unknown;
41
40
  };
42
41
  isView(modelName: string): boolean;
43
- /**
44
- * Programmatic create — writes to memory AND persists to SQL database.
45
- * Use instead of createRecord() when records must be persisted to PostgreSQL/TimescaleDB.
46
- */
47
- static create(modelName: string, data?: Record<string, unknown>): Promise<OrmRecord>;
48
- /**
49
- * Programmatic update — updates in memory AND persists to SQL database.
50
- * Captures old state for diff-based UPDATE queries.
51
- */
52
- static update(modelName: string, id: string | number, data: Record<string, unknown>): Promise<OrmRecord>;
53
- /**
54
- * Programmatic delete — removes from SQL database AND memory store.
55
- * SQL delete runs first to ensure consistency on failure.
56
- */
57
- static remove(modelName: string, id: string | number): Promise<void>;
58
42
  warn(message: string): void;
59
43
  }
60
44
  export declare const store: Store;
package/dist/main.js CHANGED
@@ -24,7 +24,6 @@ import baseTransforms from './transforms.js';
24
24
  import Store from './store.js';
25
25
  import Serializer from './serializer.js';
26
26
  import { setup } from '@stonyx/events';
27
- import { isOrmRecord } from './utils.js';
28
27
  const defaultOptions = {
29
28
  dbType: 'json'
30
29
  };
@@ -169,55 +168,6 @@ export default class Orm {
169
168
  const modelClassPrefix = kebabCaseToPascalCase(modelName);
170
169
  return !!this.views[`${modelClassPrefix}View`];
171
170
  }
172
- /**
173
- * Programmatic create — writes to memory AND persists to SQL database.
174
- * Use instead of createRecord() when records must be persisted to PostgreSQL/TimescaleDB.
175
- */
176
- static async create(modelName, data = {}) {
177
- if (!Orm.initialized)
178
- throw new Error('ORM is not ready');
179
- const { createRecord } = await import('./manage-record.js');
180
- const record = createRecord(modelName, data, { serialize: false });
181
- if (Orm.instance.sqlDb) {
182
- const response = { data: { id: record.id } };
183
- await Orm.instance.sqlDb.persist('create', modelName, {}, response);
184
- }
185
- return record;
186
- }
187
- /**
188
- * Programmatic update — updates in memory AND persists to SQL database.
189
- * Captures old state for diff-based UPDATE queries.
190
- */
191
- static async update(modelName, id, data) {
192
- if (!Orm.initialized)
193
- throw new Error('ORM is not ready');
194
- const record = Orm.store.get(modelName, id);
195
- if (!record || !isOrmRecord(record))
196
- throw new Error(`Record ${modelName}:${id} not found`);
197
- const oldState = JSON.parse(JSON.stringify(record.__data));
198
- // Apply attribute updates directly, matching the REST handler pattern
199
- for (const [key, value] of Object.entries(data)) {
200
- if (key === 'id')
201
- continue;
202
- record[key] = value;
203
- }
204
- if (Orm.instance.sqlDb) {
205
- await Orm.instance.sqlDb.persist('update', modelName, { record, oldState }, {});
206
- }
207
- return record;
208
- }
209
- /**
210
- * Programmatic delete — removes from SQL database AND memory store.
211
- * SQL delete runs first to ensure consistency on failure.
212
- */
213
- static async remove(modelName, id) {
214
- if (!Orm.initialized)
215
- throw new Error('ORM is not ready');
216
- if (Orm.instance.sqlDb) {
217
- await Orm.instance.sqlDb.persist('delete', modelName, { recordId: id }, {});
218
- }
219
- Orm.store.remove(modelName, id);
220
- }
221
171
  // Queue warnings to avoid the same error from being logged in the same iteration
222
172
  warn(message) {
223
173
  this.warnings.add(message);
@@ -4,6 +4,7 @@ interface CreateRecordOptions {
4
4
  serialize?: boolean;
5
5
  transform?: boolean;
6
6
  update?: boolean;
7
+ _skipAutoPersist?: boolean;
7
8
  [key: string]: unknown;
8
9
  }
9
10
  export declare function createRecord(modelName: string, rawData?: {
@@ -2,6 +2,7 @@ import Orm, { store } from '@stonyx/orm';
2
2
  import OrmRecord from './record.js';
3
3
  import { getGlobalRegistry, getPendingRegistry, getPendingBelongsToRegistry, getBelongsToRegistry, getHasManyRegistry } from './relationships.js';
4
4
  import { isOrmRecord } from './utils.js';
5
+ import log from 'stonyx/log';
5
6
  const defaultOptions = {
6
7
  isDbRecord: false,
7
8
  serialize: true,
@@ -79,6 +80,14 @@ export function createRecord(modelName, rawData = {}, userOptions = {}) {
79
80
  // Clear the pending queue
80
81
  pendingBelongsTo.length = 0;
81
82
  }
83
+ // Auto-persist to SQL — skip for DB loads (isDbRecord) and relationship resolution (_relationshipKey)
84
+ const shouldPersist = orm?.sqlDb && !options.isDbRecord && !userOptions._relationshipKey && !options._skipAutoPersist;
85
+ if (shouldPersist) {
86
+ const response = { data: { id: record.id } };
87
+ orm.sqlDb.persist('create', modelName, { rawData }, response).catch((err) => {
88
+ log.error?.(`[ORM] Failed to persist create for ${modelName}:${String(record.id)}`, err);
89
+ });
90
+ }
82
91
  return record;
83
92
  }
84
93
  export function updateRecord(record, rawData, userOptions = {}) {
@@ -90,7 +99,17 @@ export function updateRecord(record, rawData, userOptions = {}) {
90
99
  throw new Error(`Cannot update records for read-only view '${modelName}'`);
91
100
  }
92
101
  const options = { ...defaultOptions, ...userOptions, update: true };
102
+ // Capture old state before update for SQL diff
103
+ const oldState = record.__data ? JSON.parse(JSON.stringify(record.__data)) : {};
93
104
  record.serialize(rawData, options);
105
+ // Auto-persist to SQL — skip for DB loads (isDbRecord) and relationship resolution (_relationshipKey)
106
+ const orm = Orm.instance;
107
+ const shouldPersist = orm?.sqlDb && !options.isDbRecord && !userOptions._relationshipKey && !options._skipAutoPersist;
108
+ if (shouldPersist && modelName) {
109
+ orm.sqlDb.persist('update', modelName, { record, oldState }, {}).catch((err) => {
110
+ log.error?.(`[ORM] Failed to persist update for ${modelName}:${String(record.id)}`, err);
111
+ });
112
+ }
94
113
  }
95
114
  /**
96
115
  * gets the next available id based on last record entry.
@@ -248,7 +248,7 @@ export default class OrmRequest extends Request {
248
248
  }
249
249
  }
250
250
  const recordAttributes = id !== undefined ? { id, ...sanitizedAttributes } : sanitizedAttributes;
251
- const created = createRecord(model, recordAttributes, { serialize: false });
251
+ const created = createRecord(model, recordAttributes, { serialize: false, _skipAutoPersist: true });
252
252
  const record = isOrmRecord(created) ? created : null;
253
253
  if (!record)
254
254
  return 500;
@@ -283,7 +283,7 @@ export default class OrmRequest extends Request {
283
283
  }
284
284
  }
285
285
  if (Object.keys(relUpdates).length > 0) {
286
- updateRecord(record, relUpdates);
286
+ updateRecord(record, relUpdates, { _skipAutoPersist: true });
287
287
  }
288
288
  }
289
289
  return { data: record.toJSON?.() };
@@ -348,9 +348,9 @@ export default class OrmRequest extends Request {
348
348
  }
349
349
  // Execute main handler
350
350
  const response = await handler(request, state);
351
- // Persist to SQL database for write operations
351
+ // Persist to SQL database for create/update (delete is handled by store.remove auto-persist)
352
352
  const sqlDb = Orm.instance.sqlDb;
353
- if (sqlDb && WRITE_OPERATIONS.has(operation)) {
353
+ if (sqlDb && (operation === 'create' || operation === 'update')) {
354
354
  await sqlDb.persist(operation, this.model, context, response);
355
355
  }
356
356
  // Add response and relevant records to context
@@ -17,6 +17,7 @@ interface PersistContext {
17
17
  record?: OrmRecord;
18
18
  recordId?: unknown;
19
19
  oldState?: Record<string, unknown>;
20
+ rawData?: Record<string, unknown>;
20
21
  }
21
22
  interface PersistResponse {
22
23
  data?: {
@@ -365,7 +365,7 @@ export default class PostgresDB {
365
365
  return this._persistDelete(modelName, context);
366
366
  }
367
367
  }
368
- async _persistCreate(modelName, _context, response) {
368
+ async _persistCreate(modelName, context, response) {
369
369
  const schemas = this.deps.introspectModels();
370
370
  const schema = schemas[modelName];
371
371
  if (!schema)
@@ -375,7 +375,7 @@ export default class PostgresDB {
375
375
  const record = recordId != null ? storeRef.get(modelName, isNaN(recordId) ? recordId : parseInt(recordId)) : null;
376
376
  if (!record)
377
377
  return;
378
- const insertData = this._recordToRow(record, schema);
378
+ const insertData = this._recordToRow(record, schema, context.rawData);
379
379
  // For auto-increment models, remove the pending ID
380
380
  const isPendingId = record.__data.__pendingSqlId;
381
381
  if (isPendingId) {
@@ -420,7 +420,10 @@ export default class PostgresDB {
420
420
  // Check FK changes too
421
421
  for (const fkCol of Object.keys(schema.foreignKeys)) {
422
422
  const relName = fkCol.replace(/_id$/, '');
423
- const currentFkValue = record.__relationships[relName]?.id ?? null;
423
+ const relValue = record.__relationships[relName];
424
+ const currentFkValue = (relValue && typeof relValue === 'object' && relValue !== null)
425
+ ? relValue.id ?? null
426
+ : relValue ?? record.__data[relName] ?? null;
424
427
  const oldFkValue = oldState[relName] ?? null;
425
428
  if (currentFkValue !== oldFkValue) {
426
429
  changedData[fkCol] = currentFkValue;
@@ -444,7 +447,7 @@ export default class PostgresDB {
444
447
  const { sql, values } = this.deps.buildDelete(schema.table, id);
445
448
  await this.requirePool().query(sql, values);
446
449
  }
447
- _recordToRow(record, schema) {
450
+ _recordToRow(record, schema, rawData) {
448
451
  const row = {};
449
452
  const data = record.__data;
450
453
  // ID
@@ -464,13 +467,20 @@ export default class PostgresDB {
464
467
  for (const fkCol of Object.keys(schema.foreignKeys)) {
465
468
  const relName = fkCol.replace(/_id$/, '');
466
469
  const related = record.__relationships[relName];
467
- if (related) {
470
+ if (related && typeof related === 'object' && related !== null) {
468
471
  row[fkCol] = related.id;
469
472
  }
473
+ else if (related != null) {
474
+ // Raw FK value (e.g., string ID stored directly in __relationships)
475
+ row[fkCol] = related;
476
+ }
470
477
  else if (data[relName] !== undefined) {
471
- // Raw FK value (e.g., from create payload)
472
478
  row[fkCol] = data[relName];
473
479
  }
480
+ else if (rawData?.[relName] !== undefined) {
481
+ // Fallback to original create payload for unresolved belongsTo FKs
482
+ row[fkCol] = rawData[relName];
483
+ }
474
484
  }
475
485
  return row;
476
486
  }
package/dist/store.js CHANGED
@@ -112,6 +112,12 @@ export default class Store {
112
112
  if (Orm.instance?.isView?.(key)) {
113
113
  throw new Error(`Cannot remove records from read-only view '${key}'`);
114
114
  }
115
+ // Auto-persist delete to SQL
116
+ if (id && Orm.instance?.sqlDb) {
117
+ Orm.instance.sqlDb.persist('delete', key, { recordId: id }, {}).catch((err) => {
118
+ console.error(`[ORM] Failed to persist delete for ${key}:${id}`, err);
119
+ });
120
+ }
115
121
  if (id)
116
122
  return this.unloadRecord(key, id);
117
123
  this.unloadAllRecords(key);
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "stonyx-async",
5
5
  "stonyx-module"
6
6
  ],
7
- "version": "0.3.2-beta.4",
7
+ "version": "0.3.2-beta.6",
8
8
  "description": "",
9
9
  "main": "dist/index.js",
10
10
  "type": "module",
package/src/index.ts CHANGED
@@ -37,7 +37,7 @@ export { beforeHook, afterHook, clearHook, clearAllHooks } from './hooks.js'; //
37
37
  // store.findAll(model) -- async, all records
38
38
  // store.query(model, conditions) -- async, always hits SQL
39
39
  //
40
- // Programmatic CRUD (memory + SQL persistence):
41
- // Orm.create(model, data) -- async, createRecord + sqlDb.persist
42
- // Orm.update(model, id, data) -- async, updateRecord + sqlDb.persist
43
- // Orm.remove(model, id) -- async, sqlDb.persist + store.remove
40
+ // Data-layer auto-persist (memory + SQL persistence):
41
+ // createRecord(model, data) -- sync, auto-persists to SQL (fire-and-forget)
42
+ // updateRecord(record, data) -- sync, auto-persists to SQL (fire-and-forget)
43
+ // store.remove(model, id) -- sync, auto-persists delete to SQL (fire-and-forget)
package/src/main.ts CHANGED
@@ -25,8 +25,6 @@ import baseTransforms from './transforms.js';
25
25
  import Store from './store.js';
26
26
  import Serializer from './serializer.js';
27
27
  import { setup } from '@stonyx/events';
28
- import type { OrmRecord } from './types/orm-types.js';
29
- import { isOrmRecord } from './utils.js';
30
28
 
31
29
  interface OrmOptions {
32
30
  dbType?: string;
@@ -216,63 +214,6 @@ export default class Orm {
216
214
  return !!this.views[`${modelClassPrefix}View`];
217
215
  }
218
216
 
219
- /**
220
- * Programmatic create — writes to memory AND persists to SQL database.
221
- * Use instead of createRecord() when records must be persisted to PostgreSQL/TimescaleDB.
222
- */
223
- static async create(modelName: string, data: Record<string, unknown> = {}): Promise<OrmRecord> {
224
- if (!Orm.initialized) throw new Error('ORM is not ready');
225
-
226
- const { createRecord } = await import('./manage-record.js');
227
- const record = createRecord(modelName, data, { serialize: false }) as unknown as OrmRecord;
228
-
229
- if (Orm.instance.sqlDb) {
230
- const response: { data: { id: unknown } } = { data: { id: record.id } };
231
- await Orm.instance.sqlDb.persist('create', modelName, {}, response);
232
- }
233
-
234
- return record;
235
- }
236
-
237
- /**
238
- * Programmatic update — updates in memory AND persists to SQL database.
239
- * Captures old state for diff-based UPDATE queries.
240
- */
241
- static async update(modelName: string, id: string | number, data: Record<string, unknown>): Promise<OrmRecord> {
242
- if (!Orm.initialized) throw new Error('ORM is not ready');
243
-
244
- const record = Orm.store.get(modelName, id);
245
- if (!record || !isOrmRecord(record)) throw new Error(`Record ${modelName}:${id} not found`);
246
-
247
- const oldState = JSON.parse(JSON.stringify(record.__data));
248
-
249
- // Apply attribute updates directly, matching the REST handler pattern
250
- for (const [key, value] of Object.entries(data)) {
251
- if (key === 'id') continue;
252
- record[key] = value;
253
- }
254
-
255
- if (Orm.instance.sqlDb) {
256
- await Orm.instance.sqlDb.persist('update', modelName, { record, oldState }, {});
257
- }
258
-
259
- return record;
260
- }
261
-
262
- /**
263
- * Programmatic delete — removes from SQL database AND memory store.
264
- * SQL delete runs first to ensure consistency on failure.
265
- */
266
- static async remove(modelName: string, id: string | number): Promise<void> {
267
- if (!Orm.initialized) throw new Error('ORM is not ready');
268
-
269
- if (Orm.instance.sqlDb) {
270
- await Orm.instance.sqlDb.persist('delete', modelName, { recordId: id }, {});
271
- }
272
-
273
- Orm.store.remove(modelName, id);
274
- }
275
-
276
217
  // Queue warnings to avoid the same error from being logged in the same iteration
277
218
  warn(message: string): void {
278
219
  this.warnings.add(message);
@@ -3,12 +3,14 @@ import OrmRecord from './record.js';
3
3
  import { getGlobalRegistry, getPendingRegistry, getPendingBelongsToRegistry, getBelongsToRegistry, getHasManyRegistry } from './relationships.js';
4
4
  import type Serializer from './serializer.js';
5
5
  import { isOrmRecord } from './utils.js';
6
+ import log from 'stonyx/log';
6
7
 
7
8
  interface CreateRecordOptions {
8
9
  isDbRecord?: boolean;
9
10
  serialize?: boolean;
10
11
  transform?: boolean;
11
12
  update?: boolean;
13
+ _skipAutoPersist?: boolean;
12
14
  [key: string]: unknown;
13
15
  }
14
16
 
@@ -111,6 +113,15 @@ export function createRecord(modelName: string, rawData: { [key: string]: unknow
111
113
  pendingBelongsTo.length = 0;
112
114
  }
113
115
 
116
+ // Auto-persist to SQL — skip for DB loads (isDbRecord) and relationship resolution (_relationshipKey)
117
+ const shouldPersist = orm?.sqlDb && !options.isDbRecord && !userOptions._relationshipKey && !options._skipAutoPersist;
118
+ if (shouldPersist) {
119
+ const response = { data: { id: record.id } };
120
+ orm!.sqlDb!.persist('create', modelName, { rawData }, response).catch((err: unknown) => {
121
+ log.error?.(`[ORM] Failed to persist create for ${modelName}:${String(record.id)}`, err);
122
+ });
123
+ }
124
+
114
125
  return record;
115
126
  }
116
127
 
@@ -125,7 +136,19 @@ export function updateRecord(record: OrmRecord, rawData: unknown, userOptions: C
125
136
 
126
137
  const options = { ...defaultOptions, ...userOptions, update: true };
127
138
 
139
+ // Capture old state before update for SQL diff
140
+ const oldState = record.__data ? JSON.parse(JSON.stringify(record.__data)) : {};
141
+
128
142
  record.serialize(rawData, options);
143
+
144
+ // Auto-persist to SQL — skip for DB loads (isDbRecord) and relationship resolution (_relationshipKey)
145
+ const orm = Orm.instance;
146
+ const shouldPersist = orm?.sqlDb && !options.isDbRecord && !userOptions._relationshipKey && !options._skipAutoPersist;
147
+ if (shouldPersist && modelName) {
148
+ orm!.sqlDb!.persist('update', modelName, { record, oldState }, {}).catch((err: unknown) => {
149
+ log.error?.(`[ORM] Failed to persist update for ${modelName}:${String(record.id)}`, err);
150
+ });
151
+ }
129
152
  }
130
153
 
131
154
  /**
@@ -330,7 +330,7 @@ export default class OrmRequest extends Request {
330
330
  }
331
331
 
332
332
  const recordAttributes = id !== undefined ? { id, ...sanitizedAttributes } : sanitizedAttributes;
333
- const created = createRecord(model, recordAttributes as { [key: string]: unknown }, { serialize: false });
333
+ const created = createRecord(model, recordAttributes as { [key: string]: unknown }, { serialize: false, _skipAutoPersist: true });
334
334
  const record = isOrmRecord(created) ? created : null;
335
335
  if (!record) return 500;
336
336
 
@@ -368,7 +368,7 @@ export default class OrmRequest extends Request {
368
368
  }
369
369
  }
370
370
  if (Object.keys(relUpdates).length > 0) {
371
- updateRecord(record as never, relUpdates);
371
+ updateRecord(record as never, relUpdates, { _skipAutoPersist: true });
372
372
  }
373
373
  }
374
374
 
@@ -443,9 +443,9 @@ export default class OrmRequest extends Request {
443
443
  // Execute main handler
444
444
  const response = await handler(request, state);
445
445
 
446
- // Persist to SQL database for write operations
446
+ // Persist to SQL database for create/update (delete is handled by store.remove auto-persist)
447
447
  const sqlDb = Orm.instance.sqlDb;
448
- if (sqlDb && WRITE_OPERATIONS.has(operation)) {
448
+ if (sqlDb && (operation === 'create' || operation === 'update')) {
449
449
  await sqlDb.persist(operation, this.model, context, response);
450
450
  }
451
451
 
@@ -19,6 +19,7 @@ interface PersistContext {
19
19
  record?: OrmRecord;
20
20
  recordId?: unknown;
21
21
  oldState?: Record<string, unknown>;
22
+ rawData?: Record<string, unknown>;
22
23
  }
23
24
 
24
25
  interface PersistResponse {
@@ -477,7 +478,7 @@ export default class PostgresDB {
477
478
  }
478
479
  }
479
480
 
480
- private async _persistCreate(modelName: string, _context: PersistContext, response: PersistResponse): Promise<void> {
481
+ private async _persistCreate(modelName: string, context: PersistContext, response: PersistResponse): Promise<void> {
481
482
  const schemas = this.deps.introspectModels();
482
483
  const schema = schemas[modelName];
483
484
 
@@ -491,7 +492,7 @@ export default class PostgresDB {
491
492
 
492
493
  if (!record) return;
493
494
 
494
- const insertData = this._recordToRow(record, schema);
495
+ const insertData = this._recordToRow(record, schema, context.rawData);
495
496
 
496
497
  // For auto-increment models, remove the pending ID
497
498
  const isPendingId = record.__data.__pendingSqlId;
@@ -549,7 +550,10 @@ export default class PostgresDB {
549
550
  // Check FK changes too
550
551
  for (const fkCol of Object.keys(schema.foreignKeys)) {
551
552
  const relName = fkCol.replace(/_id$/, '');
552
- const currentFkValue = (record.__relationships[relName] as { id: unknown } | undefined)?.id ?? null;
553
+ const relValue = record.__relationships[relName];
554
+ const currentFkValue = (relValue && typeof relValue === 'object' && relValue !== null)
555
+ ? (relValue as { id: unknown }).id ?? null
556
+ : relValue ?? record.__data[relName] ?? null;
553
557
  const oldFkValue = oldState[relName] ?? null;
554
558
 
555
559
  if (currentFkValue !== oldFkValue) {
@@ -579,7 +583,7 @@ export default class PostgresDB {
579
583
  await this.requirePool().query(sql, values);
580
584
  }
581
585
 
582
- private _recordToRow(record: OrmRecord, schema: ModelSchema): Record<string, unknown> {
586
+ private _recordToRow(record: OrmRecord, schema: ModelSchema, rawData?: Record<string, unknown>): Record<string, unknown> {
583
587
  const row: Record<string, unknown> = {};
584
588
  const data = record.__data;
585
589
 
@@ -603,11 +607,16 @@ export default class PostgresDB {
603
607
  const relName = fkCol.replace(/_id$/, '');
604
608
  const related = record.__relationships[relName];
605
609
 
606
- if (related) {
610
+ if (related && typeof related === 'object' && related !== null) {
607
611
  row[fkCol] = (related as { id: unknown }).id;
612
+ } else if (related != null) {
613
+ // Raw FK value (e.g., string ID stored directly in __relationships)
614
+ row[fkCol] = related;
608
615
  } else if (data[relName] !== undefined) {
609
- // Raw FK value (e.g., from create payload)
610
616
  row[fkCol] = data[relName];
617
+ } else if (rawData?.[relName] !== undefined) {
618
+ // Fallback to original create payload for unresolved belongsTo FKs
619
+ row[fkCol] = rawData[relName];
611
620
  }
612
621
  }
613
622
 
package/src/store.ts CHANGED
@@ -176,6 +176,13 @@ export default class Store {
176
176
  throw new Error(`Cannot remove records from read-only view '${key}'`);
177
177
  }
178
178
 
179
+ // Auto-persist delete to SQL
180
+ if (id && Orm.instance?.sqlDb) {
181
+ Orm.instance.sqlDb.persist('delete', key, { recordId: id }, {}).catch((err: unknown) => {
182
+ console.error(`[ORM] Failed to persist delete for ${key}:${id}`, err);
183
+ });
184
+ }
185
+
179
186
  if (id) return this.unloadRecord(key, id);
180
187
 
181
188
  this.unloadAllRecords(key);