@stonyx/orm 0.3.2-beta.3 → 0.3.2-beta.300

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.
Files changed (61) hide show
  1. package/README.md +328 -22
  2. package/config/environment.js +8 -0
  3. package/dist/column-conversion.d.ts +34 -0
  4. package/dist/column-conversion.js +34 -0
  5. package/dist/commands.js +34 -0
  6. package/dist/dynamodb/connection.d.ts +31 -0
  7. package/dist/dynamodb/connection.js +28 -0
  8. package/dist/dynamodb/dynamodb-db.d.ts +142 -0
  9. package/dist/dynamodb/dynamodb-db.js +596 -0
  10. package/dist/dynamodb/operation-builder.d.ts +76 -0
  11. package/dist/dynamodb/operation-builder.js +116 -0
  12. package/dist/dynamodb/type-map.d.ts +31 -0
  13. package/dist/dynamodb/type-map.js +48 -0
  14. package/dist/index.d.ts +3 -0
  15. package/dist/index.js +5 -4
  16. package/dist/main.d.ts +12 -12
  17. package/dist/main.js +47 -43
  18. package/dist/manage-record.d.ts +1 -0
  19. package/dist/manage-record.js +66 -4
  20. package/dist/mysql/connection.d.ts +1 -0
  21. package/dist/mysql/mysql-db.d.ts +9 -0
  22. package/dist/mysql/mysql-db.js +48 -12
  23. package/dist/normalize-record-id.d.ts +67 -0
  24. package/dist/normalize-record-id.js +88 -0
  25. package/dist/orm-request.d.ts +17 -1
  26. package/dist/orm-request.js +78 -20
  27. package/dist/postgres/column-rules.d.ts +33 -0
  28. package/dist/postgres/column-rules.js +34 -0
  29. package/dist/postgres/connection.d.ts +1 -0
  30. package/dist/postgres/connection.js +8 -6
  31. package/dist/postgres/postgres-db.d.ts +13 -0
  32. package/dist/postgres/postgres-db.js +80 -27
  33. package/dist/record.js +7 -5
  34. package/dist/relationships.js +1 -1
  35. package/dist/serializer.js +38 -2
  36. package/dist/store.d.ts +13 -1
  37. package/dist/store.js +74 -4
  38. package/dist/types/orm-types.d.ts +11 -0
  39. package/package.json +20 -7
  40. package/src/column-conversion.ts +53 -0
  41. package/src/commands.ts +43 -0
  42. package/src/dynamodb/connection.ts +50 -0
  43. package/src/dynamodb/dynamodb-db.ts +811 -0
  44. package/src/dynamodb/operation-builder.ts +202 -0
  45. package/src/dynamodb/type-map.ts +54 -0
  46. package/src/index.ts +7 -4
  47. package/src/main.ts +55 -51
  48. package/src/manage-record.ts +72 -4
  49. package/src/mysql/connection.ts +1 -0
  50. package/src/mysql/mysql-db.ts +49 -14
  51. package/src/normalize-record-id.ts +88 -0
  52. package/src/orm-request.ts +110 -19
  53. package/src/postgres/column-rules.ts +37 -0
  54. package/src/postgres/connection.ts +10 -6
  55. package/src/postgres/postgres-db.ts +83 -29
  56. package/src/record.ts +8 -5
  57. package/src/relationships.ts +1 -1
  58. package/src/serializer.ts +39 -2
  59. package/src/store.ts +78 -4
  60. package/src/types/orm-types.ts +12 -0
  61. package/src/types/stonyx.d.ts +7 -1
@@ -8,6 +8,8 @@ import { createRecord } from '../manage-record.js';
8
8
  import { confirm } from '@stonyx/utils/prompt';
9
9
  import { readFile } from '@stonyx/utils/file';
10
10
  import { getPluralName } from '../plural-registry.js';
11
+ import { convertRow } from '../column-conversion.js';
12
+ import { postgresJsonRule } from './column-rules.js';
11
13
  import { isDbError } from '../utils.js';
12
14
  import config from 'stonyx/config';
13
15
  import log from 'stonyx/log';
@@ -18,7 +20,8 @@ const defaultDeps = {
18
20
  introspectModels, introspectViews, getTopologicalOrder, schemasToSnapshot,
19
21
  loadLatestSnapshot, detectSchemaDrift,
20
22
  buildInsert, buildUpdate, buildDelete, buildSelect, buildVectorSearch, buildHybridSearch,
21
- createRecord, store, confirm, readFile, getPluralName, config, log, path
23
+ createRecord, store, confirm, readFile, getPluralName, config, log, path,
24
+ convertRow, postgresJsonRule
22
25
  };
