fluxor-cloud-db 1.0.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/.github/workflows/npm-publish.yml +34 -0
- package/README.md +296 -0
- package/dist/index.d.mts +347 -0
- package/dist/index.d.ts +347 -0
- package/dist/index.js +1844 -0
- package/dist/index.mjs +1811 -0
- package/jest.config.ts +12 -0
- package/package.json +33 -0
- package/src/contracts/database-adapter.ts +161 -0
- package/src/dynamo/dynamo.ts +859 -0
- package/src/dynamo/dynamo.types.ts +8 -0
- package/src/fluentapi.ts +71 -0
- package/src/index.ts +39 -0
- package/src/mongo/mongo.ts +690 -0
- package/src/types/error.ts +13 -0
- package/src/types/query.ts +53 -0
- package/tests/dynamodb.test.ts +547 -0
- package/tests/mongodb.test.ts +486 -0
- package/tsconfig.json +23 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
type DynamoConfig = {
|
|
2
|
+
region: string;
|
|
3
|
+
endpoint: string;
|
|
4
|
+
accessKeyId?: string;
|
|
5
|
+
secretAccessKey?: string;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
type QueryOperator = "=" | "!=" | ">" | ">=" | "<" | "<=" | "IN" | "NOT_IN" | "BETWEEN" | "LIKE" | "EXISTS" | "BEGINS_WITH";
|
|
9
|
+
/**
|
|
10
|
+
* Represents a single condition in a WHERE clause
|
|
11
|
+
*/
|
|
12
|
+
interface QueryCondition {
|
|
13
|
+
operator: QueryOperator;
|
|
14
|
+
value: any;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Represents a single field condition or nested conditions
|
|
18
|
+
*/
|
|
19
|
+
interface WhereClause {
|
|
20
|
+
[key: string]: QueryCondition | QueryCondition[] | WhereClause;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Represents update operations on specific fields
|
|
24
|
+
*/
|
|
25
|
+
interface UpdateClause {
|
|
26
|
+
[key: string]: any;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Configuration for query operations
|
|
30
|
+
*/
|
|
31
|
+
interface QueryOptions {
|
|
32
|
+
limit?: number;
|
|
33
|
+
offset?: number;
|
|
34
|
+
orderBy?: {
|
|
35
|
+
field: string;
|
|
36
|
+
direction: "ASC" | "DESC";
|
|
37
|
+
}[];
|
|
38
|
+
projection?: string[];
|
|
39
|
+
consistent?: boolean;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Result wrapper for paginated responses
|
|
43
|
+
*/
|
|
44
|
+
type PaginatedResult<T> = {
|
|
45
|
+
items: T[];
|
|
46
|
+
total: number;
|
|
47
|
+
hasMore: boolean;
|
|
48
|
+
nextOffset?: number;
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Result wrapper for batch operations
|
|
52
|
+
*/
|
|
53
|
+
interface BatchResult {
|
|
54
|
+
successful: number;
|
|
55
|
+
failed: number;
|
|
56
|
+
errors?: {
|
|
57
|
+
index: number;
|
|
58
|
+
error: string;
|
|
59
|
+
}[];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Unified database adapter interface
|
|
64
|
+
* Supports DynamoDB, MongoDB, SQL databases, and other data stores
|
|
65
|
+
*/
|
|
66
|
+
interface DatabaseAdapter {
|
|
67
|
+
/**
|
|
68
|
+
* Establish connection to the database
|
|
69
|
+
* Implementation varies by backend (DynamoDB, MongoDB, etc.)
|
|
70
|
+
*/
|
|
71
|
+
connect(): Promise<void>;
|
|
72
|
+
/**
|
|
73
|
+
* Close connection to the database
|
|
74
|
+
*/
|
|
75
|
+
disconnect(): Promise<void>;
|
|
76
|
+
/**
|
|
77
|
+
* Check if adapter is connected
|
|
78
|
+
*/
|
|
79
|
+
isConnected(): boolean;
|
|
80
|
+
/**
|
|
81
|
+
* Fetch a single record matching the where conditions
|
|
82
|
+
* @param tableName - Table/collection name
|
|
83
|
+
* @param where - Query conditions
|
|
84
|
+
* @param options - Query options (projection, consistency, etc.)
|
|
85
|
+
* @returns Single record or null if not found
|
|
86
|
+
*/
|
|
87
|
+
selectOne<T = Record<string, any>>(tableName: string, where: WhereClause, options?: QueryOptions): Promise<T | null>;
|
|
88
|
+
/**
|
|
89
|
+
* Fetch multiple records matching the where conditions
|
|
90
|
+
* @param tableName - Table/collection name
|
|
91
|
+
* @param where - Query conditions
|
|
92
|
+
* @param options - Query options (limit, offset, orderBy, projection, etc.)
|
|
93
|
+
* @returns Array of matching records with pagination info
|
|
94
|
+
*/
|
|
95
|
+
selectMany<T = Record<string, any>>(tableName: string, where?: WhereClause, options?: QueryOptions): Promise<PaginatedResult<T>>;
|
|
96
|
+
/**
|
|
97
|
+
* Insert a single record
|
|
98
|
+
* @param tableName - Table/collection name
|
|
99
|
+
* @param data - Record to insert
|
|
100
|
+
* @returns Created record with any server-generated fields
|
|
101
|
+
*/
|
|
102
|
+
createOne<T = Record<string, any>>(tableName: string, data: Record<string, any>): Promise<T>;
|
|
103
|
+
/**
|
|
104
|
+
* Insert multiple records in a batch operation
|
|
105
|
+
* @param tableName - Table/collection name
|
|
106
|
+
* @param data - Array of records to insert
|
|
107
|
+
* @param options - Batch options (e.g., stopOnError)
|
|
108
|
+
* @returns Batch operation result with success/failure counts
|
|
109
|
+
*/
|
|
110
|
+
createMany<T = Record<string, any>>(tableName: string, data: Record<string, any>[], options?: {
|
|
111
|
+
stopOnError?: boolean;
|
|
112
|
+
}): Promise<{
|
|
113
|
+
items: T[];
|
|
114
|
+
result: BatchResult;
|
|
115
|
+
}>;
|
|
116
|
+
/**
|
|
117
|
+
* Update a single record matching the where conditions
|
|
118
|
+
* @param tableName - Table/collection name
|
|
119
|
+
* @param update - Fields to update
|
|
120
|
+
* @param where - Query conditions to identify record
|
|
121
|
+
* @returns Updated record
|
|
122
|
+
*/
|
|
123
|
+
updateOne<T = Record<string, any>>(tableName: string, update: UpdateClause, where: WhereClause): Promise<T>;
|
|
124
|
+
/**
|
|
125
|
+
* Update multiple records matching the where conditions
|
|
126
|
+
* @param tableName - Table/collection name
|
|
127
|
+
* @param update - Fields to update
|
|
128
|
+
* @param where - Query conditions to identify records
|
|
129
|
+
* @param options - Update options (e.g., limit)
|
|
130
|
+
* @returns Batch operation result with updated count
|
|
131
|
+
*/
|
|
132
|
+
updateMany<T = Record<string, any>>(tableName: string, update: UpdateClause, where?: WhereClause, options?: {
|
|
133
|
+
limit?: number;
|
|
134
|
+
}): Promise<{
|
|
135
|
+
items: T[];
|
|
136
|
+
result: BatchResult;
|
|
137
|
+
}>;
|
|
138
|
+
/**
|
|
139
|
+
* Delete a single record matching the where conditions
|
|
140
|
+
* @param tableName - Table/collection name
|
|
141
|
+
* @param where - Query conditions to identify record
|
|
142
|
+
* @returns Deleted record or confirmation
|
|
143
|
+
*/
|
|
144
|
+
deleteOne(tableName: string, where: WhereClause): Promise<{
|
|
145
|
+
success: boolean;
|
|
146
|
+
deletedCount: number;
|
|
147
|
+
}>;
|
|
148
|
+
/**
|
|
149
|
+
* Delete multiple records matching the where conditions
|
|
150
|
+
* @param tableName - Table/collection name
|
|
151
|
+
* @param where - Query conditions to identify records
|
|
152
|
+
* @param options - Delete options (e.g., limit)
|
|
153
|
+
* @returns Batch operation result with deleted count
|
|
154
|
+
*/
|
|
155
|
+
deleteMany(tableName: string, where?: WhereClause, options?: {
|
|
156
|
+
limit?: number;
|
|
157
|
+
}): Promise<{
|
|
158
|
+
success: boolean;
|
|
159
|
+
deletedCount: number;
|
|
160
|
+
result: BatchResult;
|
|
161
|
+
}>;
|
|
162
|
+
/**
|
|
163
|
+
* Execute a raw query specific to the database backend
|
|
164
|
+
* Use sparingly for complex queries not supported by the adapter
|
|
165
|
+
*/
|
|
166
|
+
executeRaw<T = any>(query: string, params?: Record<string, any>): Promise<T>;
|
|
167
|
+
/**
|
|
168
|
+
* Start a transaction (if supported by the backend)
|
|
169
|
+
*/
|
|
170
|
+
beginTransaction?(): Promise<void>;
|
|
171
|
+
/**
|
|
172
|
+
* Commit an active transaction
|
|
173
|
+
*/
|
|
174
|
+
commitTransaction?(): Promise<void>;
|
|
175
|
+
/**
|
|
176
|
+
* Rollback an active transaction
|
|
177
|
+
*/
|
|
178
|
+
rollbackTransaction?(): Promise<void>;
|
|
179
|
+
/**
|
|
180
|
+
* Check the health/status of the database connection
|
|
181
|
+
*/
|
|
182
|
+
healthCheck(): Promise<boolean>;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
declare class DynamoService implements DatabaseAdapter {
|
|
186
|
+
private client;
|
|
187
|
+
private dynamoDBClient;
|
|
188
|
+
private isConnectedFlag;
|
|
189
|
+
private config;
|
|
190
|
+
setConfig(config: DynamoConfig): void;
|
|
191
|
+
connect(): Promise<void>;
|
|
192
|
+
disconnect(): Promise<void>;
|
|
193
|
+
isConnected(): boolean;
|
|
194
|
+
healthCheck(): Promise<boolean>;
|
|
195
|
+
selectOne<T = Record<string, any>>(tableName: string, where: WhereClause, options?: QueryOptions): Promise<T | null>;
|
|
196
|
+
selectMany<T = Record<string, any>>(tableName: string, where?: WhereClause, options?: QueryOptions): Promise<PaginatedResult<T>>;
|
|
197
|
+
createOne<T = Record<string, any>>(tableName: string, data: Record<string, any>): Promise<T>;
|
|
198
|
+
createMany<T = Record<string, any>>(tableName: string, data: Record<string, any>[], options?: {
|
|
199
|
+
stopOnError?: boolean;
|
|
200
|
+
}): Promise<{
|
|
201
|
+
items: T[];
|
|
202
|
+
result: BatchResult;
|
|
203
|
+
}>;
|
|
204
|
+
updateOne<T = Record<string, any>>(tableName: string, update: UpdateClause, where: WhereClause): Promise<T>;
|
|
205
|
+
updateMany<T = Record<string, any>>(tableName: string, update: UpdateClause, where?: WhereClause, options?: {
|
|
206
|
+
limit?: number;
|
|
207
|
+
}): Promise<{
|
|
208
|
+
items: T[];
|
|
209
|
+
result: BatchResult;
|
|
210
|
+
}>;
|
|
211
|
+
deleteOne(tableName: string, where: WhereClause): Promise<{
|
|
212
|
+
success: boolean;
|
|
213
|
+
deletedCount: number;
|
|
214
|
+
}>;
|
|
215
|
+
deleteMany(tableName: string, where?: WhereClause, options?: {
|
|
216
|
+
limit?: number;
|
|
217
|
+
}): Promise<{
|
|
218
|
+
success: boolean;
|
|
219
|
+
deletedCount: number;
|
|
220
|
+
result: BatchResult;
|
|
221
|
+
}>;
|
|
222
|
+
executeRaw<T = any>(query: string, params?: Record<string, any>): Promise<T>;
|
|
223
|
+
private validateConfig;
|
|
224
|
+
private getClient;
|
|
225
|
+
private buildKey;
|
|
226
|
+
private extractKey;
|
|
227
|
+
private isKeyQuery;
|
|
228
|
+
private buildProjection;
|
|
229
|
+
private buildKeyConditionExpression;
|
|
230
|
+
private buildFilterExpression;
|
|
231
|
+
private buildUpdateExpression;
|
|
232
|
+
private handleError;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* MongoDB implementation of DatabaseAdapter interface
|
|
237
|
+
* This is a template implementation showing how to adapt another database backend
|
|
238
|
+
*/
|
|
239
|
+
|
|
240
|
+
interface MongoConfig {
|
|
241
|
+
uri: string;
|
|
242
|
+
dbName: string;
|
|
243
|
+
authSource?: string;
|
|
244
|
+
retryWrites?: boolean;
|
|
245
|
+
w?: "majority" | number;
|
|
246
|
+
}
|
|
247
|
+
declare class MongoService implements DatabaseAdapter {
|
|
248
|
+
private client;
|
|
249
|
+
private db;
|
|
250
|
+
private isConnectedFlag;
|
|
251
|
+
private config;
|
|
252
|
+
setConfig(config: MongoConfig): void;
|
|
253
|
+
connect(): Promise<void>;
|
|
254
|
+
disconnect(): Promise<void>;
|
|
255
|
+
isConnected(): boolean;
|
|
256
|
+
healthCheck(): Promise<boolean>;
|
|
257
|
+
selectOne<T = Record<string, any>>(tableName: string, where: WhereClause, options?: QueryOptions): Promise<T | null>;
|
|
258
|
+
selectMany<T = Record<string, any>>(tableName: string, where?: WhereClause, options?: QueryOptions): Promise<PaginatedResult<T>>;
|
|
259
|
+
createOne<T = Record<string, any>>(tableName: string, data: Record<string, any>): Promise<T>;
|
|
260
|
+
createMany<T = Record<string, any>>(tableName: string, data: Record<string, any>[], options?: {
|
|
261
|
+
stopOnError?: boolean;
|
|
262
|
+
}): Promise<{
|
|
263
|
+
items: T[];
|
|
264
|
+
result: BatchResult;
|
|
265
|
+
}>;
|
|
266
|
+
updateOne<T = Record<string, any>>(tableName: string, update: UpdateClause, where: WhereClause): Promise<T>;
|
|
267
|
+
updateMany<T = Record<string, any>>(tableName: string, update: UpdateClause, where?: WhereClause, options?: {
|
|
268
|
+
limit?: number;
|
|
269
|
+
}): Promise<{
|
|
270
|
+
items: T[];
|
|
271
|
+
result: BatchResult;
|
|
272
|
+
}>;
|
|
273
|
+
deleteOne(tableName: string, where: WhereClause): Promise<{
|
|
274
|
+
success: boolean;
|
|
275
|
+
deletedCount: number;
|
|
276
|
+
}>;
|
|
277
|
+
deleteMany(tableName: string, where?: WhereClause, options?: {
|
|
278
|
+
limit?: number;
|
|
279
|
+
}): Promise<{
|
|
280
|
+
success: boolean;
|
|
281
|
+
deletedCount: number;
|
|
282
|
+
result: BatchResult;
|
|
283
|
+
}>;
|
|
284
|
+
executeRaw<T = any>(query: string, params?: Record<string, any>): Promise<T>;
|
|
285
|
+
private validateConfig;
|
|
286
|
+
private getDb;
|
|
287
|
+
private getCollection;
|
|
288
|
+
private buildMongoFilter;
|
|
289
|
+
private isValidOperator;
|
|
290
|
+
private buildMongoUpdate;
|
|
291
|
+
private handleError;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
interface Condition {
|
|
295
|
+
operator: string;
|
|
296
|
+
value: unknown;
|
|
297
|
+
}
|
|
298
|
+
declare const eq: (value: unknown) => Condition;
|
|
299
|
+
declare const neq: (value: unknown) => Condition;
|
|
300
|
+
declare const gt: (value: number | string) => Condition;
|
|
301
|
+
declare const gte: (value: number | string) => Condition;
|
|
302
|
+
declare const lt: (value: number | string) => Condition;
|
|
303
|
+
declare const lte: (value: number | string) => Condition;
|
|
304
|
+
/** Inclusive range. MongoDB only. */
|
|
305
|
+
declare const between: (min: number | string, max: number | string) => Condition;
|
|
306
|
+
declare const anyOf: (values: unknown[]) => Condition;
|
|
307
|
+
declare const noneOf: (values: unknown[]) => Condition;
|
|
308
|
+
declare const startsWith: (value: string) => Condition;
|
|
309
|
+
/** Case-insensitive substring match. MongoDB only. */
|
|
310
|
+
declare const like: (value: string) => Condition;
|
|
311
|
+
/** Field presence check. MongoDB only. */
|
|
312
|
+
declare const exists: (value?: boolean) => Condition;
|
|
313
|
+
/** Alias for eq */
|
|
314
|
+
declare const equals: (value: unknown) => Condition;
|
|
315
|
+
/** Alias for neq */
|
|
316
|
+
declare const notEquals: (value: unknown) => Condition;
|
|
317
|
+
/** Alias for anyOf */
|
|
318
|
+
declare const inList: (values: unknown[]) => Condition;
|
|
319
|
+
/** Alias for noneOf */
|
|
320
|
+
declare const notInList: (values: unknown[]) => Condition;
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Resolves the {@link DynamoService} instance from the service container.
|
|
324
|
+
*
|
|
325
|
+
* @example
|
|
326
|
+
* ```typescript
|
|
327
|
+
* const dynamo = useDynamo();
|
|
328
|
+
* const user = await dynamo.selectOne("users", { id: { operator: "=", value: 1 } });
|
|
329
|
+
* ```
|
|
330
|
+
*
|
|
331
|
+
* @returns The registered {@link DynamoService} instance.
|
|
332
|
+
*/
|
|
333
|
+
declare const useDynamo: () => DynamoService;
|
|
334
|
+
/**
|
|
335
|
+
* Resolves the {@link MongoService} instance from the service container.
|
|
336
|
+
*
|
|
337
|
+
* @example
|
|
338
|
+
* ```typescript
|
|
339
|
+
* const mongo = useMongo();
|
|
340
|
+
* const users = await mongo.selectMany("users", { status: { operator: "=", value: "active" } });
|
|
341
|
+
* ```
|
|
342
|
+
*
|
|
343
|
+
* @returns The registered {@link MongoService} instance.
|
|
344
|
+
*/
|
|
345
|
+
declare const useMongo: () => MongoService;
|
|
346
|
+
|
|
347
|
+
export { type DynamoConfig, DynamoService, type MongoConfig, MongoService, anyOf, between, eq, equals, exists, gt, gte, inList, like, lt, lte, neq, noneOf, notEquals, notInList, startsWith, useDynamo, useMongo };
|