@stonyx/orm 0.3.2-beta.3 → 0.3.2-beta.300
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +328 -22
- package/config/environment.js +8 -0
- package/dist/column-conversion.d.ts +34 -0
- package/dist/column-conversion.js +34 -0
- package/dist/commands.js +34 -0
- package/dist/dynamodb/connection.d.ts +31 -0
- package/dist/dynamodb/connection.js +28 -0
- package/dist/dynamodb/dynamodb-db.d.ts +142 -0
- package/dist/dynamodb/dynamodb-db.js +596 -0
- package/dist/dynamodb/operation-builder.d.ts +76 -0
- package/dist/dynamodb/operation-builder.js +116 -0
- package/dist/dynamodb/type-map.d.ts +31 -0
- package/dist/dynamodb/type-map.js +48 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +5 -4
- package/dist/main.d.ts +12 -12
- package/dist/main.js +47 -43
- package/dist/manage-record.d.ts +1 -0
- package/dist/manage-record.js +66 -4
- package/dist/mysql/connection.d.ts +1 -0
- package/dist/mysql/mysql-db.d.ts +9 -0
- package/dist/mysql/mysql-db.js +48 -12
- package/dist/normalize-record-id.d.ts +67 -0
- package/dist/normalize-record-id.js +88 -0
- package/dist/orm-request.d.ts +17 -1
- package/dist/orm-request.js +78 -20
- package/dist/postgres/column-rules.d.ts +33 -0
- package/dist/postgres/column-rules.js +34 -0
- package/dist/postgres/connection.d.ts +1 -0
- package/dist/postgres/connection.js +8 -6
- package/dist/postgres/postgres-db.d.ts +13 -0
- package/dist/postgres/postgres-db.js +80 -27
- package/dist/record.js +7 -5
- package/dist/relationships.js +1 -1
- package/dist/serializer.js +38 -2
- package/dist/store.d.ts +13 -1
- package/dist/store.js +74 -4
- package/dist/types/orm-types.d.ts +11 -0
- package/package.json +20 -7
- package/src/column-conversion.ts +53 -0
- package/src/commands.ts +43 -0
- package/src/dynamodb/connection.ts +50 -0
- package/src/dynamodb/dynamodb-db.ts +811 -0
- package/src/dynamodb/operation-builder.ts +202 -0
- package/src/dynamodb/type-map.ts +54 -0
- package/src/index.ts +7 -4
- package/src/main.ts +55 -51
- package/src/manage-record.ts +72 -4
- package/src/mysql/connection.ts +1 -0
- package/src/mysql/mysql-db.ts +49 -14
- package/src/normalize-record-id.ts +88 -0
- package/src/orm-request.ts +110 -19
- package/src/postgres/column-rules.ts +37 -0
- package/src/postgres/connection.ts +10 -6
- package/src/postgres/postgres-db.ts +83 -29
- package/src/record.ts +8 -5
- package/src/relationships.ts +1 -1
- package/src/serializer.ts +39 -2
- package/src/store.ts +78 -4
- package/src/types/orm-types.ts +12 -0
- package/src/types/stonyx.d.ts +7 -1
package/dist/mysql/mysql-db.js
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
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();
|
|
@@ -321,8 +355,10 @@ export default class MysqlDB {
|
|
|
321
355
|
if (!record)
|
|
322
356
|
return;
|
|
323
357
|
const insertData = this._recordToRow(record, schema);
|
|
324
|
-
// For auto-increment models, remove the pending ID
|
|
325
|
-
|
|
358
|
+
// For auto-increment models, remove the pending ID.
|
|
359
|
+
// Check context.rawData (not record.__data) because __pendingSqlId is not a model
|
|
360
|
+
// attribute and gets lost during serialization.
|
|
361
|
+
const isPendingId = context.rawData?.__pendingSqlId === true;
|
|
326
362
|
if (isPendingId) {
|
|
327
363
|
delete insertData.id;
|
|
328
364
|
}
|
|
@@ -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
|
+
}
|
package/dist/orm-request.d.ts
CHANGED
|
@@ -1,10 +1,26 @@
|
|
|
1
1
|
import { Request } from '@stonyx/rest-server';
|
|
2
|
-
|
|
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
|
};
|
package/dist/orm-request.js
CHANGED
|
@@ -5,6 +5,7 @@ import { getPluralName } from './plural-registry.js';
|
|
|
5
5
|
import { getBeforeHooks, getAfterHooks } from './hooks.js';
|
|
6
6
|
import config from 'stonyx/config';
|
|
7
7
|
import { isOrmRecord } from './utils.js';
|
|
8
|
+
import normalizeRecordId from './normalize-record-id.js';
|
|
8
9
|
const methodAccessMap = {
|
|
9
10
|
GET: 'read',
|
|
10
11
|
POST: 'create',
|
|
@@ -42,19 +43,46 @@ function getModelRelationships(modelName) {
|
|
|
42
43
|
}
|
|
43
44
|
return relationships;
|
|
44
45
|
}
|
|
45
|
-
|
|
46
|
-
|
|
46
|
+
/**
|
|
47
|
+
* Build the absolute base URL that every advertised link hangs off — origin
|
|
48
|
+
* plus the prefix the ORM's routes are actually mounted at.
|
|
49
|
+
*
|
|
50
|
+
* The prefix is derived from the *request*, not from
|
|
51
|
+
* `config.orm.restServer.route`. Express sets `request.baseUrl` to the real
|
|
52
|
+
* mountpath registered by `RestServer.mountRoute`, which for this module is
|
|
53
|
+
* always `<prefix>/<pluralizedModel>` (see setup-rest-server.ts). Stripping the
|
|
54
|
+
* trailing model segment therefore yields the prefix by construction, and the
|
|
55
|
+
* link builder cannot drift from the mount registrar the way a second,
|
|
56
|
+
* independent normalisation of `route` would.
|
|
57
|
+
*
|
|
58
|
+
* When `request.baseUrl` is absent or does not end in the model segment the
|
|
59
|
+
* prefix is empty, which reproduces the previous origin-only behaviour.
|
|
60
|
+
*/
|
|
61
|
+
function getBaseUrl(request, pluralizedModel) {
|
|
47
62
|
const protocol = request.protocol || 'http';
|
|
48
63
|
const host = request.get('host');
|
|
49
|
-
|
|
64
|
+
const modelSegment = `/${pluralizedModel}`;
|
|
65
|
+
const mountPath = request.baseUrl ?? '';
|
|
66
|
+
const prefix = mountPath.endsWith(modelSegment) ? mountPath.slice(0, -modelSegment.length) : '';
|
|
67
|
+
return `${protocol}://${host}${prefix}`;
|
|
50
68
|
}
|
|
69
|
+
// Kept as a name because twelve `getId(...)` call sites read it. It is now a
|
|
70
|
+
// thin delegate: there is exactly ONE normalisation of a URL id in the repo,
|
|
71
|
+
// and it is the exported one a consumer can import.
|
|
72
|
+
//
|
|
73
|
+
// "of a URL id" is the load-bearing qualifier, and it is the same one
|
|
74
|
+
// src/normalize-record-id.ts:18 carries. Three copies of the coercion survive
|
|
75
|
+
// at this head — src/orm-request.ts (the create-response path),
|
|
76
|
+
// src/postgres/postgres-db.ts and src/mysql/mysql-db.ts — but each normalises a
|
|
77
|
+
// RESPONSE id (`response?.data?.id`), not a URL id, and each omits this
|
|
78
|
+
// function's `if (!id) return ''` guard. They are tracked as
|
|
79
|
+
// abofs/stonyx-orm#282 and enumerated by name in AC-5's allowlist
|
|
80
|
+
// (test/integration/readme-sample-test.ts).
|
|
81
|
+
//
|
|
82
|
+
// A second implementation of the URL-id normalisation here is the defect
|
|
83
|
+
// abofs/stonyx-orm#270 exists to remove — see src/normalize-record-id.ts.
|
|
51
84
|
function getId(params) {
|
|
52
|
-
|
|
53
|
-
if (!id)
|
|
54
|
-
return '';
|
|
55
|
-
if (isNaN(id))
|
|
56
|
-
return id;
|
|
57
|
-
return parseInt(id);
|
|
85
|
+
return normalizeRecordId(params.id);
|
|
58
86
|
}
|
|
59
87
|
function buildResponse(data, includeParam, recordOrRecords, options = {}) {
|
|
60
88
|
const { links, baseUrl } = options;
|
|
@@ -209,7 +237,7 @@ export default class OrmRequest extends Request {
|
|
|
209
237
|
recordsToReturn = recordsToReturn.filter(accessFilter);
|
|
210
238
|
if (queryFilterPredicate)
|
|
211
239
|
recordsToReturn = recordsToReturn.filter(queryFilterPredicate);
|
|
212
|
-
const baseUrl = getBaseUrl(request);
|
|
240
|
+
const baseUrl = getBaseUrl(request, pluralizedModel);
|
|
213
241
|
const data = recordsToReturn.map(record => record.toJSON?.({ fields: modelFields, baseUrl }));
|
|
214
242
|
return buildResponse(data, request.query?.include, recordsToReturn, {
|
|
215
243
|
links: { self: `${baseUrl}/${pluralizedModel}` },
|
|
@@ -222,7 +250,7 @@ export default class OrmRequest extends Request {
|
|
|
222
250
|
return 404;
|
|
223
251
|
const fieldsMap = parseFields(request.query);
|
|
224
252
|
const modelFields = fieldsMap.get(pluralizedModel) || fieldsMap.get(model);
|
|
225
|
-
const baseUrl = getBaseUrl(request);
|
|
253
|
+
const baseUrl = getBaseUrl(request, pluralizedModel);
|
|
226
254
|
return buildResponse(record.toJSON?.({ fields: modelFields, baseUrl }), request.query?.include, record, {
|
|
227
255
|
links: { self: `${baseUrl}/${pluralizedModel}/${request.params.id}` },
|
|
228
256
|
baseUrl
|
|
@@ -248,7 +276,7 @@ export default class OrmRequest extends Request {
|
|
|
248
276
|
}
|
|
249
277
|
}
|
|
250
278
|
const recordAttributes = id !== undefined ? { id, ...sanitizedAttributes } : sanitizedAttributes;
|
|
251
|
-
const created = createRecord(model, recordAttributes, { serialize: false });
|
|
279
|
+
const created = createRecord(model, recordAttributes, { serialize: false, _skipAutoPersist: true });
|
|
252
280
|
const record = isOrmRecord(created) ? created : null;
|
|
253
281
|
if (!record)
|
|
254
282
|
return 500;
|
|
@@ -283,13 +311,13 @@ export default class OrmRequest extends Request {
|
|
|
283
311
|
}
|
|
284
312
|
}
|
|
285
313
|
if (Object.keys(relUpdates).length > 0) {
|
|
286
|
-
updateRecord(record, relUpdates);
|
|
314
|
+
updateRecord(record, relUpdates, { _skipAutoPersist: true });
|
|
287
315
|
}
|
|
288
316
|
}
|
|
289
317
|
return { data: record.toJSON?.() };
|
|
290
318
|
};
|
|
291
319
|
const deleteHandler = ({ params }) => {
|
|
292
|
-
store.remove(model, getId(params));
|
|
320
|
+
store.remove(model, getId(params), { _skipAutoPersist: true });
|
|
293
321
|
return 204;
|
|
294
322
|
};
|
|
295
323
|
// Wrap handlers with hooks
|
|
@@ -348,7 +376,11 @@ export default class OrmRequest extends Request {
|
|
|
348
376
|
}
|
|
349
377
|
// Execute main handler
|
|
350
378
|
const response = await handler(request, state);
|
|
351
|
-
//
|
|
379
|
+
// Set context.record for update BEFORE persist so SQL drivers can read it
|
|
380
|
+
if (operation === 'update' && response?.data) {
|
|
381
|
+
context.record = store.get(this.model, getId(request.params));
|
|
382
|
+
}
|
|
383
|
+
// Persist to SQL database for all write operations (create/update/delete)
|
|
352
384
|
const sqlDb = Orm.instance.sqlDb;
|
|
353
385
|
if (sqlDb && WRITE_OPERATIONS.has(operation)) {
|
|
354
386
|
await sqlDb.persist(operation, this.model, context, response);
|
|
@@ -367,9 +399,6 @@ export default class OrmRequest extends Request {
|
|
|
367
399
|
const recordId = isNaN(responseData.id) ? responseData.id : parseInt(responseData.id);
|
|
368
400
|
context.record = store.get(this.model, recordId);
|
|
369
401
|
}
|
|
370
|
-
else if (operation === 'update' && response?.data) {
|
|
371
|
-
context.record = store.get(this.model, getId(request.params));
|
|
372
|
-
}
|
|
373
402
|
else if (operation === 'delete') {
|
|
374
403
|
// For delete, the record may no longer exist, but we have oldState
|
|
375
404
|
context.recordId = getId(request.params);
|
|
@@ -396,7 +425,7 @@ export default class OrmRequest extends Request {
|
|
|
396
425
|
if (!record)
|
|
397
426
|
return 404;
|
|
398
427
|
const relatedData = record.__relationships[relationshipName];
|
|
399
|
-
const baseUrl = getBaseUrl(request);
|
|
428
|
+
const baseUrl = getBaseUrl(request, pluralizedModel);
|
|
400
429
|
let data;
|
|
401
430
|
if (info.isArray) {
|
|
402
431
|
// hasMany - return array
|
|
@@ -418,7 +447,7 @@ export default class OrmRequest extends Request {
|
|
|
418
447
|
if (!record)
|
|
419
448
|
return 404;
|
|
420
449
|
const relatedData = record.__relationships[relationshipName];
|
|
421
|
-
const baseUrl = getBaseUrl(request);
|
|
450
|
+
const baseUrl = getBaseUrl(request, pluralizedModel);
|
|
422
451
|
let data;
|
|
423
452
|
if (info.isArray) {
|
|
424
453
|
// hasMany - return array of linkage objects
|
|
@@ -463,6 +492,35 @@ export default class OrmRequest extends Request {
|
|
|
463
492
|
return routes;
|
|
464
493
|
}
|
|
465
494
|
auth(request, state) {
|
|
495
|
+
// abofs/stonyx-orm#270 — hand the predicate the value the record is
|
|
496
|
+
// ACTUALLY resolved by, rather than the raw text it was parsed from.
|
|
497
|
+
//
|
|
498
|
+
// This is the default path precisely because it requires the consumer to
|
|
499
|
+
// remember nothing. Exporting `normalizeRecordId` alone would leave the
|
|
500
|
+
// framework owning resolution while asking every consumer to call one
|
|
501
|
+
// function, with no signal when they forget — the same silent fail-open,
|
|
502
|
+
// one step over. The export is the escape hatch for hooks and custom
|
|
503
|
+
// handlers; this line is the contract.
|
|
504
|
+
//
|
|
505
|
+
// `request.params` is deliberately NOT mutated. Twelve `getId(...)` call
|
|
506
|
+
// sites read it, and `_withHooks` assigns `params: request.params` onto the
|
|
507
|
+
// hook context, so every consumer hook reads the same object. Changing
|
|
508
|
+
// `params.id` from string to number underneath them is a silent behaviour
|
|
509
|
+
// change on paths this issue is not about.
|
|
510
|
+
//
|
|
511
|
+
// Synchronous by necessity: @stonyx/rest-server calls auth() without
|
|
512
|
+
// awaiting it.
|
|
513
|
+
//
|
|
514
|
+
// The `undefined` branch is a PRESENCE check, not a second normalisation:
|
|
515
|
+
// it answers "does this route carry an :id at all", which is how both
|
|
516
|
+
// documented samples tell a collection request from a record request. It
|
|
517
|
+
// cannot be folded into normalizeRecordId, because that function must keep
|
|
518
|
+
// returning '' for a falsy id — `store.get(key, undefined)` returns the
|
|
519
|
+
// whole model Map rather than a record (abofs/stonyx-orm#167, pinned by
|
|
520
|
+
// test/unit/store-get-falsy-id-test.ts), so the resolution path depends on
|
|
521
|
+
// the '' it returns today.
|
|
522
|
+
const rawId = request.params?.id;
|
|
523
|
+
request.recordId = rawId === undefined ? undefined : normalizeRecordId(rawId);
|
|
466
524
|
const access = this.access(request);
|
|
467
525
|
if (!access)
|
|
468
526
|
return 403;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { ColumnRule } from '../column-conversion.js';
|
|
2
|
+
/**
|
|
3
|
+
* PostgreSQL's write-path conversion rule (#292).
|
|
4
|
+
*
|
|
5
|
+
* JSONB columns must receive a JSON *string*. node-pg's `prepareValue`
|
|
6
|
+
* (`pg/lib/utils.js`) checks `Array.isArray` before its object fallback, so a
|
|
7
|
+
* raw array is rendered as a Postgres array literal (`{"1","2","3"}`) rather
|
|
8
|
+
* than JSON. `[]` becomes `{}`, which is valid JSON, so it lands silently
|
|
9
|
+
* corrupted with no error.
|
|
10
|
+
*
|
|
11
|
+
* Two behaviours this rule must keep:
|
|
12
|
+
*
|
|
13
|
+
* - `typeof value === 'string'` passes through untouched. A JSONB column
|
|
14
|
+
* already holding a JSON string (`'{"a":1}'`) must store the object it
|
|
15
|
+
* describes; an unconditional `JSON.stringify` would double-encode it.
|
|
16
|
+
* - An `undefined` column type is a no-op. `updated_at`, `id` and FK columns
|
|
17
|
+
* are added outside the attribute loop and are absent from
|
|
18
|
+
* `schema.columns`.
|
|
19
|
+
*
|
|
20
|
+
* The `'JSONB'` sentinel is the exact literal emitted by
|
|
21
|
+
* `src/postgres/type-map.ts`, the only module that produces the values in
|
|
22
|
+
* `schema.columns`. The two producing functions are `getPgType` and
|
|
23
|
+
* `getVectorType` (`src/postgres/schema-introspector.ts` writes `columns[key]`
|
|
24
|
+
* at exactly three sites, all routed through those two); `mysqlTypeToPg` is not
|
|
25
|
+
* exported and is reachable only from inside `getPgType`.
|
|
26
|
+
*
|
|
27
|
+
* One documented gap: `getPgType` returns a consumer transform's declared
|
|
28
|
+
* `pgType` verbatim, so a consumer writing `pgType: 'jsonb'` puts a value in
|
|
29
|
+
* `schema.columns` that type-map never authored and this exact-match sentinel
|
|
30
|
+
* misses. It misses identically on the create path, pre-fix and post-fix --
|
|
31
|
+
* pre-existing and symmetric, not introduced here.
|
|
32
|
+
*/
|
|
33
|
+
export declare const postgresJsonRule: ColumnRule;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PostgreSQL's write-path conversion rule (#292).
|
|
3
|
+
*
|
|
4
|
+
* JSONB columns must receive a JSON *string*. node-pg's `prepareValue`
|
|
5
|
+
* (`pg/lib/utils.js`) checks `Array.isArray` before its object fallback, so a
|
|
6
|
+
* raw array is rendered as a Postgres array literal (`{"1","2","3"}`) rather
|
|
7
|
+
* than JSON. `[]` becomes `{}`, which is valid JSON, so it lands silently
|
|
8
|
+
* corrupted with no error.
|
|
9
|
+
*
|
|
10
|
+
* Two behaviours this rule must keep:
|
|
11
|
+
*
|
|
12
|
+
* - `typeof value === 'string'` passes through untouched. A JSONB column
|
|
13
|
+
* already holding a JSON string (`'{"a":1}'`) must store the object it
|
|
14
|
+
* describes; an unconditional `JSON.stringify` would double-encode it.
|
|
15
|
+
* - An `undefined` column type is a no-op. `updated_at`, `id` and FK columns
|
|
16
|
+
* are added outside the attribute loop and are absent from
|
|
17
|
+
* `schema.columns`.
|
|
18
|
+
*
|
|
19
|
+
* The `'JSONB'` sentinel is the exact literal emitted by
|
|
20
|
+
* `src/postgres/type-map.ts`, the only module that produces the values in
|
|
21
|
+
* `schema.columns`. The two producing functions are `getPgType` and
|
|
22
|
+
* `getVectorType` (`src/postgres/schema-introspector.ts` writes `columns[key]`
|
|
23
|
+
* at exactly three sites, all routed through those two); `mysqlTypeToPg` is not
|
|
24
|
+
* exported and is reachable only from inside `getPgType`.
|
|
25
|
+
*
|
|
26
|
+
* One documented gap: `getPgType` returns a consumer transform's declared
|
|
27
|
+
* `pgType` verbatim, so a consumer writing `pgType: 'jsonb'` puts a value in
|
|
28
|
+
* `schema.columns` that type-map never authored and this exact-match sentinel
|
|
29
|
+
* misses. It misses identically on the create path, pre-fix and post-fix --
|
|
30
|
+
* pre-existing and symmetric, not introduced here.
|
|
31
|
+
*/
|
|
32
|
+
export const postgresJsonRule = (value, columnType) => columnType === 'JSONB' && typeof value !== 'string'
|
|
33
|
+
? JSON.stringify(value)
|
|
34
|
+
: value;
|
|
@@ -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
|
|
12
|
-
port
|
|
13
|
-
user
|
|
14
|
-
password
|
|
15
|
-
database
|
|
16
|
-
max:
|
|
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) {
|
|
@@ -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 config from 'stonyx/config';
|
|
12
14
|
import log from 'stonyx/log';
|
|
13
15
|
import path from 'path';
|
|
@@ -17,6 +19,7 @@ interface PersistContext {
|
|
|
17
19
|
record?: OrmRecord;
|
|
18
20
|
recordId?: unknown;
|
|
19
21
|
oldState?: Record<string, unknown>;
|
|
22
|
+
rawData?: Record<string, unknown>;
|
|
20
23
|
}
|
|
21
24
|
interface PersistResponse {
|
|
22
25
|
data?: {
|
|
@@ -56,6 +59,8 @@ interface PostgresDeps {
|
|
|
56
59
|
confirm: typeof confirm;
|
|
57
60
|
readFile: typeof readFile;
|
|
58
61
|
getPluralName: typeof getPluralName;
|
|
62
|
+
convertRow: typeof convertRow;
|
|
63
|
+
postgresJsonRule: typeof postgresJsonRule;
|
|
59
64
|
config: typeof config;
|
|
60
65
|
log: typeof log;
|
|
61
66
|
path: typeof path;
|
|
@@ -71,6 +76,14 @@ export default class PostgresDB {
|
|
|
71
76
|
deps: PostgresDeps;
|
|
72
77
|
pool: Pool | null;
|
|
73
78
|
pgConfig: Record<string, unknown>;
|
|
79
|
+
/**
|
|
80
|
+
* Promise-chain mutex for write serialization (#156).
|
|
81
|
+
* All persist() calls chain through this single queue so concurrent
|
|
82
|
+
* fire-and-forget writes never produce parallel transactions
|
|
83
|
+
* on FK-linked rows (which cause deadlocks).
|
|
84
|
+
* Reads are NOT affected — only persist() serializes.
|
|
85
|
+
*/
|
|
86
|
+
private _writeQueue;
|
|
74
87
|
constructor(deps?: Partial<PostgresDeps>);
|
|
75
88
|
protected requirePool(): Pool;
|
|
76
89
|
init(): Promise<void>;
|