@stonyx/orm 0.3.2-beta.16 → 0.3.2-beta.161

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 (47) hide show
  1. package/README.md +36 -2
  2. package/config/environment.js +99 -12
  3. package/dist/commands.js +34 -0
  4. package/dist/dynamodb/connection.d.ts +31 -0
  5. package/dist/dynamodb/connection.js +28 -0
  6. package/dist/dynamodb/dynamodb-db.d.ts +142 -0
  7. package/dist/dynamodb/dynamodb-db.js +596 -0
  8. package/dist/dynamodb/operation-builder.d.ts +76 -0
  9. package/dist/dynamodb/operation-builder.js +116 -0
  10. package/dist/dynamodb/type-map.d.ts +31 -0
  11. package/dist/dynamodb/type-map.js +48 -0
  12. package/dist/main.js +10 -0
  13. package/dist/manage-record.js +34 -3
  14. package/dist/mysql/connection.d.ts +1 -0
  15. package/dist/mysql/mysql-db.d.ts +8 -0
  16. package/dist/mysql/mysql-db.js +44 -10
  17. package/dist/orm-request.js +7 -6
  18. package/dist/postgres/connection.d.ts +1 -0
  19. package/dist/postgres/connection.js +8 -6
  20. package/dist/postgres/postgres-db.d.ts +8 -0
  21. package/dist/postgres/postgres-db.js +44 -10
  22. package/dist/record.js +7 -5
  23. package/dist/relationships.js +1 -1
  24. package/dist/serializer.js +38 -2
  25. package/dist/store.d.ts +13 -1
  26. package/dist/store.js +65 -6
  27. package/dist/types/orm-types.d.ts +11 -0
  28. package/package.json +16 -7
  29. package/src/commands.ts +43 -0
  30. package/src/dynamodb/connection.ts +50 -0
  31. package/src/dynamodb/dynamodb-db.ts +811 -0
  32. package/src/dynamodb/operation-builder.ts +202 -0
  33. package/src/dynamodb/type-map.ts +54 -0
  34. package/src/main.ts +10 -0
  35. package/src/manage-record.ts +41 -9
  36. package/src/mysql/connection.ts +1 -0
  37. package/src/mysql/mysql-db.ts +44 -12
  38. package/src/orm-request.ts +8 -5
  39. package/src/postgres/connection.ts +10 -6
  40. package/src/postgres/postgres-db.ts +44 -12
  41. package/src/record.ts +8 -5
  42. package/src/relationships.ts +1 -1
  43. package/src/serializer.ts +39 -2
  44. package/src/store.ts +68 -6
  45. package/src/types/orm-types.ts +12 -0
  46. package/src/types/stonyx.d.ts +7 -1
  47. package/config/environment.ts +0 -91
@@ -0,0 +1,116 @@
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 validEntries = Object.entries(conditions).filter(([, val]) => val !== undefined && val !== null);
68
+ if (validEntries.length > 0) {
69
+ const names = {};
70
+ const values = {};
71
+ const clauses = [];
72
+ for (const [attr, val] of validEntries) {
73
+ const nameAlias = `#${attr}`;
74
+ const valAlias = `:${attr}`;
75
+ names[nameAlias] = attr;
76
+ values[valAlias] = val;
77
+ clauses.push(`${nameAlias} = ${valAlias}`);
78
+ }
79
+ params.FilterExpression = clauses.join(' AND ');
80
+ params.ExpressionAttributeNames = names;
81
+ params.ExpressionAttributeValues = values;
82
+ }
83
+ }
84
+ return params;
85
+ }
86
+ /**
87
+ * QueryCommand params for a GSI.
88
+ * keyConditions must be in the form { attrName: value } and will be rendered
89
+ * as equality expressions joined by AND.
90
+ */
91
+ export function buildQuery(tableName, indexName, keyConditions, exclusiveStartKey) {
92
+ const validEntries = Object.entries(keyConditions).filter(([, val]) => val !== undefined && val !== null);
93
+ if (validEntries.length === 0) {
94
+ throw new Error('buildQuery: all keyCondition values are undefined/null');
95
+ }
96
+ const names = {};
97
+ const values = {};
98
+ const clauses = [];
99
+ for (const [attr, val] of validEntries) {
100
+ const nameAlias = `#${attr}`;
101
+ const valAlias = `:${attr}`;
102
+ names[nameAlias] = attr;
103
+ values[valAlias] = val;
104
+ clauses.push(`${nameAlias} = ${valAlias}`);
105
+ }
106
+ const params = {
107
+ TableName: tableName,
108
+ IndexName: indexName,
109
+ KeyConditionExpression: clauses.join(' AND '),
110
+ ExpressionAttributeNames: names,
111
+ ExpressionAttributeValues: values,
112
+ };
113
+ if (exclusiveStartKey)
114
+ params.ExclusiveStartKey = exclusiveStartKey;
115
+ return params;
116
+ }
@@ -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/main.js CHANGED
@@ -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;
@@ -52,13 +52,34 @@ export function createRecord(modelName, rawData = {}, userOptions = {}) {
52
52
  relationship.push(record);
53
53
  pendingHasMany.splice(0);
54
54
  }
