@stonyx/orm 0.3.2-beta.27 → 0.3.2-beta.270
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 +308 -14
- package/config/{environment.ts → environment.js} +8 -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 +2 -0
- package/dist/index.js +1 -0
- package/dist/main.js +29 -1
- package/dist/manage-record.js +34 -3
- package/dist/mysql/connection.d.ts +1 -0
- package/dist/mysql/mysql-db.d.ts +8 -0
- package/dist/mysql/mysql-db.js +44 -10
- 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 +77 -19
- package/dist/postgres/connection.d.ts +1 -0
- package/dist/postgres/connection.js +8 -6
- package/dist/postgres/postgres-db.d.ts +8 -0
- package/dist/postgres/postgres-db.js +44 -10
- 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 +65 -6
- package/dist/types/orm-types.d.ts +11 -0
- package/package.json +19 -7
- 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 +2 -0
- package/src/main.ts +29 -1
- package/src/manage-record.ts +41 -9
- package/src/mysql/connection.ts +1 -0
- package/src/mysql/mysql-db.ts +44 -12
- package/src/normalize-record-id.ts +88 -0
- package/src/orm-request.ts +109 -18
- package/src/postgres/connection.ts +10 -6
- package/src/postgres/postgres-db.ts +44 -12
- package/src/record.ts +8 -5
- package/src/relationships.ts +1 -1
- package/src/serializer.ts +39 -2
- package/src/store.ts +68 -6
- package/src/types/orm-types.ts +12 -0
- package/src/types/stonyx.d.ts +7 -1
package/src/orm-request.ts
CHANGED
|
@@ -7,11 +7,38 @@ import type { HookContext } from './hooks.js';
|
|
|
7
7
|
import config from 'stonyx/config';
|
|
8
8
|
import type { OrmRecord } from './types/orm-types.js';
|
|
9
9
|
import { isOrmRecord } from './utils.js';
|
|
10
|
+
import normalizeRecordId from './normalize-record-id.js';
|
|
10
11
|
|
|
11
|
-
|
|
12
|
+
/**
|
|
13
|
+
* The request object a consumer's `access()` predicate receives.
|
|
14
|
+
*
|
|
15
|
+
* Exported because `recordId` is public API — README's `access()` samples
|
|
16
|
+
* destructure it — and a public runtime field with an unreachable type asks the
|
|
17
|
+
* consumer to re-declare something the framework already knows, which is
|
|
18
|
+
* abofs/stonyx-orm#270's own defect shape one layer over into the type surface.
|
|
19
|
+
* `HookContext` (src/hooks.ts) is this repo's precedent for exporting the
|
|
20
|
+
* interface a consumer is handed. Re-exported from the root barrel as
|
|
21
|
+
* `OrmAccessRequest` (src/index.ts) — NOT as `OrmRequest`, which is already the
|
|
22
|
+
* default-exported CLASS in this file and means something else. The
|
|
23
|
+
* `./orm-request` subpath is not in the `exports` map, so the barrel is the
|
|
24
|
+
* only reachable spelling.
|
|
25
|
+
*/
|
|
26
|
+
export interface OrmRequest$ extends Request {
|
|
12
27
|
protocol?: string;
|
|
28
|
+
// Express sets this to the path the router was mounted at, e.g. '/api/animals'
|
|
29
|
+
// when orm.restServer.route is '/api'. Optional because non-Express callers
|
|
30
|
+
// (unit tests, programmatic handler invocation) do not supply it.
|
|
31
|
+
baseUrl?: string;
|
|
13
32
|
method: string;
|
|
14
33
|
params: { [key: string]: string };
|
|
34
|
+
// Attached by auth() before access() runs — abofs/stonyx-orm#270. This is the
|
|
35
|
+
// value the ORM resolves the record by; `params.id` remains the raw client
|
|
36
|
+
// text it was parsed from.
|
|
37
|
+
//
|
|
38
|
+
// `undefined` is part of the contract, not an absence: it is how a collection
|
|
39
|
+
// route is told from a record route, and both documented samples branch on
|
|
40
|
+
// it. Spelled out rather than left to `?:` for that reason.
|
|
41
|
+
recordId?: string | number | undefined;
|
|
15
42
|
body?: { [key: string]: unknown };
|
|
16
43
|
query?: { [key: string]: string };
|
|
17
44
|
get(header: string): string;
|
|
@@ -77,19 +104,49 @@ function getModelRelationships(modelName: string): { [key: string]: Relationship
|
|
|
77
104
|
return relationships;
|
|
78
105
|
}
|
|
79
106
|
|
|
80
|
-
|
|
81
|
-
|
|
107
|
+
/**
|
|
108
|
+
* Build the absolute base URL that every advertised link hangs off — origin
|
|
109
|
+
* plus the prefix the ORM's routes are actually mounted at.
|
|
110
|
+
*
|
|
111
|
+
* The prefix is derived from the *request*, not from
|
|
112
|
+
* `config.orm.restServer.route`. Express sets `request.baseUrl` to the real
|
|
113
|
+
* mountpath registered by `RestServer.mountRoute`, which for this module is
|
|
114
|
+
* always `<prefix>/<pluralizedModel>` (see setup-rest-server.ts). Stripping the
|
|
115
|
+
* trailing model segment therefore yields the prefix by construction, and the
|
|
116
|
+
* link builder cannot drift from the mount registrar the way a second,
|
|
117
|
+
* independent normalisation of `route` would.
|
|
118
|
+
*
|
|
119
|
+
* When `request.baseUrl` is absent or does not end in the model segment the
|
|
120
|
+
* prefix is empty, which reproduces the previous origin-only behaviour.
|
|
121
|
+
*/
|
|
122
|
+
function getBaseUrl(request: OrmRequest$, pluralizedModel: string): string {
|
|
82
123
|
const protocol = request.protocol || 'http';
|
|
83
124
|
const host = request.get('host');
|
|
84
|
-
|
|
125
|
+
|
|
126
|
+
const modelSegment = `/${pluralizedModel}`;
|
|
127
|
+
const mountPath = request.baseUrl ?? '';
|
|
128
|
+
const prefix = mountPath.endsWith(modelSegment) ? mountPath.slice(0, -modelSegment.length) : '';
|
|
129
|
+
|
|
130
|
+
return `${protocol}://${host}${prefix}`;
|
|
85
131
|
}
|
|
86
132
|
|
|
133
|
+
// Kept as a name because twelve `getId(...)` call sites read it. It is now a
|
|
134
|
+
// thin delegate: there is exactly ONE normalisation of a URL id in the repo,
|
|
135
|
+
// and it is the exported one a consumer can import.
|
|
136
|
+
//
|
|
137
|
+
// "of a URL id" is the load-bearing qualifier, and it is the same one
|
|
138
|
+
// src/normalize-record-id.ts:18 carries. Three copies of the coercion survive
|
|
139
|
+
// at this head — src/orm-request.ts (the create-response path),
|
|
140
|
+
// src/postgres/postgres-db.ts and src/mysql/mysql-db.ts — but each normalises a
|
|
141
|
+
// RESPONSE id (`response?.data?.id`), not a URL id, and each omits this
|
|
142
|
+
// function's `if (!id) return ''` guard. They are tracked as
|
|
143
|
+
// abofs/stonyx-orm#282 and enumerated by name in AC-5's allowlist
|
|
144
|
+
// (test/integration/readme-sample-test.ts).
|
|
145
|
+
//
|
|
146
|
+
// A second implementation of the URL-id normalisation here is the defect
|
|
147
|
+
// abofs/stonyx-orm#270 exists to remove — see src/normalize-record-id.ts.
|
|
87
148
|
function getId(params: { id?: string; [key: string]: unknown }): string | number {
|
|
88
|
-
|
|
89
|
-
if (!id) return '';
|
|
90
|
-
if (isNaN(id as unknown as number)) return id;
|
|
91
|
-
|
|
92
|
-
return parseInt(id);
|
|
149
|
+
return normalizeRecordId(params.id);
|
|
93
150
|
}
|
|
94
151
|
|
|
95
152
|
function buildResponse(
|
|
@@ -278,7 +335,7 @@ export default class OrmRequest extends Request {
|
|
|
278
335
|
if (accessFilter) recordsToReturn = recordsToReturn.filter(accessFilter as (record: OrmRecord) => boolean);
|
|
279
336
|
if (queryFilterPredicate) recordsToReturn = recordsToReturn.filter(queryFilterPredicate as (record: OrmRecord) => boolean);
|
|
280
337
|
|
|
281
|
-
const baseUrl = getBaseUrl(request);
|
|
338
|
+
const baseUrl = getBaseUrl(request, pluralizedModel);
|
|
282
339
|
const data = recordsToReturn.map(record => record.toJSON?.({ fields: modelFields, baseUrl }));
|
|
283
340
|
|
|
284
341
|
return buildResponse(data, request.query?.include, recordsToReturn, {
|
|
@@ -294,7 +351,7 @@ export default class OrmRequest extends Request {
|
|
|
294
351
|
const fieldsMap = parseFields(request.query);
|
|
295
352
|
const modelFields = fieldsMap.get(pluralizedModel) || fieldsMap.get(model);
|
|
296
353
|
|
|
297
|
-
const baseUrl = getBaseUrl(request);
|
|
354
|
+
const baseUrl = getBaseUrl(request, pluralizedModel);
|
|
298
355
|
return buildResponse(record.toJSON?.({ fields: modelFields, baseUrl }), request.query?.include, record, {
|
|
299
356
|
links: { self: `${baseUrl}/${pluralizedModel}/${request.params.id}` },
|
|
300
357
|
baseUrl
|
|
@@ -376,7 +433,7 @@ export default class OrmRequest extends Request {
|
|
|
376
433
|
};
|
|
377
434
|
|
|
378
435
|
const deleteHandler: HandlerFn = ({ params }) => {
|
|
379
|
-
store.remove(model, getId(params));
|
|
436
|
+
store.remove(model, getId(params), { _skipAutoPersist: true });
|
|
380
437
|
return 204;
|
|
381
438
|
};
|
|
382
439
|
|
|
@@ -443,9 +500,14 @@ export default class OrmRequest extends Request {
|
|
|
443
500
|
// Execute main handler
|
|
444
501
|
const response = await handler(request, state);
|
|
445
502
|
|
|
446
|
-
//
|
|
503
|
+
// Set context.record for update BEFORE persist so SQL drivers can read it
|
|
504
|
+
if (operation === 'update' && (response as JsonApiResponse)?.data) {
|
|
505
|
+
context.record = store.get(this.model, getId(request.params));
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
// Persist to SQL database for all write operations (create/update/delete)
|
|
447
509
|
const sqlDb = Orm.instance.sqlDb;
|
|
448
|
-
if (sqlDb && (operation
|
|
510
|
+
if (sqlDb && WRITE_OPERATIONS.has(operation)) {
|
|
449
511
|
await sqlDb.persist(operation, this.model, context, response);
|
|
450
512
|
}
|
|
451
513
|
|
|
@@ -461,8 +523,6 @@ export default class OrmRequest extends Request {
|
|
|
461
523
|
const responseData = (response as { data: { id: string | number } }).data;
|
|
462
524
|
const recordId = isNaN(responseData.id as unknown as number) ? responseData.id : parseInt(responseData.id as string);
|
|
463
525
|
context.record = store.get(this.model, recordId);
|
|
464
|
-
} else if (operation === 'update' && (response as JsonApiResponse)?.data) {
|
|
465
|
-
context.record = store.get(this.model, getId(request.params));
|
|
466
526
|
} else if (operation === 'delete') {
|
|
467
527
|
// For delete, the record may no longer exist, but we have oldState
|
|
468
528
|
context.recordId = getId(request.params);
|
|
@@ -499,7 +559,7 @@ export default class OrmRequest extends Request {
|
|
|
499
559
|
if (!record) return 404;
|
|
500
560
|
|
|
501
561
|
const relatedData = record.__relationships[relationshipName];
|
|
502
|
-
const baseUrl = getBaseUrl(request);
|
|
562
|
+
const baseUrl = getBaseUrl(request, pluralizedModel);
|
|
503
563
|
|
|
504
564
|
let data: unknown;
|
|
505
565
|
if (info.isArray) {
|
|
@@ -523,7 +583,7 @@ export default class OrmRequest extends Request {
|
|
|
523
583
|
if (!record) return 404;
|
|
524
584
|
|
|
525
585
|
const relatedData = record.__relationships[relationshipName];
|
|
526
|
-
const baseUrl = getBaseUrl(request);
|
|
586
|
+
const baseUrl = getBaseUrl(request, pluralizedModel);
|
|
527
587
|
|
|
528
588
|
let data: unknown;
|
|
529
589
|
if (info.isArray) {
|
|
@@ -572,6 +632,37 @@ export default class OrmRequest extends Request {
|
|
|
572
632
|
}
|
|
573
633
|
|
|
574
634
|
auth(request: OrmRequest$, state: { [key: string]: unknown }): number | undefined {
|
|
635
|
+
// abofs/stonyx-orm#270 — hand the predicate the value the record is
|
|
636
|
+
// ACTUALLY resolved by, rather than the raw text it was parsed from.
|
|
637
|
+
//
|
|
638
|
+
// This is the default path precisely because it requires the consumer to
|
|
639
|
+
// remember nothing. Exporting `normalizeRecordId` alone would leave the
|
|
640
|
+
// framework owning resolution while asking every consumer to call one
|
|
641
|
+
// function, with no signal when they forget — the same silent fail-open,
|
|
642
|
+
// one step over. The export is the escape hatch for hooks and custom
|
|
643
|
+
// handlers; this line is the contract.
|
|
644
|
+
//
|
|
645
|
+
// `request.params` is deliberately NOT mutated. Twelve `getId(...)` call
|
|
646
|
+
// sites read it, and `_withHooks` assigns `params: request.params` onto the
|
|
647
|
+
// hook context, so every consumer hook reads the same object. Changing
|
|
648
|
+
// `params.id` from string to number underneath them is a silent behaviour
|
|
649
|
+
// change on paths this issue is not about.
|
|
650
|
+
//
|
|
651
|
+
// Synchronous by necessity: @stonyx/rest-server calls auth() without
|
|
652
|
+
// awaiting it.
|
|
653
|
+
//
|
|
654
|
+
// The `undefined` branch is a PRESENCE check, not a second normalisation:
|
|
655
|
+
// it answers "does this route carry an :id at all", which is how both
|
|
656
|
+
// documented samples tell a collection request from a record request. It
|
|
657
|
+
// cannot be folded into normalizeRecordId, because that function must keep
|
|
658
|
+
// returning '' for a falsy id — `store.get(key, undefined)` returns the
|
|
659
|
+
// whole model Map rather than a record (abofs/stonyx-orm#167, pinned by
|
|
660
|
+
// test/unit/store-get-falsy-id-test.ts), so the resolution path depends on
|
|
661
|
+
// the '' it returns today.
|
|
662
|
+
const rawId = request.params?.id;
|
|
663
|
+
|
|
664
|
+
request.recordId = rawId === undefined ? undefined : normalizeRecordId(rawId);
|
|
665
|
+
|
|
575
666
|
const access = this.access(request);
|
|
576
667
|
|
|
577
668
|
if (!access) return 403;
|
|
@@ -8,6 +8,7 @@ interface PgConfig {
|
|
|
8
8
|
password: string;
|
|
9
9
|
database: string;
|
|
10
10
|
connectionLimit: number;
|
|
11
|
+
[key: string]: unknown;
|
|
11
12
|
}
|
|
12
13
|
|
|
13
14
|
let pool: PgPool | null = null;
|
|
@@ -20,15 +21,18 @@ export async function getPool(pgConfig: PgConfig, extensions: string[] = ['vecto
|
|
|
20
21
|
|
|
21
22
|
const { default: pg } = await import('pg');
|
|
22
23
|
|
|
24
|
+
const { host, port, user, password, database, connectionLimit, migrationsDir, migrationsTable, autoMigrate, ...poolOpts } = pgConfig;
|
|
25
|
+
|
|
23
26
|
pool = new pg.Pool({
|
|
24
|
-
host
|
|
25
|
-
port
|
|
26
|
-
user
|
|
27
|
-
password
|
|
28
|
-
database
|
|
29
|
-
max:
|
|
27
|
+
host,
|
|
28
|
+
port,
|
|
29
|
+
user,
|
|
30
|
+
password,
|
|
31
|
+
database,
|
|
32
|
+
max: connectionLimit,
|
|
30
33
|
idleTimeoutMillis: 30000,
|
|
31
34
|
connectionTimeoutMillis: 10000,
|
|
35
|
+
...poolOpts,
|
|
32
36
|
});
|
|
33
37
|
|
|
34
38
|
// Enable requested PostgreSQL extensions
|
|
@@ -90,6 +90,15 @@ export default class PostgresDB {
|
|
|
90
90
|
pool!: Pool | null;
|
|
91
91
|
pgConfig!: Record<string, unknown>;
|
|
92
92
|
|
|
93
|
+
/**
|
|
94
|
+
* Promise-chain mutex for write serialization (#156).
|
|
95
|
+
* All persist() calls chain through this single queue so concurrent
|
|
96
|
+
* fire-and-forget writes never produce parallel transactions
|
|
97
|
+
* on FK-linked rows (which cause deadlocks).
|
|
98
|
+
* Reads are NOT affected — only persist() serializes.
|
|
99
|
+
*/
|
|
100
|
+
private _writeQueue: Promise<void> = Promise.resolve();
|
|
101
|
+
|
|
93
102
|
constructor(deps: Partial<PostgresDeps> = {}) {
|
|
94
103
|
const Ctor = this.constructor as typeof PostgresDB;
|
|
95
104
|
if (Ctor.instance) return Ctor.instance;
|
|
@@ -125,7 +134,15 @@ export default class PostgresDB {
|
|
|
125
134
|
if (pending.length > 0) {
|
|
126
135
|
this.deps.log.db?.(`${pending.length} pending migration(s) found.`);
|
|
127
136
|
|
|
128
|
-
|
|
137
|
+
let shouldApply: boolean;
|
|
138
|
+
if (this.pgConfig.autoMigrate === true) {
|
|
139
|
+
shouldApply = true;
|
|
140
|
+
} else if (this.pgConfig.autoMigrate === false) {
|
|
141
|
+
shouldApply = false;
|
|
142
|
+
this.deps.log.warn?.(`autoMigrate is false — skipping ${pending.length} pending migration(s).`);
|
|
143
|
+
} else {
|
|
144
|
+
shouldApply = await this.deps.confirm(`${pending.length} pending migration(s) found. Apply now?`);
|
|
145
|
+
}
|
|
129
146
|
|
|
130
147
|
if (shouldApply) {
|
|
131
148
|
for (const filename of pending) {
|
|
@@ -146,9 +163,17 @@ export default class PostgresDB {
|
|
|
146
163
|
const modelCount = Object.keys(schemas).length;
|
|
147
164
|
|
|
148
165
|
if (modelCount > 0) {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
166
|
+
let shouldGenerate: boolean;
|
|
167
|
+
if (this.pgConfig.autoMigrate === true) {
|
|
168
|
+
shouldGenerate = true;
|
|
169
|
+
} else if (this.pgConfig.autoMigrate === false) {
|
|
170
|
+
shouldGenerate = false;
|
|
171
|
+
this.deps.log.warn?.(`autoMigrate is false — skipping initial migration generation for ${modelCount} model(s).`);
|
|
172
|
+
} else {
|
|
173
|
+
shouldGenerate = await this.deps.confirm(
|
|
174
|
+
`No migrations found but ${modelCount} model(s) detected. Generate and apply initial migration?`
|
|
175
|
+
);
|
|
176
|
+
}
|
|
152
177
|
|
|
153
178
|
if (shouldGenerate) {
|
|
154
179
|
const { generateMigration } = await import('./migration-generator.js');
|
|
@@ -468,14 +493,21 @@ export default class PostgresDB {
|
|
|
468
493
|
const Orm = (await import('@stonyx/orm')).default;
|
|
469
494
|
if ((Orm.instance as { isView?: (name: string) => boolean })?.isView?.(modelName)) return;
|
|
470
495
|
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
496
|
+
const work = async () => {
|
|
497
|
+
switch (operation) {
|
|
498
|
+
case 'create':
|
|
499
|
+
return this._persistCreate(modelName, context, response);
|
|
500
|
+
case 'update':
|
|
501
|
+
return this._persistUpdate(modelName, context, response);
|
|
502
|
+
case 'delete':
|
|
503
|
+
return this._persistDelete(modelName, context);
|
|
504
|
+
}
|
|
505
|
+
};
|
|
506
|
+
|
|
507
|
+
// Chain through the write queue — .then(work, work) ensures the queue
|
|
508
|
+
// advances even when a previous persist rejects (#156).
|
|
509
|
+
this._writeQueue = this._writeQueue.then(work, work);
|
|
510
|
+
return this._writeQueue;
|
|
479
511
|
}
|
|
480
512
|
|
|
481
513
|
private async _persistCreate(modelName: string, context: PersistContext, response: PersistResponse): Promise<void> {
|
package/src/record.ts
CHANGED
|
@@ -87,12 +87,15 @@ export default class Record {
|
|
|
87
87
|
|
|
88
88
|
for (const [key, childRecord] of Object.entries(this.__relationships)) {
|
|
89
89
|
if (Array.isArray(childRecord)) {
|
|
90
|
+
// Filter out cleaned records (those with no __model)
|
|
91
|
+
const live = childRecord.filter((r: Record) => r?.__model);
|
|
92
|
+
|
|
90
93
|
// Deduplicate by record ID — keep last occurrence (latest data wins)
|
|
91
94
|
const seen = new Set<unknown>();
|
|
92
95
|
const unique: Record[] = [];
|
|
93
96
|
|
|
94
|
-
for (let i =
|
|
95
|
-
const r =
|
|
97
|
+
for (let i = live.length - 1; i >= 0; i--) {
|
|
98
|
+
const r = live[i] as Record;
|
|
96
99
|
if (!seen.has(r.id)) {
|
|
97
100
|
seen.add(r.id);
|
|
98
101
|
unique.push(r);
|
|
@@ -102,7 +105,7 @@ export default class Record {
|
|
|
102
105
|
unique.reverse();
|
|
103
106
|
records[key] = unique.map((r: Record) => r.serialize());
|
|
104
107
|
} else {
|
|
105
|
-
records[key] = (childRecord as Record)?.serialize()
|
|
108
|
+
records[key] = (childRecord as Record)?.__model ? (childRecord as Record).serialize() : null;
|
|
106
109
|
}
|
|
107
110
|
}
|
|
108
111
|
|
|
@@ -136,8 +139,8 @@ export default class Record {
|
|
|
136
139
|
if (fields && !fields.has(key)) continue;
|
|
137
140
|
|
|
138
141
|
const relationshipData = Array.isArray(childRecord)
|
|
139
|
-
? childRecord.map((r: Record) => ({ type: r.__model.__name, id: r.id }))
|
|
140
|
-
: childRecord ? { type: (childRecord as Record).__model.__name, id: (childRecord as Record).id } : null;
|
|
142
|
+
? childRecord.filter((r: Record) => r?.__model).map((r: Record) => ({ type: r.__model.__name, id: r.id }))
|
|
143
|
+
: (childRecord && (childRecord as Record).__model) ? { type: (childRecord as Record).__model.__name, id: (childRecord as Record).id } : null;
|
|
141
144
|
|
|
142
145
|
// Dasherize the key for URL paths (e.g., accessLinks -> access-links)
|
|
143
146
|
const dasherizedKey = camelCaseToKebabCase(key);
|
package/src/relationships.ts
CHANGED
|
@@ -51,4 +51,4 @@ export function getPendingBelongsToRegistry(): PendingBelongsToMap {
|
|
|
51
51
|
return relationships.get('pendingBelongsTo') as PendingBelongsToMap;
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
-
export const TYPES: string[] = ['global', 'hasMany', 'belongsTo', 'pending'];
|
|
54
|
+
export const TYPES: string[] = ['global', 'hasMany', 'belongsTo', 'pending', 'pendingBelongsTo'];
|
package/src/serializer.ts
CHANGED
|
@@ -94,8 +94,45 @@ export default class Serializer {
|
|
|
94
94
|
const handlerOptions = { ...options, _relationshipKey: key };
|
|
95
95
|
const childRecord = handler(record, data, handlerOptions);
|
|
96
96
|
|
|
97
|
-
|
|
98
|
-
|
|
97
|
+
// hasMany relationships use a getter so format()/toJSON() always read
|
|
98
|
+
// the live registry array instead of a stale snapshot captured at
|
|
99
|
+
// serialization time. This is critical when child records are created
|
|
100
|
+
// in a later async frame — the belongsTo inverse wiring pushes into
|
|
101
|
+
// the shared registry array, and the getter ensures the parent sees it.
|
|
102
|
+
const isHasMany = (handler as { __relationshipType?: string }).__relationshipType === 'hasMany';
|
|
103
|
+
|
|
104
|
+
if (isHasMany) {
|
|
105
|
+
// `childRecord` IS the shared registry array — define a getter that
|
|
106
|
+
// always dereferences through the same array reference.
|
|
107
|
+
const registryArray = childRecord as unknown[];
|
|
108
|
+
Object.defineProperty(rec, key, {
|
|
109
|
+
enumerable: true,
|
|
110
|
+
configurable: true,
|
|
111
|
+
get: () => registryArray,
|
|
112
|
+
set(v: unknown) { relatedRecords[key] = v; }
|
|
113
|
+
});
|
|
114
|
+
Object.defineProperty(relatedRecords, key, {
|
|
115
|
+
enumerable: true,
|
|
116
|
+
configurable: true,
|
|
117
|
+
get: () => registryArray,
|
|
118
|
+
set(v: unknown) { Object.defineProperty(relatedRecords, key, { value: v, writable: true, enumerable: true, configurable: true }); }
|
|
119
|
+
});
|
|
120
|
+
} else {
|
|
121
|
+
rec[key] = childRecord;
|
|
122
|
+
relatedRecords[key] = childRecord;
|
|
123
|
+
|
|
124
|
+
// Preserve the raw FK value in __data when the belongsTo handler
|
|
125
|
+
// couldn't resolve the target (e.g., memory:false model not loaded).
|
|
126
|
+
// This allows adapters to read the FK from __data as a fallback
|
|
127
|
+
// when __relationships[key] is null. Only store when `data` is a
|
|
128
|
+
// truthy non-object — i.e., a raw FK string/number that the handler
|
|
129
|
+
// attempted but failed to resolve. When `data` is null/undefined
|
|
130
|
+
// (optional empty relationship) we intentionally skip to preserve
|
|
131
|
+
// the existing behavior of not populating __data for empty FKs.
|
|
132
|
+
if (childRecord === null && data && typeof data !== 'object') {
|
|
133
|
+
parsedData[key] = data;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
99
136
|
|
|
100
137
|
continue;
|
|
101
138
|
}
|
package/src/store.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import Orm, { relationships } from '@stonyx/orm';
|
|
2
|
-
import { TYPES, getHasManyRegistry, getBelongsToRegistry, getPendingRegistry } from './relationships.js';
|
|
2
|
+
import { TYPES, getHasManyRegistry, getBelongsToRegistry, getPendingRegistry, getPendingBelongsToRegistry } from './relationships.js';
|
|
3
3
|
import ViewResolver from './view-resolver.js';
|
|
4
4
|
|
|
5
5
|
interface UnloadOptions {
|
|
@@ -64,7 +64,7 @@ export default class Store {
|
|
|
64
64
|
get(key: string): Map<number | string, unknown> | undefined;
|
|
65
65
|
get(key: string, id: number | string): unknown;
|
|
66
66
|
get(key: string, id?: number | string): Map<number | string, unknown> | unknown | undefined {
|
|
67
|
-
if (
|
|
67
|
+
if (id === undefined) return this.data.get(key);
|
|
68
68
|
|
|
69
69
|
return this.data.get(key)?.get(id);
|
|
70
70
|
}
|
|
@@ -170,14 +170,15 @@ export default class Store {
|
|
|
170
170
|
this.data.set(key, value);
|
|
171
171
|
}
|
|
172
172
|
|
|
173
|
-
remove(key: string, id?: number | string): void {
|
|
173
|
+
remove(key: string, id?: number | string, options?: { _skipAutoPersist?: boolean }): void {
|
|
174
174
|
// Guard: read-only views cannot have records removed
|
|
175
175
|
if (Orm.instance?.isView?.(key)) {
|
|
176
176
|
throw new Error(`Cannot remove records from read-only view '${key}'`);
|
|
177
177
|
}
|
|
178
178
|
|
|
179
|
-
// Auto-persist delete to SQL
|
|
180
|
-
|
|
179
|
+
// Auto-persist delete to SQL (fire-and-forget) — skipped when the
|
|
180
|
+
// request path handles persist itself to avoid double-delete.
|
|
181
|
+
if (id && Orm.instance?.sqlDb && !options?._skipAutoPersist) {
|
|
181
182
|
Orm.instance.sqlDb.persist('delete', key, { recordId: id }, {}).catch((err: unknown) => {
|
|
182
183
|
Orm.instance.emitPersistError({
|
|
183
184
|
operation: 'delete',
|
|
@@ -193,6 +194,44 @@ export default class Store {
|
|
|
193
194
|
this.unloadAllRecords(key);
|
|
194
195
|
}
|
|
195
196
|
|
|
197
|
+
/**
|
|
198
|
+
* Evict a record from the store with full relationship registry cleanup.
|
|
199
|
+
* The caller retains its reference to the returned record, which is the
|
|
200
|
+
* contract memory:false post-persist eviction relies on.
|
|
201
|
+
*
|
|
202
|
+
* @param registryId - The ID used when the record's relationships were
|
|
203
|
+
* registered. For SQL models with pending IDs, this is the original
|
|
204
|
+
* negative pending ID (before the adapter re-keyed to the real DB ID).
|
|
205
|
+
*/
|
|
206
|
+
evictRecord(modelName: string, id: unknown, registryId?: unknown): void {
|
|
207
|
+
const modelStore = this.data.get(modelName);
|
|
208
|
+
if (!modelStore) return;
|
|
209
|
+
|
|
210
|
+
if (typeof id !== 'string' && typeof id !== 'number') return;
|
|
211
|
+
const raw = modelStore.get(id);
|
|
212
|
+
if (!raw || !isStoreRecord(raw)) return;
|
|
213
|
+
|
|
214
|
+
const visited = new Set([`${modelName}:${id}`]);
|
|
215
|
+
|
|
216
|
+
// Remove from hasMany arrays and nullify belongsTo references using current ID
|
|
217
|
+
// (the adapter updates record.id, so value-based matches need the current ID)
|
|
218
|
+
this._removeFromHasManyArrays(modelName, id, visited);
|
|
219
|
+
this._nullifyBelongsToReferences(modelName, id, visited);
|
|
220
|
+
|
|
221
|
+
// Clean up relationship registry entries using the registry key
|
|
222
|
+
// (belongsTo/hasMany registries were keyed by the ID at registration time,
|
|
223
|
+
// which may differ from the current ID if SQL persist re-keyed the record)
|
|
224
|
+
const cleanupId = registryId ?? id;
|
|
225
|
+
this._cleanupRelationshipRegistries(modelName, cleanupId);
|
|
226
|
+
|
|
227
|
+
// If registryId differs from id, also clean with current id as safety net
|
|
228
|
+
if (registryId !== undefined && registryId !== id) {
|
|
229
|
+
this._cleanupRelationshipRegistries(modelName, id);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
modelStore.delete(id);
|
|
233
|
+
}
|
|
234
|
+
|
|
196
235
|
unloadRecord(model: string, id: unknown, options: UnloadOptions = {}): void {
|
|
197
236
|
const modelStore = this.data.get(model);
|
|
198
237
|
|
|
@@ -219,7 +258,6 @@ export default class Store {
|
|
|
219
258
|
this._removeFromHasManyArrays(modelName, recordId, visited);
|
|
220
259
|
this._nullifyBelongsToReferences(modelName, recordId, visited);
|
|
221
260
|
this._cleanupRelationshipRegistries(modelName, recordId);
|
|
222
|
-
recordToUnload.clean();
|
|
223
261
|
|
|
224
262
|
this.data.get(modelName)?.delete(recordId as string | number);
|
|
225
263
|
}
|
|
@@ -308,6 +346,30 @@ export default class Store {
|
|
|
308
346
|
|
|
309
347
|
const pendingMap = getPendingRegistry().get(modelName);
|
|
310
348
|
if (pendingMap) pendingMap.delete(recordId);
|
|
349
|
+
|
|
350
|
+
// Clean pendingBelongsTo entries in both directions
|
|
351
|
+
const pendingBelongsToMap = getPendingBelongsToRegistry();
|
|
352
|
+
if (pendingBelongsToMap) {
|
|
353
|
+
// Direction 1: evicted record was the TARGET others were waiting for
|
|
354
|
+
const targetEntries = pendingBelongsToMap.get(modelName);
|
|
355
|
+
if (targetEntries) targetEntries.delete(recordId);
|
|
356
|
+
|
|
357
|
+
// Direction 2: evicted record was the SOURCE with unresolved forward-references
|
|
358
|
+
for (const [, targetIdMap] of pendingBelongsToMap) {
|
|
359
|
+
for (const [targetId, entries] of targetIdMap) {
|
|
360
|
+
if (!Array.isArray(entries)) continue;
|
|
361
|
+
const filtered = entries.filter((e: unknown) => {
|
|
362
|
+
const entry = e as { sourceModelName?: string; relationshipId?: unknown };
|
|
363
|
+
return !(entry.sourceModelName === modelName && entry.relationshipId === recordId);
|
|
364
|
+
});
|
|
365
|
+
if (filtered.length === 0) {
|
|
366
|
+
targetIdMap.delete(targetId);
|
|
367
|
+
} else if (filtered.length < entries.length) {
|
|
368
|
+
targetIdMap.set(targetId, filtered);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
}
|
|
311
373
|
}
|
|
312
374
|
|
|
313
375
|
/**
|
package/src/types/orm-types.ts
CHANGED
|
@@ -18,6 +18,7 @@ export interface OrmMysqlConfig {
|
|
|
18
18
|
connectionLimit?: number;
|
|
19
19
|
migrationsDir?: string;
|
|
20
20
|
migrationsTable?: string;
|
|
21
|
+
autoMigrate?: boolean;
|
|
21
22
|
[key: string]: unknown;
|
|
22
23
|
}
|
|
23
24
|
|
|
@@ -30,6 +31,7 @@ export interface OrmPostgresConfig {
|
|
|
30
31
|
connectionLimit?: number;
|
|
31
32
|
migrationsDir?: string;
|
|
32
33
|
migrationsTable?: string;
|
|
34
|
+
autoMigrate?: boolean;
|
|
33
35
|
[key: string]: unknown;
|
|
34
36
|
}
|
|
35
37
|
|
|
@@ -48,6 +50,13 @@ export interface OrmRestServerConfig {
|
|
|
48
50
|
metaRoute: boolean;
|
|
49
51
|
}
|
|
50
52
|
|
|
53
|
+
export interface OrmDynamoDBConfig {
|
|
54
|
+
region?: string;
|
|
55
|
+
endpoint?: string;
|
|
56
|
+
tablePrefix?: string;
|
|
57
|
+
[key: string]: unknown;
|
|
58
|
+
}
|
|
59
|
+
|
|
51
60
|
export interface OrmSection {
|
|
52
61
|
db: OrmDbConfig;
|
|
53
62
|
paths: OrmPaths;
|
|
@@ -55,6 +64,9 @@ export interface OrmSection {
|
|
|
55
64
|
mysql?: OrmMysqlConfig;
|
|
56
65
|
postgres?: OrmPostgresConfig;
|
|
57
66
|
timescale?: OrmPostgresConfig;
|
|
67
|
+
dynamodb?: OrmDynamoDBConfig;
|
|
68
|
+
logColor?: string;
|
|
69
|
+
logMethod?: string;
|
|
58
70
|
[key: string]: unknown;
|
|
59
71
|
}
|
|
60
72
|
|
package/src/types/stonyx.d.ts
CHANGED
|
@@ -5,7 +5,13 @@ declare module 'stonyx/config' {
|
|
|
5
5
|
}
|
|
6
6
|
|
|
7
7
|
declare module 'stonyx/log' {
|
|
8
|
-
|
|
8
|
+
interface Log {
|
|
9
|
+
db(message: string): void;
|
|
10
|
+
error(message: string, ...args: unknown[]): void;
|
|
11
|
+
defineType(type: string, setting: string, options?: Record<string, unknown> | null): void;
|
|
12
|
+
[key: string]: ((...args: unknown[]) => void) | undefined;
|
|
13
|
+
}
|
|
14
|
+
const log: Log;
|
|
9
15
|
export default log;
|
|
10
16
|
}
|
|
11
17
|
|