@stonyx/orm 0.3.2-beta.5 → 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 +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 +19 -0
- package/dist/orm-request.js +4 -4
- 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 +23 -0
- package/src/orm-request.ts +4 -4
- 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, { rawData: data }, 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,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.
|
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
|
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
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, { rawData: data }, 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
|
|
|
@@ -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
|
/**
|
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
|
|
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);
|