@stonyx/orm 0.3.2-beta.65 → 0.3.2-beta.67
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 +28 -0
- package/dist/dynamodb/connection.js +28 -0
- package/dist/dynamodb/dynamodb-db.d.ts +131 -0
- package/dist/dynamodb/dynamodb-db.js +556 -0
- package/dist/dynamodb/operation-builder.d.ts +76 -0
- package/dist/dynamodb/operation-builder.js +109 -0
- package/dist/dynamodb/type-map.d.ts +31 -0
- package/dist/dynamodb/type-map.js +48 -0
- package/dist/main.js +6 -0
- package/dist/types/orm-types.d.ts +6 -0
- package/package.json +9 -1
- package/src/commands.ts +43 -0
- package/src/dynamodb/connection.ts +49 -0
- package/src/dynamodb/dynamodb-db.ts +768 -0
- package/src/dynamodb/operation-builder.ts +188 -0
- package/src/dynamodb/type-map.ts +54 -0
- package/src/main.ts +5 -0
- package/src/types/orm-types.ts +7 -0
package/README.md
CHANGED
|
@@ -13,7 +13,7 @@ A lightweight ORM for Stonyx projects, featuring model definitions, serializers,
|
|
|
13
13
|
- **Models**: Define attributes with type-safe proxies (`attr`) and relationships (`hasMany`, `belongsTo`).
|
|
14
14
|
- **Serializers**: Map raw data into model-friendly structures, including nested properties.
|
|
15
15
|
- **Transforms**: Apply custom transformations on data values automatically.
|
|
16
|
-
- **DB Integration**: Optional file-based persistence with auto-save support, or MySQL for production workloads.
|
|
16
|
+
- **DB Integration**: Optional file-based persistence with auto-save support, or MySQL/PostgreSQL/TimescaleDB/DynamoDB for production workloads.
|
|
17
17
|
- **REST Server Integration**: Automatic route setup with customizable access control.
|
|
18
18
|
- **Lifecycle Hooks**: Middleware-based before/after hooks for validation, authorization, side effects, and auditing.
|
|
19
19
|
|
|
@@ -65,13 +65,16 @@ const {
|
|
|
65
65
|
MYSQL_DATABASE,
|
|
66
66
|
MYSQL_CONNECTION_LIMIT,
|
|
67
67
|
MYSQL_MIGRATIONS_DIR,
|
|
68
|
+
DYNAMODB_REGION,
|
|
69
|
+
DYNAMODB_ENDPOINT,
|
|
70
|
+
DYNAMODB_TABLE_PREFIX,
|
|
68
71
|
} = process.env;
|
|
69
72
|
|
|
70
73
|
export default {
|
|
71
74
|
orm: {
|
|
72
75
|
logColor: 'white',
|
|
73
76
|
logMethod: 'db',
|
|
74
|
-
|
|
77
|
+
|
|
75
78
|
db: {
|
|
76
79
|
autosave: DB_AUTO_SAVE ?? 'false',
|
|
77
80
|
file: DB_FILE ?? 'db.json',
|
|
@@ -96,6 +99,11 @@ export default {
|
|
|
96
99
|
migrationsDir: MYSQL_MIGRATIONS_DIR ?? 'migrations',
|
|
97
100
|
migrationsTable: '__migrations',
|
|
98
101
|
} : undefined,
|
|
102
|
+
dynamodb: DYNAMODB_REGION ? {
|
|
103
|
+
region: DYNAMODB_REGION,
|
|
104
|
+
endpoint: DYNAMODB_ENDPOINT, // optional, for DynamoDB Local
|
|
105
|
+
tablePrefix: DYNAMODB_TABLE_PREFIX, // optional table name prefix
|
|
106
|
+
} : undefined,
|
|
99
107
|
restServer: {
|
|
100
108
|
enabled: ORM_USE_REST_SERVER ?? 'true',
|
|
101
109
|
route: ORM_REST_ROUTE ?? '/'
|
|
@@ -243,6 +251,31 @@ Set the `MYSQL_HOST` environment variable to enable MySQL persistence. The ORM l
|
|
|
243
251
|
| `stonyx db:migrate` | Apply pending migrations |
|
|
244
252
|
| `stonyx db:migrate:rollback` | Rollback the most recent migration |
|
|
245
253
|
| `stonyx db:migrate:status` | Show migration status |
|
|
254
|
+
| `stonyx db:sync` | Sync DynamoDB table definitions to match current model schemas |
|
|
255
|
+
|
|
256
|
+
### DynamoDB Mode
|
|
257
|
+
|
|
258
|
+
Set the `DYNAMODB_REGION` environment variable to enable DynamoDB persistence. Tables are created with PAY_PER_REQUEST (on-demand) billing. Global Secondary Indexes (GSIs) are auto-provisioned at startup based on model `belongsTo` relationships — each FK column gets a GSI. `findAll()` with conditions routes to a GSI Query when the condition key matches a GSI partition key; non-indexed attribute conditions fall back to Scan + FilterExpression (expensive for large tables). ULID generation replaces auto-increment for numeric-ID models.
|
|
259
|
+
|
|
260
|
+
```javascript
|
|
261
|
+
dynamodb: {
|
|
262
|
+
region: 'us-east-1',
|
|
263
|
+
endpoint: 'http://localhost:8000', // optional, for DynamoDB Local
|
|
264
|
+
tablePrefix: 'myapp-', // optional table name prefix
|
|
265
|
+
}
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
Environment variables:
|
|
269
|
+
|
|
270
|
+
* `DYNAMODB_REGION`: AWS region for DynamoDB (e.g., `'us-east-1'`).
|
|
271
|
+
* `DYNAMODB_ENDPOINT`: Optional custom endpoint URL, useful for DynamoDB Local during development.
|
|
272
|
+
* `DYNAMODB_TABLE_PREFIX`: Optional prefix prepended to all table names (e.g., `'myapp-'` yields `'myapp-animals'`).
|
|
273
|
+
|
|
274
|
+
**Peer dependencies:** `@aws-sdk/client-dynamodb` and `@aws-sdk/lib-dynamodb` must be installed when using the DynamoDB driver. The AWS SDK is dynamically imported and only loaded when the DynamoDB driver is selected.
|
|
275
|
+
|
|
276
|
+
```bash
|
|
277
|
+
npm install @aws-sdk/client-dynamodb @aws-sdk/lib-dynamodb
|
|
278
|
+
```
|
|
246
279
|
|
|
247
280
|
### Running MySQL Tests
|
|
248
281
|
|
package/config/environment.js
CHANGED
|
@@ -33,6 +33,9 @@ const {
|
|
|
33
33
|
TIMESCALE_DATABASE,
|
|
34
34
|
TIMESCALE_CONNECTION_LIMIT,
|
|
35
35
|
TIMESCALE_MIGRATIONS_DIR,
|
|
36
|
+
DYNAMODB_REGION,
|
|
37
|
+
DYNAMODB_ENDPOINT,
|
|
38
|
+
DYNAMODB_TABLE_PREFIX,
|
|
36
39
|
} = process.env;
|
|
37
40
|
|
|
38
41
|
export default {
|
|
@@ -84,6 +87,11 @@ export default {
|
|
|
84
87
|
migrationsDir: TIMESCALE_MIGRATIONS_DIR ?? 'migrations',
|
|
85
88
|
migrationsTable: '__migrations',
|
|
86
89
|
} : undefined,
|
|
90
|
+
dynamodb: DYNAMODB_REGION ? {
|
|
91
|
+
region: DYNAMODB_REGION,
|
|
92
|
+
endpoint: DYNAMODB_ENDPOINT || undefined,
|
|
93
|
+
tablePrefix: DYNAMODB_TABLE_PREFIX || '',
|
|
94
|
+
} : undefined,
|
|
87
95
|
restServer: {
|
|
88
96
|
enabled: ORM_USE_REST_SERVER ?? 'true', // Whether to load restServer for automatic route setup or
|
|
89
97
|
route: ORM_REST_ROUTE ?? '/',
|
package/dist/commands.js
CHANGED
|
@@ -20,6 +20,11 @@ const commands = {
|
|
|
20
20
|
description: 'Generate a MySQL migration from current model schemas',
|
|
21
21
|
bootstrap: true,
|
|
22
22
|
run: async (args) => {
|
|
23
|
+
const config = (await import('stonyx/config')).default;
|
|
24
|
+
if (config.orm.dynamodb) {
|
|
25
|
+
console.log('DynamoDB does not use file-based migrations. Use db:sync to provision tables.');
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
23
28
|
const description = args?.join(' ') || 'migration';
|
|
24
29
|
const { generateMigration } = await import('./mysql/migration-generator.js');
|
|
25
30
|
const result = await generateMigration(description);
|
|
@@ -31,12 +36,33 @@ const commands = {
|
|
|
31
36
|
}
|
|
32
37
|
}
|
|
33
38
|
},
|
|
39
|
+
'db:sync': {
|
|
40
|
+
description: 'Provision DynamoDB tables and GSIs from current model schemas',
|
|
41
|
+
bootstrap: true,
|
|
42
|
+
run: async () => {
|
|
43
|
+
const config = (await import('stonyx/config')).default;
|
|
44
|
+
if (!config.orm.dynamodb) {
|
|
45
|
+
console.error('DynamoDB is not configured. Set DYNAMODB_REGION (and optionally DYNAMODB_ENDPOINT) to enable DynamoDB mode.');
|
|
46
|
+
process.exit(1);
|
|
47
|
+
}
|
|
48
|
+
const { default: DynamoDBDB } = await import('./dynamodb/dynamodb-db.js');
|
|
49
|
+
const db = new DynamoDBDB();
|
|
50
|
+
await db.init();
|
|
51
|
+
await db.startup();
|
|
52
|
+
await db.shutdown();
|
|
53
|
+
console.log('DynamoDB tables synced successfully.');
|
|
54
|
+
}
|
|
55
|
+
},
|
|
34
56
|
'db:migrate': {
|
|
35
57
|
description: 'Apply pending MySQL migrations',
|
|
36
58
|
bootstrap: true,
|
|
37
59
|
run: async () => {
|
|
38
60
|
const config = (await import('stonyx/config')).default;
|
|
39
61
|
const mysqlConfig = config.orm.mysql;
|
|
62
|
+
if (config.orm.dynamodb) {
|
|
63
|
+
console.log('DynamoDB does not use file-based migrations. Use db:sync to provision tables.');
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
40
66
|
if (!mysqlConfig) {
|
|
41
67
|
console.error('MySQL is not configured. Set MYSQL_HOST to enable MySQL mode.');
|
|
42
68
|
process.exit(1);
|
|
@@ -75,6 +101,10 @@ const commands = {
|
|
|
75
101
|
bootstrap: true,
|
|
76
102
|
run: async () => {
|
|
77
103
|
const config = (await import('stonyx/config')).default;
|
|
104
|
+
if (config.orm.dynamodb) {
|
|
105
|
+
console.log('DynamoDB does not support migration rollback. Manage table changes via the AWS console or db:sync.');
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
78
108
|
const mysqlConfig = config.orm.mysql;
|
|
79
109
|
if (!mysqlConfig) {
|
|
80
110
|
console.error('MySQL is not configured. Set MYSQL_HOST to enable MySQL mode.');
|
|
@@ -113,6 +143,10 @@ const commands = {
|
|
|
113
143
|
bootstrap: true,
|
|
114
144
|
run: async () => {
|
|
115
145
|
const config = (await import('stonyx/config')).default;
|
|
146
|
+
if (config.orm.dynamodb) {
|
|
147
|
+
console.log('DynamoDB does not use file-based migrations. Use db:sync to provision tables.');
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
116
150
|
const mysqlConfig = config.orm.mysql;
|
|
117
151
|
if (!mysqlConfig) {
|
|
118
152
|
console.error('MySQL is not configured. Set MYSQL_HOST to enable MySQL mode.');
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DynamoDB connection factory.
|
|
3
|
+
*
|
|
4
|
+
* Dynamically imports @aws-sdk/client-dynamodb and @aws-sdk/lib-dynamodb
|
|
5
|
+
* so these are optional peerDependencies (matching the pg/mysql2 pattern).
|
|
6
|
+
*/
|
|
7
|
+
export interface DynamoDBConfig {
|
|
8
|
+
region?: string;
|
|
9
|
+
endpoint?: string;
|
|
10
|
+
[key: string]: unknown;
|
|
11
|
+
}
|
|
12
|
+
export type DocumentClient = {
|
|
13
|
+
send(command: unknown): Promise<unknown>;
|
|
14
|
+
};
|
|
15
|
+
export type DocumentClientConstructor = new (options: {
|
|
16
|
+
client: unknown;
|
|
17
|
+
}) => DocumentClient;
|
|
18
|
+
export type DynamoDBClientConstructor = new (options: unknown) => unknown;
|
|
19
|
+
/**
|
|
20
|
+
* Create a DynamoDBDocumentClient from the given config.
|
|
21
|
+
* Uses dynamic import so @aws-sdk/* are optional peer deps.
|
|
22
|
+
*/
|
|
23
|
+
export declare function createDocumentClient(dbConfig: DynamoDBConfig): Promise<DocumentClient>;
|
|
24
|
+
/**
|
|
25
|
+
* Nullify the document client reference (DynamoDB connections are HTTP-based
|
|
26
|
+
* and stateless — no explicit pool close needed, but we clear the reference).
|
|
27
|
+
*/
|
|
28
|
+
export declare function destroyDocumentClient(_client: DocumentClient | null): null;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DynamoDB connection factory.
|
|
3
|
+
*
|
|
4
|
+
* Dynamically imports @aws-sdk/client-dynamodb and @aws-sdk/lib-dynamodb
|
|
5
|
+
* so these are optional peerDependencies (matching the pg/mysql2 pattern).
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Create a DynamoDBDocumentClient from the given config.
|
|
9
|
+
* Uses dynamic import so @aws-sdk/* are optional peer deps.
|
|
10
|
+
*/
|
|
11
|
+
export async function createDocumentClient(dbConfig) {
|
|
12
|
+
const { DynamoDB } = await import('@aws-sdk/client-dynamodb');
|
|
13
|
+
const { DynamoDBDocument } = await import('@aws-sdk/lib-dynamodb');
|
|
14
|
+
const clientOptions = {};
|
|
15
|
+
if (dbConfig.region)
|
|
16
|
+
clientOptions.region = dbConfig.region;
|
|
17
|
+
if (dbConfig.endpoint)
|
|
18
|
+
clientOptions.endpoint = dbConfig.endpoint;
|
|
19
|
+
const rawClient = new DynamoDB(clientOptions);
|
|
20
|
+
return new DynamoDBDocument({ client: rawClient });
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Nullify the document client reference (DynamoDB connections are HTTP-based
|
|
24
|
+
* and stateless — no explicit pool close needed, but we clear the reference).
|
|
25
|
+
*/
|
|
26
|
+
export function destroyDocumentClient(_client) {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DynamoDB driver implementing the SqlDb PAL contract.
|
|
3
|
+
*
|
|
4
|
+
* Drop-in replacement for PostgresDB / MysqlDB — zero ORM core changes.
|
|
5
|
+
* Selected via config.orm.dynamodb.
|
|
6
|
+
*/
|
|
7
|
+
import { createDocumentClient, destroyDocumentClient } from './connection.js';
|
|
8
|
+
import type { DocumentClient, DynamoDBConfig } from './connection.js';
|
|
9
|
+
import { buildPutItem, buildGetItem, buildUpdateItem, buildDeleteItem, buildScan, buildQuery } from './operation-builder.js';
|
|
10
|
+
import { introspectModels, getTopologicalOrder } from '../postgres/schema-introspector.js';
|
|
11
|
+
import { getDynamoKeyType } from './type-map.js';
|
|
12
|
+
import { store } from '@stonyx/orm';
|
|
13
|
+
import { createRecord } from '../manage-record.js';
|
|
14
|
+
import { getPluralName } from '../plural-registry.js';
|
|
15
|
+
import config from 'stonyx/config';
|
|
16
|
+
import log from 'stonyx/log';
|
|
17
|
+
import type { OrmRecord } from '../types/orm-types.js';
|
|
18
|
+
/**
|
|
19
|
+
* Load the DynamoDB DocumentClient command constructors via dynamic import.
|
|
20
|
+
* Returns a frozen object so it can be cached in deps.
|
|
21
|
+
*/
|
|
22
|
+
export declare function loadDocClientCommands(): Promise<{
|
|
23
|
+
PutCommand: new (params: unknown) => unknown;
|
|
24
|
+
GetCommand: new (params: unknown) => unknown;
|
|
25
|
+
UpdateCommand: new (params: unknown) => unknown;
|
|
26
|
+
DeleteCommand: new (params: unknown) => unknown;
|
|
27
|
+
ScanCommand: new (params: unknown) => unknown;
|
|
28
|
+
QueryCommand: new (params: unknown) => unknown;
|
|
29
|
+
}>;
|
|
30
|
+
export declare function loadTableCommands(): Promise<{
|
|
31
|
+
DynamoDBClient: new (opts: unknown) => {
|
|
32
|
+
send(cmd: unknown): Promise<unknown>;
|
|
33
|
+
};
|
|
34
|
+
DescribeTableCommand: new (params: unknown) => unknown;
|
|
35
|
+
CreateTableCommand: new (params: unknown) => unknown;
|
|
36
|
+
UpdateTableCommand: new (params: unknown) => unknown;
|
|
37
|
+
}>;
|
|
38
|
+
interface PersistContext {
|
|
39
|
+
record?: OrmRecord;
|
|
40
|
+
recordId?: unknown;
|
|
41
|
+
oldState?: Record<string, unknown>;
|
|
42
|
+
rawData?: Record<string, unknown>;
|
|
43
|
+
}
|
|
44
|
+
interface PersistResponse {
|
|
45
|
+
data?: {
|
|
46
|
+
id?: unknown;
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/** Minimal Orm module shape needed at runtime — avoids circular import at top-level. */
|
|
50
|
+
interface OrmModule {
|
|
51
|
+
default: {
|
|
52
|
+
instance: {
|
|
53
|
+
getRecordClasses(name: string): {
|
|
54
|
+
modelClass: {
|
|
55
|
+
memory?: boolean;
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
isView?(name: string): boolean;
|
|
59
|
+
};
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
export interface DynamoDBDeps {
|
|
63
|
+
createDocumentClient: typeof createDocumentClient;
|
|
64
|
+
destroyDocumentClient: typeof destroyDocumentClient;
|
|
65
|
+
loadDocClientCommands: typeof loadDocClientCommands;
|
|
66
|
+
loadTableCommands: typeof loadTableCommands;
|
|
67
|
+
buildPutItem: typeof buildPutItem;
|
|
68
|
+
buildGetItem: typeof buildGetItem;
|
|
69
|
+
buildUpdateItem: typeof buildUpdateItem;
|
|
70
|
+
buildDeleteItem: typeof buildDeleteItem;
|
|
71
|
+
buildScan: typeof buildScan;
|
|
72
|
+
buildQuery: typeof buildQuery;
|
|
73
|
+
introspectModels: typeof introspectModels;
|
|
74
|
+
getTopologicalOrder: typeof getTopologicalOrder;
|
|
75
|
+
getDynamoKeyType: typeof getDynamoKeyType;
|
|
76
|
+
createRecord: typeof createRecord;
|
|
77
|
+
store: typeof store;
|
|
78
|
+
getPluralName: typeof getPluralName;
|
|
79
|
+
config: typeof config;
|
|
80
|
+
log: typeof log;
|
|
81
|
+
/** Injected for testing — import('@stonyx/orm') replacement */
|
|
82
|
+
_importOrm?: () => Promise<OrmModule>;
|
|
83
|
+
[key: string]: unknown;
|
|
84
|
+
}
|
|
85
|
+
export default class DynamoDBDB {
|
|
86
|
+
static instance: DynamoDBDB | undefined;
|
|
87
|
+
deps: DynamoDBDeps;
|
|
88
|
+
client: DocumentClient | null;
|
|
89
|
+
dbConfig: DynamoDBConfig;
|
|
90
|
+
/** GSI registry built during init from model introspection. */
|
|
91
|
+
private _gsiRegistry;
|
|
92
|
+
constructor(deps?: Partial<DynamoDBDeps>);
|
|
93
|
+
private requireClient;
|
|
94
|
+
/** Resolve Orm singleton — falls back to real import in production. */
|
|
95
|
+
private _getOrm;
|
|
96
|
+
init(): Promise<void>;
|
|
97
|
+
/**
|
|
98
|
+
* For each model, DescribeTable — CreateTable if missing (with GSIs, PAY_PER_REQUEST).
|
|
99
|
+
* For existing tables, check for missing GSIs and UpdateTable + poll for ACTIVE.
|
|
100
|
+
*/
|
|
101
|
+
startup(): Promise<void>;
|
|
102
|
+
shutdown(): Promise<void>;
|
|
103
|
+
persist(operation: string, modelName: string, context: PersistContext, response: PersistResponse): Promise<void>;
|
|
104
|
+
findRecord(modelName: string, id: unknown): Promise<OrmRecord | undefined>;
|
|
105
|
+
findAll(modelName: string, conditions?: Record<string, unknown>): Promise<OrmRecord[]>;
|
|
106
|
+
loadMemoryRecords(): Promise<void>;
|
|
107
|
+
private _persistCreate;
|
|
108
|
+
private _persistUpdate;
|
|
109
|
+
private _persistDelete;
|
|
110
|
+
private _paginatedScan;
|
|
111
|
+
private _paginatedQuery;
|
|
112
|
+
/**
|
|
113
|
+
* Build the GSI registry from model introspection.
|
|
114
|
+
* Registry: modelName → attrName → gsiName
|
|
115
|
+
*
|
|
116
|
+
* FK columns (belonging to belongsTo relationships) get a GSI automatically.
|
|
117
|
+
*/
|
|
118
|
+
private _buildGsiRegistry;
|
|
119
|
+
/**
|
|
120
|
+
* Find a GSI that can serve the given conditions.
|
|
121
|
+
*/
|
|
122
|
+
private _findGsiMatch;
|
|
123
|
+
private _buildAttributeDefinitions;
|
|
124
|
+
private _buildGsiDefinitions;
|
|
125
|
+
private _waitForTableActive;
|
|
126
|
+
private _itemToRawData;
|
|
127
|
+
private _recordToItem;
|
|
128
|
+
private _evictIfNotMemory;
|
|
129
|
+
loadAllRecords(): Promise<void>;
|
|
130
|
+
}
|
|
131
|
+
export {};
|