@stonyx/orm 0.3.2-alpha.2 → 0.3.2-alpha.4
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 +4 -4
- package/dist/main.d.ts +0 -16
- package/dist/main.js +0 -50
- package/dist/manage-record.d.ts +1 -0
- package/dist/manage-record.js +24 -2
- package/dist/mysql/mysql-db.d.ts +1 -0
- package/dist/mysql/mysql-db.js +4 -2
- package/dist/orm-request.js +4 -4
- package/dist/postgres/postgres-db.d.ts +1 -0
- package/dist/postgres/postgres-db.js +20 -8
- package/dist/store.js +6 -0
- package/package.json +1 -1
- package/src/index.ts +4 -4
- package/src/main.ts +0 -59
- package/src/manage-record.ts +29 -2
- package/src/mysql/mysql-db.ts +5 -2
- package/src/orm-request.ts +4 -4
- package/src/postgres/postgres-db.ts +19 -8
- package/src/store.ts +7 -0
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
|
-
//
|
|
37
|
-
//
|
|
38
|
-
//
|
|
39
|
-
//
|
|
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);
|
package/dist/manage-record.d.ts
CHANGED
package/dist/manage-record.js
CHANGED
|
@@ -2,11 +2,13 @@ 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,
|
|
8
9
|
transform: true
|
|
9
10
|
};
|
|
11
|
+
let pendingIdCounter = 0;
|
|
10
12
|
export function createRecord(modelName, rawData = {}, userOptions = {}) {
|
|
11
13
|
const orm = Orm.instance;
|
|
12
14
|
const { initialized } = Orm;
|
|
@@ -79,6 +81,14 @@ export function createRecord(modelName, rawData = {}, userOptions = {}) {
|
|
|
79
81
|
// Clear the pending queue
|
|
80
82
|
pendingBelongsTo.length = 0;
|
|
81
83
|
}
|
|
84
|
+
// Auto-persist to SQL — skip for DB loads (isDbRecord) and relationship resolution (_relationshipKey)
|
|
85
|
+
const shouldPersist = orm?.sqlDb && !options.isDbRecord && !userOptions._relationshipKey && !options._skipAutoPersist;
|
|
86
|
+
if (shouldPersist) {
|
|
87
|
+
const response = { data: { id: record.id } };
|
|
88
|
+
orm.sqlDb.persist('create', modelName, { rawData }, response).catch((err) => {
|
|
89
|
+
log.error?.(`[ORM] Failed to persist create for ${modelName}:${String(record.id)}: ${err instanceof Error ? err.message : String(err)}`);
|
|
90
|
+
});
|
|
91
|
+
}
|
|
82
92
|
return record;
|
|
83
93
|
}
|
|
84
94
|
export function updateRecord(record, rawData, userOptions = {}) {
|
|
@@ -90,7 +100,17 @@ export function updateRecord(record, rawData, userOptions = {}) {
|
|
|
90
100
|
throw new Error(`Cannot update records for read-only view '${modelName}'`);
|
|
91
101
|
}
|
|
92
102
|
const options = { ...defaultOptions, ...userOptions, update: true };
|
|
103
|
+
// Capture old state before update for SQL diff
|
|
104
|
+
const oldState = record.__data ? JSON.parse(JSON.stringify(record.__data)) : {};
|
|
93
105
|
record.serialize(rawData, options);
|
|
106
|
+
// Auto-persist to SQL — skip for DB loads (isDbRecord) and relationship resolution (_relationshipKey)
|
|
107
|
+
const orm = Orm.instance;
|
|
108
|
+
const shouldPersist = orm?.sqlDb && !options.isDbRecord && !userOptions._relationshipKey && !options._skipAutoPersist;
|
|
109
|
+
if (shouldPersist && modelName) {
|
|
110
|
+
orm.sqlDb.persist('update', modelName, { record, oldState }, {}).catch((err) => {
|
|
111
|
+
log.error?.(`[ORM] Failed to persist update for ${modelName}:${String(record.id)}: ${err instanceof Error ? err.message : String(err)}`);
|
|
112
|
+
});
|
|
113
|
+
}
|
|
94
114
|
}
|
|
95
115
|
/**
|
|
96
116
|
* gets the next available id based on last record entry.
|
|
@@ -101,9 +121,11 @@ export function updateRecord(record, rawData, userOptions = {}) {
|
|
|
101
121
|
function assignRecordId(modelName, rawData) {
|
|
102
122
|
if (rawData.id)
|
|
103
123
|
return;
|
|
104
|
-
// In SQL mode with numeric IDs, defer to database auto-increment
|
|
124
|
+
// In SQL mode with numeric IDs, defer to database auto-increment.
|
|
125
|
+
// Use unique negative integers — they survive the number transform (parseInt preserves negatives)
|
|
126
|
+
// and avoid NaN store-key collisions that string pending IDs caused.
|
|
105
127
|
if (Orm.instance?.sqlDb && !isStringIdModel(modelName)) {
|
|
106
|
-
rawData.id =
|
|
128
|
+
rawData.id = -(++pendingIdCounter);
|
|
107
129
|
rawData.__pendingSqlId = true;
|
|
108
130
|
return;
|
|
109
131
|
}
|
package/dist/mysql/mysql-db.d.ts
CHANGED
package/dist/mysql/mysql-db.js
CHANGED
|
@@ -321,8 +321,10 @@ export default class MysqlDB {
|
|
|
321
321
|
if (!record)
|
|
322
322
|
return;
|
|
323
323
|
const insertData = this._recordToRow(record, schema);
|
|
324
|
-
// For auto-increment models, remove the pending ID
|
|
325
|
-
|
|
324
|
+
// For auto-increment models, remove the pending ID.
|
|
325
|
+
// Check context.rawData (not record.__data) because __pendingSqlId is not a model
|
|
326
|
+
// attribute and gets lost during serialization.
|
|
327
|
+
const isPendingId = context.rawData?.__pendingSqlId === true;
|
|
326
328
|
if (isPendingId) {
|
|
327
329
|
delete insertData.id;
|
|
328
330
|
}
|
package/dist/orm-request.js
CHANGED
|
@@ -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
|
|
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 &&
|
|
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
|
|
@@ -365,7 +365,7 @@ export default class PostgresDB {
|
|
|
365
365
|
return this._persistDelete(modelName, context);
|
|
366
366
|
}
|
|
367
367
|
}
|
|
368
|
-
async _persistCreate(modelName,
|
|
368
|
+
async _persistCreate(modelName, context, response) {
|
|
369
369
|
const schemas = this.deps.introspectModels();
|
|
370
370
|
const schema = schemas[modelName];
|
|
371
371
|
if (!schema)
|
|
@@ -375,9 +375,11 @@ 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);
|
|
379
|
-
// For auto-increment models, remove the pending ID
|
|
380
|
-
|
|
378
|
+
const insertData = this._recordToRow(record, schema, context.rawData);
|
|
379
|
+
// For auto-increment models, remove the pending ID.
|
|
380
|
+
// Check context.rawData (not record.__data) because __pendingSqlId is not a model
|
|
381
|
+
// attribute and gets lost during serialization.
|
|
382
|
+
const isPendingId = context.rawData?.__pendingSqlId === true;
|
|
381
383
|
if (isPendingId) {
|
|
382
384
|
delete insertData.id;
|
|
383
385
|
}
|
|
@@ -420,7 +422,10 @@ export default class PostgresDB {
|
|
|
420
422
|
// Check FK changes too
|
|
421
423
|
for (const fkCol of Object.keys(schema.foreignKeys)) {
|
|
422
424
|
const relName = fkCol.replace(/_id$/, '');
|
|
423
|
-
const
|
|
425
|
+
const relValue = record.__relationships[relName];
|
|
426
|
+
const currentFkValue = (relValue && typeof relValue === 'object' && relValue !== null)
|
|
427
|
+
? relValue.id ?? null
|
|
428
|
+
: relValue ?? record.__data[relName] ?? null;
|
|
424
429
|
const oldFkValue = oldState[relName] ?? null;
|
|
425
430
|
if (currentFkValue !== oldFkValue) {
|
|
426
431
|
changedData[fkCol] = currentFkValue;
|
|
@@ -444,7 +449,7 @@ export default class PostgresDB {
|
|
|
444
449
|
const { sql, values } = this.deps.buildDelete(schema.table, id);
|
|
445
450
|
await this.requirePool().query(sql, values);
|
|
446
451
|
}
|
|
447
|
-
_recordToRow(record, schema) {
|
|
452
|
+
_recordToRow(record, schema, rawData) {
|
|
448
453
|
const row = {};
|
|
449
454
|
const data = record.__data;
|
|
450
455
|
// ID
|
|
@@ -464,13 +469,20 @@ export default class PostgresDB {
|
|
|
464
469
|
for (const fkCol of Object.keys(schema.foreignKeys)) {
|
|
465
470
|
const relName = fkCol.replace(/_id$/, '');
|
|
466
471
|
const related = record.__relationships[relName];
|
|
467
|
-
if (related) {
|
|
472
|
+
if (related && typeof related === 'object' && related !== null) {
|
|
468
473
|
row[fkCol] = related.id;
|
|
469
474
|
}
|
|
475
|
+
else if (related != null) {
|
|
476
|
+
// Raw FK value (e.g., string ID stored directly in __relationships)
|
|
477
|
+
row[fkCol] = related;
|
|
478
|
+
}
|
|
470
479
|
else if (data[relName] !== undefined) {
|
|
471
|
-
// Raw FK value (e.g., from create payload)
|
|
472
480
|
row[fkCol] = data[relName];
|
|
473
481
|
}
|
|
482
|
+
else if (rawData?.[relName] !== undefined) {
|
|
483
|
+
// Fallback to original create payload for unresolved belongsTo FKs
|
|
484
|
+
row[fkCol] = rawData[relName];
|
|
485
|
+
}
|
|
474
486
|
}
|
|
475
487
|
return row;
|
|
476
488
|
}
|
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 instanceof Error ? err.message : String(err)}`);
|
|
119
|
+
});
|
|
120
|
+
}
|
|
115
121
|
if (id)
|
|
116
122
|
return this.unloadRecord(key, id);
|
|
117
123
|
this.unloadAllRecords(key);
|
package/package.json
CHANGED
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
|
-
//
|
|
41
|
-
//
|
|
42
|
-
//
|
|
43
|
-
//
|
|
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);
|
package/src/manage-record.ts
CHANGED
|
@@ -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
|
|
|
@@ -25,6 +27,8 @@ const defaultOptions: CreateRecordOptions = {
|
|
|
25
27
|
transform: true
|
|
26
28
|
};
|
|
27
29
|
|
|
30
|
+
let pendingIdCounter = 0;
|
|
31
|
+
|
|
28
32
|
export function createRecord(modelName: string, rawData: { [key: string]: unknown } = {}, userOptions: CreateRecordOptions = {}): OrmRecord {
|
|
29
33
|
const orm = Orm.instance;
|
|
30
34
|
const { initialized } = Orm;
|
|
@@ -111,6 +115,15 @@ export function createRecord(modelName: string, rawData: { [key: string]: unknow
|
|
|
111
115
|
pendingBelongsTo.length = 0;
|
|
112
116
|
}
|
|
113
117
|
|
|
118
|
+
// Auto-persist to SQL — skip for DB loads (isDbRecord) and relationship resolution (_relationshipKey)
|
|
119
|
+
const shouldPersist = orm?.sqlDb && !options.isDbRecord && !userOptions._relationshipKey && !options._skipAutoPersist;
|
|
120
|
+
if (shouldPersist) {
|
|
121
|
+
const response = { data: { id: record.id } };
|
|
122
|
+
orm!.sqlDb!.persist('create', modelName, { rawData }, response).catch((err: unknown) => {
|
|
123
|
+
log.error?.(`[ORM] Failed to persist create for ${modelName}:${String(record.id)}: ${err instanceof Error ? err.message : String(err)}`);
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
114
127
|
return record;
|
|
115
128
|
}
|
|
116
129
|
|
|
@@ -125,7 +138,19 @@ export function updateRecord(record: OrmRecord, rawData: unknown, userOptions: C
|
|
|
125
138
|
|
|
126
139
|
const options = { ...defaultOptions, ...userOptions, update: true };
|
|
127
140
|
|
|
141
|
+
// Capture old state before update for SQL diff
|
|
142
|
+
const oldState = record.__data ? JSON.parse(JSON.stringify(record.__data)) : {};
|
|
143
|
+
|
|
128
144
|
record.serialize(rawData, options);
|
|
145
|
+
|
|
146
|
+
// Auto-persist to SQL — skip for DB loads (isDbRecord) and relationship resolution (_relationshipKey)
|
|
147
|
+
const orm = Orm.instance;
|
|
148
|
+
const shouldPersist = orm?.sqlDb && !options.isDbRecord && !userOptions._relationshipKey && !options._skipAutoPersist;
|
|
149
|
+
if (shouldPersist && modelName) {
|
|
150
|
+
orm!.sqlDb!.persist('update', modelName, { record, oldState }, {}).catch((err: unknown) => {
|
|
151
|
+
log.error?.(`[ORM] Failed to persist update for ${modelName}:${String(record.id)}: ${err instanceof Error ? err.message : String(err)}`);
|
|
152
|
+
});
|
|
153
|
+
}
|
|
129
154
|
}
|
|
130
155
|
|
|
131
156
|
/**
|
|
@@ -137,9 +162,11 @@ export function updateRecord(record: OrmRecord, rawData: unknown, userOptions: C
|
|
|
137
162
|
function assignRecordId(modelName: string, rawData: { [key: string]: unknown }): void {
|
|
138
163
|
if (rawData.id) return;
|
|
139
164
|
|
|
140
|
-
// In SQL mode with numeric IDs, defer to database auto-increment
|
|
165
|
+
// In SQL mode with numeric IDs, defer to database auto-increment.
|
|
166
|
+
// Use unique negative integers — they survive the number transform (parseInt preserves negatives)
|
|
167
|
+
// and avoid NaN store-key collisions that string pending IDs caused.
|
|
141
168
|
if (Orm.instance?.sqlDb && !isStringIdModel(modelName)) {
|
|
142
|
-
rawData.id =
|
|
169
|
+
rawData.id = -(++pendingIdCounter);
|
|
143
170
|
rawData.__pendingSqlId = true;
|
|
144
171
|
return;
|
|
145
172
|
}
|
package/src/mysql/mysql-db.ts
CHANGED
|
@@ -21,6 +21,7 @@ interface PersistContext {
|
|
|
21
21
|
record?: OrmRecord;
|
|
22
22
|
recordId?: unknown;
|
|
23
23
|
oldState?: Record<string, unknown>;
|
|
24
|
+
rawData?: Record<string, unknown>;
|
|
24
25
|
}
|
|
25
26
|
|
|
26
27
|
interface PersistResponse {
|
|
@@ -420,8 +421,10 @@ export default class MysqlDB {
|
|
|
420
421
|
|
|
421
422
|
const insertData = this._recordToRow(record, schema);
|
|
422
423
|
|
|
423
|
-
// For auto-increment models, remove the pending ID
|
|
424
|
-
|
|
424
|
+
// For auto-increment models, remove the pending ID.
|
|
425
|
+
// Check context.rawData (not record.__data) because __pendingSqlId is not a model
|
|
426
|
+
// attribute and gets lost during serialization.
|
|
427
|
+
const isPendingId = context.rawData?.__pendingSqlId === true;
|
|
425
428
|
|
|
426
429
|
if (isPendingId) {
|
|
427
430
|
delete insertData.id;
|
package/src/orm-request.ts
CHANGED
|
@@ -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
|
|
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 &&
|
|
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,
|
|
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,10 +492,12 @@ 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
|
-
// For auto-increment models, remove the pending ID
|
|
497
|
-
|
|
497
|
+
// For auto-increment models, remove the pending ID.
|
|
498
|
+
// Check context.rawData (not record.__data) because __pendingSqlId is not a model
|
|
499
|
+
// attribute and gets lost during serialization.
|
|
500
|
+
const isPendingId = context.rawData?.__pendingSqlId === true;
|
|
498
501
|
|
|
499
502
|
if (isPendingId) {
|
|
500
503
|
delete insertData.id;
|
|
@@ -549,7 +552,10 @@ export default class PostgresDB {
|
|
|
549
552
|
// Check FK changes too
|
|
550
553
|
for (const fkCol of Object.keys(schema.foreignKeys)) {
|
|
551
554
|
const relName = fkCol.replace(/_id$/, '');
|
|
552
|
-
const
|
|
555
|
+
const relValue = record.__relationships[relName];
|
|
556
|
+
const currentFkValue = (relValue && typeof relValue === 'object' && relValue !== null)
|
|
557
|
+
? (relValue as { id: unknown }).id ?? null
|
|
558
|
+
: relValue ?? record.__data[relName] ?? null;
|
|
553
559
|
const oldFkValue = oldState[relName] ?? null;
|
|
554
560
|
|
|
555
561
|
if (currentFkValue !== oldFkValue) {
|
|
@@ -579,7 +585,7 @@ export default class PostgresDB {
|
|
|
579
585
|
await this.requirePool().query(sql, values);
|
|
580
586
|
}
|
|
581
587
|
|
|
582
|
-
private _recordToRow(record: OrmRecord, schema: ModelSchema): Record<string, unknown> {
|
|
588
|
+
private _recordToRow(record: OrmRecord, schema: ModelSchema, rawData?: Record<string, unknown>): Record<string, unknown> {
|
|
583
589
|
const row: Record<string, unknown> = {};
|
|
584
590
|
const data = record.__data;
|
|
585
591
|
|
|
@@ -603,11 +609,16 @@ export default class PostgresDB {
|
|
|
603
609
|
const relName = fkCol.replace(/_id$/, '');
|
|
604
610
|
const related = record.__relationships[relName];
|
|
605
611
|
|
|
606
|
-
if (related) {
|
|
612
|
+
if (related && typeof related === 'object' && related !== null) {
|
|
607
613
|
row[fkCol] = (related as { id: unknown }).id;
|
|
614
|
+
} else if (related != null) {
|
|
615
|
+
// Raw FK value (e.g., string ID stored directly in __relationships)
|
|
616
|
+
row[fkCol] = related;
|
|
608
617
|
} else if (data[relName] !== undefined) {
|
|
609
|
-
// Raw FK value (e.g., from create payload)
|
|
610
618
|
row[fkCol] = data[relName];
|
|
619
|
+
} else if (rawData?.[relName] !== undefined) {
|
|
620
|
+
// Fallback to original create payload for unresolved belongsTo FKs
|
|
621
|
+
row[fkCol] = rawData[relName];
|
|
611
622
|
}
|
|
612
623
|
}
|
|
613
624
|
|
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 instanceof Error ? err.message : String(err)}`);
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
|
|
179
186
|
if (id) return this.unloadRecord(key, id);
|
|
180
187
|
|
|
181
188
|
this.unloadAllRecords(key);
|