@aurios/mizzle 1.0.0 → 1.1.0

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/src/db.ts CHANGED
@@ -69,6 +69,14 @@ export class DynamoDB<TSchema extends Record<string, unknown> = Record<string, u
69
69
 
70
70
  /**
71
71
  * Insert a new record into the database.
72
+ *
73
+ * @example
74
+ * ```ts
75
+ * await db.insert(users).values({ id: "1", name: "Alice" }).execute();
76
+ * ```
77
+ *
78
+ * @param table The entity definition to insert into.
79
+ * @returns An InsertBuilder instance.
72
80
  */
73
81
  insert<T extends Entity>(table: T): InsertBuilder<T> {
74
82
  return new InsertBuilder(table, this.docClient);
@@ -76,6 +84,14 @@ export class DynamoDB<TSchema extends Record<string, unknown> = Record<string, u
76
84
 
77
85
  /**
78
86
  * Select records from the database.
87
+ *
88
+ * @example
89
+ * ```ts
90
+ * const results = await db.select().from(users).where(eq(users.id, "1")).execute();
91
+ * ```
92
+ *
93
+ * @param fields Optional specific fields to select. If omitted, all fields are returned.
94
+ * @returns A SelectBuilder instance.
79
95
  */
80
96
  select<TSelection extends SelectedFields>(
81
97
  fields?: TSelection,
@@ -84,7 +100,16 @@ export class DynamoDB<TSchema extends Record<string, unknown> = Record<string, u
84
100
  }
85
101
 
86
102
  /**
87
- * Batch get items from the database.
103
+ * Batch get multiple items from the database in a single request.
104
+ *
105
+ * @example
106
+ * ```ts
107
+ * const items = await db.batchGet(users, [{ id: "1" }, { id: "2" }]).execute();
108
+ * ```
109
+ *
110
+ * @param entity The entity definition.
111
+ * @param keys An array of primary key objects to fetch.
112
+ * @returns A BatchGetBuilder instance.
88
113
  */
89
114
  batchGet<T extends Entity>(
90
115
  entity: T,
@@ -94,7 +119,19 @@ export class DynamoDB<TSchema extends Record<string, unknown> = Record<string, u
94
119
  }
95
120
 
96
121
  /**
97
- * Batch write items to the database.
122
+ * Batch write (insert or delete) multiple items in a single request.
123
+ *
124
+ * @example
125
+ * ```ts
126
+ * await db.batchWrite(users, [
127
+ * { type: "put", item: { id: "1", name: "Alice" } },
128
+ * { type: "delete", key: { id: "2" } }
129
+ * ]).execute();
130
+ * ```
131
+ *
132
+ * @param entity The entity definition.
133
+ * @param operations An array of batch operations.
134
+ * @returns A BatchWriteBuilder instance.
98
135
  */
99
136
  batchWrite<T extends Entity>(
100
137
  entity: T,
@@ -113,6 +150,14 @@ export class DynamoDB<TSchema extends Record<string, unknown> = Record<string, u
113
150
 
114
151
  /**
115
152
  * Update existing records in the database.
153
+ *
154
+ * @example
155
+ * ```ts
156
+ * await db.update(users).set({ name: "Bob" }).where(eq(users.id, "1")).execute();
157
+ * ```
158
+ *
159
+ * @param table The entity definition to update.
160
+ * @returns An UpdateBuilder instance.
116
161
  */
117
162
  update<T extends Entity>(table: T): UpdateBuilder<T> {
118
163
  return new UpdateBuilder(table, this.docClient);
@@ -120,6 +165,15 @@ export class DynamoDB<TSchema extends Record<string, unknown> = Record<string, u
120
165
 
121
166
  /**
122
167
  * Delete records from the database.
168
+ *
169
+ * @example
170
+ * ```ts
171
+ * await db.delete(users, { id: "1" }).execute();
172
+ * ```
173
+ *
174
+ * @param table The entity definition to delete from.
175
+ * @param keys The primary key(s) of the item to delete.
176
+ * @returns A DeleteBuilder instance.
123
177
  */
124
178
  delete<T extends Entity>(
125
179
  table: T,
@@ -129,10 +183,20 @@ export class DynamoDB<TSchema extends Record<string, unknown> = Record<string, u
129
183
  }
130
184
 
131
185
  /**
132
- * Execute multiple operations atomically in a transaction.
186
+ * Execute multiple operations atomically in a single DynamoDB transaction.
133
187
  *
134
- * @param token ClientRequestToken for idempotency.
135
- * @param callback Callback that returns an array of operations.
188
+ * @example
189
+ * ```ts
190
+ * await db.transaction("unique-token", (tx) => [
191
+ * tx.insert(users).values({ id: "1", name: "Alice" }),
192
+ * tx.update(stats).set({ count: sql`${stats.count} + 1` }).where(eq(stats.id, "global"))
193
+ * ]);
194
+ * ```
195
+ *
196
+ * @param token A unique client request token for idempotency.
197
+ * @param callback A function that receives a transaction proxy and returns an array of operations.
198
+ * @returns A promise that resolves when the transaction completes.
199
+ * @throws Error if the transaction is cancelled or fails.
136
200
  */
137
201
  async transaction(
138
202
  token: string,
@@ -158,28 +222,36 @@ export class DynamoDB<TSchema extends Record<string, unknown> = Record<string, u
158
222
  */
159
223
  export interface MizzleConfig<TSchema extends Record<string, unknown> = Record<string, unknown>> {
160
224
  /**
161
- * AWS DynamoDB Client.
225
+ * AWS DynamoDB Client instance from `@aws-sdk/client-dynamodb`.
162
226
  */
163
227
  client: DynamoDBClient;
164
228
  /**
165
- * Relational schema definition.
229
+ * Relational schema definition for using `db.query`.
166
230
  */
167
231
  relations?: TSchema;
168
232
  /**
169
- * Retry configuration for transient errors.
233
+ * Optional retry configuration for transient DynamoDB errors.
170
234
  */
171
235
  retry?: Partial<RetryConfig>;
172
236
  }
173
237
 
174
238
  /**
175
- * Initialize Mizzle with a DynamoDB client or a configuration object.
239
+ * Initializes a Mizzle database instance.
176
240
  *
177
241
  * @example
178
242
  * ```ts
179
- * const db = mizzle(client);
180
- * // or
181
- * const db = mizzle({ client, relations });
243
+ * // Basic initialization
244
+ * const db = mizzle(new DynamoDBClient({}));
245
+ *
246
+ * // Initialization with relational schema
247
+ * const db = mizzle({
248
+ * client: new DynamoDBClient({}),
249
+ * relations: { users, posts }
250
+ * });
182
251
  * ```
252
+ *
253
+ * @param config A DynamoDBClient instance or a MizzleConfig object.
254
+ * @returns A DynamoDB instance for performing database operations.
183
255
  */
184
256
  export function mizzle<TSchema extends Record<string, unknown> = Record<string, unknown>>(
185
257
  config: DynamoDBClient | MizzleConfig<TSchema>