@stonyx/orm 0.3.2-alpha.3 → 0.3.2-alpha.31
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 +35 -2
- package/config/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 +132 -0
- package/dist/dynamodb/dynamodb-db.js +586 -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 +1 -0
- package/dist/main.d.ts +16 -0
- package/dist/main.js +36 -0
- package/dist/manage-record.js +51 -8
- package/dist/mysql/mysql-db.d.ts +1 -0
- package/dist/mysql/mysql-db.js +4 -2
- package/dist/postgres/connection.d.ts +1 -0
- package/dist/postgres/connection.js +8 -6
- package/dist/postgres/postgres-db.js +4 -2
- package/dist/relationships.js +1 -1
- package/dist/serializer.js +38 -2
- package/dist/store.d.ts +10 -0
- package/dist/store.js +66 -2
- package/dist/types/orm-types.d.ts +9 -0
- package/package.json +17 -7
- package/src/commands.ts +43 -0
- package/src/dynamodb/connection.ts +50 -0
- package/src/dynamodb/dynamodb-db.ts +801 -0
- package/src/dynamodb/operation-builder.ts +202 -0
- package/src/dynamodb/type-map.ts +54 -0
- package/src/index.ts +1 -0
- package/src/main.ts +45 -0
- package/src/manage-record.ts +54 -9
- package/src/mysql/mysql-db.ts +5 -2
- package/src/postgres/connection.ts +10 -6
- package/src/postgres/postgres-db.ts +4 -2
- package/src/relationships.ts +1 -1
- package/src/serializer.ts +39 -2
- package/src/store.ts +69 -2
- package/src/types/orm-types.ts +10 -0
- 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
|
@@ -8,6 +8,7 @@ import { createRecord, updateRecord } from './manage-record.js';
|
|
|
8
8
|
import { count, avg, sum, min, max } from './aggregates.js';
|
|
9
9
|
export { default } from './main.js';
|
|
10
10
|
export { store, relationships } from './main.js';
|
|
11
|
+
export type { PersistErrorDetail } from './main.js';
|
|
11
12
|
export { Model, View, Serializer };
|
|
12
13
|
export { attr, belongsTo, hasMany, createRecord, updateRecord };
|
|
13
14
|
export { count, avg, sum, min, max };
|
package/dist/main.d.ts
CHANGED
|
@@ -15,6 +15,12 @@ export interface OrmDB {
|
|
|
15
15
|
save(): Promise<void>;
|
|
16
16
|
init(): Promise<void>;
|
|
17
17
|
}
|
|
18
|
+
export interface PersistErrorDetail {
|
|
19
|
+
operation: 'create' | 'update' | 'delete';
|
|
20
|
+
modelName: string;
|
|
21
|
+
recordId: unknown;
|
|
22
|
+
error: Error;
|
|
23
|
+
}
|
|
18
24
|
export default class Orm {
|
|
19
25
|
static initialized: boolean;
|
|
20
26
|
static relationships: Map<string, Map<string, unknown>>;
|
|
@@ -29,6 +35,7 @@ export default class Orm {
|
|
|
29
35
|
options: OrmOptions;
|
|
30
36
|
sqlDb?: SqlDb;
|
|
31
37
|
db?: OrmDB | SqlDb;
|
|
38
|
+
private _persistErrorHandler;
|
|
32
39
|
constructor(options?: OrmOptions);
|
|
33
40
|
init(): Promise<void>;
|
|
34
41
|
startup(): Promise<void>;
|
|
@@ -39,6 +46,15 @@ export default class Orm {
|
|
|
39
46
|
serializerClass: unknown;
|
|
40
47
|
};
|
|
41
48
|
isView(modelName: string): boolean;
|
|
49
|
+
/**
|
|
50
|
+
* Register a callback to be invoked when a fire-and-forget SQL persist fails.
|
|
51
|
+
* Without a handler, persist errors are logged via log.error (backwards-compatible).
|
|
52
|
+
*/
|
|
53
|
+
onPersistError(handler: ((detail: PersistErrorDetail) => void) | null): void;
|
|
54
|
+
/**
|
|
55
|
+
* Emit a persist error to the registered handler, or fall back to log.error.
|
|
56
|
+
*/
|
|
57
|
+
emitPersistError(detail: PersistErrorDetail): void;
|
|
42
58
|
warn(message: string): void;
|
|
43
59
|
}
|
|
44
60
|
export declare const store: Store;
|
package/dist/main.js
CHANGED
|
@@ -41,6 +41,7 @@ export default class Orm {
|
|
|
41
41
|
options;
|
|
42
42
|
sqlDb;
|
|
43
43
|
db;
|
|
44
|
+
_persistErrorHandler = null;
|
|
44
45
|
constructor(options = {}) {
|
|
45
46
|
if (Orm.instance)
|
|
46
47
|
return Orm.instance;
|
|
@@ -53,6 +54,10 @@ export default class Orm {
|
|
|
53
54
|
Orm.instance = this;
|
|
54
55
|
}
|
|
55
56
|
async init() {
|
|
57
|
+
// Self-register so log.db works even when @stonyx/orm is in the
|
|
58
|
+
// consumer's `dependencies` (stonyx loader only merges devDependencies).
|
|
59
|
+
const { logColor = 'white', logMethod = 'db' } = config.orm;
|
|
60
|
+
log.defineType(logMethod, logColor);
|
|
56
61
|
const { paths, restServer } = config.orm;
|
|
57
62
|
const promises = ['Model', 'Serializer', 'Transform'].map(type => {
|
|
58
63
|
const lowerCaseType = type.toLowerCase();
|
|
@@ -114,6 +119,12 @@ export default class Orm {
|
|
|
114
119
|
this.db = this.sqlDb;
|
|
115
120
|
promises.push(this.sqlDb.init());
|
|
116
121
|
}
|
|
122
|
+
else if (config.orm.dynamodb) {
|
|
123
|
+
const { default: DynamoDBDB } = await import('./dynamodb/dynamodb-db.js');
|
|
124
|
+
this.sqlDb = new DynamoDBDB();
|
|
125
|
+
this.db = this.sqlDb;
|
|
126
|
+
promises.push(this.sqlDb.init());
|
|
127
|
+
}
|
|
117
128
|
else if (this.options.dbType !== 'none') {
|
|
118
129
|
const db = new DB();
|
|
119
130
|
this.db = db;
|
|
@@ -168,6 +179,31 @@ export default class Orm {
|
|
|
168
179
|
const modelClassPrefix = kebabCaseToPascalCase(modelName);
|
|
169
180
|
return !!this.views[`${modelClassPrefix}View`];
|
|
170
181
|
}
|
|
182
|
+
/**
|
|
183
|
+
* Register a callback to be invoked when a fire-and-forget SQL persist fails.
|
|
184
|
+
* Without a handler, persist errors are logged via log.error (backwards-compatible).
|
|
185
|
+
*/
|
|
186
|
+
onPersistError(handler) {
|
|
187
|
+
this._persistErrorHandler = handler;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Emit a persist error to the registered handler, or fall back to log.error.
|
|
191
|
+
*/
|
|
192
|
+
emitPersistError(detail) {
|
|
193
|
+
const fallbackLog = () => log.error?.(`[ORM] Failed to persist ${detail.operation} for ${detail.modelName}:${String(detail.recordId)}: ${detail.error.message}`);
|
|
194
|
+
if (this._persistErrorHandler) {
|
|
195
|
+
try {
|
|
196
|
+
this._persistErrorHandler(detail);
|
|
197
|
+
}
|
|
198
|
+
catch (handlerError) {
|
|
199
|
+
fallbackLog();
|
|
200
|
+
log.error?.(`[ORM] onPersistError handler threw: ${handlerError instanceof Error ? handlerError.message : String(handlerError)}`);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
fallbackLog();
|
|
205
|
+
}
|
|
206
|
+
}
|
|
171
207
|
// Queue warnings to avoid the same error from being logged in the same iteration
|
|
172
208
|
warn(message) {
|
|
173
209
|
this.warnings.add(message);
|
package/dist/manage-record.js
CHANGED
|
@@ -2,12 +2,12 @@ import Orm, { store } from '@stonyx/orm';
|
|
|
2
2
|
import OrmRecord from './record.js';
|
|
3
3
|
import { getGlobalRegistry, getPendingRegistry, getPendingBelongsToRegistry, getBelongsToRegistry, getHasManyRegistry } from './relationships.js';
|
|
4
4
|
import { isOrmRecord } from './utils.js';
|
|
5
|
-
import log from 'stonyx/log';
|
|
6
5
|
const defaultOptions = {
|
|
7
6
|
isDbRecord: false,
|
|
8
7
|
serialize: true,
|
|
9
8
|
transform: true
|
|
10
9
|
};
|
|
10
|
+
let pendingIdCounter = 0;
|
|
11
11
|
export function createRecord(modelName, rawData = {}, userOptions = {}) {
|
|
12
12
|
const orm = Orm.instance;
|
|
13
13
|
const { initialized } = Orm;
|
|
@@ -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
|
|
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 =
|
|
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,9 +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)
|
|
88
|
-
|
|
111
|
+
orm.sqlDb.persist('create', modelName, { rawData }, response)
|
|
112
|
+
.catch((err) => {
|
|
113
|
+
orm.emitPersistError({
|
|
114
|
+
operation: 'create',
|
|
115
|
+
modelName,
|
|
116
|
+
recordId: record.id,
|
|
117
|
+
error: err instanceof Error ? err : new Error(String(err)),
|
|
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
|
+
}
|
|
89
125
|
});
|
|
90
126
|
}
|
|
91
127
|
return record;
|
|
@@ -107,7 +143,12 @@ export function updateRecord(record, rawData, userOptions = {}) {
|
|
|
107
143
|
const shouldPersist = orm?.sqlDb && !options.isDbRecord && !userOptions._relationshipKey && !options._skipAutoPersist;
|
|
108
144
|
if (shouldPersist && modelName) {
|
|
109
145
|
orm.sqlDb.persist('update', modelName, { record, oldState }, {}).catch((err) => {
|
|
110
|
-
|
|
146
|
+
orm.emitPersistError({
|
|
147
|
+
operation: 'update',
|
|
148
|
+
modelName,
|
|
149
|
+
recordId: record.id,
|
|
150
|
+
error: err instanceof Error ? err : new Error(String(err)),
|
|
151
|
+
});
|
|
111
152
|
});
|
|
112
153
|
}
|
|
113
154
|
}
|
|
@@ -120,9 +161,11 @@ export function updateRecord(record, rawData, userOptions = {}) {
|
|
|
120
161
|
function assignRecordId(modelName, rawData) {
|
|
121
162
|
if (rawData.id)
|
|
122
163
|
return;
|
|
123
|
-
// In SQL mode with numeric IDs, defer to database auto-increment
|
|
164
|
+
// In SQL mode with numeric IDs, defer to database auto-increment.
|
|
165
|
+
// Use unique negative integers — they survive the number transform (parseInt preserves negatives)
|
|
166
|
+
// and avoid NaN store-key collisions that string pending IDs caused.
|
|
124
167
|
if (Orm.instance?.sqlDb && !isStringIdModel(modelName)) {
|
|
125
|
-
rawData.id =
|
|
168
|
+
rawData.id = -(++pendingIdCounter);
|
|
126
169
|
rawData.__pendingSqlId = true;
|
|
127
170
|
return;
|
|
128
171
|
}
|
package/dist/mysql/mysql-db.d.ts
CHANGED
package/dist/mysql/mysql-db.js
CHANGED
|
@@ -321,8 +321,10 @@ export default class MysqlDB {
|
|
|
321
321
|
if (!record)
|
|
322
322
|
return;
|
|
323
323
|
const insertData = this._recordToRow(record, schema);
|
|
324
|
-
// For auto-increment models, remove the pending ID
|
|
325
|
-
|
|
324
|
+
// For auto-increment models, remove the pending ID.
|
|
325
|
+
// Check context.rawData (not record.__data) because __pendingSqlId is not a model
|
|
326
|
+
// attribute and gets lost during serialization.
|
|
327
|
+
const isPendingId = context.rawData?.__pendingSqlId === true;
|
|
326
328
|
if (isPendingId) {
|
|
327
329
|
delete insertData.id;
|
|
328
330
|
}
|
|
@@ -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, ...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) {
|
|
@@ -376,8 +376,10 @@ export default class PostgresDB {
|
|
|
376
376
|
if (!record)
|
|
377
377
|
return;
|
|
378
378
|
const insertData = this._recordToRow(record, schema, context.rawData);
|
|
379
|
-
// For auto-increment models, remove the pending ID
|
|
380
|
-
|
|
379
|
+
// For auto-increment models, remove the pending ID.
|
|
380
|
+
// Check context.rawData (not record.__data) because __pendingSqlId is not a model
|
|
381
|
+
// attribute and gets lost during serialization.
|
|
382
|
+
const isPendingId = context.rawData?.__pendingSqlId === true;
|
|
381
383
|
if (isPendingId) {
|
|
382
384
|
delete insertData.id;
|
|
383
385
|
}
|
package/dist/relationships.js
CHANGED
|
@@ -38,4 +38,4 @@ export function getPendingRegistry() {
|
|
|
38
38
|
export function getPendingBelongsToRegistry() {
|
|
39
39
|
return relationships.get('pendingBelongsTo');
|
|
40
40
|
}
|
|
41
|
-
export const TYPES = ['global', 'hasMany', 'belongsTo', 'pending'];
|
|
41
|
+
export const TYPES = ['global', 'hasMany', 'belongsTo', 'pending', 'pendingBelongsTo'];
|
package/dist/serializer.js
CHANGED
|
@@ -79,8 +79,44 @@ export default class Serializer {
|
|
|
79
79
|
// Pass relationship key name to handler for pending fulfillment
|
|
80
80
|
const handlerOptions = { ...options, _relationshipKey: key };
|
|
81
81
|
const childRecord = handler(record, data, handlerOptions);
|
|
82
|
-
|
|
83
|
-
|
|
82
|
+
// hasMany relationships use a getter so format()/toJSON() always read
|
|
83
|
+
// the live registry array instead of a stale snapshot captured at
|
|
84
|
+
// serialization time. This is critical when child records are created
|
|
85
|
+
// in a later async frame — the belongsTo inverse wiring pushes into
|
|
86
|
+
// the shared registry array, and the getter ensures the parent sees it.
|
|
87
|
+
const isHasMany = handler.__relationshipType === 'hasMany';
|
|
88
|
+
if (isHasMany) {
|
|
89
|
+
// `childRecord` IS the shared registry array — define a getter that
|
|
90
|
+
// always dereferences through the same array reference.
|
|
91
|
+
const registryArray = childRecord;
|
|
92
|
+
Object.defineProperty(rec, key, {
|
|
93
|
+
enumerable: true,
|
|
94
|
+
configurable: true,
|
|
95
|
+
get: () => registryArray,
|
|
96
|
+
set(v) { relatedRecords[key] = v; }
|
|
97
|
+
});
|
|
98
|
+
Object.defineProperty(relatedRecords, key, {
|
|
99
|
+
enumerable: true,
|
|
100
|
+
configurable: true,
|
|
101
|
+
get: () => registryArray,
|
|
102
|
+
set(v) { Object.defineProperty(relatedRecords, key, { value: v, writable: true, enumerable: true, configurable: true }); }
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
rec[key] = childRecord;
|
|
107
|
+
relatedRecords[key] = childRecord;
|
|
108
|
+
// Preserve the raw FK value in __data when the belongsTo handler
|
|
109
|
+
// couldn't resolve the target (e.g., memory:false model not loaded).
|
|
110
|
+
// This allows adapters to read the FK from __data as a fallback
|
|
111
|
+
// when __relationships[key] is null. Only store when `data` is a
|
|
112
|
+
// truthy non-object — i.e., a raw FK string/number that the handler
|
|
113
|
+
// attempted but failed to resolve. When `data` is null/undefined
|
|
114
|
+
// (optional empty relationship) we intentionally skip to preserve
|
|
115
|
+
// the existing behavior of not populating __data for empty FKs.
|
|
116
|
+
if (childRecord === null && data && typeof data !== 'object') {
|
|
117
|
+
parsedData[key] = data;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
84
120
|
continue;
|
|
85
121
|
}
|
|
86
122
|
// Aggregate property handling — use the rawData value, not the aggregate descriptor
|
package/dist/store.d.ts
CHANGED
|
@@ -46,6 +46,16 @@ export default class Store {
|
|
|
46
46
|
private _isMemoryModel;
|
|
47
47
|
set(key: string, value: Map<number | string, unknown>): void;
|
|
48
48
|
remove(key: string, id?: number | string): void;
|
|
49
|
+
/**
|
|
50
|
+
* Evict a record from the store with full relationship registry cleanup,
|
|
51
|
+
* WITHOUT calling record.clean(). This preserves the caller's reference
|
|
52
|
+
* to the returned record (used by memory:false post-persist eviction).
|
|
53
|
+
*
|
|
54
|
+
* @param registryId - The ID used when the record's relationships were
|
|
55
|
+
* registered. For SQL models with pending IDs, this is the original
|
|
56
|
+
* negative pending ID (before the adapter re-keyed to the real DB ID).
|
|
57
|
+
*/
|
|
58
|
+
evictRecord(modelName: string, id: unknown, registryId?: unknown): void;
|
|
49
59
|
unloadRecord(model: string, id: unknown, options?: UnloadOptions): void;
|
|
50
60
|
unloadAllRecords(model: string, options?: UnloadOptions): void;
|
|
51
61
|
private _removeFromHasManyArrays;
|
package/dist/store.js
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
|
function isStoreRecord(value) {
|
|
5
5
|
return typeof value === 'object' && value !== null && '__data' in value;
|
|
@@ -115,13 +115,52 @@ export default class Store {
|
|
|
115
115
|
// Auto-persist delete to SQL
|
|
116
116
|
if (id && Orm.instance?.sqlDb) {
|
|
117
117
|
Orm.instance.sqlDb.persist('delete', key, { recordId: id }, {}).catch((err) => {
|
|
118
|
-
|
|
118
|
+
Orm.instance.emitPersistError({
|
|
119
|
+
operation: 'delete',
|
|
120
|
+
modelName: key,
|
|
121
|
+
recordId: id,
|
|
122
|
+
error: err instanceof Error ? err : new Error(String(err)),
|
|
123
|
+
});
|
|
119
124
|
});
|
|
120
125
|
}
|
|
121
126
|
if (id)
|
|
122
127
|
return this.unloadRecord(key, id);
|
|
123
128
|
this.unloadAllRecords(key);
|
|
124
129
|
}
|
|
130
|
+
/**
|
|
131
|
+
* Evict a record from the store with full relationship registry cleanup,
|
|
132
|
+
* WITHOUT calling record.clean(). This preserves the caller's reference
|
|
133
|
+
* to the returned record (used by memory:false post-persist eviction).
|
|
134
|
+
*
|
|
135
|
+
* @param registryId - The ID used when the record's relationships were
|
|
136
|
+
* registered. For SQL models with pending IDs, this is the original
|
|
137
|
+
* negative pending ID (before the adapter re-keyed to the real DB ID).
|
|
138
|
+
*/
|
|
139
|
+
evictRecord(modelName, id, registryId) {
|
|
140
|
+
const modelStore = this.data.get(modelName);
|
|
141
|
+
if (!modelStore)
|
|
142
|
+
return;
|
|
143
|
+
if (typeof id !== 'string' && typeof id !== 'number')
|
|
144
|
+
return;
|
|
145
|
+
const raw = modelStore.get(id);
|
|
146
|
+
if (!raw || !isStoreRecord(raw))
|
|
147
|
+
return;
|
|
148
|
+
const visited = new Set([`${modelName}:${id}`]);
|
|
149
|
+
// Remove from hasMany arrays and nullify belongsTo references using current ID
|
|
150
|
+
// (the adapter updates record.id, so value-based matches need the current ID)
|
|
151
|
+
this._removeFromHasManyArrays(modelName, id, visited);
|
|
152
|
+
this._nullifyBelongsToReferences(modelName, id, visited);
|
|
153
|
+
// Clean up relationship registry entries using the registry key
|
|
154
|
+
// (belongsTo/hasMany registries were keyed by the ID at registration time,
|
|
155
|
+
// which may differ from the current ID if SQL persist re-keyed the record)
|
|
156
|
+
const cleanupId = registryId ?? id;
|
|
157
|
+
this._cleanupRelationshipRegistries(modelName, cleanupId);
|
|
158
|
+
// If registryId differs from id, also clean with current id as safety net
|
|
159
|
+
if (registryId !== undefined && registryId !== id) {
|
|
160
|
+
this._cleanupRelationshipRegistries(modelName, id);
|
|
161
|
+
}
|
|
162
|
+
modelStore.delete(id);
|
|
163
|
+
}
|
|
125
164
|
unloadRecord(model, id, options = {}) {
|
|
126
165
|
const modelStore = this.data.get(model);
|
|
127
166
|
if (!modelStore) {
|
|
@@ -225,6 +264,31 @@ export default class Store {
|
|
|
225
264
|
const pendingMap = getPendingRegistry().get(modelName);
|
|
226
265
|
if (pendingMap)
|
|
227
266
|
pendingMap.delete(recordId);
|
|
267
|
+
// Clean pendingBelongsTo entries in both directions
|
|
268
|
+
const pendingBelongsToMap = getPendingBelongsToRegistry();
|
|
269
|
+
if (pendingBelongsToMap) {
|
|
270
|
+
// Direction 1: evicted record was the TARGET others were waiting for
|
|
271
|
+
const targetEntries = pendingBelongsToMap.get(modelName);
|
|
272
|
+
if (targetEntries)
|
|
273
|
+
targetEntries.delete(recordId);
|
|
274
|
+
// Direction 2: evicted record was the SOURCE with unresolved forward-references
|
|
275
|
+
for (const [, targetIdMap] of pendingBelongsToMap) {
|
|
276
|
+
for (const [targetId, entries] of targetIdMap) {
|
|
277
|
+
if (!Array.isArray(entries))
|
|
278
|
+
continue;
|
|
279
|
+
const filtered = entries.filter((e) => {
|
|
280
|
+
const entry = e;
|
|
281
|
+
return !(entry.sourceModelName === modelName && entry.relationshipId === recordId);
|
|
282
|
+
});
|
|
283
|
+
if (filtered.length === 0) {
|
|
284
|
+
targetIdMap.delete(targetId);
|
|
285
|
+
}
|
|
286
|
+
else if (filtered.length < entries.length) {
|
|
287
|
+
targetIdMap.set(targetId, filtered);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
}
|
|
228
292
|
}
|
|
229
293
|
/**
|
|
230
294
|
* Extracts hasMany and non-bidirectional belongsTo children from a record
|