@stonyx/orm 0.3.2-beta.7 → 0.3.2-beta.70

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.
@@ -0,0 +1,109 @@
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 names = {};
68
+ const values = {};
69
+ const clauses = [];
70
+ for (const [attr, val] of Object.entries(conditions)) {
71
+ const nameAlias = `#${attr}`;
72
+ const valAlias = `:${attr}`;
73
+ names[nameAlias] = attr;
74
+ values[valAlias] = val;
75
+ clauses.push(`${nameAlias} = ${valAlias}`);
76
+ }
77
+ params.FilterExpression = clauses.join(' AND ');
78
+ params.ExpressionAttributeNames = names;
79
+ params.ExpressionAttributeValues = values;
80
+ }
81
+ return params;
82
+ }
83
+ /**
84
+ * QueryCommand params for a GSI.
85
+ * keyConditions must be in the form { attrName: value } and will be rendered
86
+ * as equality expressions joined by AND.
87
+ */
88
+ export function buildQuery(tableName, indexName, keyConditions, exclusiveStartKey) {
89
+ const names = {};
90
+ const values = {};
91
+ const clauses = [];
92
+ for (const [attr, val] of Object.entries(keyConditions)) {
93
+ const nameAlias = `#${attr}`;
94
+ const valAlias = `:${attr}`;
95
+ names[nameAlias] = attr;
96
+ values[valAlias] = val;
97
+ clauses.push(`${nameAlias} = ${valAlias}`);
98
+ }
99
+ const params = {
100
+ TableName: tableName,
101
+ IndexName: indexName,
102
+ KeyConditionExpression: clauses.join(' AND '),
103
+ ExpressionAttributeNames: names,
104
+ ExpressionAttributeValues: values,
105
+ };
106
+ if (exclusiveStartKey)
107
+ params.ExclusiveStartKey = exclusiveStartKey;
108
+ return params;
109
+ }
@@ -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);
@@ -2,7 +2,6 @@ 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,
@@ -53,13 +52,34 @@ export function createRecord(modelName, rawData = {}, userOptions = {}) {
53
52
  relationship.push(record);
54
53
  pendingHasMany.splice(0);
55
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
+ }
56
76
  // Fulfill pending belongsTo relationships
57
77
  const pendingBelongsToQueue = getPendingBelongsToRegistry();
58
78
  const pendingBelongsToRaw = pendingBelongsToQueue.get(modelName)?.get(record.id);
59
79
  const pendingBelongsTo = Array.isArray(pendingBelongsToRaw) ? pendingBelongsToRaw : undefined;