23
26
  export default class PostgresDB {
24
27
  /** PostgreSQL extensions to enable on pool init. Subclasses can override. */
@@ -30,6 +33,14 @@ export default class PostgresDB {
30
33
  deps;
31
34
  pool;
32
35
  pgConfig;
36
+ /**
37
+ * Promise-chain mutex for write serialization (#156).
38
+ * All persist() calls chain through this single queue so concurrent
39
+ * fire-and-forget writes never produce parallel transactions
40
+ * on FK-linked rows (which cause deadlocks).
41
+ * Reads are NOT affected — only persist() serializes.
42
+ */
43
+ _writeQueue = Promise.resolve();
33
44
  constructor(deps = {}) {
34
45
  const Ctor = this.constructor;
35
46
  if (Ctor.instance)
@@ -57,7 +68,17 @@ export default class PostgresDB {
57
68
  const pending = files.filter(f => !applied.includes(f));
58
69
  if (pending.length > 0) {
59
70
  this.deps.log.db?.(`${pending.length} pending migration(s) found.`);
60
- const shouldApply = await this.deps.confirm(`${pending.length} pending migration(s) found. Apply now?`);
71
+ let shouldApply;
72
+ if (this.pgConfig.autoMigrate === true) {
73
+ shouldApply = true;
74
+ }
75
+ else if (this.pgConfig.autoMigrate === false) {
76
+ shouldApply = false;
77
+ this.deps.log.warn?.(`autoMigrate is false — skipping ${pending.length} pending migration(s).`);
78
+ }
79
+ else {
80
+ shouldApply = await this.deps.confirm(`${pending.length} pending migration(s) found. Apply now?`);
81
+ }
61
82
  if (shouldApply) {
62
83
  for (const filename of pending) {
63
84
  const content = await this.deps.readFile(this.deps.path.join(migrationsPath, filename));
@@ -76,7 +97,17 @@ export default class PostgresDB {
76
97
  const schemas = this.deps.introspectModels();
77
98
  const modelCount = Object.keys(schemas).length;
78
99
  if (modelCount > 0) {
79
- const shouldGenerate = await this.deps.confirm(`No migrations found but ${modelCount} model(s) detected. Generate and apply initial migration?`);
100
+ let shouldGenerate;
101
+ if (this.pgConfig.autoMigrate === true) {
102
+ shouldGenerate = true;
103
+ }
104
+ else if (this.pgConfig.autoMigrate === false) {
105
+ shouldGenerate = false;
106
+ this.deps.log.warn?.(`autoMigrate is false — skipping initial migration generation for ${modelCount} model(s).`);
107
+ }
108
+ else {
109
+ shouldGenerate = await this.deps.confirm(`No migrations found but ${modelCount} model(s) detected. Generate and apply initial migration?`);
110
+ }
80
111
  if (shouldGenerate) {
81
112
  const { generateMigration } = await import('./migration-generator.js');
82
113
  const result = await generateMigration('initial_setup');
@@ -356,16 +387,22 @@ export default class PostgresDB {
356
387
  const Orm = (await import('@stonyx/orm')).default;
357
388
  if (Orm.instance?.isView?.(modelName))
358
389
  return;
359
- switch (operation) {
360
- case 'create':
361
- return this._persistCreate(modelName, context, response);
362
- case 'update':
363
- return this._persistUpdate(modelName, context, response);
364
- case 'delete':
365
- return this._persistDelete(modelName, context);
366
- }
390
+ const work = async () => {
391
+ switch (operation) {
392
+ case 'create':
393
+ return this._persistCreate(modelName, context, response);
394
+ case 'update':
395
+ return this._persistUpdate(modelName, context, response);
396
+ case 'delete':
397
+ return this._persistDelete(modelName, context);
398
+ }
399
+ };
400
+ // Chain through the write queue — .then(work, work) ensures the queue
401
+ // advances even when a previous persist rejects (#156).
402
+ this._writeQueue = this._writeQueue.then(work, work);
403
+ return this._writeQueue;
367
404
  }
368
- async _persistCreate(modelName, _context, response) {
405
+ async _persistCreate(modelName, context, response) {
369
406
  const schemas = this.deps.introspectModels();
370
407
  const schema = schemas[modelName];
371
408
  if (!schema)
@@ -375,13 +412,17 @@ export default class PostgresDB {
375
412
  const record = recordId != null ? storeRef.get(modelName, isNaN(recordId) ? recordId : parseInt(recordId)) : null;
376
413
  if (!record)
377
414
  return;
378
- const insertData = this._recordToRow(record, schema);
379
- // For auto-increment models, remove the pending ID
380
- const isPendingId = record.__data.__pendingSqlId;
415
+ const insertData = this._recordToRow(record, schema, context.rawData);
416
+ // For auto-increment models, remove the pending ID.
417
+ // Check context.rawData (not record.__data) because __pendingSqlId is not a model
418
+ // attribute and gets lost during serialization.
419
+ const isPendingId = context.rawData?.__pendingSqlId === true;
381
420
  if (isPendingId) {
382
421
  delete insertData.id;
383
422
  }
384
- const { sql, values } = this.deps.buildInsert(schema.table, insertData);
423
+ // Convert the *complete* payload (attributes + id + FKs) in one place (#292).
424
+ const row = this.deps.convertRow(insertData, schema.columns, this.deps.postgresJsonRule);
425
+ const { sql, values } = this.deps.buildInsert(schema.table, row);
385
426
  const result = await this.requirePool().query(sql, values);
386
427
  // Re-key the record in the store if PostgreSQL generated the ID (via RETURNING)
387
428
  if (isPendingId && result.rows.length > 0) {
@@ -420,7 +461,10 @@ export default class PostgresDB {
420
461
  // Check FK changes too
421
462
  for (const fkCol of Object.keys(schema.foreignKeys)) {
422
463
  const relName = fkCol.replace(/_id$/, '');
423
- const currentFkValue = record.__relationships[relName]?.id ?? null;
464
+ const relValue = record.__relationships[relName];
465
+ const currentFkValue = (relValue && typeof relValue === 'object' && relValue !== null)
466
+ ? relValue.id ?? null
467
+ : relValue ?? record.__data[relName] ?? null;
424
468
  const oldFkValue = oldState[relName] ?? null;
425
469
  if (currentFkValue !== oldFkValue) {
426
470
  changedData[fkCol] = currentFkValue;
@@ -430,7 +474,11 @@ export default class PostgresDB {
430
474
  return;
431
475
  // PostgreSQL doesn't have ON UPDATE CURRENT_TIMESTAMP -- set updated_at manually
432
476
  changedData.updated_at = new Date();
433
- const { sql, values } = this.deps.buildUpdate(schema.table, id, changedData);
477
+ // Same conversion, same choke point as the create path (#292). Columns
478
+ // appended after the attribute loop (FKs, updated_at) are absent from
479
+ // schema.columns and pass through untouched.
480
+ const row = this.deps.convertRow(changedData, schema.columns, this.deps.postgresJsonRule);
481
+ const { sql, values } = this.deps.buildUpdate(schema.table, id, row);
434
482
  await this.requirePool().query(sql, values);
435
483
  }
436
484
  async _persistDelete(modelName, context) {
@@ -444,33 +492,38 @@ export default class PostgresDB {
444
492
  const { sql, values } = this.deps.buildDelete(schema.table, id);
445
493
  await this.requirePool().query(sql, values);
446
494
  }
447
- _recordToRow(record, schema) {
495
+ _recordToRow(record, schema, rawData) {
448
496
  const row = {};
449
497
  const data = record.__data;
450
498
  // ID
451
499
  if (data.id !== undefined) {
452
500
  row.id = data.id;
453
501
  }
454
- // Attribute columns
455
- for (const [col, pgType] of Object.entries(schema.columns)) {
502
+ // Attribute columns. Type conversion is NOT applied here -- it is applied
503
+ // once to the finished payload by the caller, via convertRow (#292).
504
+ for (const col of Object.keys(schema.columns)) {
456
505
  if (data[col] !== undefined) {
457
- // JSONB columns: stringify non-string values for PostgreSQL JSONB storage
458
- row[col] = pgType === 'JSONB' && typeof data[col] !== 'string'
459
- ? JSON.stringify(data[col])
460
- : data[col];
506
+ row[col] = data[col];
461
507
  }
462
508
  }
463
509
  // FK columns from relationships
464
510
  for (const fkCol of Object.keys(schema.foreignKeys)) {
465
511
  const relName = fkCol.replace(/_id$/, '');
466
512
  const related = record.__relationships[relName];
467
- if (related) {
513
+ if (related && typeof related === 'object' && related !== null) {
468
514
  row[fkCol] = related.id;
469
515
  }
516
+ else if (related != null) {
517
+ // Raw FK value (e.g., string ID stored directly in __relationships)
518
+ row[fkCol] = related;
519
+ }
470
520
  else if (data[relName] !== undefined) {
471
- // Raw FK value (e.g., from create payload)
472
521
  row[fkCol] = data[relName];
473
522
  }
523
+ else if (rawData?.[relName] !== undefined) {
524
+ // Fallback to original create payload for unresolved belongsTo FKs
525
+ row[fkCol] = rawData[relName];
526
+ }
474
527
  }
475
528
  return row;
476
529
  }
package/dist/record.js CHANGED
@@ -40,11 +40,13 @@ export default class Record {
40
40
  const records = {};
41
41
  for (const [key, childRecord] of Object.entries(this.__relationships)) {
42
42
  if (Array.isArray(childRecord)) {
43
+ // Filter out cleaned records (those with no __model)
44
+ const live = childRecord.filter((r) => r?.__model);
43
45
  // Deduplicate by record ID — keep last occurrence (latest data wins)
44
46
  const seen = new Set();
45
47
  const unique = [];
46
- for (let i = childRecord.length - 1; i >= 0; i--) {
47
- const r = childRecord[i];
48
+ for (let i = live.length - 1; i >= 0; i--) {
49
+ const r = live[i];
48
50
  if (!seen.has(r.id)) {
49
51
  seen.add(r.id);
50
52
  unique.push(r);
@@ -54,7 +56,7 @@ export default class Record {
54
56
  records[key] = unique.map((r) => r.serialize());
55
57
  }
56
58
  else {
57
- records[key] = childRecord?.serialize() ?? null;
59
+ records[key] = childRecord?.__model ? childRecord.serialize() : null;
58
60
  }
59
61
  }
60
62
  return { ...data, ...records };
@@ -86,8 +88,8 @@ export default class Record {
86
88
  if (fields && !fields.has(key))
87
89
  continue;
88
90
  const relationshipData = Array.isArray(childRecord)
89
- ? childRecord.map((r) => ({ type: r.__model.__name, id: r.id }))
90
- : childRecord ? { type: childRecord.__model.__name, id: childRecord.id } : null;
91
+ ? childRecord.filter((r) => r?.__model).map((r) => ({ type: r.__model.__name, id: r.id }))
92
+ : (childRecord && childRecord.__model) ? { type: childRecord.__model.__name, id: childRecord.id } : null;
91
93
  // Dasherize the key for URL paths (e.g., accessLinks -> access-links)
92
94
  const dasherizedKey = camelCaseToKebabCase(key);
93
95
  relationships[dasherizedKey] = { data: relationshipData };
@@ -38,4 +38,4 @@ export function getPendingRegistry() {
38
38
  export function getPendingBelongsToRegistry() {
39
39
  return relationships.get('pendingBelongsTo');
40
40
  }
41
- export const TYPES = ['global', 'hasMany', 'belongsTo', 'pending'];
41
+ export const TYPES = ['global', 'hasMany', 'belongsTo', 'pending', 'pendingBelongsTo'];
@@ -79,8 +79,44 @@ export default class Serializer {
79
79
  // Pass relationship key name to handler for pending fulfillment
80
80
  const handlerOptions = { ...options, _relationshipKey: key };
81
81
  const childRecord = handler(record, data, handlerOptions);
82
- rec[key] = childRecord;
83
- relatedRecords[key] = childRecord;
82
+ // hasMany relationships use a getter so format()/toJSON() always read
83
+ // the live registry array instead of a stale snapshot captured at
84
+ // serialization time. This is critical when child records are created
85
+ // in a later async frame — the belongsTo inverse wiring pushes into
86
+ // the shared registry array, and the getter ensures the parent sees it.
87
+ const isHasMany = handler.__relationshipType === 'hasMany';
88
+ if (isHasMany) {
89
+ // `childRecord` IS the shared registry array — define a getter that
90
+ // always dereferences through the same array reference.
91
+ const registryArray = childRecord;
92
+ Object.defineProperty(rec, key, {
93
+ enumerable: true,
94
+ configurable: true,
95
+ get: () => registryArray,
96
+ set(v) { relatedRecords[key] = v; }
97
+ });
98
+ Object.defineProperty(relatedRecords, key, {
99
+ enumerable: true,
100
+ configurable: true,
101
+ get: () => registryArray,
102
+ set(v) { Object.defineProperty(relatedRecords, key, { value: v, writable: true, enumerable: true, configurable: true }); }
103
+ });
104
+ }
105
+ else {
106
+ rec[key] = childRecord;
107
+ relatedRecords[key] = childRecord;
108
+ // Preserve the raw FK value in __data when the belongsTo handler
109
+ // couldn't resolve the target (e.g., memory:false model not loaded).
110
+ // This allows adapters to read the FK from __data as a fallback
111
+ // when __relationships[key] is null. Only store when `data` is a
112
+ // truthy non-object — i.e., a raw FK string/number that the handler
113
+ // attempted but failed to resolve. When `data` is null/undefined
114
+ // (optional empty relationship) we intentionally skip to preserve
115
+ // the existing behavior of not populating __data for empty FKs.
116
+ if (childRecord === null && data && typeof data !== 'object') {
117
+ parsedData[key] = data;
118
+ }
119
+ }
84
120
  continue;
85
121
  }
86
122
  // Aggregate property handling — use the rawData value, not the aggregate descriptor
package/dist/store.d.ts CHANGED
@@ -45,7 +45,19 @@ export default class Store {
45
45
  */
46
46
  private _isMemoryModel;
47
47
  set(key: string, value: Map<number | string, unknown>): void;
48
- remove(key: string, id?: number | string): void;
48
+ remove(key: string, id?: number | string, options?: {
49
+ _skipAutoPersist?: boolean;
50
+ }): void;
51
+ /**
52
+ * Evict a record from the store with full relationship registry cleanup.
53
+ * The caller retains its reference to the returned record, which is the
54
+ * contract memory:false post-persist eviction relies on.
55
+ *
56
+ * @param registryId - The ID used when the record's relationships were
57
+ * registered. For SQL models with pending IDs, this is the original
58
+ * negative pending ID (before the adapter re-keyed to the real DB ID).
59
+ */
60
+ evictRecord(modelName: string, id: unknown, registryId?: unknown): void;
49
61
  unloadRecord(model: string, id: unknown, options?: UnloadOptions): void;
50
62
  unloadAllRecords(model: string, options?: UnloadOptions): void;
51
63
  private _removeFromHasManyArrays;
package/dist/store.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import Orm, { relationships } from '@stonyx/orm';
2
- import { TYPES, getHasManyRegistry, getBelongsToRegistry, getPendingRegistry } from './relationships.js';
2
+ import { TYPES, getHasManyRegistry, getBelongsToRegistry, getPendingRegistry, getPendingBelongsToRegistry } from './relationships.js';
3
3
  import ViewResolver from './view-resolver.js';
4
4
  function isStoreRecord(value) {
5
5
  return typeof value === 'object' && value !== null && '__data' in value;
@@ -22,7 +22,7 @@ export default class Store {
22
22
  this.data = new Map();
23
23
  }
24
24
  get(key, id) {
25
- if (!id)
25
+ if (id === undefined)
26
26
  return this.data.get(key);
27
27
  return this.data.get(key)?.get(id);
28
28
  }
@@ -107,15 +107,61 @@ export default class Store {
107
107
  set(key, value) {
108
108
  this.data.set(key, value);
109
109
  }
110
- remove(key, id) {
110
+ remove(key, id, options) {
111
111
  // Guard: read-only views cannot have records removed
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 (fire-and-forget) — skipped when the
116
+ // request path handles persist itself to avoid double-delete.
117
+ if (id && Orm.instance?.sqlDb && !options?._skipAutoPersist) {
118
+ Orm.instance.sqlDb.persist('delete', key, { recordId: id }, {}).catch((err) => {
119
+ Orm.instance.emitPersistError({
120
+ operation: 'delete',
121
+ modelName: key,
122
+ recordId: id,
123
+ error: err instanceof Error ? err : new Error(String(err)),
124
+ });
125
+ });
126
+ }
115
127
  if (id)
116
128
  return this.unloadRecord(key, id);
117
129
  this.unloadAllRecords(key);
118
130
  }
131
+ /**
132
+ * Evict a record from the store with full relationship registry cleanup.
133
+ * The caller retains its reference to the returned record, which is the
134
+ * contract memory:false post-persist eviction relies on.
135
+ *
136
+ * @param registryId - The ID used when the record's relationships were
137
+ * registered. For SQL models with pending IDs, this is the original
138
+ * negative pending ID (before the adapter re-keyed to the real DB ID).
139
+ */
140
+ evictRecord(modelName, id, registryId) {
141
+ const modelStore = this.data.get(modelName);
142
+ if (!modelStore)
143
+ return;
144
+ if (typeof id !== 'string' && typeof id !== 'number')
145
+ return;
146
+ const raw = modelStore.get(id);
147
+ if (!raw || !isStoreRecord(raw))
148
+ return;
149
+ const visited = new Set([`${modelName}:${id}`]);
150
+ // Remove from hasMany arrays and nullify belongsTo references using current ID
151
+ // (the adapter updates record.id, so value-based matches need the current ID)
152
+ this._removeFromHasManyArrays(modelName, id, visited);
153
+ this._nullifyBelongsToReferences(modelName, id, visited);
154
+ // Clean up relationship registry entries using the registry key
155
+ // (belongsTo/hasMany registries were keyed by the ID at registration time,
156
+ // which may differ from the current ID if SQL persist re-keyed the record)
157
+ const cleanupId = registryId ?? id;
158
+ this._cleanupRelationshipRegistries(modelName, cleanupId);
159
+ // If registryId differs from id, also clean with current id as safety net
160
+ if (registryId !== undefined && registryId !== id) {
161
+ this._cleanupRelationshipRegistries(modelName, id);
162
+ }
163
+ modelStore.delete(id);
164
+ }
119
165
  unloadRecord(model, id, options = {}) {
120
166
  const modelStore = this.data.get(model);
121
167
  if (!modelStore) {
@@ -138,7 +184,6 @@ export default class Store {
138
184
  this._removeFromHasManyArrays(modelName, recordId, visited);
139
185
  this._nullifyBelongsToReferences(modelName, recordId, visited);
140
186
  this._cleanupRelationshipRegistries(modelName, recordId);
141
- recordToUnload.clean();
142
187
  this.data.get(modelName)?.delete(recordId);
143
188
  }
144
189
  }
@@ -219,6 +264,31 @@ export default class Store {
219
264
  const pendingMap = getPendingRegistry().get(modelName);
220
265
  if (pendingMap)
221
266
  pendingMap.delete(recordId);
267
+ // Clean pendingBelongsTo entries in both directions
268
+ const pendingBelongsToMap = getPendingBelongsToRegistry();
269
+ if (pendingBelongsToMap) {
270
+ // Direction 1: evicted record was the TARGET others were waiting for
271
+ const targetEntries = pendingBelongsToMap.get(modelName);
272
+ if (targetEntries)
273
+ targetEntries.delete(recordId);
274
+ // Direction 2: evicted record was the SOURCE with unresolved forward-references
275
+ for (const [, targetIdMap] of pendingBelongsToMap) {
276
+ for (const [targetId, entries] of targetIdMap) {
277
+ if (!Array.isArray(entries))
278
+ continue;
279
+ const filtered = entries.filter((e) => {
280
+ const entry = e;
281
+ return !(entry.sourceModelName === modelName && entry.relationshipId === recordId);
282
+ });
283
+ if (filtered.length === 0) {
284
+ targetIdMap.delete(targetId);
285
+ }
286
+ else if (filtered.length < entries.length) {
287
+ targetIdMap.set(targetId, filtered);
288
+ }
289
+ }
290
+ }
291
+ }
222
292
  }
223
293
  /**
224
294
  * Extracts hasMany and non-bidirectional belongsTo children from a record
@@ -16,6 +16,7 @@ export interface OrmMysqlConfig {
16
16
  connectionLimit?: number;
17
17
  migrationsDir?: string;
18
18
  migrationsTable?: string;
19
+ autoMigrate?: boolean;
19
20
  [key: string]: unknown;
20
21
  }
21
22
  export interface OrmPostgresConfig {
@@ -27,6 +28,7 @@ export interface OrmPostgresConfig {
27
28
  connectionLimit?: number;
28
29
  migrationsDir?: string;
29
30
  migrationsTable?: string;
31
+ autoMigrate?: boolean;
30
32
  [key: string]: unknown;
31
33
  }
32
34
  export interface OrmPaths {
@@ -42,6 +44,12 @@ export interface OrmRestServerConfig {
42
44
  route: string;
43
45
  metaRoute: boolean;
44
46
  }
47
+ export interface OrmDynamoDBConfig {
48
+ region?: string;
49
+ endpoint?: string;
50
+ tablePrefix?: string;
51
+ [key: string]: unknown;
52
+ }
45
53
  export interface OrmSection {
46
54
  db: OrmDbConfig;
47
55
  paths: OrmPaths;
@@ -49,6 +57,9 @@ export interface OrmSection {
49
57
  mysql?: OrmMysqlConfig;
50
58
  postgres?: OrmPostgresConfig;
51
59
  timescale?: OrmPostgresConfig;
60
+ dynamodb?: OrmDynamoDBConfig;
61
+ logColor?: string;
62
+ logMethod?: string;
52
63
  [key: string]: unknown;
53
64
  }
54
65
  export interface OrmConfig {
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "stonyx-async",
5
5
  "stonyx-module"
6
6
  ],
7
- "version": "0.3.2-beta.3",
7
+ "version": "0.3.2-beta.300",
8
8
  "description": "",
9
9
  "main": "dist/index.js",
10
10
  "type": "module",
@@ -61,16 +61,25 @@
61
61
  },
62
62
  "homepage": "https://github.com/abofs/stonyx-orm#readme",
63
63
  "dependencies": {
64
- "@stonyx/cron": "0.2.1-beta.29",
65
- "@stonyx/events": "0.1.1-beta.9",
66
- "stonyx": "0.2.3-beta.11"
64
+ "@stonyx/cron": "0.2.1-beta.159",
65
+ "@stonyx/events": "0.1.1-beta.71",
66
+ "@stonyx/utils": "0.2.3-beta.28",
67
+ "stonyx": "0.2.3-beta.104"
67
68
  },
68
69
  "peerDependencies": {
70
+ "@aws-sdk/client-dynamodb": "^3.0.0",
71
+ "@aws-sdk/lib-dynamodb": "^3.0.0",
69
72
  "@stonyx/rest-server": ">=0.2.1-beta.14",
70
73
  "mysql2": "^3.0.0",
71
74
  "pg": "^8.0.0"
72
75
  },
73
76
  "peerDependenciesMeta": {
77
+ "@aws-sdk/client-dynamodb": {
78
+ "optional": true
79
+ },
80
+ "@aws-sdk/lib-dynamodb": {
81
+ "optional": true
82
+ },
74
83
  "mysql2": {
75
84
  "optional": true
76
85
  },
@@ -82,18 +91,22 @@
82
91
  }
83
92
  },
84
93
  "devDependencies": {
85
- "@stonyx/rest-server": "0.2.1-beta.30",
86
- "@stonyx/utils": "0.2.3-beta.7",
94
+ "@stonyx/rest-server": "0.2.1-beta.162",
87
95
  "@types/node": "^25.6.0",
88
96
  "mysql2": "^3.20.0",
89
97
  "pg": "^8.20.0",
90
98
  "qunit": "^2.24.1",
91
99
  "sinon": "^21.0.0",
100
+ "tsx": "^4.21.0",
92
101
  "typescript": "^5.8.3"
93
102
  },
94
103
  "scripts": {
95
104
  "build": "tsc",
96
105
  "build:test": "tsc -p tsconfig.test.json",
97
- "test": "npm run build && npm run build:test && stonyx test 'dist-test/test/**/*-test.js'"
106
+ "test": "pnpm build && NODE_ENV=test node --import tsx/esm --import ./test/setup.ts node_modules/qunit/bin/qunit.js 'test/**/*-test.ts' && ORM_TEST_ROUTE=/ pnpm test:mounted && ORM_TEST_ROUTE=/api pnpm test:mounted && ORM_TEST_ROUTE=api pnpm test:mounted && ORM_TEST_ROUTE=/api/v1 pnpm test:mounted && ORM_TEST_ROUTE=/api/ pnpm test:mounted && pnpm test:readme && pnpm test:reference",
107
+ "test:mounted": "node --import tsx/esm --import ./test/integration/mounted-route/setup.ts node_modules/qunit/bin/qunit.js 'test/integration/mounted-route/links-mounted.ts' 'test/zz-exit-test.ts'",
108
+ "test:readme": "pnpm build && node --import tsx/esm --import ./test/integration/readme-access/setup.ts node_modules/qunit/bin/qunit.js 'test/integration/readme-access/readme-sample.ts' 'test/zz-exit-test.ts'",
109
+ "test:reference": "pnpm build && node --import tsx/esm --import ./test/integration/reference-access/setup.ts node_modules/qunit/bin/qunit.js 'test/integration/reference-access/reference-sample.ts' 'test/integration/reference-access/record-id-resolution.ts' 'test/zz-exit-test.ts'",
110
+ "test:dynamodb": "pnpm build && node --import tsx/esm --import ./test/integration/dynamodb/setup.ts node_modules/qunit/bin/qunit.js 'test/integration/dynamodb/**/*-test.ts'"
98
111
  }
99
112
  }
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Shared write-path column conversion (#292).
3
+ *
4
+ * This module owns the *iteration* only — the single place where a write
5
+ * payload is walked column-by-column and each value is handed to the driver's
6
+ * own conversion rule. The rules themselves are genuinely disjoint per driver
7
+ * (Postgres stringifies for JSONB; DynamoDB's DocumentClient marshals objects
8
+ * and arrays natively and must NOT be given stringified values) and therefore
9
+ * deliberately live with their drivers rather than being merged here.
10
+ *
11
+ * The duplicated create/update iteration was the drift vector that produced
12
+ * #292: `_recordToRow` converted, `_persistUpdate` did not. Routing both
13
+ * through `convertRow` removes the opportunity for the two to diverge again.
14
+ */
15
+
16
+ /**
17
+ * A driver-supplied per-column conversion rule.
18
+ *
19
+ * @param value the raw value about to be bound into the query
20
+ * @param columnType the column's declared type from `schema.columns`, or
21
+ * `undefined` when the column is not an attribute column
22
+ * (e.g. `updated_at`, FK columns and `id`, which are added
23
+ * outside the attribute loop). Rules MUST be a no-op for
24
+ * `undefined`.
25
+ */
26
+ export type ColumnRule = (value: unknown, columnType: string | undefined) => unknown;
27
+
28
+ /**
29
+ * Applies `rule` to every column of a complete write payload.
30
+ *
31
+ * Call this on the *finished* payload immediately before handing it to the
32
+ * query builder — not inside a payload-building loop. Columns appended after
33
+ * such a loop (FKs, `updated_at`) would otherwise bypass conversion, which is
34
+ * the same class of defect #292 reports.
35
+ */
36
+ export function convertRow(
37
+ row: Record<string, unknown>,
38
+ columns: Record<string, string>,
39
+ rule: ColumnRule
40
+ ): Record<string, unknown> {
41
+ const converted: Record<string, unknown> = {};
42
+
43
+ for (const [col, value] of Object.entries(row)) {
44
+ // `Object.hasOwn`, not `columns[col]`: a column named `toString` or
45
+ // `constructor` would otherwise reach the rule with a function as its
46
+ // "declared type". Inert for the shipped Postgres rule (strict equality
47
+ // against 'JSONB'), but `ColumnRule` is the extension point other drivers
48
+ // write against, and the obvious `if (columnType)` rule would invert.
49
+ converted[col] = rule(value, Object.hasOwn(columns, col) ? columns[col] : undefined);
50
+ }
51
+
52
+ return converted;
53
+ }
package/src/commands.ts CHANGED
@@ -28,6 +28,13 @@ const commands: Record<string, Command> = {
28
28
  description: 'Generate a MySQL migration from current model schemas',
29
29
  bootstrap: true,
30
30
  run: async (args) => {
31
+ const config = (await import('stonyx/config')).default;
32
+
33
+ if (config.orm.dynamodb) {
34
+ console.log('DynamoDB does not use file-based migrations. Use db:sync to provision tables.');
35
+ return;
36
+ }
37
+
31
38
  const description = args?.join(' ') || 'migration';
32
39
  const { generateMigration } = await import('./mysql/migration-generator.js');
33
40
  const result = await generateMigration(description);
@@ -39,6 +46,25 @@ const commands: Record<string, Command> = {
39
46
  }
40
47
  }
41
48
  },
49
+ 'db:sync': {
50
+ description: 'Provision DynamoDB tables and GSIs from current model schemas',
51
+ bootstrap: true,
52
+ run: async () => {
53
+ const config = (await import('stonyx/config')).default;
54
+
55
+ if (!config.orm.dynamodb) {
56
+ console.error('DynamoDB is not configured. Set DYNAMODB_REGION (and optionally DYNAMODB_ENDPOINT) to enable DynamoDB mode.');
57
+ process.exit(1);
58
+ }
59
+
60
+ const { default: DynamoDBDB } = await import('./dynamodb/dynamodb-db.js');
61
+ const db = new DynamoDBDB();
62
+ await db.init();
63
+ await db.startup();
64
+ await db.shutdown();
65
+ console.log('DynamoDB tables synced successfully.');
66
+ }
67
+ },
42
68
  'db:migrate': {
43
69
  description: 'Apply pending MySQL migrations',
44
70
  bootstrap: true,
@@ -46,6 +72,11 @@ const commands: Record<string, Command> = {
46
72
  const config = (await import('stonyx/config')).default;
47
73
  const mysqlConfig = config.orm.mysql;
48
74
 
75
+ if (config.orm.dynamodb) {
76
+ console.log('DynamoDB does not use file-based migrations. Use db:sync to provision tables.');
77
+ return;
78
+ }
79
+
49
80
  if (!mysqlConfig) {
50
81
  console.error('MySQL is not configured. Set MYSQL_HOST to enable MySQL mode.');
51
82
  process.exit(1);
@@ -92,6 +123,12 @@ const commands: Record<string, Command> = {
92
123
  bootstrap: true,
93
124
  run: async () => {
94
125
  const config = (await import('stonyx/config')).default;
126
+
127
+ if (config.orm.dynamodb) {
128
+ console.log('DynamoDB does not support migration rollback. Manage table changes via the AWS console or db:sync.');
129
+ return;
130
+ }
131
+
95
132
  const mysqlConfig = config.orm.mysql;
96
133
 
97
134
  if (!mysqlConfig) {
@@ -138,6 +175,12 @@ const commands: Record<string, Command> = {
138
175
  bootstrap: true,
139
176
  run: async () => {
140
177
  const config = (await import('stonyx/config')).default;
178
+
179
+ if (config.orm.dynamodb) {
180
+ console.log('DynamoDB does not use file-based migrations. Use db:sync to provision tables.');
181
+ return;
182
+ }
183
+
141
184
  const mysqlConfig = config.orm.mysql;
142
185
 
143
186
  if (!mysqlConfig) {