@stonyx/orm 0.3.2-alpha.12 → 0.3.2-alpha.120

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 (51) hide show
  1. package/README.md +337 -28
  2. package/dist/column-conversion.d.ts +34 -0
  3. package/dist/column-conversion.js +34 -0
  4. package/dist/dynamodb/connection.d.ts +7 -4
  5. package/dist/dynamodb/connection.js +4 -4
  6. package/dist/dynamodb/dynamodb-db.d.ts +11 -0
  7. package/dist/dynamodb/dynamodb-db.js +54 -14
  8. package/dist/dynamodb/operation-builder.js +20 -13
  9. package/dist/index.d.ts +2 -0
  10. package/dist/index.js +1 -0
  11. package/dist/main.js +19 -1
  12. package/dist/manage-record.js +34 -3
  13. package/dist/mysql/connection.d.ts +1 -0
  14. package/dist/mysql/mysql-db.d.ts +8 -0
  15. package/dist/mysql/mysql-db.js +44 -10
  16. package/dist/normalize-record-id.d.ts +67 -0
  17. package/dist/normalize-record-id.js +88 -0
  18. package/dist/orm-request.d.ts +17 -1
  19. package/dist/orm-request.js +77 -19
  20. package/dist/postgres/column-rules.d.ts +33 -0
  21. package/dist/postgres/column-rules.js +34 -0
  22. package/dist/postgres/connection.d.ts +1 -0
  23. package/dist/postgres/connection.js +8 -6
  24. package/dist/postgres/postgres-db.d.ts +12 -0
  25. package/dist/postgres/postgres-db.js +60 -19
  26. package/dist/record.js +7 -5
  27. package/dist/relationships.js +1 -1
  28. package/dist/serializer.js +38 -2
  29. package/dist/store.d.ts +13 -1
  30. package/dist/store.js +65 -6
  31. package/dist/types/orm-types.d.ts +3 -0
  32. package/package.json +11 -7
  33. package/src/column-conversion.ts +53 -0
  34. package/src/dynamodb/connection.ts +9 -8
  35. package/src/dynamodb/dynamodb-db.ts +56 -13
  36. package/src/dynamodb/operation-builder.ts +29 -15
  37. package/src/index.ts +2 -0
  38. package/src/main.ts +19 -1
  39. package/src/manage-record.ts +41 -9
  40. package/src/mysql/connection.ts +1 -0
  41. package/src/mysql/mysql-db.ts +44 -12
  42. package/src/normalize-record-id.ts +88 -0
  43. package/src/orm-request.ts +109 -18
  44. package/src/postgres/column-rules.ts +37 -0
  45. package/src/postgres/connection.ts +10 -6
  46. package/src/postgres/postgres-db.ts +64 -21
  47. package/src/record.ts +8 -5
  48. package/src/relationships.ts +1 -1
  49. package/src/serializer.ts +39 -2
  50. package/src/store.ts +68 -6
  51. package/src/types/orm-types.ts +3 -0