60
80
  if (pendingBelongsTo) {
61
81
  const belongsToReg = getBelongsToRegistry();
62
- const hasManyReg = getHasManyRegistry();
82
+ const pendingHasManyReg = getHasManyRegistry();
63
83
  for (const { sourceRecord, sourceModelName, relationshipKey, relationshipId } of pendingBelongsTo) {
64
84
  // Update the belongsTo relationship on the source record
65
85
  sourceRecord.__relationships[relationshipKey] = record;
@@ -73,7 +93,7 @@ export function createRecord(modelName, rawData = {}, userOptions = {}) {
73
93
  }
74
94
  }
75
95
  // Wire inverse hasMany if it exists
76
- const inverseHasMany = hasManyReg.get(modelName)?.get(sourceModelName)?.get(record.id);
96
+ const inverseHasMany = pendingHasManyReg.get(modelName)?.get(sourceModelName)?.get(record.id);
77
97
  if (inverseHasMany && !inverseHasMany.includes(sourceRecord)) {
78
98
  inverseHasMany.push(sourceRecord);
79
99
  }
@@ -86,7 +106,12 @@ export function createRecord(modelName, rawData = {}, userOptions = {}) {
86
106
  if (shouldPersist) {
87
107
  const response = { data: { id: record.id } };
88
108
  orm.sqlDb.persist('create', modelName, { rawData }, response).catch((err) => {
89
- log.error?.(`[ORM] Failed to persist create for ${modelName}:${String(record.id)}: ${err instanceof Error ? err.message : String(err)}`);
109
+ orm.emitPersistError({
110
+ operation: 'create',
111
+ modelName,
112
+ recordId: record.id,
113
+ error: err instanceof Error ? err : new Error(String(err)),
114
+ });
90
115
  });
91
116
  }
92
117
  return record;
@@ -108,7 +133,12 @@ export function updateRecord(record, rawData, userOptions = {}) {
108
133
  const shouldPersist = orm?.sqlDb && !options.isDbRecord && !userOptions._relationshipKey && !options._skipAutoPersist;
109
134
  if (shouldPersist && modelName) {
110
135
  orm.sqlDb.persist('update', modelName, { record, oldState }, {}).catch((err) => {
111
- log.error?.(`[ORM] Failed to persist update for ${modelName}:${String(record.id)}: ${err instanceof Error ? err.message : String(err)}`);
136
+ orm.emitPersistError({
137
+ operation: 'update',
138
+ modelName,
139
+ recordId: record.id,
140
+ error: err instanceof Error ? err : new Error(String(err)),
141
+ });
112
142
  });
113
143
  }
114
144
  }
@@ -6,6 +6,7 @@ interface PgConfig {
6
6
  password: string;
7
7
  database: string;
8
8
  connectionLimit: number;
9
+ [key: string]: unknown;
9
10
  }
10
11
  /**
11
12
  * Create or return the singleton pg Pool.
@@ -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: pgConfig.host,
12
- port: pgConfig.port,
13
- user: pgConfig.user,
14
- password: pgConfig.password,
15
- database: pgConfig.database,
16
- max: pgConfig.connectionLimit,
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) {
@@ -79,8 +79,33 @@ 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
- rec[key] = childRecord;
83
- relatedRecords[key] = childRecord;
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
+ }
84
109
  continue;
85
110
  }
86
111
  // Aggregate property handling — use the rawData value, not the aggregate descriptor
package/dist/store.js CHANGED
@@ -115,7 +115,12 @@ 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
- console.error(`[ORM] Failed to persist delete for ${key}:${id}: ${err instanceof Error ? err.message : String(err)}`);
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)
@@ -42,6 +42,11 @@ export interface OrmRestServerConfig {
42
42
  route: string;
43
43
  metaRoute: boolean;
44
44
  }
45
+ export interface OrmDynamoDBConfig {
46
+ region?: string;
47
+ endpoint?: string;
48
+ [key: string]: unknown;
49
+ }
45
50
  export interface OrmSection {
46
51
  db: OrmDbConfig;
47
52
  paths: OrmPaths;
@@ -49,6 +54,9 @@ export interface OrmSection {
49
54
  mysql?: OrmMysqlConfig;
50
55
  postgres?: OrmPostgresConfig;
51
56
  timescale?: OrmPostgresConfig;
57
+ dynamodb?: OrmDynamoDBConfig;
58
+ logColor?: string;
59
+ logMethod?: string;
52
60
  [key: string]: unknown;
53
61
  }
54
62
  export interface OrmConfig {
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "stonyx-async",
5
5
  "stonyx-module"
6
6
  ],
7
- "version": "0.3.2-beta.7",
7
+ "version": "0.3.2-beta.70",
8
8
  "description": "",
9
9
  "main": "dist/index.js",
10
10
  "type": "module",
@@ -61,16 +61,24 @@
61
61
  },
62
62
  "homepage": "https://github.com/abofs/stonyx-orm#readme",
63
63
  "dependencies": {
64
- "@stonyx/cron": "0.2.1-beta.29",
65
- "@stonyx/events": "0.1.1-beta.9",
66
- "stonyx": "0.2.3-beta.11"
64
+ "@stonyx/cron": "0.2.1-beta.66",
65
+ "@stonyx/events": "0.1.1-beta.49",
66
+ "stonyx": "0.2.3-beta.66"
67
67
  },
68
68
  "peerDependencies": {
69
+ "@aws-sdk/client-dynamodb": "^3.0.0",
70
+ "@aws-sdk/lib-dynamodb": "^3.0.0",
69
71
  "@stonyx/rest-server": ">=0.2.1-beta.14",
70
72
  "mysql2": "^3.0.0",
71
73
  "pg": "^8.0.0"
72
74
  },
73
75
  "peerDependenciesMeta": {
76
+ "@aws-sdk/client-dynamodb": {
77
+ "optional": true
78
+ },
79
+ "@aws-sdk/lib-dynamodb": {
80
+ "optional": true
81
+ },
74
82
  "mysql2": {
75
83
  "optional": true
76
84
  },
@@ -82,18 +90,20 @@
82
90
  }
83
91
  },
84
92
  "devDependencies": {
85
- "@stonyx/rest-server": "0.2.1-beta.30",
86
- "@stonyx/utils": "0.2.3-beta.7",
93
+ "@stonyx/rest-server": "0.2.1-beta.67",
94
+ "@stonyx/utils": "0.2.3-beta.24",
87
95
  "@types/node": "^25.6.0",
88
96
  "mysql2": "^3.20.0",
89
97
  "pg": "^8.20.0",
90
98
  "qunit": "^2.24.1",
91
99
  "sinon": "^21.0.0",
100
+ "tsx": "^4.21.0",
92
101
  "typescript": "^5.8.3"
93
102
  },
94
103
  "scripts": {
95
104
  "build": "tsc",
96
105
  "build:test": "tsc -p tsconfig.test.json",
97
- "test": "npm run build && npm run build:test && stonyx test 'dist-test/test/**/*-test.js'"
106
+ "test": "pnpm build && NODE_ENV=test node --import tsx/esm --import ./test/setup.ts node_modules/qunit/bin/qunit.js 'test/**/*-test.ts'",
107
+ "test:dynamodb": "pnpm build && node --import tsx/esm --import ./test/integration/dynamodb/setup.ts node_modules/qunit/bin/qunit.js 'test/integration/dynamodb/**/*-test.ts'"
98
108
  }
99
109
  }
