@stonyx/orm 0.3.2-beta.32 → 0.3.2-beta.321

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 (59) hide show
  1. package/README.md +375 -30
  2. package/config/{environment.ts → 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 +2 -0
  15. package/dist/index.js +1 -0
  16. package/dist/main.js +29 -1
  17. package/dist/manage-record.js +34 -3
  18. package/dist/mysql/connection.d.ts +1 -0
  19. package/dist/mysql/mysql-db.d.ts +8 -0
  20. package/dist/mysql/mysql-db.js +44 -10
  21. package/dist/normalize-record-id.d.ts +67 -0
  22. package/dist/normalize-record-id.js +88 -0
  23. package/dist/orm-request.d.ts +17 -1
  24. package/dist/orm-request.js +77 -19
  25. package/dist/postgres/column-rules.d.ts +33 -0
  26. package/dist/postgres/column-rules.js +34 -0
  27. package/dist/postgres/connection.d.ts +1 -0
  28. package/dist/postgres/connection.js +8 -6
  29. package/dist/postgres/postgres-db.d.ts +12 -0
  30. package/dist/postgres/postgres-db.js +60 -19
  31. package/dist/record.js +7 -5
  32. package/dist/relationships.js +1 -1
  33. package/dist/serializer.js +38 -2
  34. package/dist/store.d.ts +13 -1
  35. package/dist/store.js +65 -6
  36. package/dist/types/orm-types.d.ts +11 -0
  37. package/package.json +19 -7
  38. package/src/column-conversion.ts +53 -0
  39. package/src/commands.ts +43 -0
  40. package/src/dynamodb/connection.ts +50 -0
  41. package/src/dynamodb/dynamodb-db.ts +811 -0
  42. package/src/dynamodb/operation-builder.ts +202 -0
  43. package/src/dynamodb/type-map.ts +54 -0
  44. package/src/index.ts +2 -0
  45. package/src/main.ts +29 -1
  46. package/src/manage-record.ts +41 -9
  47. package/src/mysql/connection.ts +1 -0
  48. package/src/mysql/mysql-db.ts +44 -12
  49. package/src/normalize-record-id.ts +88 -0
  50. package/src/orm-request.ts +109 -18
  51. package/src/postgres/column-rules.ts +37 -0
  52. package/src/postgres/connection.ts +10 -6
  53. package/src/postgres/postgres-db.ts +64 -21
  54. package/src/record.ts +8 -5
  55. package/src/relationships.ts +1 -1
  56. package/src/serializer.ts +39 -2
  57. package/src/store.ts +68 -6
  58. package/src/types/orm-types.ts +12 -0
  59. package/src/types/stonyx.d.ts +7 -1
@@ -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/index.d.ts CHANGED
@@ -12,4 +12,6 @@ export type { PersistErrorDetail } from './main.js';
12
12
  export { Model, View, Serializer };
13
13
  export { attr, belongsTo, hasMany, createRecord, updateRecord };
14
14
  export { count, avg, sum, min, max };
15
+ export { default as normalizeRecordId } from './normalize-record-id.js';
16
+ export type { OrmRequest$ as OrmAccessRequest } from './orm-request.js';
15
17
  export { beforeHook, afterHook, clearHook, clearAllHooks } from './hooks.js';
package/dist/index.js CHANGED
@@ -26,6 +26,7 @@ export { store, relationships } from './main.js';
26
26
  export { Model, View, Serializer }; // base classes
27
27
  export { attr, belongsTo, hasMany, createRecord, updateRecord }; // helpers
28
28
  export { count, avg, sum, min, max }; // aggregate helpers
29
+ export { default as normalizeRecordId } from './normalize-record-id.js'; // canonical URL-id -> record-id resolution (#270)
29
30
  export { beforeHook, afterHook, clearHook, clearAllHooks } from './hooks.js'; // middleware hooks
30
31
  // Store API:
31
32
  // store.get(model, id) -- sync, memory-only
package/dist/main.js CHANGED
@@ -19,7 +19,6 @@ import log from 'stonyx/log';
19
19
  import { forEachFileImport } from '@stonyx/utils/file';
20
20
  import { kebabCaseToPascalCase, pluralize } from '@stonyx/utils/string';
21
21
  import { registerPluralName } from './plural-registry.js';
22
- import setupRestServer from './setup-rest-server.js';
23
22
  import baseTransforms from './transforms.js';
24
23
  import Store from './store.js';
25
24
  import Serializer from './serializer.js';
@@ -54,6 +53,10 @@ export default class Orm {
54
53
  Orm.instance = this;
55
54
  }
56
55
  async init() {
56
+ // Self-register so log.db works even when @stonyx/orm is in the
57
+ // consumer's `dependencies` (stonyx loader only merges devDependencies).
58
+ const { logColor = 'white', logMethod = 'db' } = config.orm;
59
+ log.defineType(logMethod, logColor);
57
60
  const { paths, restServer } = config.orm;
58
61
  const promises = ['Model', 'Serializer', 'Transform'].map(type => {
59
62
  const lowerCaseType = type.toLowerCase();
@@ -115,12 +118,37 @@ export default class Orm {
115
118
  this.db = this.sqlDb;
116
119
  promises.push(this.sqlDb.init());
117
120
  }
121
+ else if (config.orm.dynamodb) {
122
+ const { default: DynamoDBDB } = await import('./dynamodb/dynamodb-db.js');
123
+ this.sqlDb = new DynamoDBDB();
124
+ this.db = this.sqlDb;
125
+ promises.push(this.sqlDb.init());
126
+ }
118
127
  else if (this.options.dbType !== 'none') {
119
128
  const db = new DB();
120
129
  this.db = db;
121
130
  promises.push(db.init());
122
131
  }
123
132
  if (restServer.enabled === 'true') {
133
+ // MUST stay dynamic. setup-rest-server.js names the optional
134
+ // '@stonyx/rest-server' peer in its own static graph — directly, and
135
+ // through orm-request.ts / meta-request.ts, which import `Request` at
136
+ // module scope because they extend it (correctly: an `extends` base
137
+ // class cannot be awaited). Node links a module's entire static graph
138
+ // before evaluating any of it, so a static import here puts that
139
+ // specifier on the entry graph and `import('@stonyx/orm')` throws
140
+ // ERR_MODULE_NOT_FOUND for an ORM-only consumer that never installed the
141
+ // optional peer.
142
+ //
143
+ // NOT the same reason the SQL/DynamoDB drivers above are lazy: those
144
+ // modules carry no static peer specifier that survives to `dist/`
145
+ // (postgres-db.ts:15 and mysql-db.ts:17 are `import type`, erased by
146
+ // tsc), so the `await import()` there is not what isolates pg / mysql2 /
147
+ // @aws-sdk — that happens one layer down, in src/*/connection.ts (and
148
+ // src/dynamodb/dynamodb-db.ts).
149
+ // setup-rest-server.js is the only dist module whose laziness is
150
+ // load-bearing for peer resolution. (#280)
151
+ const { default: setupRestServer } = await import('./setup-rest-server.js');
124
152
  promises.push(setupRestServer(restServer.route, paths.access, restServer.metaRoute));
125
153
  }
126
154
  // Wire up memory resolver so store.find() can check model memory flags
@@ -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();
@@ -0,0 +1,67 @@
1
+ /**
2
+ * The ONE place a URL id is turned into the value a record is resolved by —
3
+ * abofs/stonyx-orm#270.
4
+ *
5
+ * Before this existed this coercion was written out seven times: twice in
6
+ * README.md, once in docs/usage-patterns.md, and four times inside the
7
+ * framework. Seven copies, not seven identical copies — the three persistence
8
+ * ones (abofs/stonyx-orm#282) omit the `if (!id) return ''` guard below, which
9
+ * is the point: nothing held them together, so they had already drifted.
10
+ * The framework's copy — `getId()`, module-private in
11
+ * orm-request.ts, unreachable through the package `exports` map — was the one
12
+ * that decided which record a request addressed, and a consumer's `access()`
13
+ * predicate had no way to obtain it. So the framework resolved the record by
14
+ * one value and asked the consumer to authorize on a different one.
15
+ *
16
+ * Measured consequence at the time of filing: with the documented predicate
17
+ * applied verbatim to a numeric-id model, `GET /animals/007`, `/7.9`, `/7e0`,
18
+ * `/0x7`, `/%207`, `/%2B7` and `/7%0A` each served the protected record, and
19
+ * `DELETE /animals/007` destroyed it — because `parseInt` folds every one of
20
+ * those onto `7` while `'007' === '7'` is false.
21
+ *
22
+ * Two things follow from this being a single exported function, and both are
23
+ * the point:
24
+ *
25
+ * 1. A permissive change here is HARMLESS, because both sides move together.
26
+ * Lowercasing string ids used to disclose and destroy `owner:angela` with
27
+ * the whole suite green; with one implementation the predicate simply sees
28
+ * the same lowercased value and still refuses.
29
+ * 2. A divergence — a second, private normaliser at the resolution site — is
30
+ * what is now dangerous, and that is what the tests pin, by observing the
31
+ * key the resolution path actually used.
32
+ *
33
+ * SEMANTICS ARE UNCHANGED FROM `getId()`, deliberately and byte-for-byte.
34
+ * Whether `7.9` / `0x7` / `%0A` *should* resolve to record 7 at all is a real
35
+ * question, and it is a behaviour change on the record-resolution path for
36
+ * every consumer rather than an authorization fix — it belongs in its own
37
+ * issue with its own compatibility argument (issue body scope item 4). #270
38
+ * preserves today's semantics exactly and makes both sides agree on them.
39
+ *
40
+ * Two details are load-bearing and are pinned by
41
+ * test/unit/normalize-record-id-test.ts:
42
+ *
43
+ * - `parseInt` is called with NO RADIX. `parseInt('0x7')` is 7;
44
+ * `parseInt('0x7', 10)` is 0. Adding the radix looks like a cleanup and
45
+ * silently changes which record every hex-spelled URL addresses.
46
+ * - The coercion applies only when the id LOOKS numeric, so a model with
47
+ * string ids is passed through untouched, case included.
48
+ *
49
+ * Must stay synchronous: `auth()` is invoked without `await` by
50
+ * @stonyx/rest-server, so a promise here would be handed to `access()` as the
51
+ * record id.
52
+ *
53
+ * FALSY AND `NaN` RETURNS ARE LOAD-BEARING ELSEWHERE — abofs/stonyx-orm#287.
54
+ * `''` is returned for any falsy id because `store.get(key, undefined)` returns
55
+ * the whole model Map rather than a record (abofs/stonyx-orm#167). But `''` is
56
+ * not the only falsy return: `'0'`, `'00'`, `'-0'` and `'0x0'` all normalise to
57
+ * `0`, and `' '`, `'\t'`, `'\n'`, `'\u00a0'` all normalise to `NaN` (their
58
+ * `Number()` is `0`, so the `isNaN` guard does not fire and `parseInt` runs).
59
+ * `store.remove(key, id)` branches on truthiness, so those spellings reach a
60
+ * fall-through this function does not own. Tracked as #287; every row is pinned
61
+ * in test/unit/normalize-record-id-test.ts so a cleanup here cannot move the
62
+ * boundary #287 is measured against.
63
+ *
64
+ * @param id the raw, already-URL-decoded id text from `request.params.id`
65
+ * @returns the value the ORM resolves the record by
66
+ */
67
+ export default function normalizeRecordId(id?: string | null): string | number;
@@ -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
+ * The ONE place a URL id is turned into the value a record is resolved by —
18
+ * abofs/stonyx-orm#270.
19
+ *
20
+ * Before this existed this coercion was written out seven times: twice in
21
+ * README.md, once in docs/usage-patterns.md, and four times inside the
22
+ * framework. Seven copies, not seven identical copies — the three persistence
23
+ * ones (abofs/stonyx-orm#282) omit the `if (!id) return ''` guard below, which
24
+ * is the point: nothing held them together, so they had already drifted.
25
+ * The framework's copy — `getId()`, module-private in
26
+ * orm-request.ts, unreachable through the package `exports` map — was the one
27
+ * that decided which record a request addressed, and a consumer's `access()`
28
+ * predicate had no way to obtain it. So the framework resolved the record by
29
+ * one value and asked the consumer to authorize on a different one.
30
+ *
31
+ * Measured consequence at the time of filing: with the documented predicate
32
+ * applied verbatim to a numeric-id model, `GET /animals/007`, `/7.9`, `/7e0`,
33
+ * `/0x7`, `/%207`, `/%2B7` and `/7%0A` each served the protected record, and
34
+ * `DELETE /animals/007` destroyed it — because `parseInt` folds every one of
35
+ * those onto `7` while `'007' === '7'` is false.
36
+ *
37
+ * Two things follow from this being a single exported function, and both are
38
+ * the point:
39
+ *
40
+ * 1. A permissive change here is HARMLESS, because both sides move together.
41
+ * Lowercasing string ids used to disclose and destroy `owner:angela` with
42
+ * the whole suite green; with one implementation the predicate simply sees
43
+ * the same lowercased value and still refuses.
44
+ * 2. A divergence — a second, private normaliser at the resolution site — is
45
+ * what is now dangerous, and that is what the tests pin, by observing the
46
+ * key the resolution path actually used.
47
+ *
48
+ * SEMANTICS ARE UNCHANGED FROM `getId()`, deliberately and byte-for-byte.
49
+ * Whether `7.9` / `0x7` / `%0A` *should* resolve to record 7 at all is a real
50
+ * question, and it is a behaviour change on the record-resolution path for
51
+ * every consumer rather than an authorization fix — it belongs in its own
52
+ * issue with its own compatibility argument (issue body scope item 4). #270
53
+ * preserves today's semantics exactly and makes both sides agree on them.
54
+ *
55
+ * Two details are load-bearing and are pinned by
56
+ * test/unit/normalize-record-id-test.ts:
57
+ *
58
+ * - `parseInt` is called with NO RADIX. `parseInt('0x7')` is 7;
59
+ * `parseInt('0x7', 10)` is 0. Adding the radix looks like a cleanup and
60
+ * silently changes which record every hex-spelled URL addresses.
61
+ * - The coercion applies only when the id LOOKS numeric, so a model with
62
+ * string ids is passed through untouched, case included.
63
+ *
64
+ * Must stay synchronous: `auth()` is invoked without `await` by
65
+ * @stonyx/rest-server, so a promise here would be handed to `access()` as the
66
+ * record id.
67
+ *
68
+ * FALSY AND `NaN` RETURNS ARE LOAD-BEARING ELSEWHERE — abofs/stonyx-orm#287.
69
+ * `''` is returned for any falsy id because `store.get(key, undefined)` returns
70
+ * the whole model Map rather than a record (abofs/stonyx-orm#167). But `''` is
71
+ * not the only falsy return: `'0'`, `'00'`, `'-0'` and `'0x0'` all normalise to
72
+ * `0`, and `' '`, `'\t'`, `'\n'`, `'\u00a0'` all normalise to `NaN` (their
73
+ * `Number()` is `0`, so the `isNaN` guard does not fire and `parseInt` runs).
74
+ * `store.remove(key, id)` branches on truthiness, so those spellings reach a
75
+ * fall-through this function does not own. Tracked as #287; every row is pinned
76
+ * in test/unit/normalize-record-id-test.ts so a cleanup here cannot move the
77
+ * boundary #287 is measured against.
78
+ *
79
+ * @param id the raw, already-URL-decoded id text from `request.params.id`
80
+ * @returns the value the ORM resolves the record by
81
+ */
82
+ export default function normalizeRecordId(id) {
83
+ if (!id)
84
+ return '';
85
+ if (isNaN(id))
86
+ return id;
87
+ return parseInt(id);
88
+ }
@@ -1,10 +1,26 @@
1
1
  import { Request } from '@stonyx/rest-server';
2
- interface OrmRequest$ extends Request {
2
+ /**
3
+ * The request object a consumer's `access()` predicate receives.
4
+ *
5
+ * Exported because `recordId` is public API — README's `access()` samples
6
+ * destructure it — and a public runtime field with an unreachable type asks the
7
+ * consumer to re-declare something the framework already knows, which is
8
+ * abofs/stonyx-orm#270's own defect shape one layer over into the type surface.
9
+ * `HookContext` (src/hooks.ts) is this repo's precedent for exporting the
10
+ * interface a consumer is handed. Re-exported from the root barrel as
11
+ * `OrmAccessRequest` (src/index.ts) — NOT as `OrmRequest`, which is already the
12
+ * default-exported CLASS in this file and means something else. The
13
+ * `./orm-request` subpath is not in the `exports` map, so the barrel is the
14
+ * only reachable spelling.
15
+ */
16
+ export interface OrmRequest$ extends Request {
3
17
  protocol?: string;
18
+ baseUrl?: string;
4
19
  method: string;
5
20
  params: {
6
21
  [key: string]: string;
7
22
  };
23
+ recordId?: string | number | undefined;
8
24
  body?: {
9
25
  [key: string]: unknown;
10
26
  };