@stonyx/orm 0.3.2-beta.64 → 0.3.2-beta.66

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,188 @@
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
+ export interface PutItemParams {
13
+ TableName: string;
14
+ Item: Record<string, unknown>;
15
+ ConditionExpression?: string;
16
+ }
17
+
18
+ export interface GetItemParams {
19
+ TableName: string;
20
+ Key: Record<string, unknown>;
21
+ }
22
+
23
+ export interface UpdateItemParams {
24
+ TableName: string;
25
+ Key: Record<string, unknown>;
26
+ UpdateExpression: string;
27
+ ExpressionAttributeNames: Record<string, string>;
28
+ ExpressionAttributeValues: Record<string, unknown>;
29
+ ReturnValues: string;
30
+ }
31
+
32
+ export interface DeleteItemParams {
33
+ TableName: string;
34
+ Key: Record<string, unknown>;
35
+ }
36
+
37
+ export interface ScanParams {
38
+ TableName: string;
39
+ FilterExpression?: string;
40
+ ExpressionAttributeNames?: Record<string, string>;
41
+ ExpressionAttributeValues?: Record<string, unknown>;
42
+ ExclusiveStartKey?: Record<string, unknown>;
43
+ }
44
+
45
+ export interface QueryParams {
46
+ TableName: string;
47
+ IndexName: string;
48
+ KeyConditionExpression: string;
49
+ ExpressionAttributeNames: Record<string, string>;
50
+ ExpressionAttributeValues: Record<string, unknown>;
51
+ ExclusiveStartKey?: Record<string, unknown>;
52
+ }
53
+
54
+ /**
55
+ * PutItem — optionally with a condition expression.
56
+ *
57
+ * Pass conditionExpression = 'attribute_not_exists(id)' to enforce uniqueness.
58
+ */
59
+ export function buildPutItem(
60
+ tableName: string,
61
+ item: Record<string, unknown>,
62
+ conditionExpression?: string,
63
+ ): PutItemParams {
64
+ const params: PutItemParams = { TableName: tableName, Item: item };
65
+ if (conditionExpression) params.ConditionExpression = conditionExpression;
66
+ return params;
67
+ }
68
+
69
+ /**
70
+ * GetItem by primary key.
71
+ */
72
+ export function buildGetItem(
73
+ tableName: string,
74
+ key: Record<string, unknown>,
75
+ ): GetItemParams {
76
+ return { TableName: tableName, Key: key };
77
+ }
78
+
79
+ /**
80
+ * UpdateItem with a SET expression built from the `updates` object.
81
+ * Only the supplied attributes are updated (diff-based call site).
82
+ */
83
+ export function buildUpdateItem(
84
+ tableName: string,
85
+ key: Record<string, unknown>,
86
+ updates: Record<string, unknown>,
87
+ ): UpdateItemParams {
88
+ const names: Record<string, string> = {};
89
+ const values: Record<string, unknown> = {};
90
+ const setClauses: string[] = [];
91
+
92
+ for (const [attr, val] of Object.entries(updates)) {
93
+ const nameAlias = `#${attr}`;
94
+ const valAlias = `:${attr}`;
95
+ names[nameAlias] = attr;
96
+ values[valAlias] = val;
97
+ setClauses.push(`${nameAlias} = ${valAlias}`);
98
+ }
99
+
100
+ return {
101
+ TableName: tableName,
102
+ Key: key,
103
+ UpdateExpression: `SET ${setClauses.join(', ')}`,
104
+ ExpressionAttributeNames: names,
105
+ ExpressionAttributeValues: values,
106
+ ReturnValues: 'NONE',
107
+ };
108
+ }
109
+
110
+ /**
111
+ * DeleteItem by primary key.
112
+ */
113
+ export function buildDeleteItem(
114
+ tableName: string,
115
+ key: Record<string, unknown>,
116
+ ): DeleteItemParams {
117
+ return { TableName: tableName, Key: key };
118
+ }
119
+
120
+ /**
121
+ * ScanCommand params.
122
+ * If conditions are supplied they are rendered as a FilterExpression using AND.
123
+ */
124
+ export function buildScan(
125
+ tableName: string,
126
+ conditions?: Record<string, unknown>,
127
+ exclusiveStartKey?: Record<string, unknown>,
128
+ ): ScanParams {
129
+ const params: ScanParams = { TableName: tableName };
130
+
131
+ if (exclusiveStartKey) params.ExclusiveStartKey = exclusiveStartKey;
132
+
133
+ if (conditions && Object.keys(conditions).length > 0) {
134
+ const names: Record<string, string> = {};
135
+ const values: Record<string, unknown> = {};
136
+ const clauses: string[] = [];
137
+
138
+ for (const [attr, val] of Object.entries(conditions)) {
139
+ const nameAlias = `#${attr}`;
140
+ const valAlias = `:${attr}`;
141
+ names[nameAlias] = attr;
142
+ values[valAlias] = val;
143
+ clauses.push(`${nameAlias} = ${valAlias}`);
144
+ }
145
+
146
+ params.FilterExpression = clauses.join(' AND ');
147
+ params.ExpressionAttributeNames = names;
148
+ params.ExpressionAttributeValues = values;
149
+ }
150
+
151
+ return params;
152
+ }
153
+
154
+ /**
155
+ * QueryCommand params for a GSI.
156
+ * keyConditions must be in the form { attrName: value } and will be rendered
157
+ * as equality expressions joined by AND.
158
+ */
159
+ export function buildQuery(
160
+ tableName: string,
161
+ indexName: string,
162
+ keyConditions: Record<string, unknown>,
163
+ exclusiveStartKey?: Record<string, unknown>,
164
+ ): QueryParams {
165
+ const names: Record<string, string> = {};
166
+ const values: Record<string, unknown> = {};
167
+ const clauses: string[] = [];
168
+
169
+ for (const [attr, val] of Object.entries(keyConditions)) {
170
+ const nameAlias = `#${attr}`;
171
+ const valAlias = `:${attr}`;
172
+ names[nameAlias] = attr;
173
+ values[valAlias] = val;
174
+ clauses.push(`${nameAlias} = ${valAlias}`);
175
+ }
176
+
177
+ const params: QueryParams = {
178
+ TableName: tableName,
179
+ IndexName: indexName,
180
+ KeyConditionExpression: clauses.join(' AND '),
181
+ ExpressionAttributeNames: names,
182
+ ExpressionAttributeValues: values,
183
+ };
184
+
185
+ if (exclusiveStartKey) params.ExclusiveStartKey = exclusiveStartKey;
186
+
187
+ return params;
188
+ }
@@ -0,0 +1,54 @@
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
+ export type DynamoScalarType = 'S' | 'N' | 'BOOL';
9
+
10
+ /**
11
+ * DynamoDB attribute-type string for a given ORM attr type.
12
+ * - string → S
13
+ * - number / float → N (stored as Number; DocumentClient handles it)
14
+ * - boolean → BOOL
15
+ * - date → S (ISO-8601 string — enables range queries)
16
+ * - timestamp → N (milliseconds since epoch)
17
+ * - passthrough/trim/etc → S (safe default)
18
+ *
19
+ * For key schema declarations only `S` and `N` are valid; BOOL
20
+ * is legal for attributes but never for a PK/SK.
21
+ */
22
+ const typeMap: Record<string, DynamoScalarType> = {
23
+ string: 'S',
24
+ number: 'N',
25
+ float: 'N',
26
+ boolean: 'BOOL',
27
+ date: 'S',
28
+ timestamp: 'N',
29
+ passthrough: 'S',
30
+ trim: 'S',
31
+ uppercase: 'S',
32
+ ceil: 'N',
33
+ floor: 'N',
34
+ round: 'N',
35
+ };
36
+
37
+ /**
38
+ * Returns the DynamoDB attribute type for a given ORM type string.
39
+ * Defaults to 'S' for any unknown/custom type.
40
+ */
41
+ export function getDynamoType(attrType: string): DynamoScalarType {
42
+ return typeMap[attrType] ?? 'S';
43
+ }
44
+
45
+ /**
46
+ * Returns the DynamoDB key type ('S' | 'N') for use in KeySchema.
47
+ * BOOL cannot be a key attribute; anything that maps to BOOL falls back to 'S'.
48
+ */
49
+ export function getDynamoKeyType(attrType: string): 'S' | 'N' {
50
+ const t = getDynamoType(attrType);
51
+ return t === 'N' ? 'N' : 'S';
52
+ }
53
+
54
+ export default typeMap;
package/src/main.ts CHANGED
@@ -164,6 +164,11 @@ export default class Orm {
164
164
  this.sqlDb = new MysqlDB() as SqlDb;
165
165
  this.db = this.sqlDb;
166
166
  promises.push(this.sqlDb.init());
167
+ } else if (config.orm.dynamodb) {
168
+ const { default: DynamoDBDB } = await import('./dynamodb/dynamodb-db.js');
169
+ this.sqlDb = new DynamoDBDB() as SqlDb;
170
+ this.db = this.sqlDb;
171
+ promises.push(this.sqlDb.init());
167
172
  } else if (this.options.dbType !== 'none') {
168
173
  const db = new DB();
169
174
  this.db = db;
@@ -48,6 +48,12 @@ export interface OrmRestServerConfig {
48
48
  metaRoute: boolean;
49
49
  }
50
50
 
51
+ export interface OrmDynamoDBConfig {
52
+ region?: string;
53
+ endpoint?: string;
54
+ [key: string]: unknown;
55
+ }
56
+
51
57
  export interface OrmSection {
52
58
  db: OrmDbConfig;
53
59
  paths: OrmPaths;
@@ -55,6 +61,7 @@ export interface OrmSection {
55
61
  mysql?: OrmMysqlConfig;
56
62
  postgres?: OrmPostgresConfig;
57
63
  timescale?: OrmPostgresConfig;
64
+ dynamodb?: OrmDynamoDBConfig;
58
65
  logColor?: string;
59
66
  logMethod?: string;
60
67
  [key: string]: unknown;