@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.
@@ -0,0 +1,109 @@
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
+ * PutItem — optionally with a condition expression.
13
+ *
14
+ * Pass conditionExpression = 'attribute_not_exists(id)' to enforce uniqueness.
15
+ */
16
+ export function buildPutItem(tableName, item, conditionExpression) {
17
+ const params = { TableName: tableName, Item: item };
18
+ if (conditionExpression)
19
+ params.ConditionExpression = conditionExpression;
20
+ return params;
21
+ }
22
+ /**
23
+ * GetItem by primary key.
24
+ */
25
+ export function buildGetItem(tableName, key) {
26
+ return { TableName: tableName, Key: key };
27
+ }
28
+ /**
29
+ * UpdateItem with a SET expression built from the `updates` object.
30
+ * Only the supplied attributes are updated (diff-based call site).
31
+ */
32
+ export function buildUpdateItem(tableName, key, updates) {
33
+ const names = {};
34
+ const values = {};
35
+ const setClauses = [];
36
+ for (const [attr, val] of Object.entries(updates)) {
37
+ const nameAlias = `#${attr}`;
38
+ const valAlias = `:${attr}`;
39
+ names[nameAlias] = attr;
40
+ values[valAlias] = val;
41
+ setClauses.push(`${nameAlias} = ${valAlias}`);
42
+ }
43
+ return {
44
+ TableName: tableName,
45
+ Key: key,
46
+ UpdateExpression: `SET ${setClauses.join(', ')}`,
47
+ ExpressionAttributeNames: names,
48
+ ExpressionAttributeValues: values,
49
+ ReturnValues: 'NONE',
50
+ };
51
+ }
52
+ /**
53
+ * DeleteItem by primary key.
54
+ */
55
+ export function buildDeleteItem(tableName, key) {
56
+ return { TableName: tableName, Key: key };
57
+ }
58
+ /**
59
+ * ScanCommand params.
60
+ * If conditions are supplied they are rendered as a FilterExpression using AND.
61
+ */
62
+ export function buildScan(tableName, conditions, exclusiveStartKey) {
63
+ const params = { TableName: tableName };
64
+ if (exclusiveStartKey)
65
+ params.ExclusiveStartKey = exclusiveStartKey;
66
+ if (conditions && Object.keys(conditions).length > 0) {
67
+ const names = {};
68
+ const values = {};
69
+ const clauses = [];
70
+ for (const [attr, val] of Object.entries(conditions)) {
71
+ const nameAlias = `#${attr}`;
72
+ const valAlias = `:${attr}`;
73
+ names[nameAlias] = attr;
74
+ values[valAlias] = val;
75
+ clauses.push(`${nameAlias} = ${valAlias}`);
76
+ }
77
+ params.FilterExpression = clauses.join(' AND ');
78
+ params.ExpressionAttributeNames = names;
79
+ params.ExpressionAttributeValues = values;
80
+ }
81
+ return params;
82
+ }
83
+ /**
84
+ * QueryCommand params for a GSI.
85
+ * keyConditions must be in the form { attrName: value } and will be rendered
86
+ * as equality expressions joined by AND.
87
+ */
88
+ export function buildQuery(tableName, indexName, keyConditions, exclusiveStartKey) {
89
+ const names = {};
90
+ const values = {};
91
+ const clauses = [];
92
+ for (const [attr, val] of Object.entries(keyConditions)) {
93
+ const nameAlias = `#${attr}`;
94
+ const valAlias = `:${attr}`;
95
+ names[nameAlias] = attr;
96
+ values[valAlias] = val;
97
+ clauses.push(`${nameAlias} = ${valAlias}`);
98
+ }
99
+ const params = {
100
+ TableName: tableName,
101
+ IndexName: indexName,
102
+ KeyConditionExpression: clauses.join(' AND '),
103
+ ExpressionAttributeNames: names,
104
+ ExpressionAttributeValues: values,
105
+ };
106
+ if (exclusiveStartKey)
107
+ params.ExclusiveStartKey = exclusiveStartKey;
108
+ return params;
109
+ }
@@ -0,0 +1,31 @@
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
+ export type DynamoScalarType = 'S' | 'N' | 'BOOL';
8
+ /**
9
+ * DynamoDB attribute-type string for a given ORM attr type.
10
+ * - string → S
11
+ * - number / float → N (stored as Number; DocumentClient handles it)
12
+ * - boolean → BOOL
13
+ * - date → S (ISO-8601 string — enables range queries)
14
+ * - timestamp → N (milliseconds since epoch)
15
+ * - passthrough/trim/etc → S (safe default)
16
+ *
17
+ * For key schema declarations only `S` and `N` are valid; BOOL
18
+ * is legal for attributes but never for a PK/SK.
19
+ */
20
+ declare const typeMap: Record<string, DynamoScalarType>;
21
+ /**
22
+ * Returns the DynamoDB attribute type for a given ORM type string.
23
+ * Defaults to 'S' for any unknown/custom type.
24
+ */
25
+ export declare function getDynamoType(attrType: string): DynamoScalarType;
26
+ /**
27
+ * Returns the DynamoDB key type ('S' | 'N') for use in KeySchema.
28
+ * BOOL cannot be a key attribute; anything that maps to BOOL falls back to 'S'.
29
+ */
30
+ export declare function getDynamoKeyType(attrType: string): 'S' | 'N';
31
+ export default typeMap;
@@ -0,0 +1,48 @@
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
+ * DynamoDB attribute-type string for a given ORM attr type.
9
+ * - string → S
10
+ * - number / float → N (stored as Number; DocumentClient handles it)
11
+ * - boolean → BOOL
12
+ * - date → S (ISO-8601 string — enables range queries)
13
+ * - timestamp → N (milliseconds since epoch)
14
+ * - passthrough/trim/etc → S (safe default)
15
+ *
16
+ * For key schema declarations only `S` and `N` are valid; BOOL
17
+ * is legal for attributes but never for a PK/SK.
18
+ */
19
+ const typeMap = {
20
+ string: 'S',
21
+ number: 'N',
22
+ float: 'N',
23
+ boolean: 'BOOL',
24
+ date: 'S',
25
+ timestamp: 'N',
26
+ passthrough: 'S',
27
+ trim: 'S',
28
+ uppercase: 'S',
29
+ ceil: 'N',
30
+ floor: 'N',
31
+ round: 'N',
32
+ };
33
+ /**
34
+ * Returns the DynamoDB attribute type for a given ORM type string.
35
+ * Defaults to 'S' for any unknown/custom type.
36
+ */
37
+ export function getDynamoType(attrType) {
38
+ return typeMap[attrType] ?? 'S';
39
+ }
40
+ /**
41
+ * Returns the DynamoDB key type ('S' | 'N') for use in KeySchema.
42
+ * BOOL cannot be a key attribute; anything that maps to BOOL falls back to 'S'.
43
+ */
44
+ export function getDynamoKeyType(attrType) {
45
+ const t = getDynamoType(attrType);
46
+ return t === 'N' ? 'N' : 'S';
47
+ }
48
+ export default typeMap;
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
- // 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
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
- * 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.
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
- static update(modelName: string, id: string | number, data: Record<string, unknown>): Promise<OrmRecord>;
53
+ onPersistError(handler: ((detail: PersistErrorDetail) => void) | null): void;
53
54
  /**
54
- * Programmatic delete removes from SQL database AND memory store.
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
- static remove(modelName: string, id: string | number): Promise<void>;
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;
@@ -54,6 +54,10 @@ export default class Orm {
54
54
  Orm.instance = this;
55
55
  }
56
56
  async init() {
57
+ // Self-register so log.db works even when @stonyx/orm is in the
58
+ // consumer's `dependencies` (stonyx loader only merges devDependencies).
59
+ const { logColor = 'white', logMethod = 'db' } = config.orm;
60
+ log.defineType(logMethod, logColor);
57
61
  const { paths, restServer } = config.orm;
58
62
  const promises = ['Model', 'Serializer', 'Transform'].map(type => {
59
63
  const lowerCaseType = type.toLowerCase();
@@ -115,6 +119,12 @@ export default class Orm {
115
119
  this.db = this.sqlDb;
116
120
  promises.push(this.sqlDb.init());
117
121
  }
122
+ else if (config.orm.dynamodb) {
123
+ const { default: DynamoDBDB } = await import('./dynamodb/dynamodb-db.js');
124
+ this.sqlDb = new DynamoDBDB();
125
+ this.db = this.sqlDb;
126
+ promises.push(this.sqlDb.init());
127
+ }
118
128
  else if (this.options.dbType !== 'none') {
119
129
  const db = new DB();
120
130
  this.db = db;
@@ -170,53 +180,29 @@ export default class Orm {
170
180
  return !!this.views[`${modelClassPrefix}View`];
171
181
  }
172
182
  /**
173
- * Programmatic create writes to memory AND persists to SQL database.
174
- * Use instead of createRecord() when records must be persisted to PostgreSQL/TimescaleDB.
183
+ * Register a callback to be invoked when a fire-and-forget SQL persist fails.
184
+ * Without a handler, persist errors are logged via log.error (backwards-compatible).
175
185
  */
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
+ onPersistError(handler) {
187
+ this._persistErrorHandler = handler;
186
188
  }
