@stonyx/orm 0.3.2-beta.5 → 0.3.2-beta.50
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.d.ts +1 -0
- package/dist/index.js +4 -4
- package/dist/main.d.ts +12 -12
- package/dist/main.js +18 -42
- package/dist/manage-record.d.ts +1 -0
- package/dist/manage-record.js +33 -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.js +4 -2
- package/dist/store.js +11 -0
- package/package.json +8 -7
- package/src/index.ts +5 -4
- package/src/main.ts +26 -50
- package/src/manage-record.ts +38 -2
- package/src/mysql/mysql-db.ts +5 -2
- package/src/orm-request.ts +4 -4
- package/src/postgres/postgres-db.ts +4 -2
- package/src/store.ts +12 -0
package/dist/index.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ import { createRecord, updateRecord } from './manage-record.js';
|
|
|
8
8
|
import { count, avg, sum, min, max } from './aggregates.js';
|
|
9
9
|
export { default } from './main.js';
|
|
10
10
|
export { store, relationships } from './main.js';
|
|
11
|
+
export type { PersistErrorDetail } from './main.js';
|
|
11
12
|
export { Model, View, Serializer };
|
|
12
13
|
export { attr, belongsTo, hasMany, createRecord, updateRecord };
|
|
13
14
|
export { count, avg, sum, min, max };
|
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
|
}
|
|
@@ -16,6 +15,12 @@ export interface OrmDB {
|
|
|
16
15
|
save(): Promise<void>;
|
|
17
16
|
init(): Promise<void>;
|
|
18
17
|
}
|
|
18
|
+
export interface PersistErrorDetail {
|
|
19
|
+
operation: 'create' | 'update' | 'delete';
|
|
20
|
+
modelName: string;
|
|
21
|
+
recordId: unknown;
|
|
22
|
+
error: Error;
|
|
23
|
+
}
|
|
19
24
|
export default class Orm {
|
|
20
25
|
static initialized: boolean;
|
|
21
26
|
static relationships: Map<string, Map<string, unknown>>;
|
|
@@ -30,6 +35,7 @@ export default class Orm {
|
|
|
30
35
|
options: OrmOptions;
|
|
31
36
|
sqlDb?: SqlDb;
|
|
32
37
|
db?: OrmDB | SqlDb;
|
|
38
|
+
private _persistErrorHandler;
|
|
33
39
|
constructor(options?: OrmOptions);
|
|
34
40
|
init(): Promise<void>;
|
|
35
41
|
startup(): Promise<void>;
|
|
@@ -41,20 +47,14 @@ export default class Orm {
|
|
|
41
47
|
};
|
|
42
48
|
isView(modelName: string): boolean;
|
|
43
49
|
/**
|
|
44
|
-
*
|
|
45
|
-
*
|
|
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.
|
|
50
|
+
* Register a callback to be invoked when a fire-and-forget SQL persist fails.
|
|
51
|
+
* Without a handler, persist errors are logged via log.error (backwards-compatible).
|
|
51
52
|
*/
|
|
52
|
-
|
|
53
|
+
onPersistError(handler: ((detail: PersistErrorDetail) => void) | null): void;
|
|
53
54
|
/**
|
|
54
|
-
*
|
|
55
|
-
* SQL delete runs first to ensure consistency on failure.
|
|
55
|
+
* Emit a persist error to the registered handler, or fall back to log.error.
|
|
56
56
|
*/
|
|
57
|
-
|
|
57
|
+
emitPersistError(detail: PersistErrorDetail): void;
|
|
58
58
|
warn(message: string): void;
|
|
59
59
|
}
|
|
60
60
|
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
|
};
|
|
@@ -42,6 +41,7 @@ export default class Orm {
|
|
|
42
41
|
options;
|
|
43
42
|
sqlDb;
|
|
44
43
|
db;
|
|
44
|
+
_persistErrorHandler = null;
|
|
45
45
|
constructor(options = {}) {
|
|
46
46
|
if (Orm.instance)
|
|
47
47
|
return Orm.instance;
|
|
@@ -170,53 +170,29 @@ export default class Orm {
|
|
|
170
170
|
return !!this.views[`${modelClassPrefix}View`];
|
|
171
171
|
}
|
|
172
172
|
/**
|
|
173
|
-
*
|
|
174
|
-
*
|
|
173
|
+
* Register a callback to be invoked when a fire-and-forget SQL persist fails.
|
|
174
|
+
* Without a handler, persist errors are logged via log.error (backwards-compatible).
|
|
175
175
|
*/
|
|
176
|
-
|
|
177
|
-
|
|
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, { rawData: data }, response);
|
|
184
|
-
}
|
|
185
|
-
return record;
|
|
176
|
+
onPersistError(handler) {
|
|
177
|
+
this._persistErrorHandler = handler;
|
|
186
178
|
}
|
|
187
179
|
/**
|
|
188
|
-
*
|
|
189
|
-
* Captures old state for diff-based UPDATE queries.
|
|
180
|
+
* Emit a persist error to the registered handler, or fall back to log.error.
|
|
190
181
|
*/
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
continue;
|
|
202
|
-
record[key] = value;
|
|
203
|
-
}
|
|
204
|
-
if (Orm.instance.sqlDb) {
|
|
205
|
-
await Orm.instance.sqlDb.persist('update', modelName, { record, oldState }, {});
|
|
182
|
+
emitPersistError(detail) {
|
|
183
|
+
const fallbackLog = () => log.error?.(`[ORM] Failed to persist ${detail.operation} for ${detail.modelName}:${String(detail.recordId)}: ${detail.error.message}`);
|
|
184
|
+
if (this._persistErrorHandler) {
|
|
185
|
+
try {
|
|
186
|
+
this._persistErrorHandler(detail);
|
|
187
|
+
}
|
|
188
|
+
catch (handlerError) {
|
|
189
|
+
fallbackLog();
|
|
190
|
+
log.error?.(`[ORM] onPersistError handler threw: ${handlerError instanceof Error ? handlerError.message : String(handlerError)}`);
|
|
191
|
+
}
|
|
206
192
|
}
|
|
207
|
-
|
|
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 }, {});
|
|
193
|
+
else {
|
|
194
|
+
fallbackLog();
|
|
218
195
|
}
|
|
219
|
-
Orm.store.remove(modelName, id);
|
|
220
196
|
}
|
|
221
197
|
// Queue warnings to avoid the same error from being logged in the same iteration
|
|
222
198
|
warn(message) {
|
package/dist/manage-record.d.ts
CHANGED
package/dist/manage-record.js
CHANGED
|
@@ -7,6 +7,7 @@ const defaultOptions = {
|
|
|
7
7
|
serialize: true,
|
|
8
8
|
transform: true
|
|
9
9
|
};
|
|
10
|
+
let pendingIdCounter = 0;
|
|
10
11
|
export function createRecord(modelName, rawData = {}, userOptions = {}) {
|
|
11
12
|
const orm = Orm.instance;
|
|
12
13
|
const { initialized } = Orm;
|
|
@@ -79,6 +80,19 @@ 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
|
+
orm.emitPersistError({
|
|
89
|
+
operation: 'create',
|
|
90
|
+
modelName,
|
|
91
|
+
recordId: record.id,
|
|
92
|
+
error: err instanceof Error ? err : new Error(String(err)),
|
|
93
|
+
});
|
|
94
|
+
});
|
|
95
|
+
}
|
|
82
96
|
return record;
|
|
83
97
|
}
|
|
84
98
|
export function updateRecord(record, rawData, userOptions = {}) {
|
|
@@ -90,7 +104,22 @@ export function updateRecord(record, rawData, userOptions = {}) {
|
|
|
90
104
|
throw new Error(`Cannot update records for read-only view '${modelName}'`);
|
|
91
105
|
}
|
|
92
106
|
const options = { ...defaultOptions, ...userOptions, update: true };
|
|
107
|
+
// Capture old state before update for SQL diff
|
|
108
|
+
const oldState = record.__data ? JSON.parse(JSON.stringify(record.__data)) : {};
|
|
93
109
|
record.serialize(rawData, options);
|
|
110
|
+
// Auto-persist to SQL — skip for DB loads (isDbRecord) and relationship resolution (_relationshipKey)
|
|
111
|
+
const orm = Orm.instance;
|
|
112
|
+
const shouldPersist = orm?.sqlDb && !options.isDbRecord && !userOptions._relationshipKey && !options._skipAutoPersist;
|
|
113
|
+
if (shouldPersist && modelName) {
|
|
114
|
+
orm.sqlDb.persist('update', modelName, { record, oldState }, {}).catch((err) => {
|
|
115
|
+
orm.emitPersistError({
|
|
116
|
+
operation: 'update',
|
|
117
|
+
modelName,
|
|
118
|
+
recordId: record.id,
|
|
119
|
+
error: err instanceof Error ? err : new Error(String(err)),
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
}
|
|
94
123
|
}
|
|
95
124
|
/**
|
|
96
125
|
* gets the next available id based on last record entry.
|
|
@@ -101,9 +130,11 @@ export function updateRecord(record, rawData, userOptions = {}) {
|
|
|
101
130
|
function assignRecordId(modelName, rawData) {
|
|
102
131
|
if (rawData.id)
|
|
103
132
|
return;
|
|
104
|
-
// In SQL mode with numeric IDs, defer to database auto-increment
|
|
133
|
+
// In SQL mode with numeric IDs, defer to database auto-increment.
|
|
134
|
+
// Use unique negative integers — they survive the number transform (parseInt preserves negatives)
|
|
135
|
+
// and avoid NaN store-key collisions that string pending IDs caused.
|
|
105
136
|
if (Orm.instance?.sqlDb && !isStringIdModel(modelName)) {
|
|
106
|
-
rawData.id =
|
|
137
|
+
rawData.id = -(++pendingIdCounter);
|
|
107
138
|
rawData.__pendingSqlId = true;
|
|
108
139
|
return;
|
|
109
140
|
}
|
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
|
|
@@ -376,8 +376,10 @@ export default class PostgresDB {
|
|
|
376
376
|
if (!record)
|
|
377
377
|
return;
|
|
378
378
|
const insertData = this._recordToRow(record, schema, context.rawData);
|
|
379
|
-
// For auto-increment models, remove the pending ID
|
|
380
|
-
|
|
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
|
}
|
package/dist/store.js
CHANGED
|
@@ -112,6 +112,17 @@ 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
|
+
Orm.instance.emitPersistError({
|
|
119
|
+
operation: 'delete',
|
|
120
|
+
modelName: key,
|
|
121
|
+
recordId: id,
|
|
122
|
+
error: err instanceof Error ? err : new Error(String(err)),
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
}
|
|
115
126
|
if (id)
|
|
116
127
|
return this.unloadRecord(key, id);
|
|
117
128
|
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.
|
|
7
|
+
"version": "0.3.2-beta.50",
|
|
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.
|
|
65
|
-
"@stonyx/events": "0.1.1-beta.
|
|
66
|
-
"stonyx": "0.2.3-beta.
|
|
64
|
+
"@stonyx/cron": "0.2.1-beta.59",
|
|
65
|
+
"@stonyx/events": "0.1.1-beta.47",
|
|
66
|
+
"stonyx": "0.2.3-beta.63"
|
|
67
67
|
},
|
|
68
68
|
"peerDependencies": {
|
|
69
69
|
"@stonyx/rest-server": ">=0.2.1-beta.14",
|
|
@@ -82,18 +82,19 @@
|
|
|
82
82
|
}
|
|
83
83
|
},
|
|
84
84
|
"devDependencies": {
|
|
85
|
-
"@stonyx/rest-server": "0.2.1-beta.
|
|
86
|
-
"@stonyx/utils": "0.2.3-beta.
|
|
85
|
+
"@stonyx/rest-server": "0.2.1-beta.60",
|
|
86
|
+
"@stonyx/utils": "0.2.3-beta.23",
|
|
87
87
|
"@types/node": "^25.6.0",
|
|
88
88
|
"mysql2": "^3.20.0",
|
|
89
89
|
"pg": "^8.20.0",
|
|
90
90
|
"qunit": "^2.24.1",
|
|
91
91
|
"sinon": "^21.0.0",
|
|
92
|
+
"tsx": "^4.21.0",
|
|
92
93
|
"typescript": "^5.8.3"
|
|
93
94
|
},
|
|
94
95
|
"scripts": {
|
|
95
96
|
"build": "tsc",
|
|
96
97
|
"build:test": "tsc -p tsconfig.test.json",
|
|
97
|
-
"test": "
|
|
98
|
+
"test": "pnpm build && NODE_ENV=test node --import tsx/esm --import ./test/setup.ts node_modules/qunit/bin/qunit.js 'test/**/*-test.ts'"
|
|
98
99
|
}
|
|
99
100
|
}
|
package/src/index.ts
CHANGED
|
@@ -26,6 +26,7 @@ import { count, avg, sum, min, max } from './aggregates.js';
|
|
|
26
26
|
|
|
27
27
|
export { default } from './main.js';
|
|
28
28
|
export { store, relationships } from './main.js';
|
|
29
|
+
export type { PersistErrorDetail } from './main.js';
|
|
29
30
|
export { Model, View, Serializer }; // base classes
|
|
30
31
|
export { attr, belongsTo, hasMany, createRecord, updateRecord }; // helpers
|
|
31
32
|
export { count, avg, sum, min, max }; // aggregate helpers
|
|
@@ -37,7 +38,7 @@ export { beforeHook, afterHook, clearHook, clearAllHooks } from './hooks.js'; //
|
|
|
37
38
|
// store.findAll(model) -- async, all records
|
|
38
39
|
// store.query(model, conditions) -- async, always hits SQL
|
|
39
40
|
//
|
|
40
|
-
//
|
|
41
|
-
//
|
|
42
|
-
//
|
|
43
|
-
//
|
|
41
|
+
// Data-layer auto-persist (memory + SQL persistence):
|
|
42
|
+
// createRecord(model, data) -- sync, auto-persists to SQL (fire-and-forget)
|
|
43
|
+
// updateRecord(record, data) -- sync, auto-persists to SQL (fire-and-forget)
|
|
44
|
+
// 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;
|
|
@@ -51,6 +49,13 @@ const defaultOptions: OrmOptions = {
|
|
|
51
49
|
dbType: 'json'
|
|
52
50
|
}
|
|
53
51
|
|
|
52
|
+
export interface PersistErrorDetail {
|
|
53
|
+
operation: 'create' | 'update' | 'delete';
|
|
54
|
+
modelName: string;
|
|
55
|
+
recordId: unknown;
|
|
56
|
+
error: Error;
|
|
57
|
+
}
|
|
58
|
+
|
|
54
59
|
export default class Orm {
|
|
55
60
|
static initialized: boolean = false;
|
|
56
61
|
static relationships: Map<string, Map<string, unknown>> = new Map();
|
|
@@ -67,6 +72,8 @@ export default class Orm {
|
|
|
67
72
|
sqlDb?: SqlDb;
|
|
68
73
|
db?: OrmDB | SqlDb;
|
|
69
74
|
|
|
75
|
+
private _persistErrorHandler: ((detail: PersistErrorDetail) => void) | null = null;
|
|
76
|
+
|
|
70
77
|
constructor(options: OrmOptions = {}) {
|
|
71
78
|
if (Orm.instance) return Orm.instance;
|
|
72
79
|
|
|
@@ -217,60 +224,29 @@ export default class Orm {
|
|
|
217
224
|
}
|
|
218
225
|
|
|
219
226
|
/**
|
|
220
|
-
*
|
|
221
|
-
*
|
|
227
|
+
* Register a callback to be invoked when a fire-and-forget SQL persist fails.
|
|
228
|
+
* Without a handler, persist errors are logged via log.error (backwards-compatible).
|
|
222
229
|
*/
|
|
223
|
-
|
|
224
|
-
|
|
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, { rawData: data }, response);
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
return record;
|
|
230
|
+
onPersistError(handler: ((detail: PersistErrorDetail) => void) | null): void {
|
|
231
|
+
this._persistErrorHandler = handler;
|
|
235
232
|
}
|
|
236
233
|
|
|
237
234
|
/**
|
|
238
|
-
*
|
|
239
|
-
* Captures old state for diff-based UPDATE queries.
|
|
235
|
+
* Emit a persist error to the registered handler, or fall back to log.error.
|
|
240
236
|
*/
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
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 }, {});
|
|
237
|
+
emitPersistError(detail: PersistErrorDetail): void {
|
|
238
|
+
const fallbackLog = () => log.error?.(`[ORM] Failed to persist ${detail.operation} for ${detail.modelName}:${String(detail.recordId)}: ${detail.error.message}`);
|
|
239
|
+
|
|
240
|
+
if (this._persistErrorHandler) {
|
|
241
|
+
try {
|
|
242
|
+
this._persistErrorHandler(detail);
|
|
243
|
+
} catch (handlerError) {
|
|
244
|
+
fallbackLog();
|
|
245
|
+
log.error?.(`[ORM] onPersistError handler threw: ${handlerError instanceof Error ? handlerError.message : String(handlerError)}`);
|
|
246
|
+
}
|
|
247
|
+
} else {
|
|
248
|
+
fallbackLog();
|
|
271
249
|
}
|
|
272
|
-
|
|
273
|
-
Orm.store.remove(modelName, id);
|
|
274
250
|
}
|
|
275
251
|
|
|
276
252
|
// Queue warnings to avoid the same error from being logged in the same iteration
|
package/src/manage-record.ts
CHANGED
|
@@ -9,6 +9,7 @@ interface CreateRecordOptions {
|
|
|
9
9
|
serialize?: boolean;
|
|
10
10
|
transform?: boolean;
|
|
11
11
|
update?: boolean;
|
|
12
|
+
_skipAutoPersist?: boolean;
|
|
12
13
|
[key: string]: unknown;
|
|
13
14
|
}
|
|
14
15
|
|
|
@@ -25,6 +26,8 @@ const defaultOptions: CreateRecordOptions = {
|
|
|
25
26
|
transform: true
|
|
26
27
|
};
|
|
27
28
|
|
|
29
|
+
let pendingIdCounter = 0;
|
|
30
|
+
|
|
28
31
|
export function createRecord(modelName: string, rawData: { [key: string]: unknown } = {}, userOptions: CreateRecordOptions = {}): OrmRecord {
|
|
29
32
|
const orm = Orm.instance;
|
|
30
33
|
const { initialized } = Orm;
|
|
@@ -111,6 +114,20 @@ export function createRecord(modelName: string, rawData: { [key: string]: unknow
|
|
|
111
114
|
pendingBelongsTo.length = 0;
|
|
112
115
|
}
|
|
113
116
|
|
|
117
|
+
// Auto-persist to SQL — skip for DB loads (isDbRecord) and relationship resolution (_relationshipKey)
|
|
118
|
+
const shouldPersist = orm?.sqlDb && !options.isDbRecord && !userOptions._relationshipKey && !options._skipAutoPersist;
|
|
119
|
+
if (shouldPersist) {
|
|
120
|
+
const response = { data: { id: record.id } };
|
|
121
|
+
orm!.sqlDb!.persist('create', modelName, { rawData }, response).catch((err: unknown) => {
|
|
122
|
+
orm!.emitPersistError({
|
|
123
|
+
operation: 'create',
|
|
124
|
+
modelName,
|
|
125
|
+
recordId: record.id,
|
|
126
|
+
error: err instanceof Error ? err : new Error(String(err)),
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
114
131
|
return record;
|
|
115
132
|
}
|
|
116
133
|
|
|
@@ -125,7 +142,24 @@ export function updateRecord(record: OrmRecord, rawData: unknown, userOptions: C
|
|
|
125
142
|
|
|
126
143
|
const options = { ...defaultOptions, ...userOptions, update: true };
|
|
127
144
|
|
|
145
|
+
// Capture old state before update for SQL diff
|
|
146
|
+
const oldState = record.__data ? JSON.parse(JSON.stringify(record.__data)) : {};
|
|
147
|
+
|
|
128
148
|
record.serialize(rawData, options);
|
|
149
|
+
|
|
150
|
+
// Auto-persist to SQL — skip for DB loads (isDbRecord) and relationship resolution (_relationshipKey)
|
|
151
|
+
const orm = Orm.instance;
|
|
152
|
+
const shouldPersist = orm?.sqlDb && !options.isDbRecord && !userOptions._relationshipKey && !options._skipAutoPersist;
|
|
153
|
+
if (shouldPersist && modelName) {
|
|
154
|
+
orm!.sqlDb!.persist('update', modelName, { record, oldState }, {}).catch((err: unknown) => {
|
|
155
|
+
orm!.emitPersistError({
|
|
156
|
+
operation: 'update',
|
|
157
|
+
modelName,
|
|
158
|
+
recordId: record.id,
|
|
159
|
+
error: err instanceof Error ? err : new Error(String(err)),
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
}
|
|
129
163
|
}
|
|
130
164
|
|
|
131
165
|
/**
|
|
@@ -137,9 +171,11 @@ export function updateRecord(record: OrmRecord, rawData: unknown, userOptions: C
|
|
|
137
171
|
function assignRecordId(modelName: string, rawData: { [key: string]: unknown }): void {
|
|
138
172
|
if (rawData.id) return;
|
|
139
173
|
|
|
140
|
-
// In SQL mode with numeric IDs, defer to database auto-increment
|
|
174
|
+
// In SQL mode with numeric IDs, defer to database auto-increment.
|
|
175
|
+
// Use unique negative integers — they survive the number transform (parseInt preserves negatives)
|
|
176
|
+
// and avoid NaN store-key collisions that string pending IDs caused.
|
|
141
177
|
if (Orm.instance?.sqlDb && !isStringIdModel(modelName)) {
|
|
142
|
-
rawData.id =
|
|
178
|
+
rawData.id = -(++pendingIdCounter);
|
|
143
179
|
rawData.__pendingSqlId = true;
|
|
144
180
|
return;
|
|
145
181
|
}
|
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
|
|
|
@@ -494,8 +494,10 @@ export default class PostgresDB {
|
|
|
494
494
|
|
|
495
495
|
const insertData = this._recordToRow(record, schema, context.rawData);
|
|
496
496
|
|
|
497
|
-
// For auto-increment models, remove the pending ID
|
|
498
|
-
|
|
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;
|
|
499
501
|
|
|
500
502
|
if (isPendingId) {
|
|
501
503
|
delete insertData.id;
|
package/src/store.ts
CHANGED
|
@@ -176,6 +176,18 @@ 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
|
+
Orm.instance.emitPersistError({
|
|
183
|
+
operation: 'delete',
|
|
184
|
+
modelName: key,
|
|
185
|
+
recordId: id,
|
|
186
|
+
error: err instanceof Error ? err : new Error(String(err)),
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
|
|
179
191
|
if (id) return this.unloadRecord(key, id);
|
|
180
192
|
|
|
181
193
|
this.unloadAllRecords(key);
|