@stonyx/orm 0.3.2-beta.0 → 0.3.2-beta.1
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 +7 -2
- package/dist/main.d.ts +16 -0
- package/dist/main.js +50 -0
- package/package.json +1 -1
- package/src/index.ts +7 -2
- package/src/main.ts +59 -0
package/dist/index.js
CHANGED
|
@@ -29,6 +29,11 @@ export { count, avg, sum, min, max }; // aggregate helpers
|
|
|
29
29
|
export { beforeHook, afterHook, clearHook, clearAllHooks } from './hooks.js'; // middleware hooks
|
|
30
30
|
// Store API:
|
|
31
31
|
// store.get(model, id) -- sync, memory-only
|
|
32
|
-
// store.find(model, id) -- async,
|
|
32
|
+
// store.find(model, id) -- async, SQL for memory:false models
|
|
33
33
|
// store.findAll(model) -- async, all records
|
|
34
|
-
// store.query(model, conditions) -- async, always hits
|
|
34
|
+
// store.query(model, conditions) -- async, always hits SQL
|
|
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
|
package/dist/main.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import Store from './store.js';
|
|
2
|
+
import type { OrmRecord } from './types/orm-types.js';
|
|
2
3
|
interface OrmOptions {
|
|
3
4
|
dbType?: string;
|
|
4
5
|
}
|
|
@@ -39,6 +40,21 @@ export default class Orm {
|
|
|
39
40
|
serializerClass: unknown;
|
|
40
41
|
};
|
|
41
42
|
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>;
|
|
42
58
|
warn(message: string): void;
|
|
43
59
|
}
|
|
44
60
|
export declare const store: Store;
|
package/dist/main.js
CHANGED
|
@@ -24,6 +24,7 @@ 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';
|
|
27
28
|
const defaultOptions = {
|
|
28
29
|
dbType: 'json'
|
|
29
30
|
};
|
|
@@ -168,6 +169,55 @@ export default class Orm {
|
|
|
168
169
|
const modelClassPrefix = kebabCaseToPascalCase(modelName);
|
|
169
170
|
return !!this.views[`${modelClassPrefix}View`];
|
|
170
171
|
}
|
|
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
|
+
}
|
|
171
221
|
// Queue warnings to avoid the same error from being logged in the same iteration
|
|
172
222
|
warn(message) {
|
|
173
223
|
this.warnings.add(message);
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -33,6 +33,11 @@ export { beforeHook, afterHook, clearHook, clearAllHooks } from './hooks.js'; //
|
|
|
33
33
|
|
|
34
34
|
// Store API:
|
|
35
35
|
// store.get(model, id) -- sync, memory-only
|
|
36
|
-
// store.find(model, id) -- async,
|
|
36
|
+
// store.find(model, id) -- async, SQL for memory:false models
|
|
37
37
|
// store.findAll(model) -- async, all records
|
|
38
|
-
// store.query(model, conditions) -- async, always hits
|
|
38
|
+
// store.query(model, conditions) -- async, always hits SQL
|
|
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
|
package/src/main.ts
CHANGED
|
@@ -25,6 +25,8 @@ 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';
|
|
28
30
|
|
|
29
31
|
interface OrmOptions {
|
|
30
32
|
dbType?: string;
|
|
@@ -214,6 +216,63 @@ export default class Orm {
|
|
|
214
216
|
return !!this.views[`${modelClassPrefix}View`];
|
|
215
217
|
}
|
|
216
218
|
|
|
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
|
+
|
|
217
276
|
// Queue warnings to avoid the same error from being logged in the same iteration
|
|
218
277
|
warn(message: string): void {
|
|
219
278
|
this.warnings.add(message);
|