@arcaelas/dynamite 1.0.29 → 3.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/README.md +13 -13
- package/{src → build/cjs}/@types/index.d.ts +26 -1
- package/build/cjs/@types/index.js.map +1 -0
- package/{src → build/cjs}/core/client.d.ts +22 -19
- package/build/cjs/core/client.js +384 -0
- package/build/cjs/core/client.js.map +1 -0
- package/build/cjs/core/decorator.d.ts +50 -0
- package/build/cjs/core/decorator.js +52 -0
- package/build/cjs/core/decorator.js.map +1 -0
- package/build/cjs/core/table.d.ts +73 -0
- package/build/cjs/core/table.js +953 -0
- package/build/cjs/core/table.js.map +1 -0
- package/build/cjs/decorators/hooks.d.ts +35 -0
- package/build/cjs/decorators/hooks.js +50 -0
- package/build/cjs/decorators/hooks.js.map +1 -0
- package/build/cjs/decorators/indexes.d.ts +20 -0
- package/build/cjs/decorators/indexes.js +45 -0
- package/build/cjs/decorators/indexes.js.map +1 -0
- package/{src → build/cjs}/decorators/relations.d.ts +8 -7
- package/{src → build/cjs}/decorators/relations.js +9 -8
- package/build/cjs/decorators/relations.js.map +1 -0
- package/build/cjs/decorators/timestamps.d.ts +20 -0
- package/build/cjs/decorators/timestamps.js +34 -0
- package/build/cjs/decorators/timestamps.js.map +1 -0
- package/build/cjs/decorators/transforms.d.ts +41 -0
- package/build/cjs/decorators/transforms.js +98 -0
- package/build/cjs/decorators/transforms.js.map +1 -0
- package/{src → build/cjs}/index.d.ts +7 -3
- package/{src → build/cjs}/index.js +15 -6
- package/build/cjs/index.js.map +1 -0
- package/build/cjs/package.json +1 -0
- package/build/cjs/test/basic.d.ts +1 -0
- package/build/cjs/test/basic.js +248 -0
- package/build/cjs/test/basic.js.map +1 -0
- package/build/cjs/test/bulk.d.ts +1 -0
- package/build/cjs/test/bulk.js +108 -0
- package/build/cjs/test/bulk.js.map +1 -0
- package/build/cjs/test/contracts.d.ts +1 -0
- package/build/cjs/test/contracts.js +343 -0
- package/build/cjs/test/contracts.js.map +1 -0
- package/build/cjs/test/filters.d.ts +1 -0
- package/build/cjs/test/filters.js +190 -0
- package/build/cjs/test/filters.js.map +1 -0
- package/build/cjs/test/hooks.d.ts +1 -0
- package/build/cjs/test/hooks.js +191 -0
- package/build/cjs/test/hooks.js.map +1 -0
- package/build/cjs/test/index.js +38 -0
- package/build/cjs/test/index.js.map +1 -0
- package/build/cjs/test/query_scan.d.ts +1 -0
- package/build/cjs/test/query_scan.js +195 -0
- package/build/cjs/test/query_scan.js.map +1 -0
- package/build/cjs/test/relations.d.ts +1 -0
- package/build/cjs/test/relations.js +246 -0
- package/build/cjs/test/relations.js.map +1 -0
- package/build/cjs/test/transactions.d.ts +1 -0
- package/build/cjs/test/transactions.js +145 -0
- package/build/cjs/test/transactions.js.map +1 -0
- package/{src → build/cjs}/utils/relations.js +1 -8
- package/build/cjs/utils/relations.js.map +1 -0
- package/build/cjs/utils/ulid.d.ts +10 -0
- package/build/cjs/utils/ulid.js +55 -0
- package/build/cjs/utils/ulid.js.map +1 -0
- package/build/esm/@types/index.d.ts +213 -0
- package/build/esm/@types/index.js +8 -0
- package/build/esm/@types/index.js.map +1 -0
- package/build/esm/core/client.d.ts +96 -0
- package/build/esm/core/client.js +375 -0
- package/build/esm/core/client.js.map +1 -0
- package/build/esm/core/decorator.d.ts +50 -0
- package/build/esm/core/decorator.js +47 -0
- package/build/esm/core/decorator.js.map +1 -0
- package/build/esm/core/table.d.ts +73 -0
- package/build/esm/core/table.js +950 -0
- package/build/esm/core/table.js.map +1 -0
- package/build/esm/decorators/hooks.d.ts +35 -0
- package/build/esm/decorators/hooks.js +47 -0
- package/build/esm/decorators/hooks.js.map +1 -0
- package/build/esm/decorators/indexes.d.ts +20 -0
- package/build/esm/decorators/indexes.js +42 -0
- package/build/esm/decorators/indexes.js.map +1 -0
- package/build/esm/decorators/relations.d.ts +75 -0
- package/build/esm/decorators/relations.js +112 -0
- package/build/esm/decorators/relations.js.map +1 -0
- package/build/esm/decorators/timestamps.d.ts +20 -0
- package/build/esm/decorators/timestamps.js +31 -0
- package/build/esm/decorators/timestamps.js.map +1 -0
- package/build/esm/decorators/transforms.d.ts +41 -0
- package/build/esm/decorators/transforms.js +92 -0
- package/build/esm/decorators/transforms.js.map +1 -0
- package/build/esm/index.d.ts +19 -0
- package/build/esm/index.js +26 -0
- package/build/esm/index.js.map +1 -0
- package/build/esm/package.json +1 -0
- package/build/esm/test/basic.d.ts +1 -0
- package/build/esm/test/basic.js +245 -0
- package/build/esm/test/basic.js.map +1 -0
- package/build/esm/test/bulk.d.ts +1 -0
- package/build/esm/test/bulk.js +105 -0
- package/build/esm/test/bulk.js.map +1 -0
- package/build/esm/test/contracts.d.ts +1 -0
- package/build/esm/test/contracts.js +340 -0
- package/build/esm/test/contracts.js.map +1 -0
- package/build/esm/test/filters.d.ts +1 -0
- package/build/esm/test/filters.js +187 -0
- package/build/esm/test/filters.js.map +1 -0
- package/build/esm/test/hooks.d.ts +1 -0
- package/build/esm/test/hooks.js +188 -0
- package/build/esm/test/hooks.js.map +1 -0
- package/build/esm/test/index.d.ts +1 -0
- package/build/esm/test/index.js +33 -0
- package/build/esm/test/index.js.map +1 -0
- package/build/esm/test/query_scan.d.ts +1 -0
- package/build/esm/test/query_scan.js +192 -0
- package/build/esm/test/query_scan.js.map +1 -0
- package/build/esm/test/relations.d.ts +1 -0
- package/build/esm/test/relations.js +243 -0
- package/build/esm/test/relations.js.map +1 -0
- package/build/esm/test/transactions.d.ts +1 -0
- package/build/esm/test/transactions.js +142 -0
- package/build/esm/test/transactions.js.map +1 -0
- package/build/esm/utils/relations.d.ts +42 -0
- package/build/esm/utils/relations.js +207 -0
- package/build/esm/utils/relations.js.map +1 -0
- package/build/esm/utils/ulid.d.ts +10 -0
- package/build/esm/utils/ulid.js +52 -0
- package/build/esm/utils/ulid.js.map +1 -0
- package/package.json +31 -9
- package/src/core/client.js +0 -296
- package/src/core/decorator.d.ts +0 -29
- package/src/core/decorator.js +0 -103
- package/src/core/table.d.ts +0 -81
- package/src/core/table.js +0 -892
- package/src/decorators/indexes.d.ts +0 -38
- package/src/decorators/indexes.js +0 -59
- package/src/decorators/timestamps.d.ts +0 -54
- package/src/decorators/timestamps.js +0 -72
- package/src/decorators/transforms.d.ts +0 -99
- package/src/decorators/transforms.js +0 -166
- package/src/index.test.js +0 -37
- /package/{src → build/cjs}/@types/index.js +0 -0
- /package/{src/index.test.d.ts → build/cjs/test/index.d.ts} +0 -0
- /package/{src → build/cjs}/utils/relations.d.ts +0 -0
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file index.ts
|
|
3
|
+
* @description Sistema de tipos para Dynamite ORM - Arquitectura con type-safety completo
|
|
4
|
+
* @autor Miguel Alejandro
|
|
5
|
+
* @fecha 2025-01-28
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Marca para identificar campos que no son atributos de BD (relaciones, métodos).
|
|
9
|
+
*/
|
|
10
|
+
export declare const NonAttributeBrand: unique symbol;
|
|
11
|
+
/**
|
|
12
|
+
* Marca para identificar campos opcionales en create() pero presentes en el modelo.
|
|
13
|
+
*/
|
|
14
|
+
export declare const CreationOptionalBrand: unique symbol;
|
|
15
|
+
/**
|
|
16
|
+
* Wrapper para marcar relaciones y campos virtuales que no se persisten en BD.
|
|
17
|
+
* @example
|
|
18
|
+
* ```typescript
|
|
19
|
+
* class User extends Table<User> {
|
|
20
|
+
* @HasMany(() => Post, 'user_id')
|
|
21
|
+
* declare posts: NonAttribute<Post[]>;
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export type NonAttribute<T> = T & {
|
|
26
|
+
[NonAttributeBrand]?: true;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Wrapper para marcar campos opcionales en create() (auto-generados: id, timestamps).
|
|
30
|
+
* @example
|
|
31
|
+
* ```typescript
|
|
32
|
+
* class User extends Table<User> {
|
|
33
|
+
* @PrimaryKey() id: CreationOptional<string>;
|
|
34
|
+
* @CreatedAt() created_at: CreationOptional<number>;
|
|
35
|
+
* name: string;
|
|
36
|
+
* }
|
|
37
|
+
* await User.create({ name: 'Juan' }); // id y created_at opcionales
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export type CreationOptional<T> = T & {
|
|
41
|
+
[CreationOptionalBrand]?: true;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Extrae atributos de BD usando intersección:
|
|
45
|
+
* - Primera parte: atributos REQUERIDOS con modificador `-?`
|
|
46
|
+
* - Segunda parte: atributos OPCIONALES (solo CreationOptional)
|
|
47
|
+
* - Ambos excluyen: never, undefined, null, NonAttribute, métodos
|
|
48
|
+
* @example
|
|
49
|
+
* ```typescript
|
|
50
|
+
* class User {
|
|
51
|
+
* id: CreationOptional<string>;
|
|
52
|
+
* created_at: CreationOptional<number>;
|
|
53
|
+
* name: string;
|
|
54
|
+
* email: string;
|
|
55
|
+
* posts: NonAttribute<Post[]>;
|
|
56
|
+
* save(): void;
|
|
57
|
+
* }
|
|
58
|
+
* // InferAttributes<User> = { name: string, email: string } & { id?: string, created_at?: number }
|
|
59
|
+
* // = { name: string, email: string, id?: string, created_at?: number }
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
export type InferAttributes<T> = {
|
|
63
|
+
[K in keyof T as T[K] extends ((...args: any[]) => any) | {
|
|
64
|
+
[NonAttributeBrand]?: true;
|
|
65
|
+
} | {
|
|
66
|
+
[CreationOptionalBrand]?: true;
|
|
67
|
+
} ? never : K]-?: Exclude<T[K], null | undefined | never>;
|
|
68
|
+
} & {
|
|
69
|
+
[K in keyof T as T[K] extends {
|
|
70
|
+
[CreationOptionalBrand]?: true;
|
|
71
|
+
} ? K : never]?: Exclude<T[K], null | undefined | never>;
|
|
72
|
+
};
|
|
73
|
+
/**
|
|
74
|
+
* Extrae solo relaciones (NonAttribute) preservando tipo Model | Model[]
|
|
75
|
+
* Excluye propiedades de Object.prototype para evitar conflictos
|
|
76
|
+
* @example
|
|
77
|
+
* ```typescript
|
|
78
|
+
* class User extends Table<User> {
|
|
79
|
+
* id: string;
|
|
80
|
+
* posts: NonAttribute<Post[]>; // Array
|
|
81
|
+
* profile: NonAttribute<Profile>; // Individual
|
|
82
|
+
* settings: NonAttribute<Settings | null>; // Nullable
|
|
83
|
+
* }
|
|
84
|
+
* // InferRelations<User> = { posts: Post[], profile: Profile, settings: Settings | null }
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
type ObjectBuiltinKeys = keyof Record<string, never> | 'toString' | 'valueOf' | 'hasOwnProperty' | 'isPrototypeOf' | 'propertyIsEnumerable' | 'toLocaleString' | 'constructor';
|
|
88
|
+
export type InferRelations<T> = {
|
|
89
|
+
[K in keyof T as T[K] extends NonAttribute<any> ? K extends ObjectBuiltinKeys ? never : K : never]: T[K] extends NonAttribute<infer P> ? P : never;
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* Extrae solo relaciones (campos NonAttribute).
|
|
93
|
+
* Usado internamente para validación.
|
|
94
|
+
* @example
|
|
95
|
+
* ```typescript
|
|
96
|
+
* class User {
|
|
97
|
+
* id: string;
|
|
98
|
+
* name: string;
|
|
99
|
+
* posts: NonAttribute<Post[]>;
|
|
100
|
+
* profile: NonAttribute<Profile>;
|
|
101
|
+
* }
|
|
102
|
+
* // PickRelations<User> = { posts: Post[], profile: Profile }
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
export type PickRelations<T> = {
|
|
106
|
+
[K in keyof T as T[K] extends NonAttribute<any> ? K : never]: T[K] extends NonAttribute<infer U> ? U : never;
|
|
107
|
+
};
|
|
108
|
+
/**
|
|
109
|
+
* Operadores de comparación soportados.
|
|
110
|
+
* Solo 8 operadores permitidos según especificación del usuario.
|
|
111
|
+
*/
|
|
112
|
+
export type QueryOperator = "=" | "$eq" | "<>" | "!=" | "$ne" | "<" | "$lt" | "<=" | "$lte" | ">" | "$gt" | ">=" | "$gte" | "in" | "$in" | "include" | "$include";
|
|
113
|
+
/**
|
|
114
|
+
* Opciones de query con pre-cache de atributos (A) y relaciones (R)
|
|
115
|
+
* @template T - Modelo de tabla
|
|
116
|
+
* @template A - Cache de InferAttributes<T>
|
|
117
|
+
* @template R - Cache de InferRelations<T>
|
|
118
|
+
* @example
|
|
119
|
+
* ```typescript
|
|
120
|
+
* await User.where({}, {
|
|
121
|
+
* where: {
|
|
122
|
+
* name: 'Juan',
|
|
123
|
+
* age: { $gte: 18, $lte: 65 }
|
|
124
|
+
* },
|
|
125
|
+
* order: 'DESC',
|
|
126
|
+
* limit: 10,
|
|
127
|
+
* attributes: ['name', 'email'],
|
|
128
|
+
* include: {
|
|
129
|
+
* posts: {
|
|
130
|
+
* where: { published: true },
|
|
131
|
+
* limit: 5,
|
|
132
|
+
* include: { comments: true }
|
|
133
|
+
* },
|
|
134
|
+
* profile: {
|
|
135
|
+
* attributes: ['bio', 'avatar']
|
|
136
|
+
* }
|
|
137
|
+
* }
|
|
138
|
+
* });
|
|
139
|
+
* ```
|
|
140
|
+
*/
|
|
141
|
+
export interface WhereOptions<T, A = InferAttributes<T>, R = InferRelations<T>> {
|
|
142
|
+
/** Filtros de búsqueda */
|
|
143
|
+
where?: {
|
|
144
|
+
[K in keyof A]?: A[K] | {
|
|
145
|
+
[N in QueryOperator]?: A[K];
|
|
146
|
+
};
|
|
147
|
+
};
|
|
148
|
+
/** Orden de resultados */
|
|
149
|
+
order?: "ASC" | "DESC" | {
|
|
150
|
+
[K in keyof A]?: "ASC" | "DESC";
|
|
151
|
+
};
|
|
152
|
+
/** Número de items a saltar */
|
|
153
|
+
offset?: number;
|
|
154
|
+
/** Alias de offset */
|
|
155
|
+
skip?: number;
|
|
156
|
+
/** Número máximo de items a retornar */
|
|
157
|
+
limit?: number;
|
|
158
|
+
/** Campos a seleccionar */
|
|
159
|
+
attributes?: Array<keyof A>;
|
|
160
|
+
/** Relaciones a cargar (recursivo) */
|
|
161
|
+
include?: {
|
|
162
|
+
[K in keyof R]?: NonNullable<R[K]> extends Array<infer U> ? true | WhereOptions<U> : true | Pick<WhereOptions<NonNullable<R[K]>>, "attributes" | "include">;
|
|
163
|
+
};
|
|
164
|
+
/** Incluir registros soft-deleted */
|
|
165
|
+
_includeTrashed?: boolean;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Input para create() - InferAttributes ya maneja opcional/requerido con intersección
|
|
169
|
+
* @example
|
|
170
|
+
* ```typescript
|
|
171
|
+
* class User extends Table<User> {
|
|
172
|
+
* id: CreationOptional<string>;
|
|
173
|
+
* created_at: CreationOptional<number>;
|
|
174
|
+
* name: string;
|
|
175
|
+
* email: string;
|
|
176
|
+
* }
|
|
177
|
+
* // CreateInput<User> = { name: string, email: string, id?: string, created_at?: number }
|
|
178
|
+
* await User.create({ name: 'Juan', email: 'juan@example.com' }); // id y created_at opcionales
|
|
179
|
+
* ```
|
|
180
|
+
*/
|
|
181
|
+
export type CreateInput<T> = InferAttributes<T>;
|
|
182
|
+
/**
|
|
183
|
+
* Tipo para update() - todos los campos parciales.
|
|
184
|
+
* @example
|
|
185
|
+
* ```typescript
|
|
186
|
+
* await user.update({ name: 'Ana' }); // Todos los campos opcionales
|
|
187
|
+
* ```
|
|
188
|
+
*/
|
|
189
|
+
export type UpdateInput<T> = Partial<InferAttributes<T>>;
|
|
190
|
+
/**
|
|
191
|
+
* Picks only keys from T whose value extends V.
|
|
192
|
+
* Selecciona solo las keys de T cuyo valor extiende V.
|
|
193
|
+
* @example
|
|
194
|
+
* ```typescript
|
|
195
|
+
* class User { name: string; age: number; score: number; }
|
|
196
|
+
* // PickByType<InferAttributes<User>, number> = { age: number, score: number }
|
|
197
|
+
* ```
|
|
198
|
+
*/
|
|
199
|
+
type StripBrands<T> = T extends number & {
|
|
200
|
+
[NonAttributeBrand]?: any;
|
|
201
|
+
} ? number : T extends string & {
|
|
202
|
+
[NonAttributeBrand]?: any;
|
|
203
|
+
} ? string : T extends number & {
|
|
204
|
+
[CreationOptionalBrand]?: any;
|
|
205
|
+
} ? number : T extends string & {
|
|
206
|
+
[CreationOptionalBrand]?: any;
|
|
207
|
+
} ? string : T extends boolean & {
|
|
208
|
+
[CreationOptionalBrand]?: any;
|
|
209
|
+
} ? boolean : T;
|
|
210
|
+
export type PickByType<T, V> = {
|
|
211
|
+
[K in keyof T as StripBrands<NonNullable<T[K]>> extends V ? K : never]: T[K];
|
|
212
|
+
};
|
|
213
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/@types/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG"}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file client.ts
|
|
3
|
+
* @description Centralized Dynamite client with multi-client support and table sync
|
|
4
|
+
* @autor Miguel Alejandro
|
|
5
|
+
* @fecha 2025-01-27
|
|
6
|
+
*/
|
|
7
|
+
import { DynamoDBClient, DynamoDBClientConfig } from "@aws-sdk/client-dynamodb";
|
|
8
|
+
/**
|
|
9
|
+
* Configuration for Dynamite client initialization
|
|
10
|
+
*/
|
|
11
|
+
export interface DynamiteConfig extends DynamoDBClientConfig {
|
|
12
|
+
tables: Array<new (...args: any[]) => any>;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Centralized Dynamite client for managing DynamoDB connections and table synchronization
|
|
16
|
+
*/
|
|
17
|
+
export declare class Dynamite {
|
|
18
|
+
private client;
|
|
19
|
+
private tables;
|
|
20
|
+
private connected;
|
|
21
|
+
private synced;
|
|
22
|
+
/**
|
|
23
|
+
* Initialize Dynamite client with configuration
|
|
24
|
+
* @param config Dynamite client configuration
|
|
25
|
+
*/
|
|
26
|
+
constructor(config: DynamiteConfig);
|
|
27
|
+
/**
|
|
28
|
+
* @description Configure the DynamoDB client. Does not create tables or GSIs -- use sync() for that.
|
|
29
|
+
* @description Configura el cliente DynamoDB. No crea tablas ni GSIs -- usar sync() para eso.
|
|
30
|
+
*/
|
|
31
|
+
connect(): Promise<void>;
|
|
32
|
+
/**
|
|
33
|
+
* @description Synchronize all tables, GSIs and pivot tables with DynamoDB.
|
|
34
|
+
* @description Sincroniza tablas, GSIs y pivot tables con DynamoDB.
|
|
35
|
+
*/
|
|
36
|
+
sync(): Promise<void>;
|
|
37
|
+
/**
|
|
38
|
+
* @description Poll DescribeTable until all GSIs on the given tables are ACTIVE.
|
|
39
|
+
* @description Polling de DescribeTable hasta que todos los GSIs de las tablas dadas estén ACTIVE.
|
|
40
|
+
*/
|
|
41
|
+
private waitForActiveGSIs;
|
|
42
|
+
/**
|
|
43
|
+
* Ejecutar operaciones en una transacción atómica.
|
|
44
|
+
* Si cualquier operación falla, todas se revierten automáticamente.
|
|
45
|
+
*
|
|
46
|
+
* @param callback Función que recibe el contexto de transacción
|
|
47
|
+
* @returns Resultado del callback
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* await dynamite.tx(async (tx) => {
|
|
51
|
+
* const user = await User.create({ name: "Juan" }, tx);
|
|
52
|
+
* await Order.create({ user_id: user.id, total: 100 }, tx);
|
|
53
|
+
* });
|
|
54
|
+
*/
|
|
55
|
+
tx<R>(callback: (tx: TransactionContext) => Promise<R>): Promise<R>;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Set global client for Table class operations
|
|
59
|
+
* @param client DynamoDB client instance
|
|
60
|
+
*/
|
|
61
|
+
export declare const setGlobalClient: (client: DynamoDBClient) => void;
|
|
62
|
+
/**
|
|
63
|
+
* Get global client for Table class operations
|
|
64
|
+
*/
|
|
65
|
+
export declare const getGlobalClient: () => DynamoDBClient;
|
|
66
|
+
/**
|
|
67
|
+
* Check if global client is available
|
|
68
|
+
*/
|
|
69
|
+
export declare const hasGlobalClient: () => boolean;
|
|
70
|
+
/**
|
|
71
|
+
* Require global client for Table operations (throws if not available)
|
|
72
|
+
*/
|
|
73
|
+
export declare const requireClient: () => DynamoDBClient;
|
|
74
|
+
/**
|
|
75
|
+
* @description Transaction context for grouping atomic operations. Max 100 operations (auto-chunked in batches of 25).
|
|
76
|
+
* @description Contexto de transacción para operaciones atómicas. Máximo 100 operaciones (auto-divididas en lotes de 25).
|
|
77
|
+
*/
|
|
78
|
+
export declare class TransactionContext {
|
|
79
|
+
private operations;
|
|
80
|
+
private after_commit;
|
|
81
|
+
private client;
|
|
82
|
+
constructor(client: DynamoDBClient);
|
|
83
|
+
addPut(table_name: string, item: Record<string, any>, condition?: {
|
|
84
|
+
expression: string;
|
|
85
|
+
names: Record<string, string>;
|
|
86
|
+
}): void;
|
|
87
|
+
addDelete(table_name: string, key: Record<string, any>): void;
|
|
88
|
+
addUpdate(table_name: string, key: Record<string, any>, expression: string, names: Record<string, string>, values: Record<string, any>): void;
|
|
89
|
+
/**
|
|
90
|
+
* @description Register a callback (sync or async) to run after successful commit.
|
|
91
|
+
* @description Registra un callback (síncrono o asíncrono) que se ejecuta después de un commit exitoso.
|
|
92
|
+
*/
|
|
93
|
+
onCommit(fn: () => void | Promise<void>): void;
|
|
94
|
+
commit(): Promise<void>;
|
|
95
|
+
private guard;
|
|
96
|
+
}
|
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file client.ts
|
|
3
|
+
* @description Centralized Dynamite client with multi-client support and table sync
|
|
4
|
+
* @autor Miguel Alejandro
|
|
5
|
+
* @fecha 2025-01-27
|
|
6
|
+
*/
|
|
7
|
+
import { CreateTableCommand, DescribeTableCommand, DynamoDBClient, TransactWriteItemsCommand, UpdateTableCommand, } from "@aws-sdk/client-dynamodb";
|
|
8
|
+
import { marshall } from "@aws-sdk/util-dynamodb";
|
|
9
|
+
import { SCHEMA } from "./decorator.js";
|
|
10
|
+
/**
|
|
11
|
+
* Centralized Dynamite client for managing DynamoDB connections and table synchronization
|
|
12
|
+
*/
|
|
13
|
+
export class Dynamite {
|
|
14
|
+
/**
|
|
15
|
+
* Initialize Dynamite client with configuration
|
|
16
|
+
* @param config Dynamite client configuration
|
|
17
|
+
*/
|
|
18
|
+
constructor(config) {
|
|
19
|
+
this.connected = false;
|
|
20
|
+
this.synced = false;
|
|
21
|
+
const { tables, ...options } = config;
|
|
22
|
+
this.client = new DynamoDBClient({
|
|
23
|
+
...options,
|
|
24
|
+
});
|
|
25
|
+
this.tables = tables;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* @description Configure the DynamoDB client. Does not create tables or GSIs -- use sync() for that.
|
|
29
|
+
* @description Configura el cliente DynamoDB. No crea tablas ni GSIs -- usar sync() para eso.
|
|
30
|
+
*/
|
|
31
|
+
async connect() {
|
|
32
|
+
if (this.connected)
|
|
33
|
+
return;
|
|
34
|
+
setGlobalClient(this.client);
|
|
35
|
+
this.connected = true;
|
|
36
|
+
// Computar GSIs esperados desde los schemas (sin llamadas API)
|
|
37
|
+
const pk_by_table = new Map();
|
|
38
|
+
for (const tc of this.tables) {
|
|
39
|
+
const s = tc[SCHEMA];
|
|
40
|
+
if (!s)
|
|
41
|
+
continue;
|
|
42
|
+
const pk_col = Object.values(s.columns).find(c => c.store.index || c.store.primaryKey);
|
|
43
|
+
pk_by_table.set(s.name, pk_col?.name ?? 'id');
|
|
44
|
+
}
|
|
45
|
+
for (const tc of this.tables) {
|
|
46
|
+
const s = tc[SCHEMA];
|
|
47
|
+
if (!s)
|
|
48
|
+
continue;
|
|
49
|
+
for (const col_name in s.columns) {
|
|
50
|
+
const rel = s.columns[col_name].store.relation;
|
|
51
|
+
if (!rel || (rel.type !== 'HasMany' && rel.type !== 'HasOne'))
|
|
52
|
+
continue;
|
|
53
|
+
const related = rel.model()?.[SCHEMA];
|
|
54
|
+
if (!related)
|
|
55
|
+
continue;
|
|
56
|
+
const related_pk = pk_by_table.get(related.name);
|
|
57
|
+
if (rel.foreignKey !== related_pk) {
|
|
58
|
+
related.gsis.add(rel.foreignKey);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* @description Synchronize all tables, GSIs and pivot tables with DynamoDB.
|
|
65
|
+
* @description Sincroniza tablas, GSIs y pivot tables con DynamoDB.
|
|
66
|
+
*/
|
|
67
|
+
async sync() {
|
|
68
|
+
if (!this.connected)
|
|
69
|
+
throw new Error('Call connect() before sync()');
|
|
70
|
+
if (this.synced)
|
|
71
|
+
return;
|
|
72
|
+
// Phase 1: Collect requirements
|
|
73
|
+
const tables = new Map();
|
|
74
|
+
const pivots = new Map();
|
|
75
|
+
for (const table_class of this.tables) {
|
|
76
|
+
const schema = table_class[SCHEMA];
|
|
77
|
+
if (!schema)
|
|
78
|
+
throw new Error(`Class ${table_class.name} not registered. Use decorators.`);
|
|
79
|
+
const cols = Object.values(schema.columns);
|
|
80
|
+
const pk = cols.find(c => c.store.index || c.store.primaryKey);
|
|
81
|
+
if (!pk)
|
|
82
|
+
throw new Error(`PartitionKey missing in ${table_class.name}`);
|
|
83
|
+
const sk = cols.find(c => c.store.indexSort) ?? null;
|
|
84
|
+
tables.set(schema.name, { pk: pk.name, sk: sk?.name ?? null, gsis: new Set() });
|
|
85
|
+
}
|
|
86
|
+
for (const table_class of this.tables) {
|
|
87
|
+
const schema = table_class[SCHEMA];
|
|
88
|
+
for (const col_name in schema.columns) {
|
|
89
|
+
const relation = schema.columns[col_name].store.relation;
|
|
90
|
+
if (!relation)
|
|
91
|
+
continue;
|
|
92
|
+
if (relation.type === 'HasMany' || relation.type === 'HasOne') {
|
|
93
|
+
const related_schema = relation.model()?.[SCHEMA];
|
|
94
|
+
if (related_schema && tables.has(related_schema.name)) {
|
|
95
|
+
const entry = tables.get(related_schema.name);
|
|
96
|
+
if (relation.foreignKey !== entry.pk) {
|
|
97
|
+
entry.gsis.add(relation.foreignKey);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (relation.type === 'ManyToMany' && relation.pivotTable) {
|
|
102
|
+
pivots.set(relation.pivotTable, {
|
|
103
|
+
foreignKey: relation.foreignKey,
|
|
104
|
+
relatedKey: relation.relatedKey,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
// Phase 2: Describe all tables + pivots in parallel
|
|
110
|
+
const all_table_names = [...tables.keys()];
|
|
111
|
+
const all_pivot_names = [...pivots.keys()];
|
|
112
|
+
const all_names = [...all_table_names, ...all_pivot_names];
|
|
113
|
+
const descriptions = await Promise.all(all_names.map(async (name) => {
|
|
114
|
+
try {
|
|
115
|
+
const result = await this.client.send(new DescribeTableCommand({ TableName: name }));
|
|
116
|
+
const existing_gsis = new Set((result.Table?.GlobalSecondaryIndexes ?? []).map(g => g.IndexName));
|
|
117
|
+
return { name, exists: true, existing_gsis };
|
|
118
|
+
}
|
|
119
|
+
catch (e) {
|
|
120
|
+
if (e.name === 'ResourceNotFoundException')
|
|
121
|
+
return { name, exists: false, existing_gsis: new Set() };
|
|
122
|
+
throw e;
|
|
123
|
+
}
|
|
124
|
+
}));
|
|
125
|
+
const described = new Map(descriptions.map(d => [d.name, d]));
|
|
126
|
+
// Phase 3: Create missing tables in parallel
|
|
127
|
+
const to_create = [];
|
|
128
|
+
for (const [name, entry] of tables) {
|
|
129
|
+
const desc = described.get(name);
|
|
130
|
+
if (desc.exists)
|
|
131
|
+
continue;
|
|
132
|
+
const attrs = new Map([[entry.pk, 'S']]);
|
|
133
|
+
if (entry.sk)
|
|
134
|
+
attrs.set(entry.sk, 'S');
|
|
135
|
+
for (const gsi_field of entry.gsis)
|
|
136
|
+
attrs.set(gsi_field, 'S');
|
|
137
|
+
const key_schema = [
|
|
138
|
+
{ AttributeName: entry.pk, KeyType: 'HASH' },
|
|
139
|
+
];
|
|
140
|
+
if (entry.sk)
|
|
141
|
+
key_schema.push({ AttributeName: entry.sk, KeyType: 'RANGE' });
|
|
142
|
+
const gsi_defs = [...entry.gsis].map(field => ({
|
|
143
|
+
IndexName: `${field}_index`,
|
|
144
|
+
KeySchema: [{ AttributeName: field, KeyType: 'HASH' }],
|
|
145
|
+
Projection: { ProjectionType: 'ALL' },
|
|
146
|
+
}));
|
|
147
|
+
to_create.push(this.client.send(new CreateTableCommand({
|
|
148
|
+
TableName: name,
|
|
149
|
+
BillingMode: 'PAY_PER_REQUEST',
|
|
150
|
+
AttributeDefinitions: [...attrs].map(([AttributeName, AttributeType]) => ({ AttributeName, AttributeType })),
|
|
151
|
+
KeySchema: key_schema,
|
|
152
|
+
...(gsi_defs.length > 0 && { GlobalSecondaryIndexes: gsi_defs }),
|
|
153
|
+
})).then(() => { }));
|
|
154
|
+
}
|
|
155
|
+
for (const [name, meta] of pivots) {
|
|
156
|
+
const desc = described.get(name);
|
|
157
|
+
if (desc.exists)
|
|
158
|
+
continue;
|
|
159
|
+
to_create.push(this.client.send(new CreateTableCommand({
|
|
160
|
+
TableName: name,
|
|
161
|
+
BillingMode: 'PAY_PER_REQUEST',
|
|
162
|
+
KeySchema: [{ AttributeName: 'id', KeyType: 'HASH' }],
|
|
163
|
+
AttributeDefinitions: [
|
|
164
|
+
{ AttributeName: 'id', AttributeType: 'S' },
|
|
165
|
+
{ AttributeName: meta.foreignKey, AttributeType: 'S' },
|
|
166
|
+
{ AttributeName: meta.relatedKey, AttributeType: 'S' },
|
|
167
|
+
],
|
|
168
|
+
GlobalSecondaryIndexes: [
|
|
169
|
+
{ IndexName: `${meta.foreignKey}_index`, KeySchema: [{ AttributeName: meta.foreignKey, KeyType: 'HASH' }], Projection: { ProjectionType: 'ALL' } },
|
|
170
|
+
{ IndexName: `${meta.relatedKey}_index`, KeySchema: [{ AttributeName: meta.relatedKey, KeyType: 'HASH' }], Projection: { ProjectionType: 'ALL' } },
|
|
171
|
+
],
|
|
172
|
+
})).then(() => { }));
|
|
173
|
+
}
|
|
174
|
+
await Promise.all(to_create);
|
|
175
|
+
// Phase 4: Add missing GSIs to existing tables (round-robin, parallel across tables)
|
|
176
|
+
const pending_gsis = new Map();
|
|
177
|
+
for (const [name, entry] of tables) {
|
|
178
|
+
const desc = described.get(name);
|
|
179
|
+
if (!desc.exists)
|
|
180
|
+
continue;
|
|
181
|
+
const missing = [...entry.gsis].filter(field => !desc.existing_gsis.has(`${field}_index`));
|
|
182
|
+
if (missing.length > 0)
|
|
183
|
+
pending_gsis.set(name, missing);
|
|
184
|
+
}
|
|
185
|
+
for (const [name, meta] of pivots) {
|
|
186
|
+
const desc = described.get(name);
|
|
187
|
+
if (!desc.exists)
|
|
188
|
+
continue;
|
|
189
|
+
const expected = [`${meta.foreignKey}_index`, `${meta.relatedKey}_index`];
|
|
190
|
+
const missing_fields = [];
|
|
191
|
+
if (!desc.existing_gsis.has(expected[0]))
|
|
192
|
+
missing_fields.push(meta.foreignKey);
|
|
193
|
+
if (!desc.existing_gsis.has(expected[1]))
|
|
194
|
+
missing_fields.push(meta.relatedKey);
|
|
195
|
+
if (missing_fields.length > 0)
|
|
196
|
+
pending_gsis.set(name, missing_fields);
|
|
197
|
+
}
|
|
198
|
+
while (pending_gsis.size > 0) {
|
|
199
|
+
const round = [];
|
|
200
|
+
for (const [table_name, fields] of pending_gsis) {
|
|
201
|
+
const field = fields.shift();
|
|
202
|
+
if (fields.length === 0)
|
|
203
|
+
pending_gsis.delete(table_name);
|
|
204
|
+
round.push(this.client.send(new UpdateTableCommand({
|
|
205
|
+
TableName: table_name,
|
|
206
|
+
AttributeDefinitions: [{ AttributeName: field, AttributeType: 'S' }],
|
|
207
|
+
GlobalSecondaryIndexUpdates: [{
|
|
208
|
+
Create: {
|
|
209
|
+
IndexName: `${field}_index`,
|
|
210
|
+
KeySchema: [{ AttributeName: field, KeyType: 'HASH' }],
|
|
211
|
+
Projection: { ProjectionType: 'ALL' },
|
|
212
|
+
},
|
|
213
|
+
}],
|
|
214
|
+
})).then(() => { }));
|
|
215
|
+
}
|
|
216
|
+
await Promise.all(round);
|
|
217
|
+
// Wait for all GSIs in this round to become ACTIVE
|
|
218
|
+
const tables_in_round = round.length;
|
|
219
|
+
if (tables_in_round > 0) {
|
|
220
|
+
const active_checks = [...pending_gsis.keys()];
|
|
221
|
+
// Also check tables that just had their last GSI added
|
|
222
|
+
for (const [table_name] of tables) {
|
|
223
|
+
if (!pending_gsis.has(table_name))
|
|
224
|
+
active_checks.push(table_name);
|
|
225
|
+
}
|
|
226
|
+
for (const [pivot_name] of pivots) {
|
|
227
|
+
if (!pending_gsis.has(pivot_name))
|
|
228
|
+
active_checks.push(pivot_name);
|
|
229
|
+
}
|
|
230
|
+
await this.waitForActiveGSIs([...new Set(active_checks)]);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
this.synced = true;
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* @description Poll DescribeTable until all GSIs on the given tables are ACTIVE.
|
|
237
|
+
* @description Polling de DescribeTable hasta que todos los GSIs de las tablas dadas estén ACTIVE.
|
|
238
|
+
*/
|
|
239
|
+
async waitForActiveGSIs(table_names) {
|
|
240
|
+
const pending = new Set(table_names);
|
|
241
|
+
while (pending.size > 0) {
|
|
242
|
+
await new Promise(r => setTimeout(r, 3000));
|
|
243
|
+
const checks = await Promise.all([...pending].map(async (name) => {
|
|
244
|
+
const result = await this.client.send(new DescribeTableCommand({ TableName: name }));
|
|
245
|
+
const all_active = (result.Table?.GlobalSecondaryIndexes ?? [])
|
|
246
|
+
.every(g => g.IndexStatus === 'ACTIVE');
|
|
247
|
+
return { name, all_active };
|
|
248
|
+
}));
|
|
249
|
+
for (const { name, all_active } of checks) {
|
|
250
|
+
if (all_active)
|
|
251
|
+
pending.delete(name);
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Ejecutar operaciones en una transacción atómica.
|
|
257
|
+
* Si cualquier operación falla, todas se revierten automáticamente.
|
|
258
|
+
*
|
|
259
|
+
* @param callback Función que recibe el contexto de transacción
|
|
260
|
+
* @returns Resultado del callback
|
|
261
|
+
*
|
|
262
|
+
* @example
|
|
263
|
+
* await dynamite.tx(async (tx) => {
|
|
264
|
+
* const user = await User.create({ name: "Juan" }, tx);
|
|
265
|
+
* await Order.create({ user_id: user.id, total: 100 }, tx);
|
|
266
|
+
* });
|
|
267
|
+
*/
|
|
268
|
+
async tx(callback) {
|
|
269
|
+
const ctx = new TransactionContext(this.client);
|
|
270
|
+
const result = await callback(ctx);
|
|
271
|
+
await ctx.commit();
|
|
272
|
+
return result;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
let globalClient;
|
|
276
|
+
/**
|
|
277
|
+
* Set global client for Table class operations
|
|
278
|
+
* @param client DynamoDB client instance
|
|
279
|
+
*/
|
|
280
|
+
export const setGlobalClient = (client) => {
|
|
281
|
+
globalClient = client;
|
|
282
|
+
};
|
|
283
|
+
/**
|
|
284
|
+
* Get global client for Table class operations
|
|
285
|
+
*/
|
|
286
|
+
export const getGlobalClient = () => {
|
|
287
|
+
if (!globalClient)
|
|
288
|
+
throw new Error("No global DynamoDB client set. Call setGlobalClient() first.");
|
|
289
|
+
return globalClient;
|
|
290
|
+
};
|
|
291
|
+
/**
|
|
292
|
+
* Check if global client is available
|
|
293
|
+
*/
|
|
294
|
+
export const hasGlobalClient = () => {
|
|
295
|
+
return globalClient !== undefined;
|
|
296
|
+
};
|
|
297
|
+
/**
|
|
298
|
+
* Require global client for Table operations (throws if not available)
|
|
299
|
+
*/
|
|
300
|
+
export const requireClient = () => {
|
|
301
|
+
if (!globalClient) {
|
|
302
|
+
throw new Error("DynamoDB client no configurado. Use Dynamite.connect() primero.");
|
|
303
|
+
}
|
|
304
|
+
return globalClient;
|
|
305
|
+
};
|
|
306
|
+
/**
|
|
307
|
+
* @description Transaction context for grouping atomic operations. Max 100 operations (auto-chunked in batches of 25).
|
|
308
|
+
* @description Contexto de transacción para operaciones atómicas. Máximo 100 operaciones (auto-divididas en lotes de 25).
|
|
309
|
+
*/
|
|
310
|
+
export class TransactionContext {
|
|
311
|
+
constructor(client) {
|
|
312
|
+
this.operations = [];
|
|
313
|
+
this.after_commit = [];
|
|
314
|
+
this.client = client;
|
|
315
|
+
}
|
|
316
|
+
addPut(table_name, item, condition) {
|
|
317
|
+
this.guard();
|
|
318
|
+
const op = {
|
|
319
|
+
Put: {
|
|
320
|
+
TableName: table_name,
|
|
321
|
+
Item: marshall(item, { removeUndefinedValues: true }),
|
|
322
|
+
},
|
|
323
|
+
};
|
|
324
|
+
if (condition) {
|
|
325
|
+
op.Put.ConditionExpression = condition.expression;
|
|
326
|
+
op.Put.ExpressionAttributeNames = condition.names;
|
|
327
|
+
}
|
|
328
|
+
this.operations.push(op);
|
|
329
|
+
}
|
|
330
|
+
addDelete(table_name, key) {
|
|
331
|
+
this.guard();
|
|
332
|
+
this.operations.push({
|
|
333
|
+
Delete: {
|
|
334
|
+
TableName: table_name,
|
|
335
|
+
Key: marshall(key),
|
|
336
|
+
},
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
addUpdate(table_name, key, expression, names, values) {
|
|
340
|
+
this.guard();
|
|
341
|
+
this.operations.push({
|
|
342
|
+
Update: {
|
|
343
|
+
TableName: table_name,
|
|
344
|
+
Key: marshall(key),
|
|
345
|
+
UpdateExpression: expression,
|
|
346
|
+
ExpressionAttributeNames: names,
|
|
347
|
+
ExpressionAttributeValues: marshall(values, { removeUndefinedValues: true }),
|
|
348
|
+
},
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
/**
|
|
352
|
+
* @description Register a callback (sync or async) to run after successful commit.
|
|
353
|
+
* @description Registra un callback (síncrono o asíncrono) que se ejecuta después de un commit exitoso.
|
|
354
|
+
*/
|
|
355
|
+
onCommit(fn) {
|
|
356
|
+
this.after_commit.push(fn);
|
|
357
|
+
}
|
|
358
|
+
async commit() {
|
|
359
|
+
if (this.operations.length === 0)
|
|
360
|
+
return;
|
|
361
|
+
// DynamoDB limit: 25 per TransactWriteItems. Chunk if needed.
|
|
362
|
+
for (let i = 0; i < this.operations.length; i += 25) {
|
|
363
|
+
const chunk = this.operations.slice(i, i + 25);
|
|
364
|
+
await this.client.send(new TransactWriteItemsCommand({ TransactItems: chunk }));
|
|
365
|
+
}
|
|
366
|
+
for (const fn of this.after_commit)
|
|
367
|
+
await fn();
|
|
368
|
+
}
|
|
369
|
+
guard() {
|
|
370
|
+
if (this.operations.length >= 100) {
|
|
371
|
+
throw new Error(`Transaction exceeds 100 operations limit (has ${this.operations.length})`);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
//# sourceMappingURL=client.js.map
|