@celerity-sdk/datastore 0.3.1 → 0.4.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/README.md +5 -5
- package/dist/index.cjs +657 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +381 -1
- package/dist/index.d.ts +381 -1
- package/dist/index.js +631 -0
- package/dist/index.js.map +1 -1
- package/package.json +18 -7
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,382 @@
|
|
|
1
|
+
import { Closeable, CelerityTracer, ServiceContainer, CelerityLayer, BaseHandlerContext } from '@celerity-sdk/types';
|
|
1
2
|
|
|
2
|
-
|
|
3
|
+
declare const DatastoreClient: unique symbol;
|
|
4
|
+
/**
|
|
5
|
+
* A data store client abstraction for NoSQL databases. Provides access to named
|
|
6
|
+
* data stores (tables/collections), each representing a logical container for items.
|
|
7
|
+
*/
|
|
8
|
+
interface DatastoreClient extends Closeable {
|
|
9
|
+
/**
|
|
10
|
+
* Retrieves a datastore instance by its logical name. The returned datastore
|
|
11
|
+
* is a lightweight handle — no network calls are made until an operation is invoked.
|
|
12
|
+
*
|
|
13
|
+
* @param name The name of the datastore (table/collection).
|
|
14
|
+
*/
|
|
15
|
+
datastore(name: string): Datastore$1;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* A datastore represents a logical container (table/collection) for items in a
|
|
19
|
+
* NoSQL data store. It provides methods for performing CRUD operations, queries,
|
|
20
|
+
* scans, and batch operations within the data store.
|
|
21
|
+
*/
|
|
22
|
+
interface Datastore$1 {
|
|
23
|
+
/**
|
|
24
|
+
* Retrieve an item from the data store by its primary key. Returns `null` if the
|
|
25
|
+
* item does not exist.
|
|
26
|
+
*
|
|
27
|
+
* @param key The primary key attributes identifying the item.
|
|
28
|
+
* @param options Optional parameters such as consistent read.
|
|
29
|
+
* @returns A promise that resolves to the item, or `null` if not found.
|
|
30
|
+
*/
|
|
31
|
+
getItem<T = Record<string, unknown>>(key: ItemKey, options?: GetItemOptions): Promise<T | null>;
|
|
32
|
+
/**
|
|
33
|
+
* Store an item in the data store. If an item with the same primary key already
|
|
34
|
+
* exists, it is replaced entirely (upsert semantics).
|
|
35
|
+
*
|
|
36
|
+
* @param item The item to store. Must include all primary key attributes.
|
|
37
|
+
* @param options Optional parameters such as condition expressions.
|
|
38
|
+
*/
|
|
39
|
+
putItem(item: Record<string, unknown>, options?: PutItemOptions): Promise<void>;
|
|
40
|
+
/**
|
|
41
|
+
* Delete an item from the data store by its primary key. This operation is
|
|
42
|
+
* idempotent — deleting a non-existent key does not throw.
|
|
43
|
+
*
|
|
44
|
+
* @param key The primary key attributes identifying the item to delete.
|
|
45
|
+
* @param options Optional parameters such as condition expressions.
|
|
46
|
+
*/
|
|
47
|
+
deleteItem(key: ItemKey, options?: DeleteItemOptions): Promise<void>;
|
|
48
|
+
/**
|
|
49
|
+
* Query items by primary key with optional range conditions. Returns an
|
|
50
|
+
* {@link ItemListing} that handles pagination transparently, allowing the caller
|
|
51
|
+
* to iterate over all matching items without managing page tokens.
|
|
52
|
+
*
|
|
53
|
+
* @param options Query parameters including key condition, range conditions,
|
|
54
|
+
* filters, and pagination controls.
|
|
55
|
+
* @returns An item listing that yields items and exposes a cursor for resuming.
|
|
56
|
+
*/
|
|
57
|
+
query<T = Record<string, unknown>>(options: QueryOptions): ItemListing<T>;
|
|
58
|
+
/**
|
|
59
|
+
* Perform a full scan of the data store. Returns an {@link ItemListing} that
|
|
60
|
+
* handles pagination transparently. Scans read every item in the table and
|
|
61
|
+
* should be used sparingly.
|
|
62
|
+
*
|
|
63
|
+
* @param options Optional scan parameters such as filters and pagination controls.
|
|
64
|
+
* @returns An item listing that yields items and exposes a cursor for resuming.
|
|
65
|
+
*/
|
|
66
|
+
scan<T = Record<string, unknown>>(options?: ScanOptions): ItemListing<T>;
|
|
67
|
+
/**
|
|
68
|
+
* Retrieve multiple items by their primary keys in a single batch operation.
|
|
69
|
+
* The batch may be split into multiple requests if it exceeds provider limits.
|
|
70
|
+
*
|
|
71
|
+
* @param keys The primary keys of the items to retrieve.
|
|
72
|
+
* @param options Optional parameters such as consistent read.
|
|
73
|
+
* @returns A promise that resolves to the batch get result, including retrieved
|
|
74
|
+
* items and any unprocessed keys.
|
|
75
|
+
*/
|
|
76
|
+
batchGetItems<T = Record<string, unknown>>(keys: ItemKey[], options?: BatchGetItemsOptions): Promise<BatchGetResult<T>>;
|
|
77
|
+
/**
|
|
78
|
+
* Perform multiple put and delete operations in a single batch. The batch may
|
|
79
|
+
* be split into multiple requests if it exceeds provider limits.
|
|
80
|
+
*
|
|
81
|
+
* @param operations The batch of put and delete operations to execute.
|
|
82
|
+
* @returns A promise that resolves to the batch write result, including any
|
|
83
|
+
* unprocessed operations.
|
|
84
|
+
*/
|
|
85
|
+
batchWriteItems(operations: BatchWriteOperation[]): Promise<BatchWriteResult>;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Primary key attributes for an item. Keys can be string or number values
|
|
89
|
+
* corresponding to primary key and optional range key attributes.
|
|
90
|
+
*/
|
|
91
|
+
type ItemKey = Record<string, string | number>;
|
|
92
|
+
/**
|
|
93
|
+
* A condition on the primary key — always an equality match.
|
|
94
|
+
*/
|
|
95
|
+
type KeyCondition = {
|
|
96
|
+
/** The attribute name of the primary key. */
|
|
97
|
+
name: string;
|
|
98
|
+
/** The primary key value to match. */
|
|
99
|
+
value: string | number;
|
|
100
|
+
};
|
|
101
|
+
/**
|
|
102
|
+
* A condition on the range key. Supports equality, comparison, range,
|
|
103
|
+
* and prefix matching operations.
|
|
104
|
+
*/
|
|
105
|
+
type RangeCondition = {
|
|
106
|
+
/** The attribute name of the range key. */
|
|
107
|
+
name: string;
|
|
108
|
+
} & ({
|
|
109
|
+
operator: "eq";
|
|
110
|
+
value: string | number;
|
|
111
|
+
} | {
|
|
112
|
+
operator: "lt";
|
|
113
|
+
value: string | number;
|
|
114
|
+
} | {
|
|
115
|
+
operator: "le";
|
|
116
|
+
value: string | number;
|
|
117
|
+
} | {
|
|
118
|
+
operator: "gt";
|
|
119
|
+
value: string | number;
|
|
120
|
+
} | {
|
|
121
|
+
operator: "ge";
|
|
122
|
+
value: string | number;
|
|
123
|
+
} | {
|
|
124
|
+
operator: "between";
|
|
125
|
+
low: string | number;
|
|
126
|
+
high: string | number;
|
|
127
|
+
} | {
|
|
128
|
+
operator: "startsWith";
|
|
129
|
+
value: string;
|
|
130
|
+
});
|
|
131
|
+
/**
|
|
132
|
+
* A filter condition on an item attribute. Used for post-read filtering
|
|
133
|
+
* in queries and scans, and for conditional writes.
|
|
134
|
+
*
|
|
135
|
+
* Only includes operators that map cleanly across all target providers
|
|
136
|
+
* (DynamoDB, Firestore, Cosmos DB). The `not_exists` operator is
|
|
137
|
+
* provider-specific and available through the concrete provider classes.
|
|
138
|
+
*/
|
|
139
|
+
type Condition = {
|
|
140
|
+
/** The attribute name to evaluate. */
|
|
141
|
+
name: string;
|
|
142
|
+
} & ({
|
|
143
|
+
operator: "eq";
|
|
144
|
+
value: unknown;
|
|
145
|
+
} | {
|
|
146
|
+
operator: "ne";
|
|
147
|
+
value: unknown;
|
|
148
|
+
} | {
|
|
149
|
+
operator: "lt";
|
|
150
|
+
value: string | number;
|
|
151
|
+
} | {
|
|
152
|
+
operator: "le";
|
|
153
|
+
value: string | number;
|
|
154
|
+
} | {
|
|
155
|
+
operator: "gt";
|
|
156
|
+
value: string | number;
|
|
157
|
+
} | {
|
|
158
|
+
operator: "ge";
|
|
159
|
+
value: string | number;
|
|
160
|
+
} | {
|
|
161
|
+
operator: "between";
|
|
162
|
+
low: string | number;
|
|
163
|
+
high: string | number;
|
|
164
|
+
} | {
|
|
165
|
+
operator: "startsWith";
|
|
166
|
+
value: string;
|
|
167
|
+
} | {
|
|
168
|
+
operator: "contains";
|
|
169
|
+
value: string | number;
|
|
170
|
+
} | {
|
|
171
|
+
operator: "exists";
|
|
172
|
+
});
|
|
173
|
+
/**
|
|
174
|
+
* One or more conditions AND'd together. A single {@link Condition} or an array
|
|
175
|
+
* of Conditions — when multiple are provided, all must be true.
|
|
176
|
+
*/
|
|
177
|
+
type ConditionExpression = Condition | Condition[];
|
|
178
|
+
/**
|
|
179
|
+
* Options for the getItem operation.
|
|
180
|
+
*/
|
|
181
|
+
type GetItemOptions = {
|
|
182
|
+
/** When true, performs a strongly consistent read. Default is eventually consistent. */
|
|
183
|
+
consistentRead?: boolean;
|
|
184
|
+
};
|
|
185
|
+
/**
|
|
186
|
+
* Options for the putItem operation.
|
|
187
|
+
*/
|
|
188
|
+
type PutItemOptions = {
|
|
189
|
+
/**
|
|
190
|
+
* Condition that must be met for the put to succeed. Throws
|
|
191
|
+
* {@link ConditionalCheckFailedError} if the condition is not met.
|
|
192
|
+
*/
|
|
193
|
+
condition?: ConditionExpression;
|
|
194
|
+
};
|
|
195
|
+
/**
|
|
196
|
+
* Options for the deleteItem operation.
|
|
197
|
+
*/
|
|
198
|
+
type DeleteItemOptions = {
|
|
199
|
+
/**
|
|
200
|
+
* Condition that must be met for the delete to succeed. Throws
|
|
201
|
+
* {@link ConditionalCheckFailedError} if the condition is not met.
|
|
202
|
+
*/
|
|
203
|
+
condition?: ConditionExpression;
|
|
204
|
+
};
|
|
205
|
+
/**
|
|
206
|
+
* Options for the query operation.
|
|
207
|
+
*/
|
|
208
|
+
type QueryOptions = {
|
|
209
|
+
/** The primary key condition (required for queries). */
|
|
210
|
+
key: KeyCondition;
|
|
211
|
+
/** Optional range key condition to narrow the query range. */
|
|
212
|
+
range?: RangeCondition;
|
|
213
|
+
/** Optional filter conditions applied after the query read (does not reduce read capacity). */
|
|
214
|
+
filter?: ConditionExpression;
|
|
215
|
+
/** Query an index instead of the base table. */
|
|
216
|
+
indexName?: string;
|
|
217
|
+
/** When true, results are returned in ascending key order (default). Set to false for descending. */
|
|
218
|
+
sortAscending?: boolean;
|
|
219
|
+
/** Maximum number of items to return per internal page fetch. */
|
|
220
|
+
maxResults?: number;
|
|
221
|
+
/** Opaque cursor token to resume a previous query from where it left off. */
|
|
222
|
+
cursor?: string;
|
|
223
|
+
/** When true, performs a strongly consistent read (not supported on secondary indexes). */
|
|
224
|
+
consistentRead?: boolean;
|
|
225
|
+
};
|
|
226
|
+
/**
|
|
227
|
+
* Options for the scan operation.
|
|
228
|
+
*/
|
|
229
|
+
type ScanOptions = {
|
|
230
|
+
/** Optional filter conditions applied after the scan read. */
|
|
231
|
+
filter?: ConditionExpression;
|
|
232
|
+
/** Scan an index instead of the base table. */
|
|
233
|
+
indexName?: string;
|
|
234
|
+
/** Maximum number of items to return per internal page fetch. */
|
|
235
|
+
maxResults?: number;
|
|
236
|
+
/** Opaque cursor token to resume a previous scan from where it left off. */
|
|
237
|
+
cursor?: string;
|
|
238
|
+
/** When true, performs a strongly consistent read. */
|
|
239
|
+
consistentRead?: boolean;
|
|
240
|
+
};
|
|
241
|
+
/**
|
|
242
|
+
* An async iterable of items that also exposes a cursor token for resuming
|
|
243
|
+
* iteration from the current position. The cursor is updated as pages are fetched
|
|
244
|
+
* and encodes enough state to resume from the exact position across all providers.
|
|
245
|
+
*/
|
|
246
|
+
interface ItemListing<T> extends AsyncIterable<T> {
|
|
247
|
+
/**
|
|
248
|
+
* A cursor token representing the current position in the listing. This token
|
|
249
|
+
* can be passed to a subsequent query or scan call to resume iteration from
|
|
250
|
+
* this position. The value is `undefined` before iteration begins or after all
|
|
251
|
+
* items have been yielded.
|
|
252
|
+
*/
|
|
253
|
+
readonly cursor: string | undefined;
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Options for the batchGetItems operation.
|
|
257
|
+
*/
|
|
258
|
+
type BatchGetItemsOptions = {
|
|
259
|
+
/** When true, performs strongly consistent reads for all items. */
|
|
260
|
+
consistentRead?: boolean;
|
|
261
|
+
};
|
|
262
|
+
/**
|
|
263
|
+
* The result of a batch get operation.
|
|
264
|
+
*/
|
|
265
|
+
type BatchGetResult<T> = {
|
|
266
|
+
/** The successfully retrieved items. */
|
|
267
|
+
items: T[];
|
|
268
|
+
/** Keys that could not be processed in this batch (caller should retry). */
|
|
269
|
+
unprocessedKeys: ItemKey[];
|
|
270
|
+
};
|
|
271
|
+
/**
|
|
272
|
+
* A single operation in a batch write request.
|
|
273
|
+
*/
|
|
274
|
+
type BatchWriteOperation = {
|
|
275
|
+
type: "put";
|
|
276
|
+
item: Record<string, unknown>;
|
|
277
|
+
} | {
|
|
278
|
+
type: "delete";
|
|
279
|
+
key: ItemKey;
|
|
280
|
+
};
|
|
281
|
+
/**
|
|
282
|
+
* The result of a batch write operation.
|
|
283
|
+
*/
|
|
284
|
+
type BatchWriteResult = {
|
|
285
|
+
/** Operations that could not be processed in this batch (caller should retry). */
|
|
286
|
+
unprocessedOperations: BatchWriteOperation[];
|
|
287
|
+
};
|
|
288
|
+
|
|
289
|
+
type DynamoDBDatastoreConfig = {
|
|
290
|
+
/** AWS region for the DynamoDB client. */
|
|
291
|
+
region?: string;
|
|
292
|
+
/** Override endpoint for DynamoDB-compatible services (LocalStack). */
|
|
293
|
+
endpoint?: string;
|
|
294
|
+
/** AWS credentials override. Omit to use the default credential chain. */
|
|
295
|
+
credentials?: {
|
|
296
|
+
accessKeyId: string;
|
|
297
|
+
secretAccessKey: string;
|
|
298
|
+
};
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
declare class DynamoDBDatastoreClient implements DatastoreClient {
|
|
302
|
+
private readonly tracer?;
|
|
303
|
+
private client;
|
|
304
|
+
private docClient;
|
|
305
|
+
private readonly config;
|
|
306
|
+
constructor(config?: DynamoDBDatastoreConfig, tracer?: CelerityTracer | undefined);
|
|
307
|
+
datastore(name: string): Datastore$1;
|
|
308
|
+
close(): void;
|
|
309
|
+
private getDocClient;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
type CreateDatastoreClientOptions = {
|
|
313
|
+
/** Override provider selection. If omitted, derived from platform config. */
|
|
314
|
+
provider?: "aws" | "local" | "gcp" | "azure";
|
|
315
|
+
/** Cloud deploy target for local environments (e.g. "aws", "gcloud", "azure"). */
|
|
316
|
+
deployTarget?: string;
|
|
317
|
+
/** DynamoDB-specific configuration overrides. */
|
|
318
|
+
aws?: DynamoDBDatastoreConfig;
|
|
319
|
+
/** Optional tracer for Celerity-level span instrumentation. */
|
|
320
|
+
tracer?: CelerityTracer;
|
|
321
|
+
};
|
|
322
|
+
declare function createDatastoreClient(options?: CreateDatastoreClientOptions): DatastoreClient;
|
|
323
|
+
|
|
324
|
+
declare function datastoreToken(resourceName: string): symbol;
|
|
325
|
+
declare const DEFAULT_DATASTORE_TOKEN: unique symbol;
|
|
326
|
+
interface Datastore extends Datastore$1 {
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* Parameter decorator that injects a {@link Datastore} instance for the given
|
|
330
|
+
* blueprint resource. Writes both DI injection metadata and CLI resource-ref
|
|
331
|
+
* metadata using well-known `Symbol.for()` keys (no dependency on core).
|
|
332
|
+
*
|
|
333
|
+
* When `resourceName` is omitted, the default datastore token is used — this
|
|
334
|
+
* auto-resolves when exactly one datastore resource exists.
|
|
335
|
+
*
|
|
336
|
+
* @example
|
|
337
|
+
* ```ts
|
|
338
|
+
* @Controller("/users")
|
|
339
|
+
* class UserController {
|
|
340
|
+
* constructor(@Datastore("usersTable") private users: Datastore) {}
|
|
341
|
+
* }
|
|
342
|
+
* ```
|
|
343
|
+
*/
|
|
344
|
+
declare function Datastore(resourceName?: string): ParameterDecorator;
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Resolves a {@link Datastore} instance from the DI container.
|
|
348
|
+
* For function-based handlers where parameter decorators aren't available.
|
|
349
|
+
*
|
|
350
|
+
* @param container - The service container (typically `context.container`).
|
|
351
|
+
* @param resourceName - The blueprint resource name. Omit when exactly one
|
|
352
|
+
* datastore resource exists to use the default.
|
|
353
|
+
*
|
|
354
|
+
* @example
|
|
355
|
+
* ```ts
|
|
356
|
+
* const handler = createHttpHandler(async (req, ctx) => {
|
|
357
|
+
* const users = await getDatastore(ctx.container, "usersTable");
|
|
358
|
+
* const user = await users.getItem({ pk: req.pathParameters.userId });
|
|
359
|
+
* });
|
|
360
|
+
* ```
|
|
361
|
+
*/
|
|
362
|
+
declare function getDatastore(container: ServiceContainer, resourceName?: string): Promise<Datastore$1>;
|
|
363
|
+
|
|
364
|
+
declare class DatastoreLayer implements CelerityLayer<BaseHandlerContext> {
|
|
365
|
+
private initialized;
|
|
366
|
+
private config;
|
|
367
|
+
handle(context: BaseHandlerContext, next: () => Promise<unknown>): Promise<unknown>;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
declare class DatastoreError extends Error {
|
|
371
|
+
readonly table: string;
|
|
372
|
+
constructor(message: string, table: string, options?: {
|
|
373
|
+
cause?: unknown;
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
declare class ConditionalCheckFailedError extends DatastoreError {
|
|
377
|
+
constructor(table: string, options?: {
|
|
378
|
+
cause?: unknown;
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
export { type BatchGetItemsOptions, type BatchGetResult, type BatchWriteOperation, type BatchWriteResult, type Condition, type ConditionExpression, ConditionalCheckFailedError, type CreateDatastoreClientOptions, DEFAULT_DATASTORE_TOKEN, Datastore, DatastoreClient, DatastoreError, DatastoreLayer, type DeleteItemOptions, DynamoDBDatastoreClient, type DynamoDBDatastoreConfig, type GetItemOptions, type ItemKey, type ItemListing, type KeyCondition, type PutItemOptions, type QueryOptions, type RangeCondition, type ScanOptions, createDatastoreClient, datastoreToken, getDatastore };
|