@arcaelas/dynamite 1.0.15 → 1.0.18
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/package.json +40 -2
- package/src/@types/index.d.ts +102 -0
- package/src/@types/index.js +9 -0
- package/src/core/client.d.ts +105 -0
- package/src/core/client.js +217 -0
- package/src/core/decorator.d.ts +44 -0
- package/src/core/decorator.js +133 -0
- package/src/core/method.d.ts +73 -0
- package/src/core/method.js +140 -0
- package/src/core/table.d.ts +56 -0
- package/src/core/table.js +659 -0
- package/src/decorators/indexes.d.ts +38 -0
- package/src/decorators/indexes.js +67 -0
- package/src/decorators/relations.d.ts +55 -0
- package/src/decorators/relations.js +84 -0
- package/src/decorators/timestamps.d.ts +54 -0
- package/src/decorators/timestamps.js +67 -0
- package/src/decorators/transforms.d.ts +86 -0
- package/src/decorators/transforms.js +154 -0
- package/src/index.d.ts +15 -0
- package/src/index.js +50 -0
- package/src/index.test.d.ts +13 -0
- package/src/index.test.js +1992 -0
- package/src/utils/relations.d.ts +39 -0
- package/src/utils/relations.js +141 -0
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @file indexes.ts
|
|
4
|
+
* @description Decoradores de índices con Symbol storage
|
|
5
|
+
* @autor Miguel Alejandro
|
|
6
|
+
* @fecha 2025-01-28
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.IndexSort = exports.Index = void 0;
|
|
10
|
+
exports.PrimaryKey = PrimaryKey;
|
|
11
|
+
const decorator_1 = require("../core/decorator");
|
|
12
|
+
/**
|
|
13
|
+
* @description Decorador para marcar propiedad como Partition Key
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* class User extends Table<User> {
|
|
17
|
+
* @Index() id: string;
|
|
18
|
+
* }
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
exports.Index = (0, decorator_1.decorator)((_schema, col) => {
|
|
22
|
+
col.store.index = true;
|
|
23
|
+
col.store.nullable = col.store.nullable ?? false;
|
|
24
|
+
});
|
|
25
|
+
/**
|
|
26
|
+
* @description Decorador para marcar propiedad como Sort Key
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* class Post extends Table<Post> {
|
|
30
|
+
* @Index() user_id: string;
|
|
31
|
+
* @IndexSort() created_at: string;
|
|
32
|
+
* }
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
exports.IndexSort = (0, decorator_1.decorator)((_schema, col) => {
|
|
36
|
+
col.store.indexSort = true;
|
|
37
|
+
col.store.nullable = col.store.nullable ?? false;
|
|
38
|
+
});
|
|
39
|
+
/**
|
|
40
|
+
* @description Decorador para marcar una propiedad como clave primaria
|
|
41
|
+
* @example
|
|
42
|
+
* ```typescript
|
|
43
|
+
* class User extends Table<User> {
|
|
44
|
+
* @PrimaryKey() id: string;
|
|
45
|
+
* name: string;
|
|
46
|
+
* }
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
function PrimaryKey() {
|
|
50
|
+
return (target, prop) => {
|
|
51
|
+
const table_class = target.constructor;
|
|
52
|
+
const column_name = String(prop);
|
|
53
|
+
const schema = (0, decorator_1.ensureSchema)(table_class);
|
|
54
|
+
// Crear columna si no existe
|
|
55
|
+
if (!schema.columns[column_name]) {
|
|
56
|
+
schema.columns[column_name] = { name: column_name, get: [], set: [], store: {} };
|
|
57
|
+
}
|
|
58
|
+
// Configurar como primary key
|
|
59
|
+
Object.assign(schema.columns[column_name].store, {
|
|
60
|
+
index: true,
|
|
61
|
+
primaryKey: true,
|
|
62
|
+
nullable: false
|
|
63
|
+
});
|
|
64
|
+
schema.primary_key = column_name;
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=indexes.js.map
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file relations.ts
|
|
3
|
+
* @description Decoradores de relaciones: @HasMany, @HasOne, @BelongsTo
|
|
4
|
+
* @autor Miguel Alejandro
|
|
5
|
+
* @fecha 2025-01-28
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* @description Decorador para relaciones uno a muchos (1:N)
|
|
9
|
+
* @param model Función que retorna la clase del modelo relacionado
|
|
10
|
+
* @param foreignKey Clave foránea en el modelo relacionado
|
|
11
|
+
* @param localKey Clave local (por defecto 'id')
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* class User extends Table<User> {
|
|
15
|
+
* @PrimaryKey() id: string;
|
|
16
|
+
*
|
|
17
|
+
* @HasMany(() => Post, 'id_user', 'id')
|
|
18
|
+
* declare posts: HasMany<Post>;
|
|
19
|
+
* }
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export declare const HasMany: (...params: any[]) => (target: any, propertyKey: string | symbol) => void;
|
|
23
|
+
/**
|
|
24
|
+
* @description Decorador para relaciones uno a uno (1:1)
|
|
25
|
+
* @param model Función que retorna la clase del modelo relacionado
|
|
26
|
+
* @param foreignKey Clave foránea en el modelo relacionado
|
|
27
|
+
* @param localKey Clave local (por defecto 'id')
|
|
28
|
+
* @example
|
|
29
|
+
* ```typescript
|
|
30
|
+
* class User extends Table<User> {
|
|
31
|
+
* @PrimaryKey() id: string;
|
|
32
|
+
*
|
|
33
|
+
* @HasOne(() => Profile, 'id_user', 'id')
|
|
34
|
+
* declare profile: HasOne<Profile>;
|
|
35
|
+
* }
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
export declare const HasOne: (...params: any[]) => (target: any, propertyKey: string | symbol) => void;
|
|
39
|
+
/**
|
|
40
|
+
* @description Decorador para relaciones muchos a uno (N:1)
|
|
41
|
+
* @param model Función que retorna la clase del modelo relacionado
|
|
42
|
+
* @param localKey Clave local que referencia al modelo padre
|
|
43
|
+
* @param foreignKey Clave en el modelo padre (por defecto 'id')
|
|
44
|
+
* @example
|
|
45
|
+
* ```typescript
|
|
46
|
+
* class Post extends Table<Post> {
|
|
47
|
+
* @PrimaryKey() id: string;
|
|
48
|
+
* id_user: string;
|
|
49
|
+
*
|
|
50
|
+
* @BelongsTo(() => User, 'id_user', 'id')
|
|
51
|
+
* declare author: BelongsTo<User>;
|
|
52
|
+
* }
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
export declare const BelongsTo: (...params: any[]) => (target: any, propertyKey: string | symbol) => void;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @file relations.ts
|
|
4
|
+
* @description Decoradores de relaciones: @HasMany, @HasOne, @BelongsTo
|
|
5
|
+
* @autor Miguel Alejandro
|
|
6
|
+
* @fecha 2025-01-28
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.BelongsTo = exports.HasOne = exports.HasMany = void 0;
|
|
10
|
+
const decorator_1 = require("../core/decorator");
|
|
11
|
+
/**
|
|
12
|
+
* @description Decorador para relaciones uno a muchos (1:N)
|
|
13
|
+
* @param model Función que retorna la clase del modelo relacionado
|
|
14
|
+
* @param foreignKey Clave foránea en el modelo relacionado
|
|
15
|
+
* @param localKey Clave local (por defecto 'id')
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* class User extends Table<User> {
|
|
19
|
+
* @PrimaryKey() id: string;
|
|
20
|
+
*
|
|
21
|
+
* @HasMany(() => Post, 'id_user', 'id')
|
|
22
|
+
* declare posts: HasMany<Post>;
|
|
23
|
+
* }
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
exports.HasMany = (0, decorator_1.decorator)((_schema, col, params) => {
|
|
27
|
+
const [model, foreignKey, localKey = 'id'] = params;
|
|
28
|
+
col.store.relation = {
|
|
29
|
+
type: 'HasMany',
|
|
30
|
+
model,
|
|
31
|
+
foreignKey,
|
|
32
|
+
localKey
|
|
33
|
+
};
|
|
34
|
+
});
|
|
35
|
+
/**
|
|
36
|
+
* @description Decorador para relaciones uno a uno (1:1)
|
|
37
|
+
* @param model Función que retorna la clase del modelo relacionado
|
|
38
|
+
* @param foreignKey Clave foránea en el modelo relacionado
|
|
39
|
+
* @param localKey Clave local (por defecto 'id')
|
|
40
|
+
* @example
|
|
41
|
+
* ```typescript
|
|
42
|
+
* class User extends Table<User> {
|
|
43
|
+
* @PrimaryKey() id: string;
|
|
44
|
+
*
|
|
45
|
+
* @HasOne(() => Profile, 'id_user', 'id')
|
|
46
|
+
* declare profile: HasOne<Profile>;
|
|
47
|
+
* }
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
50
|
+
exports.HasOne = (0, decorator_1.decorator)((_schema, col, params) => {
|
|
51
|
+
const [model, foreignKey, localKey = 'id'] = params;
|
|
52
|
+
col.store.relation = {
|
|
53
|
+
type: 'HasOne',
|
|
54
|
+
model,
|
|
55
|
+
foreignKey,
|
|
56
|
+
localKey
|
|
57
|
+
};
|
|
58
|
+
});
|
|
59
|
+
/**
|
|
60
|
+
* @description Decorador para relaciones muchos a uno (N:1)
|
|
61
|
+
* @param model Función que retorna la clase del modelo relacionado
|
|
62
|
+
* @param localKey Clave local que referencia al modelo padre
|
|
63
|
+
* @param foreignKey Clave en el modelo padre (por defecto 'id')
|
|
64
|
+
* @example
|
|
65
|
+
* ```typescript
|
|
66
|
+
* class Post extends Table<Post> {
|
|
67
|
+
* @PrimaryKey() id: string;
|
|
68
|
+
* id_user: string;
|
|
69
|
+
*
|
|
70
|
+
* @BelongsTo(() => User, 'id_user', 'id')
|
|
71
|
+
* declare author: BelongsTo<User>;
|
|
72
|
+
* }
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
exports.BelongsTo = (0, decorator_1.decorator)((_schema, col, params) => {
|
|
76
|
+
const [model, localKey, foreignKey = 'id'] = params;
|
|
77
|
+
col.store.relation = {
|
|
78
|
+
type: 'BelongsTo',
|
|
79
|
+
model,
|
|
80
|
+
foreignKey,
|
|
81
|
+
localKey
|
|
82
|
+
};
|
|
83
|
+
});
|
|
84
|
+
//# sourceMappingURL=relations.js.map
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file timestamps.ts
|
|
3
|
+
* @description Decoradores de timestamps: @CreatedAt, @UpdatedAt, @DeleteAt
|
|
4
|
+
* @autor Miguel Alejandro
|
|
5
|
+
* @fecha 2025-01-28
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* @description Decorador que establece automáticamente la fecha/hora de creación.
|
|
9
|
+
* El valor se genera solo si no existe uno previo.
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* class User extends Table<User> {
|
|
13
|
+
* @PrimaryKey() id: string;
|
|
14
|
+
* @CreatedAt() created_at: string;
|
|
15
|
+
* }
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
export declare const CreatedAt: (...params: any[]) => (target: any, propertyKey: string | symbol) => void;
|
|
19
|
+
/**
|
|
20
|
+
* @description Decorador que marca una propiedad para que se actualice automáticamente
|
|
21
|
+
* con la fecha/hora actual cada vez que se guarde el modelo.
|
|
22
|
+
* @example
|
|
23
|
+
* ```typescript
|
|
24
|
+
* class User extends Table<User> {
|
|
25
|
+
* @PrimaryKey() id: string;
|
|
26
|
+
* name: string;
|
|
27
|
+
* @UpdatedAt() updated_at: string;
|
|
28
|
+
* }
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
export declare const UpdatedAt: (...params: any[]) => (target: any, propertyKey: string | symbol) => void;
|
|
32
|
+
/**
|
|
33
|
+
* @description Decorador que marca una propiedad como columna de soft delete.
|
|
34
|
+
* Cuando se llama destroy(), en lugar de eliminar el registro,
|
|
35
|
+
* se establece esta columna con la fecha/hora actual.
|
|
36
|
+
* @example
|
|
37
|
+
* ```typescript
|
|
38
|
+
* class User extends Table<User> {
|
|
39
|
+
* @PrimaryKey() id: string;
|
|
40
|
+
* name: string;
|
|
41
|
+
* @DeleteAt() deleted_at?: string;
|
|
42
|
+
* }
|
|
43
|
+
*
|
|
44
|
+
* // Soft delete (marca deleted_at con timestamp)
|
|
45
|
+
* await user.destroy();
|
|
46
|
+
*
|
|
47
|
+
* // Queries normales excluyen soft deleted
|
|
48
|
+
* await User.where({ status: "active" });
|
|
49
|
+
*
|
|
50
|
+
* // Incluir soft deleted
|
|
51
|
+
* await User.withTrashed({ status: "active" });
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
54
|
+
export declare const DeleteAt: (...params: any[]) => (target: any, propertyKey: string | symbol) => void;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @file timestamps.ts
|
|
4
|
+
* @description Decoradores de timestamps: @CreatedAt, @UpdatedAt, @DeleteAt
|
|
5
|
+
* @autor Miguel Alejandro
|
|
6
|
+
* @fecha 2025-01-28
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.DeleteAt = exports.UpdatedAt = exports.CreatedAt = void 0;
|
|
10
|
+
const decorator_1 = require("../core/decorator");
|
|
11
|
+
/**
|
|
12
|
+
* @description Decorador que establece automáticamente la fecha/hora de creación.
|
|
13
|
+
* El valor se genera solo si no existe uno previo.
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* class User extends Table<User> {
|
|
17
|
+
* @PrimaryKey() id: string;
|
|
18
|
+
* @CreatedAt() created_at: string;
|
|
19
|
+
* }
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
exports.CreatedAt = (0, decorator_1.decorator)((_schema, col) => {
|
|
23
|
+
col.store.createdAt = true;
|
|
24
|
+
col.get.push((value) => value ?? new Date().toISOString());
|
|
25
|
+
});
|
|
26
|
+
/**
|
|
27
|
+
* @description Decorador que marca una propiedad para que se actualice automáticamente
|
|
28
|
+
* con la fecha/hora actual cada vez que se guarde el modelo.
|
|
29
|
+
* @example
|
|
30
|
+
* ```typescript
|
|
31
|
+
* class User extends Table<User> {
|
|
32
|
+
* @PrimaryKey() id: string;
|
|
33
|
+
* name: string;
|
|
34
|
+
* @UpdatedAt() updated_at: string;
|
|
35
|
+
* }
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
exports.UpdatedAt = (0, decorator_1.decorator)((_schema, col) => {
|
|
39
|
+
col.store.updatedAt = true;
|
|
40
|
+
});
|
|
41
|
+
/**
|
|
42
|
+
* @description Decorador que marca una propiedad como columna de soft delete.
|
|
43
|
+
* Cuando se llama destroy(), en lugar de eliminar el registro,
|
|
44
|
+
* se establece esta columna con la fecha/hora actual.
|
|
45
|
+
* @example
|
|
46
|
+
* ```typescript
|
|
47
|
+
* class User extends Table<User> {
|
|
48
|
+
* @PrimaryKey() id: string;
|
|
49
|
+
* name: string;
|
|
50
|
+
* @DeleteAt() deleted_at?: string;
|
|
51
|
+
* }
|
|
52
|
+
*
|
|
53
|
+
* // Soft delete (marca deleted_at con timestamp)
|
|
54
|
+
* await user.destroy();
|
|
55
|
+
*
|
|
56
|
+
* // Queries normales excluyen soft deleted
|
|
57
|
+
* await User.where({ status: "active" });
|
|
58
|
+
*
|
|
59
|
+
* // Incluir soft deleted
|
|
60
|
+
* await User.withTrashed({ status: "active" });
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
exports.DeleteAt = (0, decorator_1.decorator)((_schema, col) => {
|
|
64
|
+
col.store.softDelete = true;
|
|
65
|
+
col.store.nullable = true;
|
|
66
|
+
});
|
|
67
|
+
//# sourceMappingURL=timestamps.js.map
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file transforms.ts
|
|
3
|
+
* @description Decoradores de transformación con pipelines directas
|
|
4
|
+
* @autor Miguel Alejandro
|
|
5
|
+
* @fecha 2025-01-28
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* @description Decorador para establecer valor por defecto
|
|
9
|
+
* @param factory Valor por defecto o función que lo genera
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* class User extends Table<User> {
|
|
13
|
+
* @Default(uuid)
|
|
14
|
+
* declare id: string;
|
|
15
|
+
*
|
|
16
|
+
* @Default(() => 0)
|
|
17
|
+
* declare score: number;
|
|
18
|
+
* }
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export declare const Default: (...params: any[]) => (target: any, propertyKey: string | symbol) => void;
|
|
22
|
+
/**
|
|
23
|
+
* @description Decorador para transformar valores en cada asignación
|
|
24
|
+
* @param fn Función de transformación
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* class User extends Table<User> {
|
|
28
|
+
* @Mutate((v) => v.toLowerCase())
|
|
29
|
+
* declare email: string;
|
|
30
|
+
* }
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export declare const Mutate: (...params: any[]) => (target: any, propertyKey: string | symbol) => void;
|
|
34
|
+
/**
|
|
35
|
+
* @description Decorador para validar valores
|
|
36
|
+
* @param validators Función o array de funciones validadoras
|
|
37
|
+
* @param options Opciones de validación (lazy: true para validar en save())
|
|
38
|
+
* @example
|
|
39
|
+
* ```typescript
|
|
40
|
+
* class User extends Table<User> {
|
|
41
|
+
* @Validate((v) => v.length > 0 || 'Email requerido')
|
|
42
|
+
* declare email: string;
|
|
43
|
+
*
|
|
44
|
+
* @Validate([isEmail, isNotEmpty], { lazy: true })
|
|
45
|
+
* declare email2: string;
|
|
46
|
+
* }
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export declare const Validate: (...params: any[]) => (target: any, propertyKey: string | symbol) => void;
|
|
50
|
+
/**
|
|
51
|
+
* @description Decorador para transformar valores entre DB y aplicación
|
|
52
|
+
* @param fromDB Función que transforma al leer de DB
|
|
53
|
+
* @param toDB Función que transforma al guardar en DB
|
|
54
|
+
* @example
|
|
55
|
+
* ```typescript
|
|
56
|
+
* class User extends Table<User> {
|
|
57
|
+
* @Serialize(JSON.parse, JSON.stringify)
|
|
58
|
+
* declare metadata: Record<string, any>;
|
|
59
|
+
* }
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
export declare const Serialize: (...params: any[]) => (target: any, propertyKey: string | symbol) => void;
|
|
63
|
+
/**
|
|
64
|
+
* @description Decorador que valida que el valor no sea null, undefined o string vacío
|
|
65
|
+
* @example
|
|
66
|
+
* ```typescript
|
|
67
|
+
* class User extends Table<User> {
|
|
68
|
+
* @NotNull()
|
|
69
|
+
* declare name: string;
|
|
70
|
+
* }
|
|
71
|
+
* ```
|
|
72
|
+
*/
|
|
73
|
+
export declare const NotNull: (...params: any[]) => (target: any, propertyKey: string | symbol) => void;
|
|
74
|
+
/**
|
|
75
|
+
* @description Decorador dual para renombrar tabla o columna
|
|
76
|
+
* @param label Nombre personalizado
|
|
77
|
+
* @example
|
|
78
|
+
* ```typescript
|
|
79
|
+
* @Name('usuarios')
|
|
80
|
+
* class User extends Table<User> {
|
|
81
|
+
* @Name('user_email')
|
|
82
|
+
* declare email: string;
|
|
83
|
+
* }
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
export declare function Name(label: string): ClassDecorator & PropertyDecorator;
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @file transforms.ts
|
|
4
|
+
* @description Decoradores de transformación con pipelines directas
|
|
5
|
+
* @autor Miguel Alejandro
|
|
6
|
+
* @fecha 2025-01-28
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.NotNull = exports.Serialize = exports.Validate = exports.Mutate = exports.Default = void 0;
|
|
10
|
+
exports.Name = Name;
|
|
11
|
+
const decorator_1 = require("../core/decorator");
|
|
12
|
+
/**
|
|
13
|
+
* @description Decorador para establecer valor por defecto
|
|
14
|
+
* @param factory Valor por defecto o función que lo genera
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* class User extends Table<User> {
|
|
18
|
+
* @Default(uuid)
|
|
19
|
+
* declare id: string;
|
|
20
|
+
*
|
|
21
|
+
* @Default(() => 0)
|
|
22
|
+
* declare score: number;
|
|
23
|
+
* }
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
exports.Default = (0, decorator_1.decorator)((_schema, col, params) => {
|
|
27
|
+
const fallback = params[0];
|
|
28
|
+
col.get.push((value) => value ?? (typeof fallback === 'function' ? fallback() : fallback));
|
|
29
|
+
});
|
|
30
|
+
/**
|
|
31
|
+
* @description Decorador para transformar valores en cada asignación
|
|
32
|
+
* @param fn Función de transformación
|
|
33
|
+
* @example
|
|
34
|
+
* ```typescript
|
|
35
|
+
* class User extends Table<User> {
|
|
36
|
+
* @Mutate((v) => v.toLowerCase())
|
|
37
|
+
* declare email: string;
|
|
38
|
+
* }
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
exports.Mutate = (0, decorator_1.decorator)((_schema, col, params) => {
|
|
42
|
+
const fn = params[0];
|
|
43
|
+
col.set.push((value) => fn(value));
|
|
44
|
+
});
|
|
45
|
+
/**
|
|
46
|
+
* @description Decorador para validar valores
|
|
47
|
+
* @param validators Función o array de funciones validadoras
|
|
48
|
+
* @param options Opciones de validación (lazy: true para validar en save())
|
|
49
|
+
* @example
|
|
50
|
+
* ```typescript
|
|
51
|
+
* class User extends Table<User> {
|
|
52
|
+
* @Validate((v) => v.length > 0 || 'Email requerido')
|
|
53
|
+
* declare email: string;
|
|
54
|
+
*
|
|
55
|
+
* @Validate([isEmail, isNotEmpty], { lazy: true })
|
|
56
|
+
* declare email2: string;
|
|
57
|
+
* }
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
exports.Validate = (0, decorator_1.decorator)((_schema, col, params) => {
|
|
61
|
+
const [validators, options] = params;
|
|
62
|
+
const list = Array.isArray(validators) ? validators : [validators];
|
|
63
|
+
const is_lazy = options?.lazy ?? false;
|
|
64
|
+
if (!list.length || list.some((v) => typeof v !== 'function')) {
|
|
65
|
+
throw new TypeError('@Validate requiere funciones');
|
|
66
|
+
}
|
|
67
|
+
if (is_lazy) {
|
|
68
|
+
col.store.lazy_validators = list;
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
col.set.push((value) => {
|
|
72
|
+
for (const fn of list) {
|
|
73
|
+
const result = fn(value);
|
|
74
|
+
if (result !== true) {
|
|
75
|
+
throw new Error(typeof result === 'string' ? result : 'Validación fallida');
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return value;
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
/**
|
|
83
|
+
* @description Decorador para transformar valores entre DB y aplicación
|
|
84
|
+
* @param fromDB Función que transforma al leer de DB
|
|
85
|
+
* @param toDB Función que transforma al guardar en DB
|
|
86
|
+
* @example
|
|
87
|
+
* ```typescript
|
|
88
|
+
* class User extends Table<User> {
|
|
89
|
+
* @Serialize(JSON.parse, JSON.stringify)
|
|
90
|
+
* declare metadata: Record<string, any>;
|
|
91
|
+
* }
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
exports.Serialize = (0, decorator_1.decorator)((_schema, col, params) => {
|
|
95
|
+
const [fromDB, toDB] = params;
|
|
96
|
+
if (fromDB)
|
|
97
|
+
col.get.push((v) => v !== null && v !== undefined ? fromDB(v) : v);
|
|
98
|
+
if (toDB)
|
|
99
|
+
col.set.push((v) => v !== null && v !== undefined ? toDB(v) : v);
|
|
100
|
+
});
|
|
101
|
+
/**
|
|
102
|
+
* @description Decorador que valida que el valor no sea null, undefined o string vacío
|
|
103
|
+
* @example
|
|
104
|
+
* ```typescript
|
|
105
|
+
* class User extends Table<User> {
|
|
106
|
+
* @NotNull()
|
|
107
|
+
* declare name: string;
|
|
108
|
+
* }
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
exports.NotNull = (0, decorator_1.decorator)((_schema, col) => {
|
|
112
|
+
col.store.nullable = false;
|
|
113
|
+
col.set.push((value) => {
|
|
114
|
+
const is_empty = value === null || value === undefined || (typeof value === 'string' && value.trim() === '');
|
|
115
|
+
if (is_empty) {
|
|
116
|
+
throw new Error(`El campo ${col.name} no puede estar vacío`);
|
|
117
|
+
}
|
|
118
|
+
return value;
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
/**
|
|
122
|
+
* @description Decorador dual para renombrar tabla o columna
|
|
123
|
+
* @param label Nombre personalizado
|
|
124
|
+
* @example
|
|
125
|
+
* ```typescript
|
|
126
|
+
* @Name('usuarios')
|
|
127
|
+
* class User extends Table<User> {
|
|
128
|
+
* @Name('user_email')
|
|
129
|
+
* declare email: string;
|
|
130
|
+
* }
|
|
131
|
+
* ```
|
|
132
|
+
*/
|
|
133
|
+
function Name(label) {
|
|
134
|
+
if (!label || typeof label !== 'string') {
|
|
135
|
+
throw new TypeError('@Name requiere una cadena no vacía');
|
|
136
|
+
}
|
|
137
|
+
return (target, prop) => {
|
|
138
|
+
const ctor = prop === undefined ? target : target.constructor;
|
|
139
|
+
const schema = (0, decorator_1.ensureSchema)(ctor);
|
|
140
|
+
if (prop === undefined) {
|
|
141
|
+
// @Name en clase - renombrar tabla
|
|
142
|
+
schema.name = label;
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
// @Name en propiedad - renombrar columna
|
|
146
|
+
const column_name = String(prop);
|
|
147
|
+
if (!schema.columns[column_name]) {
|
|
148
|
+
schema.columns[column_name] = { name: column_name, get: [], set: [], store: {} };
|
|
149
|
+
}
|
|
150
|
+
schema.columns[column_name].name = label;
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
//# sourceMappingURL=transforms.js.map
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file index.ts
|
|
3
|
+
* @description Punto de entrada público de la librería - Arquitectura Minimalista
|
|
4
|
+
* @autor Miguel Alejandro
|
|
5
|
+
* @fecha 2025-01-28
|
|
6
|
+
*/
|
|
7
|
+
export { Dynamite } from "./core/client";
|
|
8
|
+
export { default as Table } from "./core/table";
|
|
9
|
+
export { decorator, relationDecorator, SCHEMA, VALUES } from "./core/decorator";
|
|
10
|
+
export { Index, IndexSort, PrimaryKey } from "./decorators/indexes";
|
|
11
|
+
export { CreatedAt, UpdatedAt, DeleteAt } from "./decorators/timestamps";
|
|
12
|
+
export { Default, Mutate, Validate, Serialize, NotNull, Name } from "./decorators/transforms";
|
|
13
|
+
export { HasMany, BelongsTo, HasOne } from "./decorators/relations";
|
|
14
|
+
export type { QueryOptions, IncludeRelationOptions, QueryOperator, } from "./core/table";
|
|
15
|
+
export { TransactionContext } from "./core/client";
|
package/src/index.js
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @file index.ts
|
|
4
|
+
* @description Punto de entrada público de la librería - Arquitectura Minimalista
|
|
5
|
+
* @autor Miguel Alejandro
|
|
6
|
+
* @fecha 2025-01-28
|
|
7
|
+
*/
|
|
8
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
9
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.TransactionContext = exports.HasOne = exports.BelongsTo = exports.HasMany = exports.Name = exports.NotNull = exports.Serialize = exports.Validate = exports.Mutate = exports.Default = exports.DeleteAt = exports.UpdatedAt = exports.CreatedAt = exports.PrimaryKey = exports.IndexSort = exports.Index = exports.VALUES = exports.SCHEMA = exports.relationDecorator = exports.decorator = exports.Table = exports.Dynamite = void 0;
|
|
13
|
+
// Clases núcleo
|
|
14
|
+
var client_1 = require("./core/client");
|
|
15
|
+
Object.defineProperty(exports, "Dynamite", { enumerable: true, get: function () { return client_1.Dynamite; } });
|
|
16
|
+
var table_1 = require("./core/table");
|
|
17
|
+
Object.defineProperty(exports, "Table", { enumerable: true, get: function () { return __importDefault(table_1).default; } });
|
|
18
|
+
// Factory para crear decoradores personalizados
|
|
19
|
+
var decorator_1 = require("./core/decorator");
|
|
20
|
+
Object.defineProperty(exports, "decorator", { enumerable: true, get: function () { return decorator_1.decorator; } });
|
|
21
|
+
Object.defineProperty(exports, "relationDecorator", { enumerable: true, get: function () { return decorator_1.relationDecorator; } });
|
|
22
|
+
Object.defineProperty(exports, "SCHEMA", { enumerable: true, get: function () { return decorator_1.SCHEMA; } });
|
|
23
|
+
Object.defineProperty(exports, "VALUES", { enumerable: true, get: function () { return decorator_1.VALUES; } });
|
|
24
|
+
// Decoradores - Índices
|
|
25
|
+
var indexes_1 = require("./decorators/indexes");
|
|
26
|
+
Object.defineProperty(exports, "Index", { enumerable: true, get: function () { return indexes_1.Index; } });
|
|
27
|
+
Object.defineProperty(exports, "IndexSort", { enumerable: true, get: function () { return indexes_1.IndexSort; } });
|
|
28
|
+
Object.defineProperty(exports, "PrimaryKey", { enumerable: true, get: function () { return indexes_1.PrimaryKey; } });
|
|
29
|
+
// Decoradores - Timestamps
|
|
30
|
+
var timestamps_1 = require("./decorators/timestamps");
|
|
31
|
+
Object.defineProperty(exports, "CreatedAt", { enumerable: true, get: function () { return timestamps_1.CreatedAt; } });
|
|
32
|
+
Object.defineProperty(exports, "UpdatedAt", { enumerable: true, get: function () { return timestamps_1.UpdatedAt; } });
|
|
33
|
+
Object.defineProperty(exports, "DeleteAt", { enumerable: true, get: function () { return timestamps_1.DeleteAt; } });
|
|
34
|
+
// Decoradores - Transformación
|
|
35
|
+
var transforms_1 = require("./decorators/transforms");
|
|
36
|
+
Object.defineProperty(exports, "Default", { enumerable: true, get: function () { return transforms_1.Default; } });
|
|
37
|
+
Object.defineProperty(exports, "Mutate", { enumerable: true, get: function () { return transforms_1.Mutate; } });
|
|
38
|
+
Object.defineProperty(exports, "Validate", { enumerable: true, get: function () { return transforms_1.Validate; } });
|
|
39
|
+
Object.defineProperty(exports, "Serialize", { enumerable: true, get: function () { return transforms_1.Serialize; } });
|
|
40
|
+
Object.defineProperty(exports, "NotNull", { enumerable: true, get: function () { return transforms_1.NotNull; } });
|
|
41
|
+
Object.defineProperty(exports, "Name", { enumerable: true, get: function () { return transforms_1.Name; } });
|
|
42
|
+
// Decoradores - Relaciones
|
|
43
|
+
var relations_1 = require("./decorators/relations");
|
|
44
|
+
Object.defineProperty(exports, "HasMany", { enumerable: true, get: function () { return relations_1.HasMany; } });
|
|
45
|
+
Object.defineProperty(exports, "BelongsTo", { enumerable: true, get: function () { return relations_1.BelongsTo; } });
|
|
46
|
+
Object.defineProperty(exports, "HasOne", { enumerable: true, get: function () { return relations_1.HasOne; } });
|
|
47
|
+
// Re-exportar TransactionContext para compatibilidad
|
|
48
|
+
var client_2 = require("./core/client");
|
|
49
|
+
Object.defineProperty(exports, "TransactionContext", { enumerable: true, get: function () { return client_2.TransactionContext; } });
|
|
50
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file index.test.ts
|
|
3
|
+
* @description Suite de testing profesional para Dynamite ORM
|
|
4
|
+
* @features
|
|
5
|
+
* - 12 fases de validación completa
|
|
6
|
+
* - Métricas de performance
|
|
7
|
+
* - Detección de memory leaks
|
|
8
|
+
* - Tests de race conditions
|
|
9
|
+
* - Validación de circular references
|
|
10
|
+
* - Bulk operations
|
|
11
|
+
* - Pipeline analysis
|
|
12
|
+
*/
|
|
13
|
+
import "reflect-metadata";
|