@flusys/nestjs-core 1.1.0 ā 1.2.0-rc.1
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 +113 -581
- package/cjs/config/env-config.service.js +13 -9
- package/cjs/docs/docs.config.js +25 -15
- package/cjs/index.js +0 -1
- package/config/env-config.service.d.ts +3 -1
- package/docs/docs.config.d.ts +1 -0
- package/fesm/config/env-config.service.js +13 -9
- package/fesm/docs/docs.config.js +27 -21
- package/fesm/index.js +0 -2
- package/index.d.ts +0 -1
- package/interfaces/base-entity.interface.d.ts +0 -3
- package/interfaces/database.interface.d.ts +1 -1
- package/package.json +8 -8
- package/cjs/seeders/base-seeder.js +0 -76
- package/cjs/seeders/cli.js +0 -291
- package/cjs/seeders/data-generator.js +0 -215
- package/cjs/seeders/entity-reader.js +0 -151
- package/cjs/seeders/field-patterns.js +0 -176
- package/cjs/seeders/index.js +0 -85
- package/cjs/seeders/seed-config.js +0 -62
- package/cjs/seeders/seed-runner.js +0 -314
- package/fesm/seeders/base-seeder.js +0 -66
- package/fesm/seeders/cli.js +0 -249
- package/fesm/seeders/data-generator.js +0 -210
- package/fesm/seeders/entity-reader.js +0 -153
- package/fesm/seeders/field-patterns.js +0 -147
- package/fesm/seeders/index.js +0 -7
- package/fesm/seeders/seed-config.js +0 -35
- package/fesm/seeders/seed-runner.js +0 -317
- package/seeders/base-seeder.d.ts +0 -14
- package/seeders/cli.d.ts +0 -3
- package/seeders/data-generator.d.ts +0 -15
- package/seeders/entity-reader.d.ts +0 -44
- package/seeders/field-patterns.d.ts +0 -12
- package/seeders/index.d.ts +0 -7
- package/seeders/seed-config.d.ts +0 -12
- package/seeders/seed-runner.d.ts +0 -48
|
@@ -1,176 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
function _export(target, all) {
|
|
6
|
-
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
-
enumerable: true,
|
|
8
|
-
get: Object.getOwnPropertyDescriptor(all, name).get
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
_export(exports, {
|
|
12
|
-
get AUDIT_FIELDS () {
|
|
13
|
-
return AUDIT_FIELDS;
|
|
14
|
-
},
|
|
15
|
-
get BOOLEAN_KEYWORDS () {
|
|
16
|
-
return BOOLEAN_KEYWORDS;
|
|
17
|
-
},
|
|
18
|
-
get IDENTITY_FIELDS () {
|
|
19
|
-
return IDENTITY_FIELDS;
|
|
20
|
-
},
|
|
21
|
-
get SYSTEM_FIELDS () {
|
|
22
|
-
return SYSTEM_FIELDS;
|
|
23
|
-
},
|
|
24
|
-
get detectFieldPattern () {
|
|
25
|
-
return detectFieldPattern;
|
|
26
|
-
},
|
|
27
|
-
get detectTypeCategory () {
|
|
28
|
-
return detectTypeCategory;
|
|
29
|
-
},
|
|
30
|
-
get getStringLengthCategory () {
|
|
31
|
-
return getStringLengthCategory;
|
|
32
|
-
},
|
|
33
|
-
get getTokenLength () {
|
|
34
|
-
return getTokenLength;
|
|
35
|
-
},
|
|
36
|
-
get isSystemField () {
|
|
37
|
-
return isSystemField;
|
|
38
|
-
}
|
|
39
|
-
});
|
|
40
|
-
const SYSTEM_FIELDS = [
|
|
41
|
-
'id',
|
|
42
|
-
'createdAt',
|
|
43
|
-
'updatedAt',
|
|
44
|
-
'deletedAt',
|
|
45
|
-
'createdById',
|
|
46
|
-
'updatedById',
|
|
47
|
-
'deletedById'
|
|
48
|
-
];
|
|
49
|
-
const AUDIT_FIELDS = [
|
|
50
|
-
'createdbyid',
|
|
51
|
-
'updatedbyid',
|
|
52
|
-
'deletedbyid'
|
|
53
|
-
];
|
|
54
|
-
const IDENTITY_FIELDS = [
|
|
55
|
-
'id',
|
|
56
|
-
'createdat',
|
|
57
|
-
'updatedat',
|
|
58
|
-
'deletedat'
|
|
59
|
-
];
|
|
60
|
-
const BOOLEAN_KEYWORDS = [
|
|
61
|
-
'verified',
|
|
62
|
-
'active',
|
|
63
|
-
'enabled',
|
|
64
|
-
'public',
|
|
65
|
-
'readonly',
|
|
66
|
-
'valid'
|
|
67
|
-
];
|
|
68
|
-
function detectFieldPattern(column) {
|
|
69
|
-
const nameLower = column.propertyName.toLowerCase();
|
|
70
|
-
const typeLower = column.type.toLowerCase();
|
|
71
|
-
// Identity fields (skip)
|
|
72
|
-
if (IDENTITY_FIELDS.includes(nameLower)) return 'skip';
|
|
73
|
-
// Audit fields (null)
|
|
74
|
-
if (AUDIT_FIELDS.includes(nameLower)) return 'null';
|
|
75
|
-
// Boolean fields with specific keywords
|
|
76
|
-
if ((typeLower === 'boolean' || typeLower === 'bool') && BOOLEAN_KEYWORDS.some((k)=>nameLower.includes(k))) {
|
|
77
|
-
return 'boolean';
|
|
78
|
-
}
|
|
79
|
-
// Token fields
|
|
80
|
-
if (nameLower.includes('token')) return 'token';
|
|
81
|
-
// Name fields
|
|
82
|
-
if (nameLower.includes('firstname')) return 'firstName';
|
|
83
|
-
if (nameLower.includes('lastname')) return 'lastName';
|
|
84
|
-
if (nameLower.includes('fullname') || nameLower === 'name') return 'fullName';
|
|
85
|
-
// Contact fields
|
|
86
|
-
if (nameLower.includes('email') && typeLower !== 'boolean') return 'email';
|
|
87
|
-
if (nameLower.includes('phone') || nameLower.includes('mobile')) return 'phone';
|
|
88
|
-
// Location fields
|
|
89
|
-
if (nameLower.includes('address')) return 'address';
|
|
90
|
-
if (nameLower.includes('street')) return 'street';
|
|
91
|
-
if (nameLower.includes('city')) return 'city';
|
|
92
|
-
if (nameLower.includes('state')) return 'state';
|
|
93
|
-
if (nameLower.includes('country')) return 'country';
|
|
94
|
-
if (nameLower.includes('zipcode') || nameLower.includes('postalcode')) return 'zipCode';
|
|
95
|
-
// URL/Web fields
|
|
96
|
-
if (nameLower.includes('url') || nameLower.includes('website')) return 'url';
|
|
97
|
-
if (nameLower.includes('domain')) return 'domain';
|
|
98
|
-
if (nameLower.includes('slug')) return 'slug';
|
|
99
|
-
// Text fields
|
|
100
|
-
if (nameLower.includes('description')) return 'description';
|
|
101
|
-
if (nameLower.includes('summary')) return 'summary';
|
|
102
|
-
if (nameLower.includes('content')) return 'content';
|
|
103
|
-
if (nameLower.includes('title')) return 'title';
|
|
104
|
-
// User fields
|
|
105
|
-
if (nameLower.includes('username')) return 'username';
|
|
106
|
-
if (nameLower.includes('password')) return 'password';
|
|
107
|
-
// Boolean status fields (fallback)
|
|
108
|
-
if (nameLower.includes('isactive') || nameLower.includes('isenabled') || nameLower.includes('ispublic') || nameLower.includes('isverified')) {
|
|
109
|
-
return 'boolean';
|
|
110
|
-
}
|
|
111
|
-
// Date fields
|
|
112
|
-
if (nameLower.includes('birthdate') || nameLower.includes('dateofbirth')) return 'birthdate';
|
|
113
|
-
if (nameLower.includes('expiresat') || nameLower.includes('expirydate')) return 'futureDate';
|
|
114
|
-
if (nameLower.includes('verifiedat')) return 'recentDateOrNull';
|
|
115
|
-
// Serial/Order fields
|
|
116
|
-
if (nameLower.includes('serial') || nameLower.includes('order')) return 'serial';
|
|
117
|
-
// Company
|
|
118
|
-
if (nameLower.includes('company') && !nameLower.includes('id')) return 'company';
|
|
119
|
-
// Email provider type
|
|
120
|
-
if (nameLower === 'provider' || nameLower === 'emailprovider') return 'emailProvider';
|
|
121
|
-
// Form access type
|
|
122
|
-
if (nameLower === 'accesstype' || nameLower === 'access_type') return 'formAccessType';
|
|
123
|
-
return undefined;
|
|
124
|
-
}
|
|
125
|
-
function detectTypeCategory(type) {
|
|
126
|
-
const typeLower = type.toLowerCase();
|
|
127
|
-
if ([
|
|
128
|
-
'varchar',
|
|
129
|
-
'character varying',
|
|
130
|
-
'text',
|
|
131
|
-
'string'
|
|
132
|
-
].includes(typeLower)) return 'string';
|
|
133
|
-
if ([
|
|
134
|
-
'int',
|
|
135
|
-
'integer',
|
|
136
|
-
'smallint',
|
|
137
|
-
'bigint'
|
|
138
|
-
].includes(typeLower)) return 'integer';
|
|
139
|
-
if ([
|
|
140
|
-
'decimal',
|
|
141
|
-
'numeric',
|
|
142
|
-
'float',
|
|
143
|
-
'double',
|
|
144
|
-
'real'
|
|
145
|
-
].includes(typeLower)) return 'decimal';
|
|
146
|
-
if ([
|
|
147
|
-
'boolean',
|
|
148
|
-
'bool'
|
|
149
|
-
].includes(typeLower)) return 'boolean';
|
|
150
|
-
if (typeLower === 'date') return 'date';
|
|
151
|
-
if ([
|
|
152
|
-
'timestamp',
|
|
153
|
-
'datetime'
|
|
154
|
-
].includes(typeLower)) return 'timestamp';
|
|
155
|
-
if (typeLower === 'time') return 'time';
|
|
156
|
-
if (typeLower === 'uuid') return 'uuid';
|
|
157
|
-
if ([
|
|
158
|
-
'json',
|
|
159
|
-
'jsonb'
|
|
160
|
-
].includes(typeLower)) return 'json';
|
|
161
|
-
if (typeLower === 'array') return 'array';
|
|
162
|
-
return 'unknown';
|
|
163
|
-
}
|
|
164
|
-
function getStringLengthCategory(length) {
|
|
165
|
-
const maxLength = typeof length === 'number' ? length : 255;
|
|
166
|
-
if (maxLength <= 50) return 'word';
|
|
167
|
-
if (maxLength <= 255) return 'sentence';
|
|
168
|
-
return 'paragraph';
|
|
169
|
-
}
|
|
170
|
-
function isSystemField(fieldName) {
|
|
171
|
-
return SYSTEM_FIELDS.includes(fieldName);
|
|
172
|
-
}
|
|
173
|
-
function getTokenLength(column) {
|
|
174
|
-
const maxLength = typeof column.length === 'number' ? column.length : 64;
|
|
175
|
-
return Math.min(maxLength, 32);
|
|
176
|
-
}
|
package/cjs/seeders/index.js
DELETED
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
function _export(target, all) {
|
|
6
|
-
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
-
enumerable: true,
|
|
8
|
-
get: Object.getOwnPropertyDescriptor(all, name).get
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
_export(exports, {
|
|
12
|
-
get BaseSeeder () {
|
|
13
|
-
return _baseseeder.BaseSeeder;
|
|
14
|
-
},
|
|
15
|
-
get DataGenerator () {
|
|
16
|
-
return _datagenerator.DataGenerator;
|
|
17
|
-
},
|
|
18
|
-
get EntityReader () {
|
|
19
|
-
return _entityreader.EntityReader;
|
|
20
|
-
},
|
|
21
|
-
get IColumnInfo () {
|
|
22
|
-
return _entityreader.IColumnInfo;
|
|
23
|
-
},
|
|
24
|
-
get IEntityInfo () {
|
|
25
|
-
return _entityreader.IEntityInfo;
|
|
26
|
-
},
|
|
27
|
-
get IRelationInfo () {
|
|
28
|
-
return _entityreader.IRelationInfo;
|
|
29
|
-
},
|
|
30
|
-
get ISeedConfig () {
|
|
31
|
-
return _seedconfig.ISeedConfig;
|
|
32
|
-
},
|
|
33
|
-
get ISeedOptions () {
|
|
34
|
-
return _seedrunner.ISeedOptions;
|
|
35
|
-
},
|
|
36
|
-
get ISeedResult () {
|
|
37
|
-
return _seedrunner.ISeedResult;
|
|
38
|
-
},
|
|
39
|
-
get ISeederLogger () {
|
|
40
|
-
return _seedrunner.ISeederLogger;
|
|
41
|
-
},
|
|
42
|
-
get SYSTEM_FIELDS () {
|
|
43
|
-
return _fieldpatterns.SYSTEM_FIELDS;
|
|
44
|
-
},
|
|
45
|
-
get SeedRunner () {
|
|
46
|
-
return _seedrunner.SeedRunner;
|
|
47
|
-
},
|
|
48
|
-
get configureSeedConfig () {
|
|
49
|
-
return _seedconfig.configureSeedConfig;
|
|
50
|
-
},
|
|
51
|
-
get defaultLogger () {
|
|
52
|
-
return _seedrunner.defaultLogger;
|
|
53
|
-
},
|
|
54
|
-
get detectFieldPattern () {
|
|
55
|
-
return _fieldpatterns.detectFieldPattern;
|
|
56
|
-
},
|
|
57
|
-
get detectTypeCategory () {
|
|
58
|
-
return _fieldpatterns.detectTypeCategory;
|
|
59
|
-
},
|
|
60
|
-
get getEntityCount () {
|
|
61
|
-
return _seedconfig.getEntityCount;
|
|
62
|
-
},
|
|
63
|
-
get getSeedingOrder () {
|
|
64
|
-
return _seedconfig.getSeedingOrder;
|
|
65
|
-
},
|
|
66
|
-
get isSystemField () {
|
|
67
|
-
return _fieldpatterns.isSystemField;
|
|
68
|
-
},
|
|
69
|
-
get runSeedCli () {
|
|
70
|
-
return _cli.runSeedCli;
|
|
71
|
-
},
|
|
72
|
-
get seedConfig () {
|
|
73
|
-
return _seedconfig.seedConfig;
|
|
74
|
-
},
|
|
75
|
-
get shouldSkipEntity () {
|
|
76
|
-
return _seedconfig.shouldSkipEntity;
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
const _baseseeder = require("./base-seeder");
|
|
80
|
-
const _entityreader = require("./entity-reader");
|
|
81
|
-
const _datagenerator = require("./data-generator");
|
|
82
|
-
const _seedrunner = require("./seed-runner");
|
|
83
|
-
const _seedconfig = require("./seed-config");
|
|
84
|
-
const _fieldpatterns = require("./field-patterns");
|
|
85
|
-
const _cli = require("./cli");
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
function _export(target, all) {
|
|
6
|
-
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
-
enumerable: true,
|
|
8
|
-
get: Object.getOwnPropertyDescriptor(all, name).get
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
_export(exports, {
|
|
12
|
-
get configureSeedConfig () {
|
|
13
|
-
return configureSeedConfig;
|
|
14
|
-
},
|
|
15
|
-
get getEntityCount () {
|
|
16
|
-
return getEntityCount;
|
|
17
|
-
},
|
|
18
|
-
get getSeedingOrder () {
|
|
19
|
-
return getSeedingOrder;
|
|
20
|
-
},
|
|
21
|
-
get seedConfig () {
|
|
22
|
-
return seedConfig;
|
|
23
|
-
},
|
|
24
|
-
get shouldSkipEntity () {
|
|
25
|
-
return shouldSkipEntity;
|
|
26
|
-
}
|
|
27
|
-
});
|
|
28
|
-
const seedConfig = {
|
|
29
|
-
counts: {},
|
|
30
|
-
order: [],
|
|
31
|
-
skipEntities: [
|
|
32
|
-
'migrations',
|
|
33
|
-
'typeorm_metadata',
|
|
34
|
-
'Migration',
|
|
35
|
-
'Typeorm_Metadata'
|
|
36
|
-
],
|
|
37
|
-
locale: 'en',
|
|
38
|
-
respectSoftDelete: true
|
|
39
|
-
};
|
|
40
|
-
function configureSeedConfig(config) {
|
|
41
|
-
Object.assign(seedConfig, config);
|
|
42
|
-
}
|
|
43
|
-
function getEntityCount(entityName, config = seedConfig) {
|
|
44
|
-
return config.counts[entityName] || 10;
|
|
45
|
-
}
|
|
46
|
-
function shouldSkipEntity(entityName, config = seedConfig) {
|
|
47
|
-
return config.skipEntities.some((skip)=>skip.toLowerCase() === entityName.toLowerCase());
|
|
48
|
-
}
|
|
49
|
-
function getSeedingOrder(availableEntities, config = seedConfig) {
|
|
50
|
-
const ordered = [];
|
|
51
|
-
for (const entityName of config.order){
|
|
52
|
-
if (availableEntities.includes(entityName) && !shouldSkipEntity(entityName, config)) {
|
|
53
|
-
ordered.push(entityName);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
for (const entityName of availableEntities){
|
|
57
|
-
if (!ordered.includes(entityName) && !shouldSkipEntity(entityName, config)) {
|
|
58
|
-
ordered.push(entityName);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
return ordered;
|
|
62
|
-
}
|
|
@@ -1,314 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", {
|
|
3
|
-
value: true
|
|
4
|
-
});
|
|
5
|
-
function _export(target, all) {
|
|
6
|
-
for(var name in all)Object.defineProperty(target, name, {
|
|
7
|
-
enumerable: true,
|
|
8
|
-
get: Object.getOwnPropertyDescriptor(all, name).get
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
_export(exports, {
|
|
12
|
-
get SeedRunner () {
|
|
13
|
-
return SeedRunner;
|
|
14
|
-
},
|
|
15
|
-
get defaultLogger () {
|
|
16
|
-
return defaultLogger;
|
|
17
|
-
}
|
|
18
|
-
});
|
|
19
|
-
const _entityreader = require("./entity-reader");
|
|
20
|
-
const _datagenerator = require("./data-generator");
|
|
21
|
-
const _baseseeder = require("./base-seeder");
|
|
22
|
-
const _seedconfig = require("./seed-config");
|
|
23
|
-
function _define_property(obj, key, value) {
|
|
24
|
-
if (key in obj) {
|
|
25
|
-
Object.defineProperty(obj, key, {
|
|
26
|
-
value: value,
|
|
27
|
-
enumerable: true,
|
|
28
|
-
configurable: true,
|
|
29
|
-
writable: true
|
|
30
|
-
});
|
|
31
|
-
} else {
|
|
32
|
-
obj[key] = value;
|
|
33
|
-
}
|
|
34
|
-
return obj;
|
|
35
|
-
}
|
|
36
|
-
const defaultLogger = {
|
|
37
|
-
log: (message)=>console.log(message),
|
|
38
|
-
error: (message)=>console.error(message)
|
|
39
|
-
};
|
|
40
|
-
let SeedRunner = class SeedRunner {
|
|
41
|
-
/**
|
|
42
|
-
* Register a custom seeder for specific entity
|
|
43
|
-
* Custom seeders take precedence over generic seeders
|
|
44
|
-
* @param entityName Entity name
|
|
45
|
-
* @param seeder Custom seeder instance
|
|
46
|
-
*/ registerCustomSeeder(entityName, seeder) {
|
|
47
|
-
this.customSeeders.set(entityName, seeder);
|
|
48
|
-
}
|
|
49
|
-
/**
|
|
50
|
-
* Unregister a custom seeder
|
|
51
|
-
* @param entityName Entity name
|
|
52
|
-
*/ unregisterCustomSeeder(entityName) {
|
|
53
|
-
this.customSeeders.delete(entityName);
|
|
54
|
-
}
|
|
55
|
-
/**
|
|
56
|
-
* Check if entity has custom seeder registered
|
|
57
|
-
* @param entityName Entity name
|
|
58
|
-
*/ hasCustomSeeder(entityName) {
|
|
59
|
-
return this.customSeeders.has(entityName);
|
|
60
|
-
}
|
|
61
|
-
getSeeder(entityName, entity, dataSource, entityInfo, batchSize) {
|
|
62
|
-
if (this.customSeeders.has(entityName)) {
|
|
63
|
-
return this.customSeeders.get(entityName);
|
|
64
|
-
}
|
|
65
|
-
return new GenericSeeder(dataSource, entity, this.dataGenerator, entityInfo, batchSize);
|
|
66
|
-
}
|
|
67
|
-
/**
|
|
68
|
-
* Run seeds for all entities
|
|
69
|
-
* @param options Seeding options
|
|
70
|
-
* @returns Array of seed results
|
|
71
|
-
*/ async runAll(options = {}) {
|
|
72
|
-
const results = [];
|
|
73
|
-
const continueOnError = options.continueOnError ?? true;
|
|
74
|
-
// Get all entities
|
|
75
|
-
const allEntities = this.entityReader.getAllEntities();
|
|
76
|
-
const entityNames = allEntities.map((e)=>e.name).filter((name)=>!(0, _seedconfig.shouldSkipEntity)(name));
|
|
77
|
-
// Get seeding order
|
|
78
|
-
const orderedNames = (0, _seedconfig.getSeedingOrder)(entityNames);
|
|
79
|
-
const mode = options.dryRun ? 'Previewing' : 'Seeding';
|
|
80
|
-
this.logger.log(`\nš± ${mode} ${orderedNames.length} entities...\n`);
|
|
81
|
-
for(let i = 0; i < orderedNames.length; i++){
|
|
82
|
-
const entityName = orderedNames[i];
|
|
83
|
-
// Call progress callback
|
|
84
|
-
options.onProgress?.(i + 1, orderedNames.length, entityName);
|
|
85
|
-
const result = await this.runSingle(entityName, options);
|
|
86
|
-
results.push(result);
|
|
87
|
-
// Log result
|
|
88
|
-
if (result.success) {
|
|
89
|
-
if (result.dryRun) {
|
|
90
|
-
this.logger.log(`ā ${entityName}: ${result.count} records (dry run)`);
|
|
91
|
-
} else if (result.skipped) {
|
|
92
|
-
this.logger.log(`ā ${entityName}: skipped (already has data)`);
|
|
93
|
-
} else {
|
|
94
|
-
this.logger.log(`ā ${entityName}: ${result.count} records`);
|
|
95
|
-
}
|
|
96
|
-
} else {
|
|
97
|
-
this.logger.error(`ā ${entityName}: ${result.error}`);
|
|
98
|
-
// Fail-fast if continueOnError is false
|
|
99
|
-
if (!continueOnError) {
|
|
100
|
-
this.logger.error(`\nā Seeding failed at ${entityName}, stopping...\n`);
|
|
101
|
-
break;
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
const successCount = results.filter((r)=>r.success).length;
|
|
106
|
-
const failedCount = results.length - successCount;
|
|
107
|
-
if (failedCount > 0) {
|
|
108
|
-
this.logger.log(`\nā ļø Completed with errors: ${successCount}/${results.length} entities seeded (${failedCount} failed)\n`);
|
|
109
|
-
} else {
|
|
110
|
-
this.logger.log(`\nā Completed: ${successCount}/${results.length} entities seeded\n`);
|
|
111
|
-
}
|
|
112
|
-
return results;
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* Run seed for a single entity
|
|
116
|
-
* @param entityName Entity name to seed
|
|
117
|
-
* @param options Seeding options
|
|
118
|
-
* @returns Seed result
|
|
119
|
-
*/ async runSingle(entityName, options = {}) {
|
|
120
|
-
const queryRunner = this.dataSource.createQueryRunner();
|
|
121
|
-
await queryRunner.connect();
|
|
122
|
-
await queryRunner.startTransaction();
|
|
123
|
-
try {
|
|
124
|
-
// Get entity info
|
|
125
|
-
const entityInfo = this.entityReader.getEntityInfo(entityName);
|
|
126
|
-
// Find entity target
|
|
127
|
-
const entity = this.dataSource.entityMetadatas.find((e)=>e.name === entityName)?.target;
|
|
128
|
-
if (!entity) {
|
|
129
|
-
throw new Error(`Entity ${entityName} not found`);
|
|
130
|
-
}
|
|
131
|
-
const seeder = this.getSeeder(entityName, entity, queryRunner.manager.connection, entityInfo, options.batchSize);
|
|
132
|
-
// Dry run mode - preview without executing
|
|
133
|
-
if (options.dryRun) {
|
|
134
|
-
const count = options.count ?? (0, _seedconfig.getEntityCount)(entityName);
|
|
135
|
-
await queryRunner.rollbackTransaction();
|
|
136
|
-
return {
|
|
137
|
-
entity: entityName,
|
|
138
|
-
success: true,
|
|
139
|
-
count,
|
|
140
|
-
dryRun: true
|
|
141
|
-
};
|
|
142
|
-
}
|
|
143
|
-
// Skip if exists
|
|
144
|
-
if (options.skipIfExists) {
|
|
145
|
-
const existingCount = await seeder.count();
|
|
146
|
-
if (existingCount > 0) {
|
|
147
|
-
await queryRunner.rollbackTransaction();
|
|
148
|
-
return {
|
|
149
|
-
entity: entityName,
|
|
150
|
-
success: true,
|
|
151
|
-
count: 0,
|
|
152
|
-
skipped: true
|
|
153
|
-
};
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
// Clear if requested
|
|
157
|
-
if (options.clear) {
|
|
158
|
-
await seeder.clear(options.respectSoftDelete ?? _seedconfig.seedConfig.respectSoftDelete);
|
|
159
|
-
}
|
|
160
|
-
// Generate data
|
|
161
|
-
const count = options.count ?? (0, _seedconfig.getEntityCount)(entityName);
|
|
162
|
-
const entities = await seeder.generate(count);
|
|
163
|
-
// Commit transaction
|
|
164
|
-
await queryRunner.commitTransaction();
|
|
165
|
-
return {
|
|
166
|
-
entity: entityName,
|
|
167
|
-
success: true,
|
|
168
|
-
count: entities.length
|
|
169
|
-
};
|
|
170
|
-
} catch (error) {
|
|
171
|
-
// Rollback on error
|
|
172
|
-
await queryRunner.rollbackTransaction();
|
|
173
|
-
return {
|
|
174
|
-
entity: entityName,
|
|
175
|
-
success: false,
|
|
176
|
-
count: 0,
|
|
177
|
-
error: error instanceof Error ? error.message : String(error)
|
|
178
|
-
};
|
|
179
|
-
} finally{
|
|
180
|
-
await queryRunner.release();
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
/**
|
|
184
|
-
* Clear all seeded data
|
|
185
|
-
* @param hard If true, hard delete (ignore soft delete)
|
|
186
|
-
* @param continueOnError If true, continue even if an entity fails (default: true)
|
|
187
|
-
* @returns Array of clear results
|
|
188
|
-
*/ async clearAll(hard = false, continueOnError = true) {
|
|
189
|
-
const results = [];
|
|
190
|
-
// Get all entities
|
|
191
|
-
const allEntities = this.entityReader.getAllEntities();
|
|
192
|
-
const entityNames = allEntities.map((e)=>e.name).filter((name)=>!(0, _seedconfig.shouldSkipEntity)(name));
|
|
193
|
-
// Reverse order for clearing (delete children before parents)
|
|
194
|
-
const orderedNames = (0, _seedconfig.getSeedingOrder)(entityNames).reverse();
|
|
195
|
-
const mode = hard ? 'Hard deleting' : 'Clearing';
|
|
196
|
-
this.logger.log(`\nšļø ${mode} ${orderedNames.length} entities...\n`);
|
|
197
|
-
for (const entityName of orderedNames){
|
|
198
|
-
try {
|
|
199
|
-
const entity = this.dataSource.entityMetadatas.find((e)=>e.name === entityName)?.target;
|
|
200
|
-
if (!entity) {
|
|
201
|
-
throw new Error(`Entity ${entityName} not found`);
|
|
202
|
-
}
|
|
203
|
-
const seeder = this.getSeeder(entityName, entity, this.dataSource, this.entityReader.getEntityInfo(entityName));
|
|
204
|
-
const countBefore = await seeder.count(true);
|
|
205
|
-
// Skip if already empty
|
|
206
|
-
if (countBefore === 0) {
|
|
207
|
-
results.push({
|
|
208
|
-
entity: entityName,
|
|
209
|
-
success: true,
|
|
210
|
-
count: 0
|
|
211
|
-
});
|
|
212
|
-
this.logger.log(`ā ${entityName}: already empty`);
|
|
213
|
-
continue;
|
|
214
|
-
}
|
|
215
|
-
await seeder.clear(hard);
|
|
216
|
-
const countAfter = await seeder.count(true);
|
|
217
|
-
results.push({
|
|
218
|
-
entity: entityName,
|
|
219
|
-
success: true,
|
|
220
|
-
count: countBefore - countAfter
|
|
221
|
-
});
|
|
222
|
-
this.logger.log(`ā ${entityName}: ${countBefore - countAfter} records cleared`);
|
|
223
|
-
} catch (error) {
|
|
224
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
225
|
-
results.push({
|
|
226
|
-
entity: entityName,
|
|
227
|
-
success: false,
|
|
228
|
-
count: 0,
|
|
229
|
-
error: errorMessage
|
|
230
|
-
});
|
|
231
|
-
this.logger.error(`ā ${entityName}: ${errorMessage}`);
|
|
232
|
-
// Fail-fast if continueOnError is false
|
|
233
|
-
if (!continueOnError) {
|
|
234
|
-
this.logger.error(`\nā Clearing failed at ${entityName}, stopping...\n`);
|
|
235
|
-
break;
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
const successCount = results.filter((r)=>r.success).length;
|
|
240
|
-
const failedCount = results.length - successCount;
|
|
241
|
-
if (failedCount > 0) {
|
|
242
|
-
this.logger.log(`\nā ļø Completed with errors: ${successCount}/${results.length} entities cleared (${failedCount} failed)\n`);
|
|
243
|
-
} else {
|
|
244
|
-
this.logger.log(`\nā Completed: ${successCount}/${results.length} entities cleared\n`);
|
|
245
|
-
}
|
|
246
|
-
return results;
|
|
247
|
-
}
|
|
248
|
-
/**
|
|
249
|
-
* Get status for all entities
|
|
250
|
-
* @returns Array of entity status
|
|
251
|
-
*/ async getStatus() {
|
|
252
|
-
const allEntities = this.entityReader.getAllEntities();
|
|
253
|
-
const status = [];
|
|
254
|
-
for (const metadata of allEntities){
|
|
255
|
-
if ((0, _seedconfig.shouldSkipEntity)(metadata.name)) {
|
|
256
|
-
continue;
|
|
257
|
-
}
|
|
258
|
-
const repo = this.dataSource.getRepository(metadata.target);
|
|
259
|
-
const count = await repo.count();
|
|
260
|
-
const isEmpty = count === 0;
|
|
261
|
-
const hasSoftDelete = metadata.deleteDateColumn !== undefined;
|
|
262
|
-
status.push({
|
|
263
|
-
entity: metadata.name,
|
|
264
|
-
tableName: metadata.tableName,
|
|
265
|
-
count,
|
|
266
|
-
isEmpty,
|
|
267
|
-
hasSoftDelete
|
|
268
|
-
});
|
|
269
|
-
}
|
|
270
|
-
return status;
|
|
271
|
-
}
|
|
272
|
-
constructor(dataSource, logger){
|
|
273
|
-
_define_property(this, "dataSource", void 0);
|
|
274
|
-
_define_property(this, "entityReader", void 0);
|
|
275
|
-
_define_property(this, "dataGenerator", void 0);
|
|
276
|
-
_define_property(this, "logger", void 0);
|
|
277
|
-
_define_property(this, "customSeeders", void 0);
|
|
278
|
-
this.dataSource = dataSource;
|
|
279
|
-
this.customSeeders = new Map();
|
|
280
|
-
this.entityReader = new _entityreader.EntityReader(dataSource);
|
|
281
|
-
this.dataGenerator = new _datagenerator.DataGenerator(_seedconfig.seedConfig.locale);
|
|
282
|
-
this.logger = logger ?? defaultLogger;
|
|
283
|
-
}
|
|
284
|
-
};
|
|
285
|
-
/**
|
|
286
|
-
* Generic Seeder Implementation
|
|
287
|
-
*
|
|
288
|
-
* Used by SeedRunner for entities without custom seeders.
|
|
289
|
-
* Generates data based on entity metadata with batch processing support.
|
|
290
|
-
*/ let GenericSeeder = class GenericSeeder extends _baseseeder.BaseSeeder {
|
|
291
|
-
async generate(count) {
|
|
292
|
-
const allSaved = [];
|
|
293
|
-
// Process in batches to optimize memory usage for large datasets
|
|
294
|
-
for(let offset = 0; offset < count; offset += this.batchSize){
|
|
295
|
-
const currentBatchSize = Math.min(this.batchSize, count - offset);
|
|
296
|
-
const batch = [];
|
|
297
|
-
for(let i = 0; i < currentBatchSize; i++){
|
|
298
|
-
const data = this.generator.generateEntity(this.entityInfo.columns);
|
|
299
|
-
// TypeORM's create method returns T, but TypeScript needs explicit type assertion
|
|
300
|
-
const entity = this.repository.create(data);
|
|
301
|
-
batch.push(entity);
|
|
302
|
-
}
|
|
303
|
-
// TypeORM's save method has overloads for T and T[]
|
|
304
|
-
// We need to explicitly cast to handle the generic type resolution
|
|
305
|
-
const saved = await this.repository.save(batch);
|
|
306
|
-
allSaved.push(...saved);
|
|
307
|
-
}
|
|
308
|
-
return allSaved;
|
|
309
|
-
}
|
|
310
|
-
constructor(dataSource, entity, generator, entityInfo, batchSize){
|
|
311
|
-
super(dataSource, entity), _define_property(this, "generator", void 0), _define_property(this, "entityInfo", void 0), _define_property(this, "batchSize", void 0), this.generator = generator, this.entityInfo = entityInfo;
|
|
312
|
-
this.batchSize = batchSize ?? 1000; // Default batch size
|
|
313
|
-
}
|
|
314
|
-
};
|