55
+ // FK-based inverse hasMany wiring — when a child record is created with a
56
+ // foreign-key field (e.g. `owner: 'owner-1'` on an animal), find any parent
57
+ // whose hasMany registry targets this model and push the child into the
58
+ // parent's shared array. This covers edge cases where the child is created
59
+ // in a separate async frame without a belongsTo handler firing.
60
+ const hasManyReg = getHasManyRegistry();
61
+ if (hasManyReg) {
62
+ for (const [parentModelName, targetMap] of hasManyReg) {
63
+ const childArrayMap = targetMap.get(modelName);
64
+ if (!childArrayMap)
65
+ continue;
66
+ // Check if rawData contains a FK field matching the parent model name
67
+ const fkValue = rawData[parentModelName];
68
+ if (fkValue === undefined || fkValue === null)
69
+ continue;
70
+ const parentArray = childArrayMap.get(fkValue);
71
+ if (parentArray && !parentArray.includes(record)) {
72
+ parentArray.push(record);
73
+ }
74
+ }
75
+ }
55
76
  // Fulfill pending belongsTo relationships
56
77
  const pendingBelongsToQueue = getPendingBelongsToRegistry();
57
78
  const pendingBelongsToRaw = pendingBelongsToQueue.get(modelName)?.get(record.id);
58
79
  const pendingBelongsTo = Array.isArray(pendingBelongsToRaw) ? pendingBelongsToRaw : undefined;
