@aurios/mizzle 1.1.1 → 1.1.3
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/.turbo/turbo-build.log +37 -0
- package/LICENSE +21 -0
- package/README.md +57 -0
- package/dist/chunk-AQVECMXP.js +1 -0
- package/dist/chunk-DU7UPWBW.js +1 -0
- package/dist/chunk-GPYZK4WY.js +1 -0
- package/dist/chunk-NPPZW6VT.js +1 -0
- package/dist/chunk-TOYV2M4M.js +1 -0
- package/dist/chunk-UM3YF5EC.js +1 -0
- package/dist/columns.d.ts +1 -0
- package/dist/columns.js +1 -0
- package/dist/db-zHIHBm1E.d.ts +815 -0
- package/dist/db.d.ts +3 -0
- package/dist/db.js +1 -0
- package/dist/diff.d.ts +18 -0
- package/dist/diff.js +1 -0
- package/dist/index.d.ts +42 -0
- package/dist/index.js +1 -0
- package/dist/introspection.d.ts +7 -0
- package/dist/introspection.js +1 -0
- package/dist/operators-BVreW0ky.d.ts +719 -0
- package/dist/snapshot.d.ts +24 -0
- package/dist/snapshot.js +1 -0
- package/dist/table.d.ts +1 -0
- package/dist/table.js +1 -0
- package/dist/transaction-RE7LXTGV.js +1 -0
- package/package.json +73 -24
- package/src/builders/base.ts +53 -56
- package/src/builders/batch-get.ts +63 -58
- package/src/builders/batch-write.ts +81 -78
- package/src/builders/delete.ts +46 -53
- package/src/builders/insert.ts +158 -150
- package/src/builders/query-promise.ts +26 -35
- package/src/builders/relational-builder.ts +214 -191
- package/src/builders/select.ts +250 -238
- package/src/builders/transaction.ts +170 -151
- package/src/builders/update.ts +198 -191
- package/src/columns/binary-set.ts +29 -38
- package/src/columns/binary.ts +25 -35
- package/src/columns/boolean.ts +25 -30
- package/src/columns/date.ts +57 -64
- package/src/columns/index.ts +15 -15
- package/src/columns/json.ts +39 -48
- package/src/columns/list.ts +26 -36
- package/src/columns/map.ts +26 -34
- package/src/columns/number-set.ts +29 -38
- package/src/columns/number.ts +33 -40
- package/src/columns/string-set.ts +38 -47
- package/src/columns/string.ts +37 -49
- package/src/columns/uuid.ts +26 -33
- package/src/core/client.ts +9 -9
- package/src/core/column-builder.ts +194 -220
- package/src/core/column.ts +127 -135
- package/src/core/diff.ts +40 -34
- package/src/core/errors.ts +20 -17
- package/src/core/introspection.ts +62 -58
- package/src/core/operations.ts +17 -23
- package/src/core/parser.ts +82 -88
- package/src/core/relations.ts +165 -152
- package/src/core/retry.ts +52 -52
- package/src/core/snapshot.ts +131 -130
- package/src/core/strategies.ts +222 -214
- package/src/core/table.ts +189 -202
- package/src/core/validation.ts +52 -52
- package/src/db.ts +216 -213
- package/src/expressions/actions.ts +26 -26
- package/src/expressions/builder.ts +62 -54
- package/src/expressions/operators.ts +48 -48
- package/src/expressions/update-builder.ts +79 -75
- package/src/index.ts +2 -1
- package/src/indexes.ts +8 -8
- package/test/batch-resilience.test.ts +138 -0
- package/test/builders/delete.test.ts +100 -0
- package/test/builders/insert.test.ts +216 -0
- package/test/builders/relational-types.test.ts +55 -0
- package/test/builders/relational.integration.test.ts +291 -0
- package/test/builders/relational.test.ts +66 -0
- package/test/builders/select.test.ts +411 -0
- package/test/builders/transaction-errors.test.ts +46 -0
- package/test/builders/transaction-execution.test.ts +99 -0
- package/test/builders/transaction-proxy.test.ts +41 -0
- package/test/builders/update-expression.test.ts +106 -0
- package/test/builders/update.test.ts +179 -0
- package/test/core/diff.test.ts +152 -0
- package/test/core/expressions.test.ts +64 -0
- package/test/core/introspection.test.ts +47 -0
- package/test/core/parser.test.ts +69 -0
- package/test/core/snapshot-gen.test.ts +155 -0
- package/test/core/snapshot.test.ts +52 -0
- package/test/date-column.test.ts +159 -0
- package/test/fluent-writes.integration.test.ts +148 -0
- package/test/integration-retry.test.ts +77 -0
- package/test/integration.test.ts +105 -0
- package/test/item-size-error.test.ts +16 -0
- package/test/item-size-validation.test.ts +82 -0
- package/test/item-size.test.ts +47 -0
- package/test/iterator-pagination.integration.test.ts +132 -0
- package/test/jsdoc-builders.test.ts +55 -0
- package/test/jsdoc-schema.test.ts +107 -0
- package/test/json-column.test.ts +51 -0
- package/test/metadata.test.ts +54 -0
- package/test/mizzle-package.test.ts +20 -0
- package/test/relational-centralized.test.ts +83 -0
- package/test/relational-definition.test.ts +75 -0
- package/test/relational-init.test.ts +30 -0
- package/test/relational-proxy.test.ts +52 -0
- package/test/relations.test.ts +45 -0
- package/test/resilience-config.test.ts +34 -0
- package/test/retry-handler.test.ts +63 -0
- package/test/transaction.integration.test.ts +153 -0
- package/test/unified-select.integration.test.ts +153 -0
- package/test/unified-update.integration.test.ts +139 -0
- package/test/update.integration.test.ts +132 -0
- package/tsconfig.json +12 -9
- package/tsup.config.ts +11 -11
- package/vitest.config.ts +8 -0
package/src/db.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb";
|
|
|
3
3
|
import { InsertBuilder } from "./builders/insert";
|
|
4
4
|
import { RelationnalQueryBuilder } from "./builders/relational-builder";
|
|
5
5
|
import { SelectBuilder, type SelectedFields } from "./builders/select";
|
|
6
|
+
import type { BaseBuilder } from "./builders/base";
|
|
6
7
|
import type { Entity, InferInsertModel, InferSelectModel } from "./core/table";
|
|
7
8
|
import { UpdateBuilder } from "./builders/update";
|
|
8
9
|
import { DeleteBuilder } from "./builders/delete";
|
|
@@ -13,259 +14,261 @@ import { extractMetadata, type InternalRelationalSchema } from "./core/relations
|
|
|
13
14
|
import { RetryHandler, type RetryConfig } from "./core/retry";
|
|
14
15
|
import { MizzleClient, type IMizzleClient } from "./core/client";
|
|
15
16
|
|
|
16
|
-
export type QuerySchema<TSchema extends Record<string,
|
|
17
|
-
|
|
17
|
+
export type QuerySchema<TSchema extends Record<string, any>> = {
|
|
18
|
+
[K in keyof TSchema as TSchema[K] extends Entity ? K : never]: RelationnalQueryBuilder<
|
|
19
|
+
TSchema[K] extends Entity ? TSchema[K] : never
|
|
20
|
+
>;
|
|
18
21
|
};
|
|
19
22
|
|
|
20
23
|
/**
|
|
21
24
|
* DynamoDB database instance.
|
|
22
25
|
*/
|
|
23
|
-
export class DynamoDB<TSchema extends Record<string,
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
26
|
+
export class DynamoDB<TSchema extends Record<string, any> = Record<string, any>> {
|
|
27
|
+
private docClient: IMizzleClient;
|
|
28
|
+
private schema?: InternalRelationalSchema;
|
|
29
|
+
private retryConfig: RetryConfig;
|
|
27
30
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
31
|
+
/**
|
|
32
|
+
* Access relational queries for entities defined in the schema.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* const users = await db.query.users.findMany();
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
public readonly query: QuerySchema<TSchema>;
|
|
37
40
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
if (relations) {
|
|
49
|
-
this.schema = extractMetadata(relations);
|
|
50
|
-
}
|
|
41
|
+
constructor(client: DynamoDBClient, relations?: TSchema, retry?: Partial<RetryConfig>) {
|
|
42
|
+
this.retryConfig = {
|
|
43
|
+
maxAttempts: retry?.maxAttempts ?? 3,
|
|
44
|
+
baseDelay: retry?.baseDelay ?? 100,
|
|
45
|
+
};
|
|
46
|
+
this.docClient = new MizzleClient(
|
|
47
|
+
DynamoDBDocumentClient.from(client),
|
|
48
|
+
new RetryHandler(this.retryConfig),
|
|
49
|
+
);
|
|
51
50
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
51
|
+
if (relations) {
|
|
52
|
+
this.schema = extractMetadata(relations);
|
|
53
|
+
}
|
|
55
54
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
55
|
+
this.query = new Proxy({} as QuerySchema<TSchema>, {
|
|
56
|
+
get: (_, prop) => {
|
|
57
|
+
if (typeof prop !== "string") return undefined;
|
|
59
58
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
59
|
+
if (!this.schema) {
|
|
60
|
+
throw new Error(
|
|
61
|
+
"No relations defined. Initialize mizzle with a relations object to use db.query.",
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
64
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
65
|
+
const entityMetadata = this.schema.entities[prop];
|
|
66
|
+
if (!entityMetadata) {
|
|
67
|
+
throw new Error(`Entity ${prop} not found in relations schema.`);
|
|
68
|
+
}
|
|
69
69
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
* @returns An InsertBuilder instance.
|
|
80
|
-
*/
|
|
81
|
-
insert<T extends Entity>(table: T): InsertBuilder<T> {
|
|
82
|
-
return new InsertBuilder(table, this.docClient);
|
|
83
|
-
}
|
|
70
|
+
return new RelationnalQueryBuilder(
|
|
71
|
+
this.docClient,
|
|
72
|
+
entityMetadata.entity,
|
|
73
|
+
this.schema,
|
|
74
|
+
prop,
|
|
75
|
+
);
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
}
|
|
84
79
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
return new SelectBuilder(this.docClient, fields);
|
|
100
|
-
}
|
|
80
|
+
/**
|
|
81
|
+
* Insert a new record into the database.
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```ts
|
|
85
|
+
* await db.insert(users).values({ id: "1", name: "Alice" }).execute();
|
|
86
|
+
* ```
|
|
87
|
+
*
|
|
88
|
+
* @param table The entity definition to insert into.
|
|
89
|
+
* @returns An InsertBuilder instance.
|
|
90
|
+
*/
|
|
91
|
+
insert<T extends Entity>(table: T): InsertBuilder<T> {
|
|
92
|
+
return new InsertBuilder(table, this.docClient);
|
|
93
|
+
}
|
|
101
94
|
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
return new BatchGetBuilder(this.docClient).items(entity, keys);
|
|
119
|
-
}
|
|
95
|
+
/**
|
|
96
|
+
* Select records from the database.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* const results = await db.select().from(users).where(eq(users.id, "1")).execute();
|
|
101
|
+
* ```
|
|
102
|
+
*
|
|
103
|
+
* @param fields Optional specific fields to select. If omitted, all fields are returned.
|
|
104
|
+
* @returns A SelectBuilder instance.
|
|
105
|
+
*/
|
|
106
|
+
select<TSelection extends SelectedFields>(
|
|
107
|
+
fields?: TSelection,
|
|
108
|
+
): SelectBuilder<TSelection | undefined> {
|
|
109
|
+
return new SelectBuilder(this.docClient, fields);
|
|
110
|
+
}
|
|
120
111
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
batchWrite<T extends Entity>(
|
|
137
|
-
entity: T,
|
|
138
|
-
operations: BatchWriteOperation<T>[],
|
|
139
|
-
) {
|
|
140
|
-
return new BatchWriteBuilder(this.docClient).operations(entity, operations);
|
|
141
|
-
}
|
|
112
|
+
/**
|
|
113
|
+
* Batch get multiple items from the database in a single request.
|
|
114
|
+
*
|
|
115
|
+
* @example
|
|
116
|
+
* ```ts
|
|
117
|
+
* const items = await db.batchGet(users, [{ id: "1" }, { id: "2" }]).execute();
|
|
118
|
+
* ```
|
|
119
|
+
*
|
|
120
|
+
* @param entity The entity definition.
|
|
121
|
+
* @param keys An array of primary key objects to fetch.
|
|
122
|
+
* @returns A BatchGetBuilder instance.
|
|
123
|
+
*/
|
|
124
|
+
batchGet<T extends Entity>(entity: T, keys: Partial<InferSelectModel<T>>[]) {
|
|
125
|
+
return new BatchGetBuilder(this.docClient).items(entity, keys);
|
|
126
|
+
}
|
|
142
127
|
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
128
|
+
/**
|
|
129
|
+
* Batch write (insert or delete) multiple items in a single request.
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* ```ts
|
|
133
|
+
* await db.batchWrite(users, [
|
|
134
|
+
* { type: "put", item: { id: "1", name: "Alice" } },
|
|
135
|
+
* { type: "delete", key: { id: "2" } }
|
|
136
|
+
* ]).execute();
|
|
137
|
+
* ```
|
|
138
|
+
*
|
|
139
|
+
* @param entity The entity definition.
|
|
140
|
+
* @param operations An array of batch operations.
|
|
141
|
+
* @returns A BatchWriteBuilder instance.
|
|
142
|
+
*/
|
|
143
|
+
batchWrite<T extends Entity>(entity: T, operations: BatchWriteOperation<T>[]) {
|
|
144
|
+
return new BatchWriteBuilder(this.docClient).operations(entity, operations);
|
|
145
|
+
}
|
|
150
146
|
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
*
|
|
159
|
-
* @param table The entity definition to update.
|
|
160
|
-
* @returns An UpdateBuilder instance.
|
|
161
|
-
*/
|
|
162
|
-
update<T extends Entity>(table: T): UpdateBuilder<T> {
|
|
163
|
-
return new UpdateBuilder(table, this.docClient);
|
|
164
|
-
}
|
|
147
|
+
/**
|
|
148
|
+
* Start a relational query manually for a specific entity.
|
|
149
|
+
* @internal
|
|
150
|
+
*/
|
|
151
|
+
_query<T extends Entity>(table: T) {
|
|
152
|
+
return new RelationnalQueryBuilder<T>(this.docClient, table);
|
|
153
|
+
}
|
|
165
154
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
keys: Partial<InferInsertModel<T>>,
|
|
181
|
-
): DeleteBuilder<T> {
|
|
182
|
-
return new DeleteBuilder(table, this.docClient, keys as Record<string, unknown>);
|
|
183
|
-
}
|
|
155
|
+
/**
|
|
156
|
+
* Update existing records in the database.
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```ts
|
|
160
|
+
* await db.update(users).set({ name: "Bob" }).where(eq(users.id, "1")).execute();
|
|
161
|
+
* ```
|
|
162
|
+
*
|
|
163
|
+
* @param table The entity definition to update.
|
|
164
|
+
* @returns An UpdateBuilder instance.
|
|
165
|
+
*/
|
|
166
|
+
update<T extends Entity>(table: T): UpdateBuilder<T> {
|
|
167
|
+
return new UpdateBuilder(table, this.docClient);
|
|
168
|
+
}
|
|
184
169
|
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
170
|
+
/**
|
|
171
|
+
* Delete records from the database.
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```ts
|
|
175
|
+
* await db.delete(users, { id: "1" }).execute();
|
|
176
|
+
* ```
|
|
177
|
+
*
|
|
178
|
+
* @param table The entity definition to delete from.
|
|
179
|
+
* @param keys The primary key(s) of the item to delete.
|
|
180
|
+
* @returns A DeleteBuilder instance.
|
|
181
|
+
*/
|
|
182
|
+
delete<T extends Entity>(table: T, keys: Partial<InferInsertModel<T>>): DeleteBuilder<T> {
|
|
183
|
+
return new DeleteBuilder(table, this.docClient, keys as Record<string, unknown>);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Execute multiple operations atomically in a single DynamoDB transaction.
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* ```ts
|
|
191
|
+
* await db.transaction("unique-token", (tx) => [
|
|
192
|
+
* tx.insert(users).values({ id: "1", name: "Alice" }),
|
|
193
|
+
* tx.update(stats).set({ count: sql`${stats.count} + 1` }).where(eq(stats.id, "global"))
|
|
194
|
+
* ]);
|
|
195
|
+
* ```
|
|
196
|
+
*
|
|
197
|
+
* @param token A unique client request token for idempotency.
|
|
198
|
+
* @param callback A function that receives a transaction proxy and returns an array of operations.
|
|
199
|
+
* @returns A promise that resolves when the transaction completes.
|
|
200
|
+
* @throws Error if the transaction is cancelled or fails.
|
|
201
|
+
*/
|
|
202
|
+
async transaction(
|
|
203
|
+
token: string,
|
|
204
|
+
callback: (
|
|
205
|
+
tx: TransactionProxy,
|
|
206
|
+
) => BaseBuilder<Entity, unknown>[] | Promise<BaseBuilder<Entity, unknown>[]>,
|
|
207
|
+
): Promise<void> {
|
|
208
|
+
const proxy = new TransactionProxy(this.docClient);
|
|
209
|
+
const operations = await callback(proxy);
|
|
212
210
|
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
await executor.execute(token, operations);
|
|
211
|
+
if (operations.length === 0) return;
|
|
212
|
+
if (operations.length > 100) {
|
|
213
|
+
throw new Error("DynamoDB transactions are limited to 100 items.");
|
|
217
214
|
}
|
|
215
|
+
|
|
216
|
+
// Executor will be implemented in transaction.ts
|
|
217
|
+
const { TransactionExecutor } = await import("./builders/transaction");
|
|
218
|
+
const executor = new TransactionExecutor(this.docClient);
|
|
219
|
+
await executor.execute(token, operations);
|
|
220
|
+
}
|
|
218
221
|
}
|
|
219
222
|
|
|
220
223
|
/**
|
|
221
224
|
* Configuration for initializing Mizzle.
|
|
222
225
|
*/
|
|
223
|
-
export interface MizzleConfig<TSchema extends Record<string,
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
226
|
+
export interface MizzleConfig<TSchema extends Record<string, any> = Record<string, any>> {
|
|
227
|
+
/**
|
|
228
|
+
* AWS DynamoDB Client instance from `@aws-sdk/client-dynamodb`.
|
|
229
|
+
*/
|
|
230
|
+
client: DynamoDBClient;
|
|
231
|
+
/**
|
|
232
|
+
* Relational schema definition for using `db.query`.
|
|
233
|
+
*/
|
|
234
|
+
relations?: TSchema;
|
|
235
|
+
/**
|
|
236
|
+
* Optional retry configuration for transient DynamoDB errors.
|
|
237
|
+
*/
|
|
238
|
+
retry?: Partial<RetryConfig>;
|
|
236
239
|
}
|
|
237
240
|
|
|
238
241
|
/**
|
|
239
242
|
* Initializes a Mizzle database instance.
|
|
240
|
-
*
|
|
243
|
+
*
|
|
241
244
|
* @example
|
|
242
245
|
* ```ts
|
|
243
246
|
* // Basic initialization
|
|
244
247
|
* const db = mizzle(new DynamoDBClient({}));
|
|
245
|
-
*
|
|
248
|
+
*
|
|
246
249
|
* // Initialization with relational schema
|
|
247
|
-
* const db = mizzle({
|
|
250
|
+
* const db = mizzle({
|
|
248
251
|
* client: new DynamoDBClient({}),
|
|
249
|
-
* relations: { users, posts }
|
|
252
|
+
* relations: { users, posts }
|
|
250
253
|
* });
|
|
251
254
|
* ```
|
|
252
|
-
*
|
|
255
|
+
*
|
|
253
256
|
* @param config A DynamoDBClient instance or a MizzleConfig object.
|
|
254
257
|
* @returns A DynamoDB instance for performing database operations.
|
|
255
258
|
*/
|
|
256
|
-
export function mizzle<TSchema extends Record<string,
|
|
257
|
-
|
|
259
|
+
export function mizzle<TSchema extends Record<string, any> = Record<string, any>>(
|
|
260
|
+
config: DynamoDBClient | MizzleConfig<TSchema>,
|
|
258
261
|
): DynamoDB<TSchema> {
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
262
|
+
if (config instanceof DynamoDBClient) {
|
|
263
|
+
return new DynamoDB(config);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
if ("client" in config && config.client instanceof DynamoDBClient) {
|
|
267
|
+
return new DynamoDB(config.client, config.relations, config.retry);
|
|
268
|
+
}
|
|
269
|
+
// Fallback for cases where instanceof might fail due to multiple SDK versions
|
|
270
|
+
if ("client" in config && config.client) {
|
|
271
|
+
return new DynamoDB(config.client as DynamoDBClient, config.relations, config.retry);
|
|
272
|
+
}
|
|
273
|
+
return new DynamoDB(config as unknown as DynamoDBClient);
|
|
271
274
|
}
|
|
@@ -1,66 +1,66 @@
|
|
|
1
1
|
export type ActionType = "SET" | "ADD" | "REMOVE" | "DELETE";
|
|
2
2
|
|
|
3
3
|
export abstract class UpdateAction {
|
|
4
|
-
|
|
4
|
+
abstract readonly action: ActionType;
|
|
5
5
|
}
|
|
6
6
|
|
|
7
7
|
export class SetAction extends UpdateAction {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
8
|
+
readonly action = "SET";
|
|
9
|
+
constructor(
|
|
10
|
+
readonly value: unknown,
|
|
11
|
+
readonly functionName?: "list_append" | "if_not_exists",
|
|
12
|
+
readonly usePathAsFirstArg: boolean = false,
|
|
13
|
+
) {
|
|
14
|
+
super();
|
|
15
|
+
}
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
export class AddAction extends UpdateAction {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
19
|
+
readonly action = "ADD";
|
|
20
|
+
constructor(readonly value: unknown) {
|
|
21
|
+
super();
|
|
22
|
+
}
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
export class RemoveAction extends UpdateAction {
|
|
26
|
-
|
|
26
|
+
readonly action = "REMOVE";
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
export class DeleteAction extends UpdateAction {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
30
|
+
readonly action = "DELETE";
|
|
31
|
+
constructor(readonly value: unknown) {
|
|
32
|
+
super();
|
|
33
|
+
}
|
|
34
34
|
}
|
|
35
35
|
|
|
36
36
|
export function add(value: number | Set<unknown>) {
|
|
37
|
-
|
|
37
|
+
return new AddAction(value);
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
/**
|
|
41
41
|
* Maps to list_append(path, :value)
|
|
42
42
|
*/
|
|
43
43
|
export function append(value: unknown[]) {
|
|
44
|
-
|
|
44
|
+
return new SetAction(value, "list_append", true);
|
|
45
45
|
}
|
|
46
46
|
|
|
47
47
|
/**
|
|
48
48
|
* Maps to if_not_exists(path, :value)
|
|
49
49
|
*/
|
|
50
50
|
export function ifNotExists(value: unknown) {
|
|
51
|
-
|
|
51
|
+
return new SetAction(value, "if_not_exists", true);
|
|
52
52
|
}
|
|
53
53
|
|
|
54
54
|
export function remove() {
|
|
55
|
-
|
|
55
|
+
return new RemoveAction();
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
export function addToSet(values: Set<unknown> | unknown[]) {
|
|
59
|
-
|
|
60
|
-
|
|
59
|
+
const value = Array.isArray(values) ? new Set(values) : values;
|
|
60
|
+
return new AddAction(value);
|
|
61
61
|
}
|
|
62
62
|
|
|
63
63
|
export function deleteFromSet(values: Set<unknown> | unknown[]) {
|
|
64
|
-
|
|
65
|
-
|
|
64
|
+
const value = Array.isArray(values) ? new Set(values) : values;
|
|
65
|
+
return new DeleteAction(value);
|
|
66
66
|
}
|