@stonyx/orm 0.3.2-alpha.3 → 0.3.2-alpha.30

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.
Files changed (44) hide show
  1. package/README.md +35 -2
  2. package/config/environment.js +8 -0
  3. package/dist/commands.js +34 -0
  4. package/dist/dynamodb/connection.d.ts +31 -0
  5. package/dist/dynamodb/connection.js +28 -0
  6. package/dist/dynamodb/dynamodb-db.d.ts +132 -0
  7. package/dist/dynamodb/dynamodb-db.js +586 -0
  8. package/dist/dynamodb/operation-builder.d.ts +76 -0
  9. package/dist/dynamodb/operation-builder.js +116 -0
  10. package/dist/dynamodb/type-map.d.ts +31 -0
  11. package/dist/dynamodb/type-map.js +48 -0
  12. package/dist/index.d.ts +1 -0
  13. package/dist/main.d.ts +16 -0
  14. package/dist/main.js +36 -0
  15. package/dist/manage-record.js +51 -8
  16. package/dist/mysql/mysql-db.d.ts +1 -0
  17. package/dist/mysql/mysql-db.js +4 -2
  18. package/dist/orm-request.js +4 -3
  19. package/dist/postgres/connection.d.ts +1 -0
  20. package/dist/postgres/connection.js +8 -6
  21. package/dist/postgres/postgres-db.js +4 -2
  22. package/dist/relationships.js +1 -1
  23. package/dist/serializer.js +38 -2
  24. package/dist/store.d.ts +10 -0
  25. package/dist/store.js +66 -2
  26. package/dist/types/orm-types.d.ts +9 -0
  27. package/package.json +17 -7
  28. package/src/commands.ts +43 -0
  29. package/src/dynamodb/connection.ts +50 -0
  30. package/src/dynamodb/dynamodb-db.ts +801 -0
  31. package/src/dynamodb/operation-builder.ts +202 -0
  32. package/src/dynamodb/type-map.ts +54 -0
  33. package/src/index.ts +1 -0
  34. package/src/main.ts +45 -0
  35. package/src/manage-record.ts +54 -9
  36. package/src/mysql/mysql-db.ts +5 -2
  37. package/src/orm-request.ts +5 -2
  38. package/src/postgres/connection.ts +10 -6
  39. package/src/postgres/postgres-db.ts +4 -2
  40. package/src/relationships.ts +1 -1
  41. package/src/serializer.ts +39 -2
  42. package/src/store.ts +69 -2
  43. package/src/types/orm-types.ts +10 -0
  44. package/src/types/stonyx.d.ts +7 -1
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
- console.error(`[ORM] Failed to persist delete for ${key}:${id}`, 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)
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
@@ -42,6 +42,12 @@ export interface OrmRestServerConfig {
42
42
  route: string;
43
43
  metaRoute: boolean;
44
44
  }
45
+ export interface OrmDynamoDBConfig {
46
+ region?: string;
47
+ endpoint?: string;
48
+ tablePrefix?: string;
49
+ [key: string]: unknown;
50
+ }
45
51
  export interface OrmSection {
46
52
  db: OrmDbConfig;
47
53
  paths: OrmPaths;
@@ -49,6 +55,9 @@ export interface OrmSection {
49
55
  mysql?: OrmMysqlConfig;
50
56
  postgres?: OrmPostgresConfig;
51
57
  timescale?: OrmPostgresConfig;
58
+ dynamodb?: OrmDynamoDBConfig;
59
+ logColor?: string;
60
+ logMethod?: string;
52
61
  [key: string]: unknown;
53
62
  }
54
63
  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-alpha.3",
7
+ "version": "0.3.2-alpha.30",
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.70",
65
+ "@stonyx/events": "0.1.1-beta.51",
66
+ "stonyx": "0.2.3-beta.68"
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.71",
94
+ "@stonyx/utils": "0.2.3-beta.25",
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,50 @@
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
+ tablePrefix?: string;
12
+ [key: string]: unknown;
13
+ }
14
+
15
+ // Type aliases — declared loose so we don't need to import the real SDK types
16
+ // at compile time (they're optional peer deps).
17
+ export type DocumentClient = {
18
+ send(command: unknown): Promise<unknown>;
19
+ };
20
+
21
+ export type DynamoDBClientConstructor = new (options: unknown) => { config: unknown };
22
+ export type DocumentClientFromFn = { from(client: unknown): DocumentClient };
23
+
24
+ /**
25
+ * Create a DynamoDBDocumentClient from the given config.
26
+ * Uses dynamic import so @aws-sdk/* are optional peer deps.
27
+ */
28
+ export async function createDocumentClient(dbConfig: DynamoDBConfig): Promise<DocumentClient> {
29
+ const { DynamoDBClient } = await import('@aws-sdk/client-dynamodb' as string) as {
30
+ DynamoDBClient: DynamoDBClientConstructor;
31
+ };
32
+ const { DynamoDBDocumentClient } = await import('@aws-sdk/lib-dynamodb' as string) as {
33
+ DynamoDBDocumentClient: DocumentClientFromFn;
34
+ };
35
+
36
+ const clientOptions: Record<string, unknown> = {};
37
+ if (dbConfig.region) clientOptions.region = dbConfig.region;
38
+ if (dbConfig.endpoint) clientOptions.endpoint = dbConfig.endpoint;
39
+
40
+ const rawClient = new DynamoDBClient(clientOptions);
41
+ return DynamoDBDocumentClient.from(rawClient);
42
+ }
43
+
44
+ /**
45
+ * Nullify the document client reference (DynamoDB connections are HTTP-based
46
+ * and stateless — no explicit pool close needed, but we clear the reference).
47
+ */
48
+ export function destroyDocumentClient(_client: DocumentClient | null): null {
49
+ return null;
50
+ }