59
80
  if (pendingBelongsTo) {
60
81
  const belongsToReg = getBelongsToRegistry();
61
- const hasManyReg = getHasManyRegistry();
82
+ const pendingHasManyReg = getHasManyRegistry();
62
83
  for (const { sourceRecord, sourceModelName, relationshipKey, relationshipId } of pendingBelongsTo) {
63
84
  // Update the belongsTo relationship on the source record
64
85
  sourceRecord.__relationships[relationshipKey] = record;
@@ -72,7 +93,7 @@ export function createRecord(modelName, rawData = {}, userOptions = {}) {
72
93
  }
73
94
  }
74
95
  // Wire inverse hasMany if it exists
75
- const inverseHasMany = hasManyReg.get(modelName)?.get(sourceModelName)?.get(record.id);
96
+ const inverseHasMany = pendingHasManyReg.get(modelName)?.get(sourceModelName)?.get(record.id);
76
97
  if (inverseHasMany && !inverseHasMany.includes(sourceRecord)) {
77
98
  inverseHasMany.push(sourceRecord);
78
99
  }
@@ -83,14 +104,24 @@ export function createRecord(modelName, rawData = {}, userOptions = {}) {
83
104
  // Auto-persist to SQL — skip for DB loads (isDbRecord) and relationship resolution (_relationshipKey)
84
105
  const shouldPersist = orm?.sqlDb && !options.isDbRecord && !userOptions._relationshipKey && !options._skipAutoPersist;
85
106
  if (shouldPersist) {
107
+ // Capture ID before persist — SQL adapters re-key pending IDs to real DB IDs,
108
+ // but relationship registries were keyed with this original ID
109
+ const registryId = record.id;
86
110
  const response = { data: { id: record.id } };
87
- orm.sqlDb.persist('create', modelName, { rawData }, response).catch((err) => {
111
+ orm.sqlDb.persist('create', modelName, { rawData }, response)
112
+ .catch((err) => {
88
113
  orm.emitPersistError({
89
114
  operation: 'create',
90
115
  modelName,
91
116
  recordId: record.id,
92
117
  error: err instanceof Error ? err : new Error(String(err)),
93
118
  });
119
+ })
120
+ .finally(() => {
121
+ // Evict non-memory records after persist to prevent unbounded heap growth (stonyx#81)
122
+ if (store._memoryResolver && !store._memoryResolver(modelName)) {
123
+ store.evictRecord(modelName, record.id, registryId);
124
+ }
94
125
  });
95
126
  }
96
127
  return record;
@@ -8,6 +8,7 @@ interface MysqlConfig {
8
8
  connectionLimit?: number;
9
9
  migrationsTable?: string;
10
10
  migrationsDir?: string;
11
+ autoMigrate?: boolean;
11
12
  }
12
13
  export declare function getPool(mysqlConfig: MysqlConfig): Promise<Pool>;
13
14
  export declare function closePool(): Promise<void>;
@@ -61,6 +61,14 @@ export default class MysqlDB {
61
61
  deps: MysqlDBDeps;
62
62
  pool: Pool | null;
63
63
  mysqlConfig: MysqlConfig;
64
+ /**
65
+ * Promise-chain mutex for write serialization (#156).
66
+ * All persist() calls chain through this single queue so concurrent
67
+ * fire-and-forget writes never produce parallel InnoDB transactions
68
+ * on FK-linked rows (which cause deadlocks).
69
+ * Reads are NOT affected — only persist() serializes.
70
+ */
71
+ private _writeQueue;
64
72
  constructor(deps?: Partial<MysqlDBDeps>);
65
73
  private requirePool;
66
74
  init(): Promise<void>;
@@ -26,6 +26,14 @@ export default class MysqlDB {
26
26
  deps;
27
27
  pool;
28
28
  mysqlConfig;
29
+ /**
30
+ * Promise-chain mutex for write serialization (#156).
31
+ * All persist() calls chain through this single queue so concurrent
32
+ * fire-and-forget writes never produce parallel InnoDB transactions
33
+ * on FK-linked rows (which cause deadlocks).
34
+ * Reads are NOT affected — only persist() serializes.
35
+ */
36
+ _writeQueue = Promise.resolve();
29
37
  constructor(deps = {}) {
30
38
  if (MysqlDB.instance)
31
39
  return MysqlDB.instance;
@@ -57,7 +65,17 @@ export default class MysqlDB {
57
65
  const pending = files.filter(f => !applied.includes(f));
58
66
  if (pending.length > 0) {
59
67
  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?`);
68
+ let shouldApply;
69
+ if (this.mysqlConfig.autoMigrate === true) {
70
+ shouldApply = true;
71
+ }
72
+ else if (this.mysqlConfig.autoMigrate === false) {
73
+ shouldApply = false;
74
+ this.deps.log.warn?.(`autoMigrate is false — skipping ${pending.length} pending migration(s).`);
75
+ }
76
+ else {
77
+ shouldApply = await this.deps.confirm(`${pending.length} pending migration(s) found. Apply now?`);
78
+ }
61
79
  if (shouldApply) {
62
80
  for (const filename of pending) {
63
81
  const content = await this.deps.readFile(this.deps.path.join(migrationsPath, filename));
@@ -76,7 +94,17 @@ export default class MysqlDB {
76
94
  const schemas = this.deps.introspectModels();
77
95
  const modelCount = Object.keys(schemas).length;
78
96
  if (modelCount > 0) {
79
- const shouldGenerate = await this.deps.confirm(`No migrations found but ${modelCount} model(s) detected. Generate and apply initial migration?`);
97
+ let shouldGenerate;
98
+ if (this.mysqlConfig.autoMigrate === true) {
99
+ shouldGenerate = true;
100
+ }
101
+ else if (this.mysqlConfig.autoMigrate === false) {
102
+ shouldGenerate = false;
103
+ this.deps.log.warn?.(`autoMigrate is false — skipping initial migration generation for ${modelCount} model(s).`);
104
+ }
105
+ else {
106
+ shouldGenerate = await this.deps.confirm(`No migrations found but ${modelCount} model(s) detected. Generate and apply initial migration?`);
107
+ }
80
108
  if (shouldGenerate) {
81
109
  const { generateMigration } = await import('./migration-generator.js');
82
110
  const result = await generateMigration('initial_setup');
@@ -302,14 +330,20 @@ export default class MysqlDB {
302
330
  const Orm = (await import('@stonyx/orm')).default;
303
331
  if (Orm.instance?.isView?.(modelName))
304
332
  return;
305
- switch (operation) {
306
- case 'create':
307
- return this._persistCreate(modelName, context, response);
308
- case 'update':
309
- return this._persistUpdate(modelName, context, response);
310
- case 'delete':
311
- return this._persistDelete(modelName, context);
312
- }
333
+ const work = async () => {
334
+ switch (operation) {
335
+ case 'create':
336
+ return this._persistCreate(modelName, context, response);
337
+ case 'update':
338
+ return this._persistUpdate(modelName, context, response);
339
+ case 'delete':
340
+ return this._persistDelete(modelName, context);
341
+ }
342
+ };
343
+ // Chain through the write queue — .then(work, work) ensures the queue
344
+ // advances even when a previous persist rejects (#156).
345
+ this._writeQueue = this._writeQueue.then(work, work);
346
+ return this._writeQueue;
313
347
  }
314
348
  async _persistCreate(modelName, context, response) {
315
349
  const schemas = this.deps.introspectModels();
@@ -289,7 +289,7 @@ export default class OrmRequest extends Request {
289
289
  return { data: record.toJSON?.() };
290
290
  };
291
291
  const deleteHandler = ({ params }) => {
292
- store.remove(model, getId(params));
292
+ store.remove(model, getId(params), { _skipAutoPersist: true });
293
293
  return 204;
294
294
  };
295
295
  // Wrap handlers with hooks
@@ -348,9 +348,13 @@ 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 create/update (delete is handled by store.remove auto-persist)
351
+ // Set context.record for update BEFORE persist so SQL drivers can read it
352
+ if (operation === 'update' && response?.data) {
353
+ context.record = store.get(this.model, getId(request.params));
354
+ }
355
+ // Persist to SQL database for all write operations (create/update/delete)
352
356
  const sqlDb = Orm.instance.sqlDb;
353
- if (sqlDb && (operation === 'create' || operation === 'update')) {
357
+ if (sqlDb && WRITE_OPERATIONS.has(operation)) {
354
358
  await sqlDb.persist(operation, this.model, context, response);
355
359
  }
356
360
  // Add response and relevant records to context
@@ -367,9 +371,6 @@ export default class OrmRequest extends Request {
367
371
  const recordId = isNaN(responseData.id) ? responseData.id : parseInt(responseData.id);
368
372
  context.record = store.get(this.model, recordId);
369
373
  }
370
- else if (operation === 'update' && response?.data) {
371
- context.record = store.get(this.model, getId(request.params));
372
- }
373
374
  else if (operation === 'delete') {
374
375
  // For delete, the record may no longer exist, but we have oldState
375
376
  context.recordId = getId(request.params);
@@ -6,6 +6,7 @@ interface PgConfig {
6
6
  password: string;
7
7
  database: string;
8
8
  connectionLimit: number;
9
+ [key: string]: unknown;
9
10
  }
10
11
  /**
11
12
  * Create or return the singleton pg Pool.
@@ -7,15 +7,17 @@ export async function getPool(pgConfig, extensions = ['vector']) {
7
7
  if (pool)
8
8
  return pool;
9
9
  const { default: pg } = await import('pg');
10
+ const { host, port, user, password, database, connectionLimit, migrationsDir, migrationsTable, autoMigrate, ...poolOpts } = pgConfig;
10
11
  pool = new pg.Pool({
11
- host: pgConfig.host,
12
- port: pgConfig.port,
13
- user: pgConfig.user,
14
- password: pgConfig.password,
15
- database: pgConfig.database,
16
- max: pgConfig.connectionLimit,
12
+ host,
13
+ port,
14
+ user,
15
+ password,
16
+ database,
17
+ max: connectionLimit,
17
18
  idleTimeoutMillis: 30000,
18
19
  connectionTimeoutMillis: 10000,
20
+ ...poolOpts,
19
21
  });
20
22
  // Enable requested PostgreSQL extensions
21
23
  for (const ext of extensions) {
@@ -72,6 +72,14 @@ export default class PostgresDB {
72
72
  deps: PostgresDeps;
73
73
  pool: Pool | null;
74
74
  pgConfig: Record<string, unknown>;
75
+ /**
76
+ * Promise-chain mutex for write serialization (#156).
77
+ * All persist() calls chain through this single queue so concurrent
78
+ * fire-and-forget writes never produce parallel transactions
79
+ * on FK-linked rows (which cause deadlocks).
80
+ * Reads are NOT affected — only persist() serializes.
81
+ */
82
+ private _writeQueue;
75
83
  constructor(deps?: Partial<PostgresDeps>);
76
84
  protected requirePool(): Pool;
77
85
  init(): Promise<void>;
@@ -30,6 +30,14 @@ export default class PostgresDB {
30
30
  deps;
31
31
  pool;
32
32
  pgConfig;
33
+ /**
34
+ * Promise-chain mutex for write serialization (#156).
35
+ * All persist() calls chain through this single queue so concurrent
36
+ * fire-and-forget writes never produce parallel transactions
37
+ * on FK-linked rows (which cause deadlocks).
38
+ * Reads are NOT affected — only persist() serializes.
39
+ */
40
+ _writeQueue = Promise.resolve();
33
41
  constructor(deps = {}) {
34
42
  const Ctor = this.constructor;
35
43
  if (Ctor.instance)
@@ -57,7 +65,17 @@ export default class PostgresDB {
57
65
  const pending = files.filter(f => !applied.includes(f));
58
66
  if (pending.length > 0) {
59
67
  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?`);
68
+ let shouldApply;
69
+ if (this.pgConfig.autoMigrate === true) {
70
+ shouldApply = true;
71
+ }
72
+ else if (this.pgConfig.autoMigrate === false) {
73
+ shouldApply = false;
74
+ this.deps.log.warn?.(`autoMigrate is false — skipping ${pending.length} pending migration(s).`);
75
+ }
76
+ else {
77
+ shouldApply = await this.deps.confirm(`${pending.length} pending migration(s) found. Apply now?`);
78
+ }
61
79
  if (shouldApply) {
62
80
  for (const filename of pending) {
63
81
  const content = await this.deps.readFile(this.deps.path.join(migrationsPath, filename));
@@ -76,7 +94,17 @@ export default class PostgresDB {
76
94
  const schemas = this.deps.introspectModels();
77
95
  const modelCount = Object.keys(schemas).length;
78
96
  if (modelCount > 0) {
79
- const shouldGenerate = await this.deps.confirm(`No migrations found but ${modelCount} model(s) detected. Generate and apply initial migration?`);
97
+ let shouldGenerate;
98
+ if (this.pgConfig.autoMigrate === true) {
99
+ shouldGenerate = true;
100
+ }
101
+ else if (this.pgConfig.autoMigrate === false) {
102
+ shouldGenerate = false;
103
+ this.deps.log.warn?.(`autoMigrate is false — skipping initial migration generation for ${modelCount} model(s).`);
104
+ }
105
+ else {
106
+ shouldGenerate = await this.deps.confirm(`No migrations found but ${modelCount} model(s) detected. Generate and apply initial migration?`);
107
+ }
80
108
  if (shouldGenerate) {
81
109
  const { generateMigration } = await import('./migration-generator.js');
82
110
  const result = await generateMigration('initial_setup');
@@ -356,14 +384,20 @@ export default class PostgresDB {
356
384
  const Orm = (await import('@stonyx/orm')).default;
357
385
  if (Orm.instance?.isView?.(modelName))
358
386
  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
- }
387
+ const work = async () => {
388
+ switch (operation) {
389
+ case 'create':
390
+ return this._persistCreate(modelName, context, response);
391
+ case 'update':
392
+ return this._persistUpdate(modelName, context, response);
393
+ case 'delete':
394
+ return this._persistDelete(modelName, context);
395
+ }
396
+ };
397
+ // Chain through the write queue — .then(work, work) ensures the queue
398
+ // advances even when a previous persist rejects (#156).
399
+ this._writeQueue = this._writeQueue.then(work, work);
400
+ return this._writeQueue;
367
401
  }
368
402
  async _persistCreate(modelName, context, response) {
369
403
  const schemas = this.deps.introspectModels();
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