@stonyx/orm 0.3.2-alpha.1 → 0.3.2-alpha.11
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/README.md +35 -2
- package/config/environment.js +8 -0
- package/dist/commands.js +34 -0
- package/dist/dynamodb/connection.d.ts +28 -0
- package/dist/dynamodb/connection.js +28 -0
- package/dist/dynamodb/dynamodb-db.d.ts +131 -0
- package/dist/dynamodb/dynamodb-db.js +556 -0
- package/dist/dynamodb/operation-builder.d.ts +76 -0
- package/dist/dynamodb/operation-builder.js +109 -0
- package/dist/dynamodb/type-map.d.ts +31 -0
- package/dist/dynamodb/type-map.js +48 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +4 -4
- package/dist/main.d.ts +12 -12
- package/dist/main.js +28 -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.d.ts +1 -0
- package/dist/postgres/postgres-db.js +20 -8
- package/dist/store.js +11 -0
- package/dist/types/orm-types.d.ts +8 -0
- package/package.json +16 -7
- package/src/commands.ts +43 -0
- package/src/dynamodb/connection.ts +49 -0
- package/src/dynamodb/dynamodb-db.ts +768 -0
- package/src/dynamodb/operation-builder.ts +188 -0
- package/src/dynamodb/type-map.ts +54 -0
- package/src/index.ts +5 -4
- package/src/main.ts +36 -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 +19 -8
- package/src/store.ts +12 -0
- package/src/types/orm-types.ts +9 -0
- package/src/types/stonyx.d.ts +7 -1
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DynamoDB operation parameter builders.
|
|
3
|
+
*
|
|
4
|
+
* Each function returns a plain-object "params" bag that can be passed
|
|
5
|
+
* directly to the corresponding DocumentClient command
|
|
6
|
+
* (PutCommand, GetCommand, UpdateCommand, DeleteCommand, ScanCommand, QueryCommand).
|
|
7
|
+
*
|
|
8
|
+
* All functions are pure — no SDK imports here; the caller wraps params
|
|
9
|
+
* in the appropriate Command class.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
export interface PutItemParams {
|
|
13
|
+
TableName: string;
|
|
14
|
+
Item: Record<string, unknown>;
|
|
15
|
+
ConditionExpression?: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface GetItemParams {
|
|
19
|
+
TableName: string;
|
|
20
|
+
Key: Record<string, unknown>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface UpdateItemParams {
|
|
24
|
+
TableName: string;
|
|
25
|
+
Key: Record<string, unknown>;
|
|
26
|
+
UpdateExpression: string;
|
|
27
|
+
ExpressionAttributeNames: Record<string, string>;
|
|
28
|
+
ExpressionAttributeValues: Record<string, unknown>;
|
|
29
|
+
ReturnValues: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface DeleteItemParams {
|
|
33
|
+
TableName: string;
|
|
34
|
+
Key: Record<string, unknown>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export interface ScanParams {
|
|
38
|
+
TableName: string;
|
|
39
|
+
FilterExpression?: string;
|
|
40
|
+
ExpressionAttributeNames?: Record<string, string>;
|
|
41
|
+
ExpressionAttributeValues?: Record<string, unknown>;
|
|
42
|
+
ExclusiveStartKey?: Record<string, unknown>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export interface QueryParams {
|
|
46
|
+
TableName: string;
|
|
47
|
+
IndexName: string;
|
|
48
|
+
KeyConditionExpression: string;
|
|
49
|
+
ExpressionAttributeNames: Record<string, string>;
|
|
50
|
+
ExpressionAttributeValues: Record<string, unknown>;
|
|
51
|
+
ExclusiveStartKey?: Record<string, unknown>;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* PutItem — optionally with a condition expression.
|
|
56
|
+
*
|
|
57
|
+
* Pass conditionExpression = 'attribute_not_exists(id)' to enforce uniqueness.
|
|
58
|
+
*/
|
|
59
|
+
export function buildPutItem(
|
|
60
|
+
tableName: string,
|
|
61
|
+
item: Record<string, unknown>,
|
|
62
|
+
conditionExpression?: string,
|
|
63
|
+
): PutItemParams {
|
|
64
|
+
const params: PutItemParams = { TableName: tableName, Item: item };
|
|
65
|
+
if (conditionExpression) params.ConditionExpression = conditionExpression;
|
|
66
|
+
return params;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* GetItem by primary key.
|
|
71
|
+
*/
|
|
72
|
+
export function buildGetItem(
|
|
73
|
+
tableName: string,
|
|
74
|
+
key: Record<string, unknown>,
|
|
75
|
+
): GetItemParams {
|
|
76
|
+
return { TableName: tableName, Key: key };
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* UpdateItem with a SET expression built from the `updates` object.
|
|
81
|
+
* Only the supplied attributes are updated (diff-based call site).
|
|
82
|
+
*/
|
|
83
|
+
export function buildUpdateItem(
|
|
84
|
+
tableName: string,
|
|
85
|
+
key: Record<string, unknown>,
|
|
86
|
+
updates: Record<string, unknown>,
|
|
87
|
+
): UpdateItemParams {
|
|
88
|
+
const names: Record<string, string> = {};
|
|
89
|
+
const values: Record<string, unknown> = {};
|
|
90
|
+
const setClauses: string[] = [];
|
|
91
|
+
|
|
92
|
+
for (const [attr, val] of Object.entries(updates)) {
|
|
93
|
+
const nameAlias = `#${attr}`;
|
|
94
|
+
const valAlias = `:${attr}`;
|
|
95
|
+
names[nameAlias] = attr;
|
|
96
|
+
values[valAlias] = val;
|
|
97
|
+
setClauses.push(`${nameAlias} = ${valAlias}`);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return {
|
|
101
|
+
TableName: tableName,
|
|
102
|
+
Key: key,
|
|
103
|
+
UpdateExpression: `SET ${setClauses.join(', ')}`,
|
|
104
|
+
ExpressionAttributeNames: names,
|
|
105
|
+
ExpressionAttributeValues: values,
|
|
106
|
+
ReturnValues: 'NONE',
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* DeleteItem by primary key.
|
|
112
|
+
*/
|
|
113
|
+
export function buildDeleteItem(
|
|
114
|
+
tableName: string,
|
|
115
|
+
key: Record<string, unknown>,
|
|
116
|
+
): DeleteItemParams {
|
|
117
|
+
return { TableName: tableName, Key: key };
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* ScanCommand params.
|
|
122
|
+
* If conditions are supplied they are rendered as a FilterExpression using AND.
|
|
123
|
+
*/
|
|
124
|
+
export function buildScan(
|
|
125
|
+
tableName: string,
|
|
126
|
+
conditions?: Record<string, unknown>,
|
|
127
|
+
exclusiveStartKey?: Record<string, unknown>,
|
|
128
|
+
): ScanParams {
|
|
129
|
+
const params: ScanParams = { TableName: tableName };
|
|
130
|
+
|
|
131
|
+
if (exclusiveStartKey) params.ExclusiveStartKey = exclusiveStartKey;
|
|
132
|
+
|
|
133
|
+
if (conditions && Object.keys(conditions).length > 0) {
|
|
134
|
+
const names: Record<string, string> = {};
|
|
135
|
+
const values: Record<string, unknown> = {};
|
|
136
|
+
const clauses: string[] = [];
|
|
137
|
+
|
|
138
|
+
for (const [attr, val] of Object.entries(conditions)) {
|
|
139
|
+
const nameAlias = `#${attr}`;
|
|
140
|
+
const valAlias = `:${attr}`;
|
|
141
|
+
names[nameAlias] = attr;
|
|
142
|
+
values[valAlias] = val;
|
|
143
|
+
clauses.push(`${nameAlias} = ${valAlias}`);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
params.FilterExpression = clauses.join(' AND ');
|
|
147
|
+
params.ExpressionAttributeNames = names;
|
|
148
|
+
params.ExpressionAttributeValues = values;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
return params;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* QueryCommand params for a GSI.
|
|
156
|
+
* keyConditions must be in the form { attrName: value } and will be rendered
|
|
157
|
+
* as equality expressions joined by AND.
|
|
158
|
+
*/
|
|
159
|
+
export function buildQuery(
|
|
160
|
+
tableName: string,
|
|
161
|
+
indexName: string,
|
|
162
|
+
keyConditions: Record<string, unknown>,
|
|
163
|
+
exclusiveStartKey?: Record<string, unknown>,
|
|
164
|
+
): QueryParams {
|
|
165
|
+
const names: Record<string, string> = {};
|
|
166
|
+
const values: Record<string, unknown> = {};
|
|
167
|
+
const clauses: string[] = [];
|
|
168
|
+
|
|
169
|
+
for (const [attr, val] of Object.entries(keyConditions)) {
|
|
170
|
+
const nameAlias = `#${attr}`;
|
|
171
|
+
const valAlias = `:${attr}`;
|
|
172
|
+
names[nameAlias] = attr;
|
|
173
|
+
values[valAlias] = val;
|
|
174
|
+
clauses.push(`${nameAlias} = ${valAlias}`);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const params: QueryParams = {
|
|
178
|
+
TableName: tableName,
|
|
179
|
+
IndexName: indexName,
|
|
180
|
+
KeyConditionExpression: clauses.join(' AND '),
|
|
181
|
+
ExpressionAttributeNames: names,
|
|
182
|
+
ExpressionAttributeValues: values,
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
if (exclusiveStartKey) params.ExclusiveStartKey = exclusiveStartKey;
|
|
186
|
+
|
|
187
|
+
return params;
|
|
188
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Maps ORM attribute types to DynamoDB scalar attribute types.
|
|
3
|
+
* DynamoDB DocumentClient auto-marshalls JS objects, so most values
|
|
4
|
+
* are sent as their native JS types. This map is used by the
|
|
5
|
+
* schema-introspector and startup provisioner for table/GSI creation.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export type DynamoScalarType = 'S' | 'N' | 'BOOL';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* DynamoDB attribute-type string for a given ORM attr type.
|
|
12
|
+
* - string → S
|
|
13
|
+
* - number / float → N (stored as Number; DocumentClient handles it)
|
|
14
|
+
* - boolean → BOOL
|
|
15
|
+
* - date → S (ISO-8601 string — enables range queries)
|
|
16
|
+
* - timestamp → N (milliseconds since epoch)
|
|
17
|
+
* - passthrough/trim/etc → S (safe default)
|
|
18
|
+
*
|
|
19
|
+
* For key schema declarations only `S` and `N` are valid; BOOL
|
|
20
|
+
* is legal for attributes but never for a PK/SK.
|
|
21
|
+
*/
|
|
22
|
+
const typeMap: Record<string, DynamoScalarType> = {
|
|
23
|
+
string: 'S',
|
|
24
|
+
number: 'N',
|
|
25
|
+
float: 'N',
|
|
26
|
+
boolean: 'BOOL',
|
|
27
|
+
date: 'S',
|
|
28
|
+
timestamp: 'N',
|
|
29
|
+
passthrough: 'S',
|
|
30
|
+
trim: 'S',
|
|
31
|
+
uppercase: 'S',
|
|
32
|
+
ceil: 'N',
|
|
33
|
+
floor: 'N',
|
|
34
|
+
round: 'N',
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Returns the DynamoDB attribute type for a given ORM type string.
|
|
39
|
+
* Defaults to 'S' for any unknown/custom type.
|
|
40
|
+
*/
|
|
41
|
+
export function getDynamoType(attrType: string): DynamoScalarType {
|
|
42
|
+
return typeMap[attrType] ?? 'S';
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Returns the DynamoDB key type ('S' | 'N') for use in KeySchema.
|
|
47
|
+
* BOOL cannot be a key attribute; anything that maps to BOOL falls back to 'S'.
|
|
48
|
+
*/
|
|
49
|
+
export function getDynamoKeyType(attrType: string): 'S' | 'N' {
|
|
50
|
+
const t = getDynamoType(attrType);
|
|
51
|
+
return t === 'N' ? 'N' : 'S';
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export default typeMap;
|
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
|
|
|
@@ -83,6 +90,11 @@ export default class Orm {
|
|
|
83
90
|
}
|
|
84
91
|
|
|
85
92
|
async init(): Promise<void> {
|
|
93
|
+
// Self-register so log.db works even when @stonyx/orm is in the
|
|
94
|
+
// consumer's `dependencies` (stonyx loader only merges devDependencies).
|
|
95
|
+
const { logColor = 'white', logMethod = 'db' } = config.orm;
|
|
96
|
+
log.defineType(logMethod, logColor);
|
|
97
|
+
|
|
86
98
|
const { paths, restServer } = config.orm;
|
|
87
99
|
|
|
88
100
|
const promises: Promise<unknown>[] = ['Model', 'Serializer', 'Transform'].map(type => {
|
|
@@ -152,6 +164,11 @@ export default class Orm {
|
|
|
152
164
|
this.sqlDb = new MysqlDB() as SqlDb;
|
|
153
165
|
this.db = this.sqlDb;
|
|
154
166
|
promises.push(this.sqlDb.init());
|
|
167
|
+
} else if (config.orm.dynamodb) {
|
|
168
|
+
const { default: DynamoDBDB } = await import('./dynamodb/dynamodb-db.js');
|
|
169
|
+
this.sqlDb = new DynamoDBDB() as SqlDb;
|
|
170
|
+
this.db = this.sqlDb;
|
|
171
|
+
promises.push(this.sqlDb.init());
|
|
155
172
|
} else if (this.options.dbType !== 'none') {
|
|
156
173
|
const db = new DB();
|
|
157
174
|
this.db = db;
|
|
@@ -217,60 +234,29 @@ export default class Orm {
|
|
|
217
234
|
}
|
|
218
235
|
|
|
219
236
|
/**
|
|
220
|
-
*
|
|
221
|
-
*
|
|
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.
|
|
237
|
+
* Register a callback to be invoked when a fire-and-forget SQL persist fails.
|
|
238
|
+
* Without a handler, persist errors are logged via log.error (backwards-compatible).
|
|
240
239
|
*/
|
|
241
|
-
|
|
242
|
-
|
|
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;
|
|
240
|
+
onPersistError(handler: ((detail: PersistErrorDetail) => void) | null): void {
|
|
241
|
+
this._persistErrorHandler = handler;
|
|
260
242
|
}
|
|
261
243
|
|
|
262
244
|
/**
|
|
263
|
-
*
|
|
264
|
-
* SQL delete runs first to ensure consistency on failure.
|
|
245
|
+
* Emit a persist error to the registered handler, or fall back to log.error.
|
|
265
246
|
*/
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
if (
|
|
270
|
-
|
|
247
|
+
emitPersistError(detail: PersistErrorDetail): void {
|
|
248
|
+
const fallbackLog = () => log.error?.(`[ORM] Failed to persist ${detail.operation} for ${detail.modelName}:${String(detail.recordId)}: ${detail.error.message}`);
|
|
249
|
+
|
|
250
|
+
if (this._persistErrorHandler) {
|
|
251
|
+
try {
|
|
252
|
+
this._persistErrorHandler(detail);
|
|
253
|
+
} catch (handlerError) {
|
|
254
|
+
fallbackLog();
|
|
255
|
+
log.error?.(`[ORM] onPersistError handler threw: ${handlerError instanceof Error ? handlerError.message : String(handlerError)}`);
|
|
256
|
+
}
|
|
257
|
+
} else {
|
|
258
|
+
fallbackLog();
|
|
271
259
|
}
|
|
272
|
-
|
|
273
|
-
Orm.store.remove(modelName, id);
|
|
274
260
|
}
|
|
275
261
|
|
|
276
262
|
// 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
|
|
|
@@ -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,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);
|
package/src/types/orm-types.ts
CHANGED
|
@@ -48,6 +48,12 @@ export interface OrmRestServerConfig {
|
|
|
48
48
|
metaRoute: boolean;
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
+
export interface OrmDynamoDBConfig {
|
|
52
|
+
region?: string;
|
|
53
|
+
endpoint?: string;
|
|
54
|
+
[key: string]: unknown;
|
|
55
|
+
}
|
|
56
|
+
|
|
51
57
|
export interface OrmSection {
|
|
52
58
|
db: OrmDbConfig;
|
|
53
59
|
paths: OrmPaths;
|
|
@@ -55,6 +61,9 @@ export interface OrmSection {
|
|
|
55
61
|
mysql?: OrmMysqlConfig;
|
|
56
62
|
postgres?: OrmPostgresConfig;
|
|
57
63
|
timescale?: OrmPostgresConfig;
|
|
64
|
+
dynamodb?: OrmDynamoDBConfig;
|
|
65
|
+
logColor?: string;
|
|
66
|
+
logMethod?: string;
|
|
58
67
|
[key: string]: unknown;
|
|
59
68
|
}
|
|
60
69
|
|
package/src/types/stonyx.d.ts
CHANGED
|
@@ -5,7 +5,13 @@ declare module 'stonyx/config' {
|
|
|
5
5
|
}
|
|
6
6
|
|
|
7
7
|
declare module 'stonyx/log' {
|
|
8
|
-
|
|
8
|
+
interface Log {
|
|
9
|
+
db(message: string): void;
|
|
10
|
+
error(message: string, ...args: unknown[]): void;
|
|
11
|
+
defineType(type: string, setting: string, options?: Record<string, unknown> | null): void;
|
|
12
|
+
[key: string]: ((...args: unknown[]) => void) | undefined;
|
|
13
|
+
}
|
|
14
|
+
const log: Log;
|
|
9
15
|
export default log;
|
|
10
16
|
}
|
|
11
17
|
|