@stonyx/orm 0.3.2-beta.33 → 0.3.2-beta.330
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 +377 -30
- package/config/environment.js +111 -12
- 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 +4 -2
- package/dist/index.js +3 -2
- package/dist/main.d.ts +1 -0
- package/dist/main.js +29 -1
- package/dist/manage-record.d.ts +4 -0
- package/dist/manage-record.js +95 -34
- package/dist/mysql/connection.d.ts +3 -0
- package/dist/mysql/mysql-db.d.ts +32 -2
- package/dist/mysql/mysql-db.js +239 -45
- package/dist/mysql/query-builder.d.ts +1 -0
- package/dist/mysql/query-builder.js +17 -0
- 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/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 +36 -2
- package/dist/postgres/postgres-db.js +244 -45
- package/dist/postgres/query-builder.d.ts +1 -0
- package/dist/postgres/query-builder.js +17 -0
- 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/timescale/query-builder.d.ts +1 -1
- package/dist/timescale/query-builder.js +1 -2
- package/dist/types/orm-types.d.ts +11 -0
- package/package.json +19 -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 +4 -2
- package/src/main.ts +30 -1
- package/src/manage-record.ts +112 -33
- package/src/mysql/connection.ts +3 -0
- package/src/mysql/mysql-db.ts +264 -46
- package/src/mysql/query-builder.ts +21 -0
- package/src/normalize-record-id.ts +88 -0
- package/src/orm-request.ts +109 -18
- package/src/postgres/column-rules.ts +37 -0
- package/src/postgres/connection.ts +10 -6
- package/src/postgres/postgres-db.ts +277 -49
- package/src/postgres/query-builder.ts +21 -0
- 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/timescale/query-builder.ts +1 -2
- package/src/types/orm-types.ts +12 -0
- package/src/types/stonyx.d.ts +7 -1
- package/config/environment.ts +0 -91
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DynamoDB operation parameter builders.
|
|
3
|
+
*
|
|
4
|
+
* Each function returns a plain-object "params" bag that can be passed
|
|
5
|
+
* directly to the corresponding DocumentClient command
|
|
6
|
+
* (PutCommand, GetCommand, UpdateCommand, DeleteCommand, ScanCommand, QueryCommand).
|
|
7
|
+
*
|
|
8
|
+
* All functions are pure — no SDK imports here; the caller wraps params
|
|
9
|
+
* in the appropriate Command class.
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* PutItem — optionally with a condition expression.
|
|
13
|
+
*
|
|
14
|
+
* Pass conditionExpression = 'attribute_not_exists(id)' to enforce uniqueness.
|
|
15
|
+
*/
|
|
16
|
+
export function buildPutItem(tableName, item, conditionExpression) {
|
|
17
|
+
const params = { TableName: tableName, Item: item };
|
|
18
|
+
if (conditionExpression)
|
|
19
|
+
params.ConditionExpression = conditionExpression;
|
|
20
|
+
return params;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* GetItem by primary key.
|
|
24
|
+
*/
|
|
25
|
+
export function buildGetItem(tableName, key) {
|
|
26
|
+
return { TableName: tableName, Key: key };
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* UpdateItem with a SET expression built from the `updates` object.
|
|
30
|
+
* Only the supplied attributes are updated (diff-based call site).
|
|
31
|
+
*/
|
|
32
|
+
export function buildUpdateItem(tableName, key, updates) {
|
|
33
|
+
const names = {};
|
|
34
|
+
const values = {};
|
|
35
|
+
const setClauses = [];
|
|
36
|
+
for (const [attr, val] of Object.entries(updates)) {
|
|
37
|
+
const nameAlias = `#${attr}`;
|
|
38
|
+
const valAlias = `:${attr}`;
|
|
39
|
+
names[nameAlias] = attr;
|
|
40
|
+
values[valAlias] = val;
|
|
41
|
+
setClauses.push(`${nameAlias} = ${valAlias}`);
|
|
42
|
+
}
|
|
43
|
+
return {
|
|
44
|
+
TableName: tableName,
|
|
45
|
+
Key: key,
|
|
46
|
+
UpdateExpression: `SET ${setClauses.join(', ')}`,
|
|
47
|
+
ExpressionAttributeNames: names,
|
|
48
|
+
ExpressionAttributeValues: values,
|
|
49
|
+
ReturnValues: 'NONE',
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* DeleteItem by primary key.
|
|
54
|
+
*/
|
|
55
|
+
export function buildDeleteItem(tableName, key) {
|
|
56
|
+
return { TableName: tableName, Key: key };
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* ScanCommand params.
|
|
60
|
+
* If conditions are supplied they are rendered as a FilterExpression using AND.
|
|
61
|
+
*/
|
|
62
|
+
export function buildScan(tableName, conditions, exclusiveStartKey) {
|
|
63
|
+
const params = { TableName: tableName };
|
|
64
|
+
if (exclusiveStartKey)
|
|
65
|
+
params.ExclusiveStartKey = exclusiveStartKey;
|
|
66
|
+
if (conditions && Object.keys(conditions).length > 0) {
|
|
67
|
+
const validEntries = Object.entries(conditions).filter(([, val]) => val !== undefined && val !== null);
|
|
68
|
+
if (validEntries.length > 0) {
|
|
69
|
+
const names = {};
|
|
70
|
+
const values = {};
|
|
71
|
+
const clauses = [];
|
|
72
|
+
for (const [attr, val] of validEntries) {
|
|
73
|
+
const nameAlias = `#${attr}`;
|
|
74
|
+
const valAlias = `:${attr}`;
|
|
75
|
+
names[nameAlias] = attr;
|
|
76
|
+
values[valAlias] = val;
|
|
77
|
+
clauses.push(`${nameAlias} = ${valAlias}`);
|
|
78
|
+
}
|
|
79
|
+
params.FilterExpression = clauses.join(' AND ');
|
|
80
|
+
params.ExpressionAttributeNames = names;
|
|
81
|
+
params.ExpressionAttributeValues = values;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
return params;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* QueryCommand params for a GSI.
|
|
88
|
+
* keyConditions must be in the form { attrName: value } and will be rendered
|
|
89
|
+
* as equality expressions joined by AND.
|
|
90
|
+
*/
|
|
91
|
+
export function buildQuery(tableName, indexName, keyConditions, exclusiveStartKey) {
|
|
92
|
+
const validEntries = Object.entries(keyConditions).filter(([, val]) => val !== undefined && val !== null);
|
|
93
|
+
if (validEntries.length === 0) {
|
|
94
|
+
throw new Error('buildQuery: all keyCondition values are undefined/null');
|
|
95
|
+
}
|
|
96
|
+
const names = {};
|
|
97
|
+
const values = {};
|
|
98
|
+
const clauses = [];
|
|
99
|
+
for (const [attr, val] of validEntries) {
|
|
100
|
+
const nameAlias = `#${attr}`;
|
|
101
|
+
const valAlias = `:${attr}`;
|
|
102
|
+
names[nameAlias] = attr;
|
|
103
|
+
values[valAlias] = val;
|
|
104
|
+
clauses.push(`${nameAlias} = ${valAlias}`);
|
|
105
|
+
}
|
|
106
|
+
const params = {
|
|
107
|
+
TableName: tableName,
|
|
108
|
+
IndexName: indexName,
|
|
109
|
+
KeyConditionExpression: clauses.join(' AND '),
|
|
110
|
+
ExpressionAttributeNames: names,
|
|
111
|
+
ExpressionAttributeValues: values,
|
|
112
|
+
};
|
|
113
|
+
if (exclusiveStartKey)
|
|
114
|
+
params.ExclusiveStartKey = exclusiveStartKey;
|
|
115
|
+
return params;
|
|
116
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Maps ORM attribute types to DynamoDB scalar attribute types.
|
|
3
|
+
* DynamoDB DocumentClient auto-marshalls JS objects, so most values
|
|
4
|
+
* are sent as their native JS types. This map is used by the
|
|
5
|
+
* schema-introspector and startup provisioner for table/GSI creation.
|
|
6
|
+
*/
|
|
7
|
+
export type DynamoScalarType = 'S' | 'N' | 'BOOL';
|
|
8
|
+
/**
|
|
9
|
+
* DynamoDB attribute-type string for a given ORM attr type.
|
|
10
|
+
* - string → S
|
|
11
|
+
* - number / float → N (stored as Number; DocumentClient handles it)
|
|
12
|
+
* - boolean → BOOL
|
|
13
|
+
* - date → S (ISO-8601 string — enables range queries)
|
|
14
|
+
* - timestamp → N (milliseconds since epoch)
|
|
15
|
+
* - passthrough/trim/etc → S (safe default)
|
|
16
|
+
*
|
|
17
|
+
* For key schema declarations only `S` and `N` are valid; BOOL
|
|
18
|
+
* is legal for attributes but never for a PK/SK.
|
|
19
|
+
*/
|
|
20
|
+
declare const typeMap: Record<string, DynamoScalarType>;
|
|
21
|
+
/**
|
|
22
|
+
* Returns the DynamoDB attribute type for a given ORM type string.
|
|
23
|
+
* Defaults to 'S' for any unknown/custom type.
|
|
24
|
+
*/
|
|
25
|
+
export declare function getDynamoType(attrType: string): DynamoScalarType;
|
|
26
|
+
/**
|
|
27
|
+
* Returns the DynamoDB key type ('S' | 'N') for use in KeySchema.
|
|
28
|
+
* BOOL cannot be a key attribute; anything that maps to BOOL falls back to 'S'.
|
|
29
|
+
*/
|
|
30
|
+
export declare function getDynamoKeyType(attrType: string): 'S' | 'N';
|
|
31
|
+
export default typeMap;
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Maps ORM attribute types to DynamoDB scalar attribute types.
|
|
3
|
+
* DynamoDB DocumentClient auto-marshalls JS objects, so most values
|
|
4
|
+
* are sent as their native JS types. This map is used by the
|
|
5
|
+
* schema-introspector and startup provisioner for table/GSI creation.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* DynamoDB attribute-type string for a given ORM attr type.
|
|
9
|
+
* - string → S
|
|
10
|
+
* - number / float → N (stored as Number; DocumentClient handles it)
|
|
11
|
+
* - boolean → BOOL
|
|
12
|
+
* - date → S (ISO-8601 string — enables range queries)
|
|
13
|
+
* - timestamp → N (milliseconds since epoch)
|
|
14
|
+
* - passthrough/trim/etc → S (safe default)
|
|
15
|
+
*
|
|
16
|
+
* For key schema declarations only `S` and `N` are valid; BOOL
|
|
17
|
+
* is legal for attributes but never for a PK/SK.
|
|
18
|
+
*/
|
|
19
|
+
const typeMap = {
|
|
20
|
+
string: 'S',
|
|
21
|
+
number: 'N',
|
|
22
|
+
float: 'N',
|
|
23
|
+
boolean: 'BOOL',
|
|
24
|
+
date: 'S',
|
|
25
|
+
timestamp: 'N',
|
|
26
|
+
passthrough: 'S',
|
|
27
|
+
trim: 'S',
|
|
28
|
+
uppercase: 'S',
|
|
29
|
+
ceil: 'N',
|
|
30
|
+
floor: 'N',
|
|
31
|
+
round: 'N',
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Returns the DynamoDB attribute type for a given ORM type string.
|
|
35
|
+
* Defaults to 'S' for any unknown/custom type.
|
|
36
|
+
*/
|
|
37
|
+
export function getDynamoType(attrType) {
|
|
38
|
+
return typeMap[attrType] ?? 'S';
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Returns the DynamoDB key type ('S' | 'N') for use in KeySchema.
|
|
42
|
+
* BOOL cannot be a key attribute; anything that maps to BOOL falls back to 'S'.
|
|
43
|
+
*/
|
|
44
|
+
export function getDynamoKeyType(attrType) {
|
|
45
|
+
const t = getDynamoType(attrType);
|
|
46
|
+
return t === 'N' ? 'N' : 'S';
|
|
47
|
+
}
|
|
48
|
+
export default typeMap;
|
package/dist/index.d.ts
CHANGED
|
@@ -4,12 +4,14 @@ import Serializer from './serializer.js';
|
|
|
4
4
|
import attr from './attr.js';
|
|
5
5
|
import belongsTo from './belongs-to.js';
|
|
6
6
|
import hasMany from './has-many.js';
|
|
7
|
-
import { createRecord, updateRecord } from './manage-record.js';
|
|
7
|
+
import { createRecord, updateRecord, createRecordAndPersist, updateRecordAndPersist } 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
11
|
export type { PersistErrorDetail } from './main.js';
|
|
12
12
|
export { Model, View, Serializer };
|
|
13
|
-
export { attr, belongsTo, hasMany, createRecord, updateRecord };
|
|
13
|
+
export { attr, belongsTo, hasMany, createRecord, updateRecord, createRecordAndPersist, updateRecordAndPersist };
|
|
14
14
|
export { count, avg, sum, min, max };
|
|
15
|
+
export { default as normalizeRecordId } from './normalize-record-id.js';
|
|
16
|
+
export type { OrmRequest$ as OrmAccessRequest } from './orm-request.js';
|
|
15
17
|
export { beforeHook, afterHook, clearHook, clearAllHooks } from './hooks.js';
|
package/dist/index.js
CHANGED
|
@@ -19,13 +19,14 @@ import Serializer from './serializer.js';
|
|
|
19
19
|
import attr from './attr.js';
|
|
20
20
|
import belongsTo from './belongs-to.js';
|
|
21
21
|
import hasMany from './has-many.js';
|
|
22
|
-
import { createRecord, updateRecord } from './manage-record.js';
|
|
22
|
+
import { createRecord, updateRecord, createRecordAndPersist, updateRecordAndPersist } from './manage-record.js';
|
|
23
23
|
import { count, avg, sum, min, max } from './aggregates.js';
|
|
24
24
|
export { default } from './main.js';
|
|
25
25
|
export { store, relationships } from './main.js';
|
|
26
26
|
export { Model, View, Serializer }; // base classes
|
|
27
|
-
export { attr, belongsTo, hasMany, createRecord, updateRecord };
|
|
27
|
+
export { attr, belongsTo, hasMany, createRecord, updateRecord, createRecordAndPersist, updateRecordAndPersist };
|
|
28
28
|
export { count, avg, sum, min, max }; // aggregate helpers
|
|
29
|
+
export { default as normalizeRecordId } from './normalize-record-id.js'; // canonical URL-id -> record-id resolution (#270)
|
|
29
30
|
export { beforeHook, afterHook, clearHook, clearAllHooks } from './hooks.js'; // middleware hooks
|
|
30
31
|
// Store API:
|
|
31
32
|
// store.get(model, id) -- sync, memory-only
|
package/dist/main.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ export interface SqlDb {
|
|
|
7
7
|
startup(): Promise<void>;
|
|
8
8
|
shutdown(): Promise<void>;
|
|
9
9
|
persist(operation: string, model: string, context: unknown, response: unknown): Promise<void>;
|
|
10
|
+
persistResolvesAfterWrite?: boolean;
|
|
10
11
|
findRecord(modelName: string, id: unknown): Promise<unknown>;
|
|
11
12
|
findAll(modelName: string, conditions?: Record<string, unknown>): Promise<unknown[]>;
|
|
12
13
|
}
|
package/dist/main.js
CHANGED
|
@@ -19,7 +19,6 @@ import log from 'stonyx/log';
|
|
|
19
19
|
import { forEachFileImport } from '@stonyx/utils/file';
|
|
20
20
|
import { kebabCaseToPascalCase, pluralize } from '@stonyx/utils/string';
|
|
21
21
|
import { registerPluralName } from './plural-registry.js';
|
|
22
|
-
import setupRestServer from './setup-rest-server.js';
|
|
23
22
|
import baseTransforms from './transforms.js';
|
|
24
23
|
import Store from './store.js';
|
|
25
24
|
import Serializer from './serializer.js';
|
|
@@ -54,6 +53,10 @@ export default class Orm {
|
|
|
54
53
|
Orm.instance = this;
|
|
55
54
|
}
|
|
56
55
|
async init() {
|
|
56
|
+
// Self-register so log.db works even when @stonyx/orm is in the
|
|
57
|
+
// consumer's `dependencies` (stonyx loader only merges devDependencies).
|
|
58
|
+
const { logColor = 'white', logMethod = 'db' } = config.orm;
|
|
59
|
+
log.defineType(logMethod, logColor);
|
|
57
60
|
const { paths, restServer } = config.orm;
|
|
58
61
|
const promises = ['Model', 'Serializer', 'Transform'].map(type => {
|
|
59
62
|
const lowerCaseType = type.toLowerCase();
|
|
@@ -115,12 +118,37 @@ export default class Orm {
|
|
|
115
118
|
this.db = this.sqlDb;
|
|
116
119
|
promises.push(this.sqlDb.init());
|
|
117
120
|
}
|
|
121
|
+
else if (config.orm.dynamodb) {
|
|
122
|
+
const { default: DynamoDBDB } = await import('./dynamodb/dynamodb-db.js');
|
|
123
|
+
this.sqlDb = new DynamoDBDB();
|
|
124
|
+
this.db = this.sqlDb;
|
|
125
|
+
promises.push(this.sqlDb.init());
|
|
126
|
+
}
|
|
118
127
|
else if (this.options.dbType !== 'none') {
|
|
119
128
|
const db = new DB();
|
|
120
129
|
this.db = db;
|
|
121
130
|
promises.push(db.init());
|
|
122
131
|
}
|
|
123
132
|
if (restServer.enabled === 'true') {
|
|
133
|
+
// MUST stay dynamic. setup-rest-server.js names the optional
|
|
134
|
+
// '@stonyx/rest-server' peer in its own static graph — directly, and
|
|
135
|
+
// through orm-request.ts / meta-request.ts, which import `Request` at
|
|
136
|
+
// module scope because they extend it (correctly: an `extends` base
|
|
137
|
+
// class cannot be awaited). Node links a module's entire static graph
|
|
138
|
+
// before evaluating any of it, so a static import here puts that
|
|
139
|
+
// specifier on the entry graph and `import('@stonyx/orm')` throws
|
|
140
|
+
// ERR_MODULE_NOT_FOUND for an ORM-only consumer that never installed the
|
|
141
|
+
// optional peer.
|
|
142
|
+
//
|
|
143
|
+
// NOT the same reason the SQL/DynamoDB drivers above are lazy: those
|
|
144
|
+
// modules carry no static peer specifier that survives to `dist/`
|
|
145
|
+
// (postgres-db.ts:15 and mysql-db.ts:17 are `import type`, erased by
|
|
146
|
+
// tsc), so the `await import()` there is not what isolates pg / mysql2 /
|
|
147
|
+
// @aws-sdk — that happens one layer down, in src/*/connection.ts (and
|
|
148
|
+
// src/dynamodb/dynamodb-db.ts).
|
|
149
|
+
// setup-rest-server.js is the only dist module whose laziness is
|
|
150
|
+
// load-bearing for peer resolution. (#280)
|
|
151
|
+
const { default: setupRestServer } = await import('./setup-rest-server.js');
|
|
124
152
|
promises.push(setupRestServer(restServer.route, paths.access, restServer.metaRoute));
|
|
125
153
|
}
|
|
126
154
|
// Wire up memory resolver so store.find() can check model memory flags
|
package/dist/manage-record.d.ts
CHANGED
|
@@ -11,4 +11,8 @@ export declare function createRecord(modelName: string, rawData?: {
|
|
|
11
11
|
[key: string]: unknown;
|
|
12
12
|
}, userOptions?: CreateRecordOptions): OrmRecord;
|
|
13
13
|
export declare function updateRecord(record: OrmRecord, rawData: unknown, userOptions?: CreateRecordOptions): void;
|
|
14
|
+
export declare function createRecordAndPersist(modelName: string, rawData?: {
|
|
15
|
+
[key: string]: unknown;
|
|
16
|
+
}, userOptions?: CreateRecordOptions): Promise<OrmRecord>;
|
|
17
|
+
export declare function updateRecordAndPersist(record: OrmRecord, rawData: unknown, userOptions?: CreateRecordOptions): Promise<void>;
|
|
14
18
|
export {};
|
package/dist/manage-record.js
CHANGED
|
@@ -8,7 +8,57 @@ const defaultOptions = {
|
|
|
8
8
|
transform: true
|
|
9
9
|
};
|
|
10
10
|
let pendingIdCounter = 0;
|
|
11
|
+
const writeNotRequested = Promise.resolve();
|
|
12
|
+
function ignoreReportedFailure() { }
|
|
13
|
+
function persistRequested(options, userOptions) {
|
|
14
|
+
return !options.isDbRecord && !userOptions._relationshipKey && !options._skipAutoPersist;
|
|
15
|
+
}
|
|
16
|
+
function reportPersistFailure(operation, modelName, recordId, failure) {
|
|
17
|
+
const error = failure instanceof Error ? failure : new Error(String(failure));
|
|
18
|
+
Orm.instance.emitPersistError({ operation, modelName, recordId, error });
|
|
19
|
+
return error;
|
|
20
|
+
}
|
|
11
21
|
export function createRecord(modelName, rawData = {}, userOptions = {}) {
|
|
22
|
+
const { result: record, written } = createRecordWrite(modelName, rawData, userOptions);
|
|
23
|
+
written.catch(ignoreReportedFailure);
|
|
24
|
+
return record;
|
|
25
|
+
}
|
|
26
|
+
export function updateRecord(record, rawData, userOptions = {}) {
|
|
27
|
+
updateRecordWrite(record, rawData, userOptions).written.catch(ignoreReportedFailure);
|
|
28
|
+
}
|
|
29
|
+
export async function createRecordAndPersist(modelName, rawData = {}, userOptions = {}) {
|
|
30
|
+
assertWriteCanBeAwaited();
|
|
31
|
+
const { result: record, written } = createRecordWrite(modelName, rawData, userOptions);
|
|
32
|
+
await written;
|
|
33
|
+
await saveJsonDatabase('create', modelName, record.id, { ...defaultOptions, ...userOptions }, userOptions);
|
|
34
|
+
return record;
|
|
35
|
+
}
|
|
36
|
+
export async function updateRecordAndPersist(record, rawData, userOptions = {}) {
|
|
37
|
+
assertWriteCanBeAwaited();
|
|
38
|
+
await updateRecordWrite(record, rawData, userOptions).written;
|
|
39
|
+
await saveJsonDatabase('update', record?.__model?.__name ?? '', record.id, { ...defaultOptions, ...userOptions }, userOptions);
|
|
40
|
+
}
|
|
41
|
+
function assertWriteCanBeAwaited() {
|
|
42
|
+
const sqlDb = Orm.instance?.sqlDb;
|
|
43
|
+
if (sqlDb && sqlDb.persistResolvesAfterWrite !== true) {
|
|
44
|
+
throw new Error('The configured database adapter cannot confirm a write has landed, so createRecordAndPersist/updateRecordAndPersist are unavailable for it (DynamoDB: abofs/stonyx-orm#299)');
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
async function saveJsonDatabase(operation, modelName, recordId, options, userOptions) {
|
|
48
|
+
const orm = Orm.instance;
|
|
49
|
+
if (orm.sqlDb || !persistRequested(options, userOptions))
|
|
50
|
+
return;
|
|
51
|
+
const jsonDatabase = orm.db;
|
|
52
|
+
if (!jsonDatabase?.save)
|
|
53
|
+
return;
|
|
54
|
+
try {
|
|
55
|
+
await jsonDatabase.save();
|
|
56
|
+
}
|
|
57
|
+
catch (failure) {
|
|
58
|
+
throw reportPersistFailure(operation, modelName, recordId, failure);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function createRecordWrite(modelName, rawData, userOptions) {
|
|
12
62
|
const orm = Orm.instance;
|
|
13
63
|
const { initialized } = Orm;
|
|
14
64
|
const options = { ...defaultOptions, ...userOptions };
|
|
@@ -26,9 +76,7 @@ export function createRecord(modelName, rawData = {}, userOptions = {}) {
|
|
|
26
76
|
assignRecordId(modelName, rawData);
|
|
27
77
|
const existingRecord = modelStore.get(rawData.id);
|
|
28
78
|
if (existingRecord instanceof OrmRecord) {
|
|
29
|
-
|
|
30
|
-
updateRecord(existingRecord, rawData, { ...options, update: true });
|
|
31
|
-
return existingRecord;
|
|
79
|
+
return { result: existingRecord, written: updateRecordWrite(existingRecord, rawData, { ...options, update: true }).written };
|
|
32
80
|
}
|
|
33
81
|
const recordClasses = orm.getRecordClasses(modelName);
|
|
34
82
|
const modelClass = recordClasses.modelClass;
|
|
@@ -52,13 +100,34 @@ export function createRecord(modelName, rawData = {}, userOptions = {}) {
|
|
|
52
100
|
relationship.push(record);
|
|
53
101
|
pendingHasMany.splice(0);
|
|
54
102
|
}
|
|
103
|
+
// FK-based inverse hasMany wiring — when a child record is created with a
|
|
104
|
+
// foreign-key field (e.g. `owner: 'owner-1'` on an animal), find any parent
|
|
105
|
+
// whose hasMany registry targets this model and push the child into the
|
|
106
|
+
// parent's shared array. This covers edge cases where the child is created
|
|
107
|
+
// in a separate async frame without a belongsTo handler firing.
|
|
108
|
+
const hasManyReg = getHasManyRegistry();
|
|
109
|
+
if (hasManyReg) {
|
|
110
|
+
for (const [parentModelName, targetMap] of hasManyReg) {
|
|
111
|
+
const childArrayMap = targetMap.get(modelName);
|
|
112
|
+
if (!childArrayMap)
|
|
113
|
+
continue;
|
|
114
|
+
// Check if rawData contains a FK field matching the parent model name
|
|
115
|
+
const fkValue = rawData[parentModelName];
|
|
116
|
+
if (fkValue === undefined || fkValue === null)
|
|
117
|
+
continue;
|
|
118
|
+
const parentArray = childArrayMap.get(fkValue);
|
|
119
|
+
if (parentArray && !parentArray.includes(record)) {
|
|
120
|
+
parentArray.push(record);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
55
124
|
// Fulfill pending belongsTo relationships
|
|
56
125
|
const pendingBelongsToQueue = getPendingBelongsToRegistry();
|
|
57
126
|
const pendingBelongsToRaw = pendingBelongsToQueue.get(modelName)?.get(record.id);
|
|
58
127
|
const pendingBelongsTo = Array.isArray(pendingBelongsToRaw) ? pendingBelongsToRaw : undefined;
|
|
59
128
|
if (pendingBelongsTo) {
|
|
60
129
|
const belongsToReg = getBelongsToRegistry();
|
|
61
|
-
const
|
|
130
|
+
const pendingHasManyReg = getHasManyRegistry();
|
|
62
131
|
for (const { sourceRecord, sourceModelName, relationshipKey, relationshipId } of pendingBelongsTo) {
|
|
63
132
|
// Update the belongsTo relationship on the source record
|
|
64
133
|
sourceRecord.__relationships[relationshipKey] = record;
|
|
@@ -72,7 +141,7 @@ export function createRecord(modelName, rawData = {}, userOptions = {}) {
|
|
|
72
141
|
}
|
|
73
142
|
}
|
|
74
143
|
// Wire inverse hasMany if it exists
|
|
75
|
-
const inverseHasMany =
|
|
144
|
+
const inverseHasMany = pendingHasManyReg.get(modelName)?.get(sourceModelName)?.get(record.id);
|
|
76
145
|
if (inverseHasMany && !inverseHasMany.includes(sourceRecord)) {
|
|
77
146
|
inverseHasMany.push(sourceRecord);
|
|
78
147
|
}
|
|
@@ -80,46 +149,38 @@ export function createRecord(modelName, rawData = {}, userOptions = {}) {
|
|
|
80
149
|
// Clear the pending queue
|
|
81
150
|
pendingBelongsTo.length = 0;
|
|
82
151
|
}
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
return record;
|
|
152
|
+
if (!orm?.sqlDb || !persistRequested(options, userOptions))
|
|
153
|
+
return { result: record, written: writeNotRequested };
|
|
154
|
+
const registryIdBeforePersist = record.id;
|
|
155
|
+
const response = { data: { id: record.id } };
|
|
156
|
+
const written = orm.sqlDb.persist('create', modelName, { rawData }, response)
|
|
157
|
+
.catch((failure) => {
|
|
158
|
+
throw reportPersistFailure('create', modelName, record.id, failure);
|
|
159
|
+
})
|
|
160
|
+
.finally(() => {
|
|
161
|
+
if (store._memoryResolver && !store._memoryResolver(modelName)) {
|
|
162
|
+
store.evictRecord(modelName, record.id, registryIdBeforePersist);
|
|
163
|
+
}
|
|
164
|
+
});
|
|
165
|
+
return { result: record, written };
|
|
97
166
|
}
|
|
98
|
-
|
|
167
|
+
function updateRecordWrite(record, rawData, userOptions) {
|
|
99
168
|
if (!rawData)
|
|
100
169
|
throw new Error('rawData must be passed in to updateRecord call');
|
|
101
|
-
// Guard: read-only views cannot be updated
|
|
102
170
|
const modelName = record?.__model?.__name;
|
|
103
171
|
if (modelName && Orm.instance?.isView?.(modelName)) {
|
|
104
172
|
throw new Error(`Cannot update records for read-only view '${modelName}'`);
|
|
105
173
|
}
|
|
106
174
|
const options = { ...defaultOptions, ...userOptions, update: true };
|
|
107
|
-
// Capture old state before update for SQL diff
|
|
108
175
|
const oldState = record.__data ? JSON.parse(JSON.stringify(record.__data)) : {};
|
|
109
176
|
record.serialize(rawData, options);
|
|
110
|
-
// Auto-persist to SQL — skip for DB loads (isDbRecord) and relationship resolution (_relationshipKey)
|
|
111
177
|
const orm = Orm.instance;
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
recordId: record.id,
|
|
119
|
-
error: err instanceof Error ? err : new Error(String(err)),
|
|
120
|
-
});
|
|
121
|
-
});
|
|
122
|
-
}
|
|
178
|
+
if (!orm?.sqlDb || !modelName || !persistRequested(options, userOptions))
|
|
179
|
+
return { result: undefined, written: writeNotRequested };
|
|
180
|
+
const written = orm.sqlDb.persist('update', modelName, { record, oldState }, {}).catch((failure) => {
|
|
181
|
+
throw reportPersistFailure('update', modelName, record.id, failure);
|
|
182
|
+
});
|
|
183
|
+
return { result: undefined, written };
|
|
123
184
|
}
|
|
124
185
|
/**
|
|
125
186
|
* gets the next available id based on last record entry.
|
|
@@ -8,6 +8,9 @@ interface MysqlConfig {
|
|
|
8
8
|
connectionLimit?: number;
|
|
9
9
|
migrationsTable?: string;
|
|
10
10
|
migrationsDir?: string;
|
|
11
|
+
autoMigrate?: boolean;
|
|
12
|
+
writeBatchSize?: number;
|
|
13
|
+
writeQueueLimit?: number;
|
|
11
14
|
}
|
|
12
15
|
export declare function getPool(mysqlConfig: MysqlConfig): Promise<Pool>;
|
|
13
16
|
export declare function closePool(): Promise<void>;
|
package/dist/mysql/mysql-db.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import type { MysqlConfig } from './connection.js';
|
|
|
3
3
|
import { ensureMigrationsTable, getAppliedMigrations, getMigrationFiles, applyMigration, parseMigrationFile } from './migration-runner.js';
|
|
4
4
|
import { introspectModels, introspectViews, getTopologicalOrder, schemasToSnapshot } from './schema-introspector.js';
|
|
5
5
|
import { loadLatestSnapshot, detectSchemaDrift } from './migration-generator.js';
|
|
6
|
-
import { buildInsert, buildUpdate, buildDelete, buildSelect } from './query-builder.js';
|
|
6
|
+
import { buildInsert, buildInsertMany, buildUpdate, buildDelete, buildSelect } from './query-builder.js';
|
|
7
7
|
import { createRecord } from '../manage-record.js';
|
|
8
8
|
import { confirm } from '@stonyx/utils/prompt';
|
|
9
9
|
import { readFile } from '@stonyx/utils/file';
|
|
@@ -23,6 +23,13 @@ interface PersistResponse {
|
|
|
23
23
|
id?: unknown;
|
|
24
24
|
};
|
|
25
25
|
}
|
|
26
|
+
type ExitEmitter = Pick<NodeJS.Process, 'once' | 'removeListener'>;
|
|
27
|
+
export declare class WriteQueueFullError extends Error {
|
|
28
|
+
constructor(limit: number);
|
|
29
|
+
}
|
|
30
|
+
export declare class WriteQueueClosedError extends Error {
|
|
31
|
+
constructor();
|
|
32
|
+
}
|
|
26
33
|
interface OrmStore {
|
|
27
34
|
get(key: string): Map<number | string, unknown> | undefined;
|
|
28
35
|
get(key: string, id: number | string): unknown;
|
|
@@ -44,6 +51,7 @@ interface MysqlDBDeps {
|
|
|
44
51
|
loadLatestSnapshot: typeof loadLatestSnapshot;
|
|
45
52
|
detectSchemaDrift: typeof detectSchemaDrift;
|
|
46
53
|
buildInsert: typeof buildInsert;
|
|
54
|
+
buildInsertMany: typeof buildInsertMany;
|
|
47
55
|
buildUpdate: typeof buildUpdate;
|
|
48
56
|
buildDelete: typeof buildDelete;
|
|
49
57
|
buildSelect: typeof buildSelect;
|
|
@@ -55,17 +63,30 @@ interface MysqlDBDeps {
|
|
|
55
63
|
config: typeof config;
|
|
56
64
|
log: Record<string, ((...args: unknown[]) => void) | undefined>;
|
|
57
65
|
path: typeof path;
|
|
66
|
+
process: ExitEmitter;
|
|
58
67
|
}
|
|
59
68
|
export default class MysqlDB {
|
|
60
69
|
static instance: MysqlDB | undefined;
|
|
61
70
|
deps: MysqlDBDeps;
|
|
62
71
|
pool: Pool | null;
|
|
63
72
|
mysqlConfig: MysqlConfig;
|
|
73
|
+
readonly persistResolvesAfterWrite = true;
|
|
74
|
+
private _queuedWrites;
|
|
75
|
+
private _inFlightWrites;
|
|
76
|
+
private _drainingWrites;
|
|
77
|
+
private _acceptingWrites;
|
|
78
|
+
private _exitReport;
|
|
64
79
|
constructor(deps?: Partial<MysqlDBDeps>);
|
|
65
80
|
private requirePool;
|
|
66
81
|
init(): Promise<void>;
|
|
82
|
+
get writeBatchSize(): number;
|
|
83
|
+
get writeQueueLimit(): number;
|
|
84
|
+
get pendingWriteCount(): number;
|
|
67
85
|
startup(): Promise<void>;
|
|
68
86
|
shutdown(): Promise<void>;
|
|
87
|
+
private _registerExitReport;
|
|
88
|
+
private _unregisterExitReport;
|
|
89
|
+
private _reportUnpersistedWrites;
|
|
69
90
|
save(): Promise<void>;
|
|
70
91
|
/**
|
|
71
92
|
* Loads only models with memory: true into the in-memory store on startup.
|
|
@@ -93,7 +114,16 @@ export default class MysqlDB {
|
|
|
93
114
|
private _evictIfNotMemory;
|
|
94
115
|
private _rowToRawData;
|
|
95
116
|
persist(operation: string, modelName: string, context: PersistContext, response: PersistResponse): Promise<void>;
|
|
96
|
-
private
|
|
117
|
+
private _drainWritesInBackground;
|
|
118
|
+
private _drainQueuedWrites;
|
|
119
|
+
private _takeNextWriteGroup;
|
|
120
|
+
private _executeWriteGroup;
|
|
121
|
+
private _persistCreates;
|
|
122
|
+
private _prepareCreate;
|
|
123
|
+
private _chunkForSingleInsert;
|
|
124
|
+
private _insertCreates;
|
|
125
|
+
private _insertCreatesOneByOne;
|
|
126
|
+
private _adoptGeneratedId;
|
|
97
127
|
private _persistUpdate;
|
|
98
128
|
private _persistDelete;
|
|
99
129
|
private _recordToRow;
|