@@ -84,6 +84,15 @@ export default class MysqlDB {
84
84
  pool!: Pool | null;
85
85
  mysqlConfig!: MysqlConfig;
86
86
 
87
+ /**
88
+ * Promise-chain mutex for write serialization (#156).
89
+ * All persist() calls chain through this single queue so concurrent
90
+ * fire-and-forget writes never produce parallel InnoDB transactions
91
+ * on FK-linked rows (which cause deadlocks).
92
+ * Reads are NOT affected — only persist() serializes.
93
+ */
94
+ private _writeQueue: Promise<void> = Promise.resolve();
95
+
87
96
  constructor(deps: Partial<MysqlDBDeps> = {}) {
88
97
  if (MysqlDB.instance) return MysqlDB.instance;
89
98
  MysqlDB.instance = this;
@@ -118,7 +127,15 @@ export default class MysqlDB {
118
127
  if (pending.length > 0) {
119
128
  this.deps.log.db?.(`${pending.length} pending migration(s) found.`);
120
129
 
121
- const shouldApply = await this.deps.confirm(`${pending.length} pending migration(s) found. Apply now?`);
130
+ let shouldApply: boolean;
131
+ if (this.mysqlConfig.autoMigrate === true) {
132
+ shouldApply = true;
133
+ } else if (this.mysqlConfig.autoMigrate === false) {
134
+ shouldApply = false;
135
+ this.deps.log.warn?.(`autoMigrate is false — skipping ${pending.length} pending migration(s).`);
136
+ } else {
137
+ shouldApply = await this.deps.confirm(`${pending.length} pending migration(s) found. Apply now?`);
138
+ }
122
139
 
123
140
  if (shouldApply) {
124
141
  for (const filename of pending) {
@@ -139,9 +156,17 @@ export default class MysqlDB {
139
156
  const modelCount = Object.keys(schemas).length;
140
157
 
141
158
  if (modelCount > 0) {
142
- const shouldGenerate = await this.deps.confirm(
143
- `No migrations found but ${modelCount} model(s) detected. Generate and apply initial migration?`
144
- );
159
+ let shouldGenerate: boolean;
160
+ if (this.mysqlConfig.autoMigrate === true) {
161
+ shouldGenerate = true;
162
+ } else if (this.mysqlConfig.autoMigrate === false) {
163
+ shouldGenerate = false;
164
+ this.deps.log.warn?.(`autoMigrate is false — skipping initial migration generation for ${modelCount} model(s).`);
165
+ } else {
166
+ shouldGenerate = await this.deps.confirm(
167
+ `No migrations found but ${modelCount} model(s) detected. Generate and apply initial migration?`
168
+ );
169
+ }
145
170
 
146
171
  if (shouldGenerate) {
147
172
  const { generateMigration } = await import('./migration-generator.js');
@@ -398,14 +423,21 @@ export default class MysqlDB {
398
423
  const Orm = (await import('@stonyx/orm')).default;
399
424
  if ((Orm as unknown as { instance?: { isView?: (name: string) => boolean } }).instance?.isView?.(modelName)) return;
400
425
 
401
- switch (operation) {
402
- case 'create':
403
- return this._persistCreate(modelName, context, response);
404
- case 'update':
405
- return this._persistUpdate(modelName, context, response);
406
- case 'delete':
407
- return this._persistDelete(modelName, context);
408
- }
426
+ const work = async () => {
427
+ switch (operation) {
428
+ case 'create':
429
+ return this._persistCreate(modelName, context, response);
430
+ case 'update':
431
+ return this._persistUpdate(modelName, context, response);
432
+ case 'delete':
433
+ return this._persistDelete(modelName, context);
434
+ }
435
+ };
436
+
437
+ // Chain through the write queue — .then(work, work) ensures the queue
438
+ // advances even when a previous persist rejects (#156).
439
+ this._writeQueue = this._writeQueue.then(work, work);
440
+ return this._writeQueue;
409
441
  }
410
442
 
411
443
  private async _persistCreate(modelName: string, context: PersistContext, response: PersistResponse): Promise<void> {
@@ -0,0 +1,88 @@
1
+ /*
2
+ * Copyright 2025 Stone Costa
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the 'License');
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an 'AS IS' BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ /**
18
+ * The ONE place a URL id is turned into the value a record is resolved by —
19
+ * abofs/stonyx-orm#270.
20
+ *
21
+ * Before this existed this coercion was written out seven times: twice in
22
+ * README.md, once in docs/usage-patterns.md, and four times inside the
23
+ * framework. Seven copies, not seven identical copies — the three persistence
24
+ * ones (abofs/stonyx-orm#282) omit the `if (!id) return ''` guard below, which
25
+ * is the point: nothing held them together, so they had already drifted.
26
+ * The framework's copy — `getId()`, module-private in
27
+ * orm-request.ts, unreachable through the package `exports` map — was the one
28
+ * that decided which record a request addressed, and a consumer's `access()`
29
+ * predicate had no way to obtain it. So the framework resolved the record by
30
+ * one value and asked the consumer to authorize on a different one.
31
+ *
32
+ * Measured consequence at the time of filing: with the documented predicate
33
+ * applied verbatim to a numeric-id model, `GET /animals/007`, `/7.9`, `/7e0`,
34
+ * `/0x7`, `/%207`, `/%2B7` and `/7%0A` each served the protected record, and
35
+ * `DELETE /animals/007` destroyed it — because `parseInt` folds every one of
36
+ * those onto `7` while `'007' === '7'` is false.
37
+ *
38
+ * Two things follow from this being a single exported function, and both are
39
+ * the point:
40
+ *
41
+ * 1. A permissive change here is HARMLESS, because both sides move together.
42
+ * Lowercasing string ids used to disclose and destroy `owner:angela` with
43
+ * the whole suite green; with one implementation the predicate simply sees
44
+ * the same lowercased value and still refuses.
45
+ * 2. A divergence — a second, private normaliser at the resolution site — is
46
+ * what is now dangerous, and that is what the tests pin, by observing the
47
+ * key the resolution path actually used.
48
+ *
49
+ * SEMANTICS ARE UNCHANGED FROM `getId()`, deliberately and byte-for-byte.
50
+ * Whether `7.9` / `0x7` / `%0A` *should* resolve to record 7 at all is a real
51
+ * question, and it is a behaviour change on the record-resolution path for
52
+ * every consumer rather than an authorization fix — it belongs in its own
53
+ * issue with its own compatibility argument (issue body scope item 4). #270
54
+ * preserves today's semantics exactly and makes both sides agree on them.
55
+ *
56
+ * Two details are load-bearing and are pinned by
57
+ * test/unit/normalize-record-id-test.ts:
58
+ *
59
+ * - `parseInt` is called with NO RADIX. `parseInt('0x7')` is 7;
60
+ * `parseInt('0x7', 10)` is 0. Adding the radix looks like a cleanup and
61
+ * silently changes which record every hex-spelled URL addresses.
62
+ * - The coercion applies only when the id LOOKS numeric, so a model with
63
+ * string ids is passed through untouched, case included.
64
+ *
65
+ * Must stay synchronous: `auth()` is invoked without `await` by
66
+ * @stonyx/rest-server, so a promise here would be handed to `access()` as the
67
+ * record id.
68
+ *
69
+ * FALSY AND `NaN` RETURNS ARE LOAD-BEARING ELSEWHERE — abofs/stonyx-orm#287.
70
+ * `''` is returned for any falsy id because `store.get(key, undefined)` returns
71
+ * the whole model Map rather than a record (abofs/stonyx-orm#167). But `''` is
72
+ * not the only falsy return: `'0'`, `'00'`, `'-0'` and `'0x0'` all normalise to
73
+ * `0`, and `' '`, `'\t'`, `'\n'`, `'\u00a0'` all normalise to `NaN` (their
74
+ * `Number()` is `0`, so the `isNaN` guard does not fire and `parseInt` runs).
75
+ * `store.remove(key, id)` branches on truthiness, so those spellings reach a
76
+ * fall-through this function does not own. Tracked as #287; every row is pinned
77
+ * in test/unit/normalize-record-id-test.ts so a cleanup here cannot move the
78
+ * boundary #287 is measured against.
79
+ *
80
+ * @param id the raw, already-URL-decoded id text from `request.params.id`
81
+ * @returns the value the ORM resolves the record by
82
+ */
83
+ export default function normalizeRecordId(id?: string | null): string | number {
84
+ if (!id) return '';
85
+ if (isNaN(id as unknown as number)) return id;
86
+
87
+ return parseInt(id);
88
+ }
@@ -7,11 +7,38 @@ import type { HookContext } from './hooks.js';
7
7
  import config from 'stonyx/config';
8
8
  import type { OrmRecord } from './types/orm-types.js';
9
9
  import { isOrmRecord } from './utils.js';
10
+ import normalizeRecordId from './normalize-record-id.js';
10
11
 
11
- interface OrmRequest$ extends Request {
12
+ /**
13
+ * The request object a consumer's `access()` predicate receives.
14
+ *
15
+ * Exported because `recordId` is public API — README's `access()` samples
16
+ * destructure it — and a public runtime field with an unreachable type asks the
17
+ * consumer to re-declare something the framework already knows, which is
18
+ * abofs/stonyx-orm#270's own defect shape one layer over into the type surface.
19
+ * `HookContext` (src/hooks.ts) is this repo's precedent for exporting the
20
+ * interface a consumer is handed. Re-exported from the root barrel as
21
+ * `OrmAccessRequest` (src/index.ts) — NOT as `OrmRequest`, which is already the
22
+ * default-exported CLASS in this file and means something else. The
23
+ * `./orm-request` subpath is not in the `exports` map, so the barrel is the
24
+ * only reachable spelling.
25
+ */
26
+ export interface OrmRequest$ extends Request {
12
27
  protocol?: string;
28
+ // Express sets this to the path the router was mounted at, e.g. '/api/animals'
29
+ // when orm.restServer.route is '/api'. Optional because non-Express callers
30
+ // (unit tests, programmatic handler invocation) do not supply it.
31
+ baseUrl?: string;
13
32
  method: string;
14
33
  params: { [key: string]: string };
34
+ // Attached by auth() before access() runs — abofs/stonyx-orm#270. This is the
35
+ // value the ORM resolves the record by; `params.id` remains the raw client
36
+ // text it was parsed from.
37
+ //
38
+ // `undefined` is part of the contract, not an absence: it is how a collection
39
+ // route is told from a record route, and both documented samples branch on
40
+ // it. Spelled out rather than left to `?:` for that reason.
41
+ recordId?: string | number | undefined;
15
42
  body?: { [key: string]: unknown };
16
43
  query?: { [key: string]: string };
17
44
  get(header: string): string;
@@ -77,19 +104,49 @@ function getModelRelationships(modelName: string): { [key: string]: Relationship
77
104
  return relationships;
78
105
  }
79
106
 
80
- // Helper to build base URL from request
81
- function getBaseUrl(request: OrmRequest$): string {
107
+ /**
108
+ * Build the absolute base URL that every advertised link hangs off — origin
109
+ * plus the prefix the ORM's routes are actually mounted at.
110
+ *
111
+ * The prefix is derived from the *request*, not from
112
+ * `config.orm.restServer.route`. Express sets `request.baseUrl` to the real
113
+ * mountpath registered by `RestServer.mountRoute`, which for this module is
114
+ * always `<prefix>/<pluralizedModel>` (see setup-rest-server.ts). Stripping the
115
+ * trailing model segment therefore yields the prefix by construction, and the
116
+ * link builder cannot drift from the mount registrar the way a second,
117
+ * independent normalisation of `route` would.
118
+ *
119
+ * When `request.baseUrl` is absent or does not end in the model segment the
120
+ * prefix is empty, which reproduces the previous origin-only behaviour.
121
+ */
122
+ function getBaseUrl(request: OrmRequest$, pluralizedModel: string): string {
82
123
  const protocol = request.protocol || 'http';
83
124
  const host = request.get('host');
84
- return `${protocol}://${host}`;
125
+
126
+ const modelSegment = `/${pluralizedModel}`;
127
+ const mountPath = request.baseUrl ?? '';
128
+ const prefix = mountPath.endsWith(modelSegment) ? mountPath.slice(0, -modelSegment.length) : '';
129
+
130
+ return `${protocol}://${host}${prefix}`;
85
131
  }
86
132
 
133
+ // Kept as a name because twelve `getId(...)` call sites read it. It is now a
134
+ // thin delegate: there is exactly ONE normalisation of a URL id in the repo,
135
+ // and it is the exported one a consumer can import.
136
+ //
137
+ // "of a URL id" is the load-bearing qualifier, and it is the same one
138
+ // src/normalize-record-id.ts:18 carries. Three copies of the coercion survive
139
+ // at this head — src/orm-request.ts (the create-response path),
140
+ // src/postgres/postgres-db.ts and src/mysql/mysql-db.ts — but each normalises a
141
+ // RESPONSE id (`response?.data?.id`), not a URL id, and each omits this
142
+ // function's `if (!id) return ''` guard. They are tracked as
143
+ // abofs/stonyx-orm#282 and enumerated by name in AC-5's allowlist
144
+ // (test/integration/readme-sample-test.ts).
145
+ //
146
+ // A second implementation of the URL-id normalisation here is the defect
147
+ // abofs/stonyx-orm#270 exists to remove — see src/normalize-record-id.ts.
87
148
  function getId(params: { id?: string; [key: string]: unknown }): string | number {
88
- const id = params.id;
89
- if (!id) return '';
90
- if (isNaN(id as unknown as number)) return id;
91
-
92
- return parseInt(id);
149
+ return normalizeRecordId(params.id);
93
150
  }
94
151
 
95
152
  function buildResponse(
@@ -278,7 +335,7 @@ export default class OrmRequest extends Request {
278
335
  if (accessFilter) recordsToReturn = recordsToReturn.filter(accessFilter as (record: OrmRecord) => boolean);
279
336
  if (queryFilterPredicate) recordsToReturn = recordsToReturn.filter(queryFilterPredicate as (record: OrmRecord) => boolean);
280
337
 
281
- const baseUrl = getBaseUrl(request);
338
+ const baseUrl = getBaseUrl(request, pluralizedModel);
282
339
  const data = recordsToReturn.map(record => record.toJSON?.({ fields: modelFields, baseUrl }));
283
340
 
284
341
  return buildResponse(data, request.query?.include, recordsToReturn, {
@@ -294,7 +351,7 @@ export default class OrmRequest extends Request {
294
351
  const fieldsMap = parseFields(request.query);
295
352
  const modelFields = fieldsMap.get(pluralizedModel) || fieldsMap.get(model);
296
353
 
297
- const baseUrl = getBaseUrl(request);
354
+ const baseUrl = getBaseUrl(request, pluralizedModel);
298
355
  return buildResponse(record.toJSON?.({ fields: modelFields, baseUrl }), request.query?.include, record, {
299
356
  links: { self: `${baseUrl}/${pluralizedModel}/${request.params.id}` },
300
357
  baseUrl
@@ -376,7 +433,7 @@ export default class OrmRequest extends Request {
376
433
  };
377
434
 
378
435
  const deleteHandler: HandlerFn = ({ params }) => {
379
- store.remove(model, getId(params));
436
+ store.remove(model, getId(params), { _skipAutoPersist: true });
380
437
  return 204;
381
438
  };
382
439
 
@@ -443,9 +500,14 @@ export default class OrmRequest extends Request {
443
500
  // Execute main handler
444
501
  const response = await handler(request, state);
445
502
 
446
- // Persist to SQL database for create/update (delete is handled by store.remove auto-persist)
503
+ // Set context.record for update BEFORE persist so SQL drivers can read it
504
+ if (operation === 'update' && (response as JsonApiResponse)?.data) {
505
+ context.record = store.get(this.model, getId(request.params));
506
+ }
507
+
508
+ // Persist to SQL database for all write operations (create/update/delete)
447
509
  const sqlDb = Orm.instance.sqlDb;
448
- if (sqlDb && (operation === 'create' || operation === 'update')) {
510
+ if (sqlDb && WRITE_OPERATIONS.has(operation)) {
449
511
  await sqlDb.persist(operation, this.model, context, response);
450
512
  }
451
513
 
@@ -461,8 +523,6 @@ export default class OrmRequest extends Request {
461
523
  const responseData = (response as { data: { id: string | number } }).data;
462
524
  const recordId = isNaN(responseData.id as unknown as number) ? responseData.id : parseInt(responseData.id as string);
463
525
  context.record = store.get(this.model, recordId);
464
- } else if (operation === 'update' && (response as JsonApiResponse)?.data) {
465
- context.record = store.get(this.model, getId(request.params));
466
526
  } else if (operation === 'delete') {
467
527
  // For delete, the record may no longer exist, but we have oldState
468
528
  context.recordId = getId(request.params);
@@ -499,7 +559,7 @@ export default class OrmRequest extends Request {
499
559
  if (!record) return 404;
500
560
 
501
561
  const relatedData = record.__relationships[relationshipName];
502
- const baseUrl = getBaseUrl(request);
562
+ const baseUrl = getBaseUrl(request, pluralizedModel);
503
563
 
504
564
  let data: unknown;
505
565
  if (info.isArray) {
@@ -523,7 +583,7 @@ export default class OrmRequest extends Request {
523
583
  if (!record) return 404;
524
584
 
525
585
  const relatedData = record.__relationships[relationshipName];
526
- const baseUrl = getBaseUrl(request);
586
+ const baseUrl = getBaseUrl(request, pluralizedModel);
527
587
 
528
588
  let data: unknown;
529
589
  if (info.isArray) {
@@ -572,6 +632,37 @@ export default class OrmRequest extends Request {
572
632
  }
573
633
 
574
634
  auth(request: OrmRequest$, state: { [key: string]: unknown }): number | undefined {
635
+ // abofs/stonyx-orm#270 — hand the predicate the value the record is
636
+ // ACTUALLY resolved by, rather than the raw text it was parsed from.
637
+ //
638
+ // This is the default path precisely because it requires the consumer to
639
+ // remember nothing. Exporting `normalizeRecordId` alone would leave the
640
+ // framework owning resolution while asking every consumer to call one
641
+ // function, with no signal when they forget — the same silent fail-open,
642
+ // one step over. The export is the escape hatch for hooks and custom
643
+ // handlers; this line is the contract.
644
+ //
645
+ // `request.params` is deliberately NOT mutated. Twelve `getId(...)` call
646
+ // sites read it, and `_withHooks` assigns `params: request.params` onto the
647
+ // hook context, so every consumer hook reads the same object. Changing
648
+ // `params.id` from string to number underneath them is a silent behaviour
649
+ // change on paths this issue is not about.
650
+ //
651
+ // Synchronous by necessity: @stonyx/rest-server calls auth() without
652
+ // awaiting it.
653
+ //
654
+ // The `undefined` branch is a PRESENCE check, not a second normalisation:
655
+ // it answers "does this route carry an :id at all", which is how both
656
+ // documented samples tell a collection request from a record request. It
657
+ // cannot be folded into normalizeRecordId, because that function must keep
658
+ // returning '' for a falsy id — `store.get(key, undefined)` returns the
659
+ // whole model Map rather than a record (abofs/stonyx-orm#167, pinned by
660
+ // test/unit/store-get-falsy-id-test.ts), so the resolution path depends on
661
+ // the '' it returns today.
662
+ const rawId = request.params?.id;
663
+
664
+ request.recordId = rawId === undefined ? undefined : normalizeRecordId(rawId);
665
+
575
666
  const access = this.access(request);
576
667
 
577
668
  if (!access) return 403;
@@ -0,0 +1,37 @@
1
+ import type { ColumnRule } from '../column-conversion.js';
2
+
3
+ /**
4
+ * PostgreSQL's write-path conversion rule (#292).
5
+ *
6
+ * JSONB columns must receive a JSON *string*. node-pg's `prepareValue`
7
+ * (`pg/lib/utils.js`) checks `Array.isArray` before its object fallback, so a
8
+ * raw array is rendered as a Postgres array literal (`{"1","2","3"}`) rather
9
+ * than JSON. `[]` becomes `{}`, which is valid JSON, so it lands silently
10
+ * corrupted with no error.
11
+ *
12
+ * Two behaviours this rule must keep:
13
+ *
14
+ * - `typeof value === 'string'` passes through untouched. A JSONB column
15
+ * already holding a JSON string (`'{"a":1}'`) must store the object it
16
+ * describes; an unconditional `JSON.stringify` would double-encode it.
17
+ * - An `undefined` column type is a no-op. `updated_at`, `id` and FK columns
18
+ * are added outside the attribute loop and are absent from
19
+ * `schema.columns`.
20
+ *
21
+ * The `'JSONB'` sentinel is the exact literal emitted by
22
+ * `src/postgres/type-map.ts`, the only module that produces the values in
23
+ * `schema.columns`. The two producing functions are `getPgType` and
24
+ * `getVectorType` (`src/postgres/schema-introspector.ts` writes `columns[key]`
25
+ * at exactly three sites, all routed through those two); `mysqlTypeToPg` is not
26
+ * exported and is reachable only from inside `getPgType`.
27
+ *
28
+ * One documented gap: `getPgType` returns a consumer transform's declared
29
+ * `pgType` verbatim, so a consumer writing `pgType: 'jsonb'` puts a value in
30
+ * `schema.columns` that type-map never authored and this exact-match sentinel
31
+ * misses. It misses identically on the create path, pre-fix and post-fix --
32
+ * pre-existing and symmetric, not introduced here.
33
+ */
34
+ export const postgresJsonRule: ColumnRule = (value, columnType) =>
35
+ columnType === 'JSONB' && typeof value !== 'string'
36
+ ? JSON.stringify(value)
37
+ : value;
@@ -8,6 +8,7 @@ interface PgConfig {
8
8
  password: string;
9
9
  database: string;
10
10
  connectionLimit: number;
11
+ [key: string]: unknown;
11
12
  }
12
13
 
13
14
  let pool: PgPool | null = null;
@@ -20,15 +21,18 @@ export async function getPool(pgConfig: PgConfig, extensions: string[] = ['vecto
20
21
 
21
22
  const { default: pg } = await import('pg');
22
23
 
24
+ const { host, port, user, password, database, connectionLimit, migrationsDir, migrationsTable, autoMigrate, ...poolOpts } = pgConfig;
25
+
23
26
  pool = new pg.Pool({
24
- host: pgConfig.host,
25
- port: pgConfig.port,
26
- user: pgConfig.user,
27
- password: pgConfig.password,
28
- database: pgConfig.database,
29
- max: pgConfig.connectionLimit,
27
+ host,
28
+ port,
29
+ user,
30
+ password,
31
+ database,
32
+ max: connectionLimit,
30
33
  idleTimeoutMillis: 30000,
31
34
  connectionTimeoutMillis: 10000,
35
+ ...poolOpts,
32
36
  });
33
37
 
34
38
  // Enable requested PostgreSQL extensions
@@ -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';
@@ -61,6 +63,8 @@ interface PostgresDeps {
61
63
  confirm: typeof confirm;
62
64
  readFile: typeof readFile;
63
65
  getPluralName: typeof getPluralName;
66
+ convertRow: typeof convertRow;
67
+ postgresJsonRule: typeof postgresJsonRule;
64
68
  config: typeof config;
65
69
  log: typeof log;
66
70
  path: typeof path;
@@ -73,7 +77,8 @@ const defaultDeps: PostgresDeps = {
73
77
  introspectModels, introspectViews, getTopologicalOrder, schemasToSnapshot,
74
78
  loadLatestSnapshot, detectSchemaDrift,
75
79
  buildInsert, buildUpdate, buildDelete, buildSelect, buildVectorSearch, buildHybridSearch,
76
- createRecord, store, confirm, readFile, getPluralName, config, log, path
80
+ createRecord, store, confirm, readFile, getPluralName, config, log, path,
81
+ convertRow, postgresJsonRule
77
82
  };
78
83
 
79
84
  export default class PostgresDB {
@@ -90,6 +95,15 @@ export default class PostgresDB {
90
95
  pool!: Pool | null;
91
96
  pgConfig!: Record<string, unknown>;
92
97
 
98
+ /**
99
+ * Promise-chain mutex for write serialization (#156).
100
+ * All persist() calls chain through this single queue so concurrent
101
+ * fire-and-forget writes never produce parallel transactions
102
+ * on FK-linked rows (which cause deadlocks).
103
+ * Reads are NOT affected — only persist() serializes.
104
+ */
105
+ private _writeQueue: Promise<void> = Promise.resolve();
106
+
93
107
  constructor(deps: Partial<PostgresDeps> = {}) {
94
108
  const Ctor = this.constructor as typeof PostgresDB;
95
109
  if (Ctor.instance) return Ctor.instance;
@@ -125,7 +139,15 @@ export default class PostgresDB {
125
139
  if (pending.length > 0) {
126
140
  this.deps.log.db?.(`${pending.length} pending migration(s) found.`);
127
141
 
128
- const shouldApply = await this.deps.confirm(`${pending.length} pending migration(s) found. Apply now?`);
142
+ let shouldApply: boolean;
143
+ if (this.pgConfig.autoMigrate === true) {
144
+ shouldApply = true;
145
+ } else if (this.pgConfig.autoMigrate === false) {
146
+ shouldApply = false;
147
+ this.deps.log.warn?.(`autoMigrate is false — skipping ${pending.length} pending migration(s).`);
148
+ } else {
149
+ shouldApply = await this.deps.confirm(`${pending.length} pending migration(s) found. Apply now?`);
150
+ }
129
151
 
130
152
  if (shouldApply) {
131
153
  for (const filename of pending) {
@@ -146,9 +168,17 @@ export default class PostgresDB {
146
168
  const modelCount = Object.keys(schemas).length;
147
169
 
148
170
  if (modelCount > 0) {
149
- const shouldGenerate = await this.deps.confirm(
150
- `No migrations found but ${modelCount} model(s) detected. Generate and apply initial migration?`
151
- );
171
+ let shouldGenerate: boolean;
172
+ if (this.pgConfig.autoMigrate === true) {
173
+ shouldGenerate = true;
174
+ } else if (this.pgConfig.autoMigrate === false) {
175
+ shouldGenerate = false;
176
+ this.deps.log.warn?.(`autoMigrate is false — skipping initial migration generation for ${modelCount} model(s).`);
177
+ } else {
178
+ shouldGenerate = await this.deps.confirm(
179
+ `No migrations found but ${modelCount} model(s) detected. Generate and apply initial migration?`
180
+ );
181
+ }
152
182
 
153
183
  if (shouldGenerate) {
154
184
  const { generateMigration } = await import('./migration-generator.js');
@@ -468,14 +498,21 @@ export default class PostgresDB {
468
498
  const Orm = (await import('@stonyx/orm')).default;
469
499
  if ((Orm.instance as { isView?: (name: string) => boolean })?.isView?.(modelName)) return;
470
500
 
471
- switch (operation) {
472
- case 'create':
473
- return this._persistCreate(modelName, context, response);
474
- case 'update':
475
- return this._persistUpdate(modelName, context, response);
476
- case 'delete':
477
- return this._persistDelete(modelName, context);
478
- }
501
+ const work = async () => {
502
+ switch (operation) {
503
+ case 'create':
504
+ return this._persistCreate(modelName, context, response);
505
+ case 'update':
506
+ return this._persistUpdate(modelName, context, response);
507
+ case 'delete':
508
+ return this._persistDelete(modelName, context);
509
+ }
510
+ };
511
+
512
+ // Chain through the write queue — .then(work, work) ensures the queue
513
+ // advances even when a previous persist rejects (#156).
514
+ this._writeQueue = this._writeQueue.then(work, work);
515
+ return this._writeQueue;
479
516
  }
480
517
 
481
518
  private async _persistCreate(modelName: string, context: PersistContext, response: PersistResponse): Promise<void> {
@@ -503,7 +540,10 @@ export default class PostgresDB {
503
540
  delete insertData.id;
504
541
  }
505
542
 
506
- const { sql, values } = this.deps.buildInsert(schema.table, insertData);
543
+ // Convert the *complete* payload (attributes + id + FKs) in one place (#292).
544
+ const row = this.deps.convertRow(insertData, schema.columns, this.deps.postgresJsonRule);
545
+
546
+ const { sql, values } = this.deps.buildInsert(schema.table, row);
507
547
 
508
548
  const result = await this.requirePool().query(sql, values);
509
549
 
@@ -568,7 +608,12 @@ export default class PostgresDB {
568
608
  // PostgreSQL doesn't have ON UPDATE CURRENT_TIMESTAMP -- set updated_at manually
569
609
  changedData.updated_at = new Date();
570
610
 
571
- const { sql, values } = this.deps.buildUpdate(schema.table, id, changedData);
611
+ // Same conversion, same choke point as the create path (#292). Columns
612
+ // appended after the attribute loop (FKs, updated_at) are absent from
613
+ // schema.columns and pass through untouched.
614
+ const row = this.deps.convertRow(changedData, schema.columns, this.deps.postgresJsonRule);
615
+
616
+ const { sql, values } = this.deps.buildUpdate(schema.table, id, row);
572
617
  await this.requirePool().query(sql, values);
573
618
  }
574
619
 
@@ -594,13 +639,11 @@ export default class PostgresDB {
594
639
  row.id = data.id;
595
640
  }
596
641
 
597
- // Attribute columns
598
- for (const [col, pgType] of Object.entries(schema.columns)) {
642
+ // Attribute columns. Type conversion is NOT applied here -- it is applied
643
+ // once to the finished payload by the caller, via convertRow (#292).
644
+ for (const col of Object.keys(schema.columns)) {
599
645
  if (data[col] !== undefined) {
600
- // JSONB columns: stringify non-string values for PostgreSQL JSONB storage
601
- row[col] = pgType === 'JSONB' && typeof data[col] !== 'string'
602
- ? JSON.stringify(data[col])
603
- : data[col];
646
+ row[col] = data[col];
604
647
  }
605
648
  }
606
649
 
package/src/record.ts CHANGED
@@ -87,12 +87,15 @@ export default class Record {
87
87
 
88
88
  for (const [key, childRecord] of Object.entries(this.__relationships)) {
89
89
  if (Array.isArray(childRecord)) {
90
+ // Filter out cleaned records (those with no __model)
91
+ const live = childRecord.filter((r: Record) => r?.__model);
92
+
90
93
  // Deduplicate by record ID — keep last occurrence (latest data wins)
91
94
  const seen = new Set<unknown>();
92
95
  const unique: Record[] = [];
93
96
 
94
- for (let i = childRecord.length - 1; i >= 0; i--) {
95
- const r = childRecord[i] as Record;
97
+ for (let i = live.length - 1; i >= 0; i--) {
98
+ const r = live[i] as Record;
96
99
  if (!seen.has(r.id)) {
97
100
  seen.add(r.id);
98
101
  unique.push(r);
@@ -102,7 +105,7 @@ export default class Record {
102
105
  unique.reverse();
103
106
  records[key] = unique.map((r: Record) => r.serialize());
104
107
  } else {
105
- records[key] = (childRecord as Record)?.serialize() ?? null;
108
+ records[key] = (childRecord as Record)?.__model ? (childRecord as Record).serialize() : null;
106
109
  }
107
110
  }
108
111
 
@@ -136,8 +139,8 @@ export default class Record {
136
139
  if (fields && !fields.has(key)) continue;
137
140
 
138
141
  const relationshipData = Array.isArray(childRecord)
139
- ? childRecord.map((r: Record) => ({ type: r.__model.__name, id: r.id }))
140
- : childRecord ? { type: (childRecord as Record).__model.__name, id: (childRecord as Record).id } : null;
142
+ ? childRecord.filter((r: Record) => r?.__model).map((r: Record) => ({ type: r.__model.__name, id: r.id }))
143
+ : (childRecord && (childRecord as Record).__model) ? { type: (childRecord as Record).__model.__name, id: (childRecord as Record).id } : null;
141
144
 
142
145
  // Dasherize the key for URL paths (e.g., accessLinks -> access-links)
143
146
  const dasherizedKey = camelCaseToKebabCase(key);
@@ -51,4 +51,4 @@ export function getPendingBelongsToRegistry(): PendingBelongsToMap {
51
51
  return relationships.get('pendingBelongsTo') as PendingBelongsToMap;
52
52
  }
53
53
 
54
- export const TYPES: string[] = ['global', 'hasMany', 'belongsTo', 'pending'];
54
+ export const TYPES: string[] = ['global', 'hasMany', 'belongsTo', 'pending', 'pendingBelongsTo'];