@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.
Files changed (71) hide show
  1. package/README.md +377 -30
  2. package/config/environment.js +111 -12
  3. package/dist/column-conversion.d.ts +34 -0
  4. package/dist/column-conversion.js +34 -0
  5. package/dist/commands.js +34 -0
  6. package/dist/dynamodb/connection.d.ts +31 -0
  7. package/dist/dynamodb/connection.js +28 -0
  8. package/dist/dynamodb/dynamodb-db.d.ts +142 -0
  9. package/dist/dynamodb/dynamodb-db.js +596 -0
  10. package/dist/dynamodb/operation-builder.d.ts +76 -0
  11. package/dist/dynamodb/operation-builder.js +116 -0
  12. package/dist/dynamodb/type-map.d.ts +31 -0
  13. package/dist/dynamodb/type-map.js +48 -0
  14. package/dist/index.d.ts +4 -2
  15. package/dist/index.js +3 -2
  16. package/dist/main.d.ts +1 -0
  17. package/dist/main.js +29 -1
  18. package/dist/manage-record.d.ts +4 -0
  19. package/dist/manage-record.js +95 -34
  20. package/dist/mysql/connection.d.ts +3 -0
  21. package/dist/mysql/mysql-db.d.ts +32 -2
  22. package/dist/mysql/mysql-db.js +239 -45
  23. package/dist/mysql/query-builder.d.ts +1 -0
  24. package/dist/mysql/query-builder.js +17 -0
  25. package/dist/normalize-record-id.d.ts +67 -0
  26. package/dist/normalize-record-id.js +88 -0
  27. package/dist/orm-request.d.ts +17 -1
  28. package/dist/orm-request.js +77 -19
  29. package/dist/postgres/column-rules.d.ts +33 -0
  30. package/dist/postgres/column-rules.js +34 -0
  31. package/dist/postgres/connection.d.ts +1 -0
  32. package/dist/postgres/connection.js +8 -6
  33. package/dist/postgres/postgres-db.d.ts +36 -2
  34. package/dist/postgres/postgres-db.js +244 -45
  35. package/dist/postgres/query-builder.d.ts +1 -0
  36. package/dist/postgres/query-builder.js +17 -0
  37. package/dist/record.js +7 -5
  38. package/dist/relationships.js +1 -1
  39. package/dist/serializer.js +38 -2
  40. package/dist/store.d.ts +13 -1
  41. package/dist/store.js +65 -6
  42. package/dist/timescale/query-builder.d.ts +1 -1
  43. package/dist/timescale/query-builder.js +1 -2
  44. package/dist/types/orm-types.d.ts +11 -0
  45. package/package.json +19 -7
  46. package/src/column-conversion.ts +53 -0
  47. package/src/commands.ts +43 -0
  48. package/src/dynamodb/connection.ts +50 -0
  49. package/src/dynamodb/dynamodb-db.ts +811 -0
  50. package/src/dynamodb/operation-builder.ts +202 -0
  51. package/src/dynamodb/type-map.ts +54 -0
  52. package/src/index.ts +4 -2
  53. package/src/main.ts +30 -1
  54. package/src/manage-record.ts +112 -33
  55. package/src/mysql/connection.ts +3 -0
  56. package/src/mysql/mysql-db.ts +264 -46
  57. package/src/mysql/query-builder.ts +21 -0
  58. package/src/normalize-record-id.ts +88 -0
  59. package/src/orm-request.ts +109 -18
  60. package/src/postgres/column-rules.ts +37 -0
  61. package/src/postgres/connection.ts +10 -6
  62. package/src/postgres/postgres-db.ts +277 -49
  63. package/src/postgres/query-builder.ts +21 -0
  64. package/src/record.ts +8 -5
  65. package/src/relationships.ts +1 -1
  66. package/src/serializer.ts +39 -2
  67. package/src/store.ts +68 -6
  68. package/src/timescale/query-builder.ts +1 -2
  69. package/src/types/orm-types.ts +12 -0
  70. package/src/types/stonyx.d.ts +7 -1
  71. package/config/environment.ts +0 -91
