@elsikora/nestjs-crud-config 5.0.0-dev.3 → 5.0.0-dev.4
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/dist/modules/config/data/entity/entity.js +4 -0
- package/dist/modules/config/migration/entity/entity.js +4 -0
- package/dist/modules/config/section/entity/entity.js +4 -0
- package/dist/shared/interface/utility/create-dynamic-entity/options.interface.d.ts +4 -0
- package/dist/shared/utility/create-dynamic/entity.utility.d.ts +3 -1
- package/dist/shared/utility/create-dynamic/entity.utility.js +19 -4
- package/package.json +1 -1
|
@@ -28,6 +28,8 @@ function createConfigDataEntity(options) {
|
|
|
28
28
|
createdAt: {
|
|
29
29
|
default: () => "CURRENT_TIMESTAMP",
|
|
30
30
|
nullable: false,
|
|
31
|
+
// Without it TypeORM gives MySQL create and update date columns datetime(6), which no CURRENT_TIMESTAMP default fits
|
|
32
|
+
precision: null,
|
|
31
33
|
type: timestampColumnType,
|
|
32
34
|
},
|
|
33
35
|
description: {
|
|
@@ -53,6 +55,8 @@ function createConfigDataEntity(options) {
|
|
|
53
55
|
updatedAt: {
|
|
54
56
|
default: () => "CURRENT_TIMESTAMP",
|
|
55
57
|
nullable: false,
|
|
58
|
+
onUpdate: "CURRENT_TIMESTAMP",
|
|
59
|
+
precision: null,
|
|
56
60
|
type: timestampColumnType,
|
|
57
61
|
},
|
|
58
62
|
value: {
|
|
@@ -25,6 +25,8 @@ function createConfigMigrationEntity(options) {
|
|
|
25
25
|
createdAt: {
|
|
26
26
|
default: () => "CURRENT_TIMESTAMP",
|
|
27
27
|
nullable: false,
|
|
28
|
+
// Without it TypeORM gives MySQL create and update date columns datetime(6), which no CURRENT_TIMESTAMP default fits
|
|
29
|
+
precision: null,
|
|
28
30
|
type: timestampColumnType,
|
|
29
31
|
},
|
|
30
32
|
executedAt: {
|
|
@@ -53,6 +55,8 @@ function createConfigMigrationEntity(options) {
|
|
|
53
55
|
updatedAt: {
|
|
54
56
|
default: () => "CURRENT_TIMESTAMP",
|
|
55
57
|
nullable: false,
|
|
58
|
+
onUpdate: "CURRENT_TIMESTAMP",
|
|
59
|
+
precision: null,
|
|
56
60
|
type: timestampColumnType,
|
|
57
61
|
},
|
|
58
62
|
},
|
|
@@ -25,6 +25,8 @@ function createConfigSectionEntity(options) {
|
|
|
25
25
|
createdAt: {
|
|
26
26
|
default: () => "CURRENT_TIMESTAMP",
|
|
27
27
|
nullable: false,
|
|
28
|
+
// Without it TypeORM gives MySQL create and update date columns datetime(6), which no CURRENT_TIMESTAMP default fits
|
|
29
|
+
precision: null,
|
|
28
30
|
type: timestampColumnType,
|
|
29
31
|
},
|
|
30
32
|
description: {
|
|
@@ -40,6 +42,8 @@ function createConfigSectionEntity(options) {
|
|
|
40
42
|
updatedAt: {
|
|
41
43
|
default: () => "CURRENT_TIMESTAMP",
|
|
42
44
|
nullable: false,
|
|
45
|
+
onUpdate: "CURRENT_TIMESTAMP",
|
|
46
|
+
precision: null,
|
|
43
47
|
type: timestampColumnType,
|
|
44
48
|
},
|
|
45
49
|
},
|
|
@@ -3,6 +3,10 @@ import { ColumnOptions } from 'typeorm';
|
|
|
3
3
|
export interface ICreateDynamicEntityOptions {
|
|
4
4
|
classDecorators?: Array<ClassDecorator>;
|
|
5
5
|
columns: Record<string, ColumnOptions>;
|
|
6
|
+
/**
|
|
7
|
+
* Property decorators, applied before the column. A TypeORM column decorator among them, such as `CreateDateColumn`,
|
|
8
|
+
* sets the column's mode, and the property's `columns` options are merged into it, its own options winning.
|
|
9
|
+
*/
|
|
6
10
|
decorators?: Record<string, Array<PropertyDecorator>>;
|
|
7
11
|
indexes?: Array<{
|
|
8
12
|
columns: Array<string>;
|
|
@@ -3,7 +3,9 @@ import { ICreateDynamicEntityOptions } from '../../interface/index.js';
|
|
|
3
3
|
import { TDynamicEntity } from '../../type/index.js';
|
|
4
4
|
/**
|
|
5
5
|
* Creates a dynamic entity class with TypeORM decorators
|
|
6
|
-
* This approach allows full customization of table names and properties at runtime
|
|
6
|
+
* This approach allows full customization of table names and properties at runtime.
|
|
7
|
+
* A TypeORM column decorator in `decorators`, such as `CreateDateColumn`, sets the column's mode; the property's
|
|
8
|
+
* `columns` options are merged into it instead of registering a plain column that TypeORM would keep first.
|
|
7
9
|
* @see {@link https://elsikora.com/docs/nestjs-crud-config/api-reference/utilities/create-dynamic-entity-class | API Reference - createDynamicEntityClass}
|
|
8
10
|
* @param {ICreateDynamicEntityOptions} options Configuration options for the dynamic entity
|
|
9
11
|
* @returns {TDynamicEntity<unknown>} Dynamically created entity class with all decorators applied
|
|
@@ -1,8 +1,10 @@
|
|
|
1
|
-
import { Entity, Unique, Index, PrimaryGeneratedColumn, Column, ManyToOne, JoinColumn } from 'typeorm';
|
|
1
|
+
import { Entity, Unique, Index, PrimaryGeneratedColumn, getMetadataArgsStorage, Column, ManyToOne, JoinColumn } from 'typeorm';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Creates a dynamic entity class with TypeORM decorators
|
|
5
|
-
* This approach allows full customization of table names and properties at runtime
|
|
5
|
+
* This approach allows full customization of table names and properties at runtime.
|
|
6
|
+
* A TypeORM column decorator in `decorators`, such as `CreateDateColumn`, sets the column's mode; the property's
|
|
7
|
+
* `columns` options are merged into it instead of registering a plain column that TypeORM would keep first.
|
|
6
8
|
* @see {@link https://elsikora.com/docs/nestjs-crud-config/api-reference/utilities/create-dynamic-entity-class | API Reference - createDynamicEntityClass}
|
|
7
9
|
* @param {ICreateDynamicEntityOptions} options Configuration options for the dynamic entity
|
|
8
10
|
* @returns {TDynamicEntity<unknown>} Dynamically created entity class with all decorators applied
|
|
@@ -32,14 +34,27 @@ function createDynamicEntityClass(options) {
|
|
|
32
34
|
}
|
|
33
35
|
}
|
|
34
36
|
for (const [propertyKey, columnOptions] of Object.entries(columns)) {
|
|
35
|
-
// eslint-disable-next-line @elsikora/typescript/no-unsafe-argument
|
|
36
|
-
Column(columnOptions)(DynamicEntity.prototype, propertyKey);
|
|
37
37
|
if (decorators[propertyKey]) {
|
|
38
38
|
for (const decorator of decorators[propertyKey]) {
|
|
39
39
|
// eslint-disable-next-line @elsikora/typescript/no-unsafe-argument
|
|
40
40
|
decorator(DynamicEntity.prototype, propertyKey);
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
|
+
// TypeORM keeps the first column registered for a property, so a column decorator such as CreateDateColumn sets the
|
|
44
|
+
// mode and the column options are merged into it instead of registering a plain column ahead of it
|
|
45
|
+
const storedColumns = getMetadataArgsStorage().columns;
|
|
46
|
+
const decoratedIndex = storedColumns.findIndex((column) => column.target === DynamicEntity && column.propertyName === propertyKey);
|
|
47
|
+
const decoratedColumn = storedColumns[decoratedIndex];
|
|
48
|
+
if (decoratedColumn) {
|
|
49
|
+
storedColumns[decoratedIndex] = {
|
|
50
|
+
...decoratedColumn,
|
|
51
|
+
options: { ...columnOptions, ...decoratedColumn.options },
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
// eslint-disable-next-line @elsikora/typescript/no-unsafe-argument
|
|
56
|
+
Column(columnOptions)(DynamicEntity.prototype, propertyKey);
|
|
57
|
+
}
|
|
43
58
|
}
|
|
44
59
|
for (const [propertyKey, relationOptions] of Object.entries(relations)) {
|
|
45
60
|
const extendedOptions = relationOptions;
|
package/package.json
CHANGED