187
189
  /**
188
- * Programmatic update updates in memory AND persists to SQL database.
189
- * Captures old state for diff-based UPDATE queries.
190
+ * Emit a persist error to the registered handler, or fall back to log.error.
190
191
  */
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 }, {});
192
+ emitPersistError(detail) {
193
+ const fallbackLog = () => log.error?.(`[ORM] Failed to persist ${detail.operation} for ${detail.modelName}:${String(detail.recordId)}: ${detail.error.message}`);
194
+ if (this._persistErrorHandler) {
195
+ try {
196
+ this._persistErrorHandler(detail);
197
+ }
198
+ catch (handlerError) {
199
+ fallbackLog();
200
+ log.error?.(`[ORM] onPersistError handler threw: ${handlerError instanceof Error ? handlerError.message : String(handlerError)}`);
201
+ }
206
202
  }
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 }, {});
203
+ else {
204
+ fallbackLog();
218
205
  }
219
- Orm.store.remove(modelName, id);
220
206
  }
221
207
  // Queue warnings to avoid the same error from being logged in the same iteration
222
208
  warn(message) {
@@ -4,6 +4,7 @@ interface CreateRecordOptions {
4
4
  serialize?: boolean;
5
5
  transform?: boolean;
6
6
  update?: boolean;
7
+ _skipAutoPersist?: boolean;
7
8
  [key: string]: unknown;
8
9
  }
9
10
  export declare function createRecord(modelName: string, rawData?: {
@@ -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 = `__pending_${Date.now()}_${Math.random()}`;
137
+ rawData.id = -(++pendingIdCounter);
107
138
  rawData.__pendingSqlId = true;
108
139
  return;
109
140
  }
@@ -16,6 +16,7 @@ interface PersistContext {
16
16
  record?: OrmRecord;
17
17
  recordId?: unknown;
18
18
  oldState?: Record<string, unknown>;
19
+ rawData?: Record<string, unknown>;
19
20
  }
20
21
  interface PersistResponse {
21
22
  data?: {
@@ -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
- const isPendingId = record.__data.__pendingSqlId;
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
  }
@@ -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 write operations
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 && WRITE_OPERATIONS.has(operation)) {
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
@@ -17,6 +17,7 @@ interface PersistContext {
17
17
  record?: OrmRecord;
18
18
  recordId?: unknown;
19
19
  oldState?: Record<string, unknown>;
20
+ rawData?: Record<string, unknown>;
20
21
  }
21
22
  interface PersistResponse {
22
23
  data?: {
@@ -365,7 +365,7 @@ export default class PostgresDB {
365
365
  return this._persistDelete(modelName, context);
366
366
  }
367
367
  }
368
- async _persistCreate(modelName, _context, response) {
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
- const isPendingId = record.__data.__pendingSqlId;
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 currentFkValue = record.__relationships[relName]?.id ?? null;
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,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);
@@ -42,6 +42,11 @@ export interface OrmRestServerConfig {
42
42
  route: string;
43
43
  metaRoute: boolean;
44
44
  }
45
+ export interface OrmDynamoDBConfig {
46
+ region?: string;
47
+ endpoint?: string;
48
+ [key: string]: unknown;
49
+ }
45
50
  export interface OrmSection {
46
51
  db: OrmDbConfig;
47
52
  paths: OrmPaths;
@@ -49,6 +54,9 @@ export interface OrmSection {
49
54
  mysql?: OrmMysqlConfig;
50
55
  postgres?: OrmPostgresConfig;
51
56
  timescale?: OrmPostgresConfig;
57
+ dynamodb?: OrmDynamoDBConfig;
58
+ logColor?: string;
59
+ logMethod?: string;
52
60
  [key: string]: unknown;
53
61
  }
54
62
  export interface OrmConfig {