package/src/commands.ts CHANGED
@@ -28,6 +28,13 @@ const commands: Record<string, Command> = {
28
28
  description: 'Generate a MySQL migration from current model schemas',
29
29
  bootstrap: true,
30
30
  run: async (args) => {
31
+ const config = (await import('stonyx/config')).default;
32
+
33
+ if (config.orm.dynamodb) {
34
+ console.log('DynamoDB does not use file-based migrations. Use db:sync to provision tables.');
35
+ return;
36
+ }
37
+
31
38
  const description = args?.join(' ') || 'migration';
32
39
  const { generateMigration } = await import('./mysql/migration-generator.js');
33
40
  const result = await generateMigration(description);
@@ -39,6 +46,25 @@ const commands: Record<string, Command> = {
39
46
  }
40
47
  }
41
48
  },
49
+ 'db:sync': {
50
+ description: 'Provision DynamoDB tables and GSIs from current model schemas',
51
+ bootstrap: true,
52
+ run: async () => {
53
+ const config = (await import('stonyx/config')).default;
54
+
55
+ if (!config.orm.dynamodb) {
56
+ console.error('DynamoDB is not configured. Set DYNAMODB_REGION (and optionally DYNAMODB_ENDPOINT) to enable DynamoDB mode.');
57
+ process.exit(1);
58
+ }
59
+
60
+ const { default: DynamoDBDB } = await import('./dynamodb/dynamodb-db.js');
61
+ const db = new DynamoDBDB();
62
+ await db.init();
63
+ await db.startup();
64
+ await db.shutdown();
65
+ console.log('DynamoDB tables synced successfully.');
66
+ }
67
+ },
42
68
  'db:migrate': {
43
69
  description: 'Apply pending MySQL migrations',
44
70
  bootstrap: true,
@@ -46,6 +72,11 @@ const commands: Record<string, Command> = {
46
72
  const config = (await import('stonyx/config')).default;
47
73
  const mysqlConfig = config.orm.mysql;
48
74
 
75
+ if (config.orm.dynamodb) {
76
+ console.log('DynamoDB does not use file-based migrations. Use db:sync to provision tables.');
77
+ return;
78
+ }
79
+
49
80
  if (!mysqlConfig) {
50
81
  console.error('MySQL is not configured. Set MYSQL_HOST to enable MySQL mode.');
51
82
  process.exit(1);
@@ -92,6 +123,12 @@ const commands: Record<string, Command> = {
92
123
  bootstrap: true,
93
124
  run: async () => {
94
125
  const config = (await import('stonyx/config')).default;
126
+
127
+ if (config.orm.dynamodb) {
128
+ console.log('DynamoDB does not support migration rollback. Manage table changes via the AWS console or db:sync.');
129
+ return;
130
+ }
131
+
95
132
  const mysqlConfig = config.orm.mysql;
96
133
 
97
134
  if (!mysqlConfig) {
@@ -138,6 +175,12 @@ const commands: Record<string, Command> = {
138
175
  bootstrap: true,
139
176
  run: async () => {
140
177
  const config = (await import('stonyx/config')).default;
178
+
179
+ if (config.orm.dynamodb) {
180
+ console.log('DynamoDB does not use file-based migrations. Use db:sync to provision tables.');
181
+ return;
182
+ }
183
+
141
184
  const mysqlConfig = config.orm.mysql;
142
185
 
143
186
  if (!mysqlConfig) {
@@ -0,0 +1,49 @@
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
+ export interface DynamoDBConfig {
9
+ region?: string;
10
+ endpoint?: string;
11
+ [key: string]: unknown;
12
+ }
13
+
14
+ // Type aliases — declared loose so we don't need to import the real SDK types
15
+ // at compile time (they're optional peer deps).
16
+ export type DocumentClient = {
17
+ send(command: unknown): Promise<unknown>;
18
+ };
19
+
20
+ export type DynamoDBClientConstructor = new (options: unknown) => { config: unknown };
21
+ export type DocumentClientFromFn = { from(client: unknown): DocumentClient };
22
+
23
+ /**
24
+ * Create a DynamoDBDocumentClient from the given config.
25
+ * Uses dynamic import so @aws-sdk/* are optional peer deps.
26
+ */
27
+ export async function createDocumentClient(dbConfig: DynamoDBConfig): Promise<DocumentClient> {
28
+ const { DynamoDBClient } = await import('@aws-sdk/client-dynamodb' as string) as {
29
+ DynamoDBClient: DynamoDBClientConstructor;
30
+ };
31
+ const { DynamoDBDocumentClient } = await import('@aws-sdk/lib-dynamodb' as string) as {
32
+ DynamoDBDocumentClient: DocumentClientFromFn;
33
+ };
34
+
35
+ const clientOptions: Record<string, unknown> = {};
36
+ if (dbConfig.region) clientOptions.region = dbConfig.region;
37
+ if (dbConfig.endpoint) clientOptions.endpoint = dbConfig.endpoint;
38
+
39
+ const rawClient = new DynamoDBClient(clientOptions);
40
+ return DynamoDBDocumentClient.from(rawClient);
41
+ }
42
+
43
+ /**
44
+ * Nullify the document client reference (DynamoDB connections are HTTP-based
45
+ * and stateless — no explicit pool close needed, but we clear the reference).
46
+ */
47
+ export function destroyDocumentClient(_client: DocumentClient | null): null {
48
+ return null;
49
+ }