@@ -16,6 +16,7 @@ export interface OrmMysqlConfig {
16
16
  connectionLimit?: number;
17
17
  migrationsDir?: string;
18
18
  migrationsTable?: string;
19
+ autoMigrate?: boolean;
19
20
  [key: string]: unknown;
20
21
  }
21
22
  export interface OrmPostgresConfig {
@@ -27,6 +28,7 @@ export interface OrmPostgresConfig {
27
28
  connectionLimit?: number;
28
29
  migrationsDir?: string;
29
30
  migrationsTable?: string;
31
+ autoMigrate?: boolean;
30
32
  [key: string]: unknown;
31
33
  }
32
34
  export interface OrmPaths {
@@ -42,6 +44,12 @@ export interface OrmRestServerConfig {
42
44
  route: string;
43
45
  metaRoute: boolean;
44
46
  }
47
+ export interface OrmDynamoDBConfig {
48
+ region?: string;
49
+ endpoint?: string;
50
+ tablePrefix?: string;
51
+ [key: string]: unknown;
52
+ }
45
53
  export interface OrmSection {
46
54
  db: OrmDbConfig;
47
55
  paths: OrmPaths;
@@ -49,6 +57,9 @@ export interface OrmSection {
49
57
  mysql?: OrmMysqlConfig;
50
58
  postgres?: OrmPostgresConfig;
51
59
  timescale?: OrmPostgresConfig;
60
+ dynamodb?: OrmDynamoDBConfig;
61
+ logColor?: string;
62
+ logMethod?: string;
52
63
  [key: string]: unknown;
53
64
  }
54
65
  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.33",
7
+ "version": "0.3.2-beta.330",
8
8
  "description": "",
9
9
  "main": "dist/index.js",
10
10
  "type": "module",
@@ -61,16 +61,25 @@
61
61
  },
62
62
  "homepage": "https://github.com/abofs/stonyx-orm#readme",
63
63
  "dependencies": {
64
- "@stonyx/cron": "0.2.1-beta.54",
65
- "@stonyx/events": "0.1.1-beta.47",
66
- "stonyx": "0.2.3-beta.61"
64
+ "@stonyx/cron": "0.2.1-beta.176",
65
+ "@stonyx/events": "0.1.1-beta.74",
66
+ "@stonyx/utils": "0.2.3-beta.30",
67
+ "stonyx": "0.2.3-beta.110"
67
68
  },
68
69
  "peerDependencies": {
70
+ "@aws-sdk/client-dynamodb": "^3.0.0",
71
+ "@aws-sdk/lib-dynamodb": "^3.0.0",
69
72
  "@stonyx/rest-server": ">=0.2.1-beta.14",
70
73
  "mysql2": "^3.0.0",
71
74
  "pg": "^8.0.0"
72
75
  },
73
76
  "peerDependenciesMeta": {
77
+ "@aws-sdk/client-dynamodb": {
78
+ "optional": true
79
+ },
80
+ "@aws-sdk/lib-dynamodb": {
81
+ "optional": true
82
+ },
74
83
  "mysql2": {
75
84
  "optional": true
76
85
  },
@@ -82,8 +91,7 @@
82
91
  }
83
92
  },
84
93
  "devDependencies": {
85
- "@stonyx/rest-server": "0.2.1-beta.54",
86
- "@stonyx/utils": "0.2.3-beta.23",
94
+ "@stonyx/rest-server": "0.2.1-beta.179",
87
95
  "@types/node": "^25.6.0",
88
96
  "mysql2": "^3.20.0",
89
97
  "pg": "^8.20.0",
@@ -95,6 +103,10 @@
95
103
  "scripts": {
96
104
  "build": "tsc",
97
105
  "build:test": "tsc -p tsconfig.test.json",
98
- "test": "pnpm build && NODE_ENV=test node --import tsx/esm --import ./test/setup.ts node_modules/qunit/bin/qunit.js 'test/**/*-test.ts'"
106
+ "test": "pnpm build && NODE_ENV=test node --import tsx/esm --import ./test/setup.ts node_modules/qunit/bin/qunit.js 'test/**/*-test.ts' && ORM_TEST_ROUTE=/ pnpm test:mounted && ORM_TEST_ROUTE=/api pnpm test:mounted && ORM_TEST_ROUTE=api pnpm test:mounted && ORM_TEST_ROUTE=/api/v1 pnpm test:mounted && ORM_TEST_ROUTE=/api/ pnpm test:mounted && pnpm test:readme && pnpm test:reference",
107
+ "test:mounted": "node --import tsx/esm --import ./test/integration/mounted-route/setup.ts node_modules/qunit/bin/qunit.js 'test/integration/mounted-route/links-mounted.ts' 'test/zz-exit-test.ts'",
108
+ "test:readme": "pnpm build && node --import tsx/esm --import ./test/integration/readme-access/setup.ts node_modules/qunit/bin/qunit.js 'test/integration/readme-access/readme-sample.ts' 'test/zz-exit-test.ts'",
109
+ "test:reference": "pnpm build && node --import tsx/esm --import ./test/integration/reference-access/setup.ts node_modules/qunit/bin/qunit.js 'test/integration/reference-access/reference-sample.ts' 'test/integration/reference-access/record-id-resolution.ts' 'test/zz-exit-test.ts'",
110
+ "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'"
99
111
  }
100
112
  }
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Shared write-path column conversion (#292).
3
+ *
4
+ * This module owns the *iteration* only — the single place where a write
5
+ * payload is walked column-by-column and each value is handed to the driver's
6
+ * own conversion rule. The rules themselves are genuinely disjoint per driver
7
+ * (Postgres stringifies for JSONB; DynamoDB's DocumentClient marshals objects
8
+ * and arrays natively and must NOT be given stringified values) and therefore
9
+ * deliberately live with their drivers rather than being merged here.
10
+ *
11
+ * The duplicated create/update iteration was the drift vector that produced
12
+ * #292: `_recordToRow` converted, `_persistUpdate` did not. Routing both
13
+ * through `convertRow` removes the opportunity for the two to diverge again.
14
+ */
15
+
16
+ /**
17
+ * A driver-supplied per-column conversion rule.
18
+ *
19
+ * @param value the raw value about to be bound into the query
20
+ * @param columnType the column's declared type from `schema.columns`, or
21
+ * `undefined` when the column is not an attribute column
22
+ * (e.g. `updated_at`, FK columns and `id`, which are added
23
+ * outside the attribute loop). Rules MUST be a no-op for
24
+ * `undefined`.
25
+ */
26
+ export type ColumnRule = (value: unknown, columnType: string | undefined) => unknown;
27
+
28
+ /**
29
+ * Applies `rule` to every column of a complete write payload.
30
+ *
31
+ * Call this on the *finished* payload immediately before handing it to the
32
+ * query builder — not inside a payload-building loop. Columns appended after
33
+ * such a loop (FKs, `updated_at`) would otherwise bypass conversion, which is
34
+ * the same class of defect #292 reports.
35
+ */
36
+ export function convertRow(
37
+ row: Record<string, unknown>,
38
+ columns: Record<string, string>,
39
+ rule: ColumnRule
40
+ ): Record<string, unknown> {
41
+ const converted: Record<string, unknown> = {};
42
+
43
+ for (const [col, value] of Object.entries(row)) {
44
+ // `Object.hasOwn`, not `columns[col]`: a column named `toString` or
45
+ // `constructor` would otherwise reach the rule with a function as its
46
+ // "declared type". Inert for the shipped Postgres rule (strict equality
47
+ // against 'JSONB'), but `ColumnRule` is the extension point other drivers
48
+ // write against, and the obvious `if (columnType)` rule would invert.
49
+ converted[col] = rule(value, Object.hasOwn(columns, col) ? columns[col] : undefined);
50
+ }
51
+
52
+ return converted;
53
+ }
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
+ }