nestcraftx 0.2.6 → 0.4.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/AUDIT_ROADMAP.md +798 -0
- package/CHANGELOG.fr.md +22 -0
- package/CHANGELOG.md +22 -0
- package/PROGRESS.md +259 -0
- package/commands/demo.js +2 -42
- package/commands/generate.js +5 -4
- package/commands/help.js +29 -2
- package/commands/new.js +6 -49
- package/package.json +1 -1
- package/utils/app-module.updater.js +102 -0
- package/utils/cliParser.js +0 -45
- package/utils/configs/setupCleanArchitecture.js +2 -2
- package/utils/envGenerator.js +6 -0
- package/utils/file-system.js +90 -0
- package/utils/file-utils/packageJsonUtils.js +6 -0
- package/utils/file-utils/saveProjectConfig.js +6 -0
- package/utils/fullModeInput.js +8 -229
- package/utils/generators/application/dtoGenerator.js +252 -0
- package/utils/generators/application/dtoUpdater.js +5 -0
- package/utils/generators/database/setupDatabase.js +32 -14
- package/utils/generators/domain/entityGenerator.js +126 -0
- package/utils/generators/domain/entityUpdater.js +5 -0
- package/utils/generators/infrastructure/mapperGenerator.js +114 -0
- package/utils/generators/infrastructure/mapperUpdater.js +5 -0
- package/utils/generators/infrastructure/middlewareGenerator.js +645 -0
- package/utils/generators/infrastructure/mongooseSchemaGenerator.js +229 -0
- package/utils/generators/infrastructure/repositoryGenerator.js +334 -0
- package/utils/generators/presentation/controllerGenerator.js +142 -0
- package/utils/helpers.js +115 -0
- package/utils/interactive/askEntityInputs.js +5 -68
- package/utils/interactive/entityBuilder.js +400 -0
- package/utils/lightModeInput.js +13 -246
- package/utils/setups/orms/typeOrmSetup.js +31 -4
- package/utils/setups/projectSetup.js +78 -8
- package/utils/setups/setupAuth.js +6 -7
- package/utils/setups/setupDatabase.js +2 -1
- package/utils/setups/setupMongoose.js +96 -30
- package/utils/setups/setupPrisma.js +118 -134
- package/utils/setups/setupSwagger.js +12 -8
- package/utils/shell.js +125 -10
- package/utils/userInput.js +37 -101
- package/utils/utils.js +29 -2164
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* utils/generators/infrastructure/mongooseSchemaGenerator.js
|
|
3
|
+
*
|
|
4
|
+
* Générateur du schéma Mongoose (Document Schema).
|
|
5
|
+
* Extrait de utils.js — Phase 1B.
|
|
6
|
+
*
|
|
7
|
+
* Supporte les modes "full" et "light" :
|
|
8
|
+
* - full : chemins d'import depuis infrastructure/persistence/mongoose/
|
|
9
|
+
* - light : chemins d'import depuis entities/
|
|
10
|
+
* - Role : full → domain/enums/role.enum | light → common/enums/role.enum
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const { capitalize } = require("../../helpers");
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Vérifie si un champ est un objet relationnel (non scalaire).
|
|
17
|
+
* Utilisé pour filtrer les champs qui ne doivent pas être inclus dans le schéma.
|
|
18
|
+
*
|
|
19
|
+
* @param {object} field - Le champ { name, type }
|
|
20
|
+
* @param {object} entitiesData - Les données complètes { entities[], relations[] }
|
|
21
|
+
* @returns {boolean}
|
|
22
|
+
*/
|
|
23
|
+
function isRelationObjectField(field, entitiesData) {
|
|
24
|
+
const typeLower = field.type.toLowerCase();
|
|
25
|
+
return entitiesData.entities?.some((e) => e.name.toLowerCase() === typeLower);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Génère le contenu TypeScript du schéma Mongoose pour une entité.
|
|
30
|
+
*
|
|
31
|
+
* @param {object} entity - L'objet entité { name, fields[] }
|
|
32
|
+
* @param {object} entitiesData - { entities[], relations[] }
|
|
33
|
+
* @param {"full"|"light"} mode - Mode d'architecture
|
|
34
|
+
* @returns {Promise<string>} Le code TypeScript du schéma Mongoose
|
|
35
|
+
*/
|
|
36
|
+
async function generateMongooseSchemaFileContent(
|
|
37
|
+
entity,
|
|
38
|
+
entitiesData,
|
|
39
|
+
mode = "full",
|
|
40
|
+
) {
|
|
41
|
+
const entityName = capitalize(entity.name);
|
|
42
|
+
const entityNameLower = entity.name.toLowerCase();
|
|
43
|
+
const isFull = mode === "full";
|
|
44
|
+
|
|
45
|
+
// --- 1. IMPORTS DYNAMIQUES ---
|
|
46
|
+
const relatedEntities = entitiesData.relations
|
|
47
|
+
.filter((rel) => rel.from === entityNameLower || rel.to === entityNameLower)
|
|
48
|
+
.map((rel) => (rel.from === entityNameLower ? rel.to : rel.from));
|
|
49
|
+
|
|
50
|
+
const uniqueRelated = [...new Set(relatedEntities)].filter(
|
|
51
|
+
(e) => e !== entityNameLower,
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
let extraImports = "";
|
|
55
|
+
|
|
56
|
+
uniqueRelated.forEach((target) => {
|
|
57
|
+
const targetCap = capitalize(target);
|
|
58
|
+
const moduleName = target === "session" ? "auth" : target;
|
|
59
|
+
|
|
60
|
+
let importPath = "";
|
|
61
|
+
|
|
62
|
+
if (isFull) {
|
|
63
|
+
// Mode Clean Architecture (Full)
|
|
64
|
+
importPath = `../../../../${moduleName}/infrastructure/persistence/mongoose/${target}.schema`;
|
|
65
|
+
} else {
|
|
66
|
+
// Mode Light
|
|
67
|
+
if (target === "session") {
|
|
68
|
+
importPath = `../../auth/persistence/${target}.schema`;
|
|
69
|
+
} else {
|
|
70
|
+
importPath = `../../${moduleName}/entities/${target}.schema`;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
extraImports += `import { ${targetCap} } from '${importPath}';\n`;
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
if (entityNameLower === "user") {
|
|
78
|
+
const rolePath = isFull
|
|
79
|
+
? "../../../domain/enums/role.enum"
|
|
80
|
+
: "../../common/enums/role.enum";
|
|
81
|
+
extraImports += `import { Role } from '${rolePath}';\n`;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// --- 2. LOGIQUE DES RELATIONS DYNAMIQUES (Prioritaire) ---
|
|
85
|
+
const dynamicRelations = entitiesData.relations
|
|
86
|
+
.map((rel) => {
|
|
87
|
+
const isFrom = rel.from === entityNameLower;
|
|
88
|
+
const isTo = rel.to === entityNameLower;
|
|
89
|
+
if (!isFrom && !isTo) return null;
|
|
90
|
+
|
|
91
|
+
const otherEntity = isFrom ? rel.to : rel.from;
|
|
92
|
+
const otherCap = capitalize(otherEntity);
|
|
93
|
+
|
|
94
|
+
switch (rel.type) {
|
|
95
|
+
case "1-n":
|
|
96
|
+
return isTo
|
|
97
|
+
? ` @Prop({ type: mongoose.Schema.Types.ObjectId, ref: ${otherCap}.name, required: true })\n ${otherEntity}Id: mongoose.Types.ObjectId;`
|
|
98
|
+
: null;
|
|
99
|
+
case "n-1":
|
|
100
|
+
return isFrom
|
|
101
|
+
? ` @Prop({ type: mongoose.Schema.Types.ObjectId, ref: ${otherCap}.name, required: true })\n ${otherEntity}Id: mongoose.Types.ObjectId;`
|
|
102
|
+
: null;
|
|
103
|
+
case "1-1":
|
|
104
|
+
return isFrom
|
|
105
|
+
? ` @Prop({ type: mongoose.Schema.Types.ObjectId, ref: ${otherCap}.name, unique: true })\n ${otherEntity}Id: mongoose.Types.ObjectId;`
|
|
106
|
+
: null;
|
|
107
|
+
case "n-n":
|
|
108
|
+
return ` @Prop({ type: [{ type: mongoose.Schema.Types.ObjectId, ref: ${otherCap}.name }] })\n ${otherEntity}Ids: mongoose.Types.ObjectId[];`;
|
|
109
|
+
default:
|
|
110
|
+
return null;
|
|
111
|
+
}
|
|
112
|
+
})
|
|
113
|
+
.filter(Boolean);
|
|
114
|
+
|
|
115
|
+
// --- 3. FILTRAGE DES CHAMPS DIRECTS (Scalaires uniquement) ---
|
|
116
|
+
const scalarTypes = [
|
|
117
|
+
"string",
|
|
118
|
+
"text",
|
|
119
|
+
"number",
|
|
120
|
+
"int",
|
|
121
|
+
"float",
|
|
122
|
+
"decimal",
|
|
123
|
+
"boolean",
|
|
124
|
+
"date",
|
|
125
|
+
"uuid",
|
|
126
|
+
"json",
|
|
127
|
+
];
|
|
128
|
+
|
|
129
|
+
const directFields = entity.fields
|
|
130
|
+
.filter((f) => {
|
|
131
|
+
const nameLow = f.name.toLowerCase();
|
|
132
|
+
const isRelId = uniqueRelated.some(
|
|
133
|
+
(rel) => nameLow === rel + "id" || nameLow === rel,
|
|
134
|
+
);
|
|
135
|
+
return scalarTypes.includes(f.type.toLowerCase()) && !isRelId;
|
|
136
|
+
})
|
|
137
|
+
.map((f) => {
|
|
138
|
+
const fieldName = f.name;
|
|
139
|
+
const rawType = f.type.toLowerCase();
|
|
140
|
+
|
|
141
|
+
// 1. Gestion spécifique du Role
|
|
142
|
+
if (entityNameLower === "user" && fieldName === "role") {
|
|
143
|
+
return ` @Prop({ type: String, enum: Role, default: Role.USER })\n role: Role;`;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// 2. Mapping des types CLI -> TypeScript/Mongoose
|
|
147
|
+
let tsType = "string";
|
|
148
|
+
let propOpts = [];
|
|
149
|
+
|
|
150
|
+
switch (rawType) {
|
|
151
|
+
case "text":
|
|
152
|
+
case "uuid":
|
|
153
|
+
case "string":
|
|
154
|
+
tsType = "string";
|
|
155
|
+
propOpts.push("type: String");
|
|
156
|
+
break;
|
|
157
|
+
case "int":
|
|
158
|
+
case "number":
|
|
159
|
+
case "float":
|
|
160
|
+
case "decimal":
|
|
161
|
+
tsType = "number";
|
|
162
|
+
propOpts.push("type: Number");
|
|
163
|
+
break;
|
|
164
|
+
case "boolean":
|
|
165
|
+
tsType = "boolean";
|
|
166
|
+
propOpts.push("type: Boolean");
|
|
167
|
+
break;
|
|
168
|
+
case "date":
|
|
169
|
+
tsType = "Date";
|
|
170
|
+
propOpts.push("type: Date");
|
|
171
|
+
break;
|
|
172
|
+
case "json":
|
|
173
|
+
tsType = "Record<string, any>";
|
|
174
|
+
propOpts.push("type: Object");
|
|
175
|
+
break;
|
|
176
|
+
default:
|
|
177
|
+
tsType = "any";
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const isRequired = f.nullable !== undefined ? !f.nullable : (fieldName.toLowerCase() !== "isactive" && fieldName.toLowerCase() !== "password");
|
|
181
|
+
if (isRequired) {
|
|
182
|
+
propOpts.push("required: true");
|
|
183
|
+
}
|
|
184
|
+
if (f.unique) {
|
|
185
|
+
propOpts.push("unique: true");
|
|
186
|
+
}
|
|
187
|
+
if (f.default !== undefined && f.default !== null && f.default !== "") {
|
|
188
|
+
let defaultVal = f.default;
|
|
189
|
+
if (typeof defaultVal === "string") {
|
|
190
|
+
if (defaultVal === "true") defaultVal = true;
|
|
191
|
+
else if (defaultVal === "false") defaultVal = false;
|
|
192
|
+
else if (defaultVal.toLowerCase() === "now" || defaultVal.toLowerCase() === "now()") {
|
|
193
|
+
defaultVal = "Date.now";
|
|
194
|
+
} else if (!isNaN(defaultVal) && defaultVal.trim() !== "") {
|
|
195
|
+
defaultVal = Number(defaultVal);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
if (defaultVal === "Date.now") {
|
|
199
|
+
propOpts.push("default: Date.now");
|
|
200
|
+
} else if (typeof defaultVal === "boolean" || typeof defaultVal === "number") {
|
|
201
|
+
propOpts.push(`default: ${defaultVal}`);
|
|
202
|
+
} else {
|
|
203
|
+
propOpts.push(`default: '${defaultVal}'`);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const propOptions = propOpts.join(", ");
|
|
208
|
+
return ` @Prop({ ${propOptions} })\n ${fieldName}${f.nullable ? "?" : ""}: ${tsType};`;
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
const allFields = [...new Set([...directFields, ...dynamicRelations])].join(
|
|
212
|
+
"\n\n",
|
|
213
|
+
);
|
|
214
|
+
|
|
215
|
+
return `import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
|
|
216
|
+
import * as mongoose from 'mongoose';
|
|
217
|
+
import { Document } from 'mongoose';
|
|
218
|
+
${extraImports}
|
|
219
|
+
export type ${entityName}Document = ${entityName} & Document;
|
|
220
|
+
|
|
221
|
+
@Schema({ timestamps: true })
|
|
222
|
+
export class ${entityName} {
|
|
223
|
+
${allFields}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export const ${entityName}Schema = SchemaFactory.createForClass(${entityName});`.trim();
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
module.exports = { generateMongooseSchemaFileContent };
|
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* utils/generators/infrastructure/repositoryGenerator.js
|
|
3
|
+
*
|
|
4
|
+
* Générateur du Repository (Full/Clean Architecture uniquement).
|
|
5
|
+
* Extrait de utils.js — Phase 1B.
|
|
6
|
+
*
|
|
7
|
+
* ATTENTION : Ce générateur est UNIQUEMENT pour le mode Full (Clean Architecture).
|
|
8
|
+
* Il implémente l'interface I${Entity}Repository et utilise le Mapper.
|
|
9
|
+
*
|
|
10
|
+
* Le mode Light a son propre générateur `generateLightRepository` dans
|
|
11
|
+
* setupLightArchitecture.js avec une implémentation entièrement différente :
|
|
12
|
+
* - Pas de Mapper, méthode toEntity() locale
|
|
13
|
+
* - Imports depuis '../dtos/' et '../entities/' (chemins relatifs simples)
|
|
14
|
+
* - Pas d'interface IEntityRepository
|
|
15
|
+
*
|
|
16
|
+
* Ne jamais fusionner les deux.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
const { capitalize } = require("../../helpers");
|
|
20
|
+
const { createFile } = require("../../file-system");
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Génère et écrit le fichier repository pour une entité (Full Architecture).
|
|
24
|
+
*
|
|
25
|
+
* @param {string} entityName - Le nom de l'entité (ex: "post", "user")
|
|
26
|
+
* @param {"prisma"|"typeorm"|"mongoose"|"sequelize"} orm - L'ORM cible
|
|
27
|
+
* @returns {Promise<void>}
|
|
28
|
+
*/
|
|
29
|
+
async function generateRepository(entityName, orm) {
|
|
30
|
+
const entityNameCapitalized = capitalize(entityName);
|
|
31
|
+
const entityNameLower = entityName.toLowerCase();
|
|
32
|
+
const entityPath = `src/${entityNameLower}`;
|
|
33
|
+
|
|
34
|
+
// Méthode supplémentaire findByEmail pour l'entité User
|
|
35
|
+
const isUser = entityNameLower === "user";
|
|
36
|
+
const getExtraMethods = (ormType) => {
|
|
37
|
+
if (!isUser) return "";
|
|
38
|
+
|
|
39
|
+
switch (ormType) {
|
|
40
|
+
case "typeorm":
|
|
41
|
+
return `
|
|
42
|
+
async findByEmail(email: string): Promise<${entityNameCapitalized}Entity | null> {
|
|
43
|
+
const record = await this.repository.findOne({ where: { email } as any });
|
|
44
|
+
return record ? this.mapper.toDomain(record) : null;
|
|
45
|
+
}`;
|
|
46
|
+
case "prisma":
|
|
47
|
+
return `
|
|
48
|
+
async findByEmail(email: string): Promise<${entityNameCapitalized}Entity | null> {
|
|
49
|
+
const record = await this.prisma.${entityNameLower}.findFirst({ where: { email } });
|
|
50
|
+
return record ? this.mapper.toDomain(record) : null;
|
|
51
|
+
}`;
|
|
52
|
+
case "mongoose":
|
|
53
|
+
return `
|
|
54
|
+
async findByEmail(email: string): Promise<${entityNameCapitalized}Entity | null> {
|
|
55
|
+
const record = await this.model.findOne({ email }).exec();
|
|
56
|
+
return record ? this.mapper.toDomain(record) : null;
|
|
57
|
+
}`;
|
|
58
|
+
case "sequelize":
|
|
59
|
+
return `
|
|
60
|
+
async findByEmail(email: string): Promise<${entityNameCapitalized}Entity | null> {
|
|
61
|
+
const record = await this.model.findOne({ where: { email } });
|
|
62
|
+
return record ? this.mapper.toDomain(record) : null;
|
|
63
|
+
}`;
|
|
64
|
+
default:
|
|
65
|
+
return "";
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const extraMethods = getExtraMethods(orm);
|
|
70
|
+
|
|
71
|
+
switch (orm) {
|
|
72
|
+
case "typeorm":
|
|
73
|
+
await createFile({
|
|
74
|
+
path: `${entityPath}/infrastructure/repositories/${entityNameLower}.repository.ts`,
|
|
75
|
+
contente: `/**
|
|
76
|
+
* PostRepository handles data persistence
|
|
77
|
+
* for the Post entity.
|
|
78
|
+
*
|
|
79
|
+
* This layer abstracts the database engine (Prisma/TypeORM)
|
|
80
|
+
* and provides a clean interface for data operations.
|
|
81
|
+
*/
|
|
82
|
+
|
|
83
|
+
import { Injectable, NotFoundException } from '@nestjs/common';
|
|
84
|
+
import { Repository } from 'typeorm';
|
|
85
|
+
import { InjectRepository } from '@nestjs/typeorm';
|
|
86
|
+
import { ${entityNameCapitalized}Entity } from '${entityPath}/domain/entities/${entityNameLower}.entity';
|
|
87
|
+
import { ${entityNameCapitalized} } from 'src/entities/${entityNameCapitalized}.entity';
|
|
88
|
+
import type { I${entityNameCapitalized}Repository } from '${entityPath}/domain/interfaces/${entityNameLower}.repository.interface';
|
|
89
|
+
import { Create${entityNameCapitalized}Dto, Update${entityNameCapitalized}Dto } from '${entityPath}/application/dtos/${entityNameLower}.dto';
|
|
90
|
+
import { ${entityNameCapitalized}Mapper } from '${entityPath}/infrastructure/mappers/${entityNameLower}.mapper';
|
|
91
|
+
|
|
92
|
+
@Injectable()
|
|
93
|
+
export class ${entityNameCapitalized}Repository implements I${entityNameCapitalized}Repository {
|
|
94
|
+
constructor(
|
|
95
|
+
@InjectRepository(${entityNameCapitalized})
|
|
96
|
+
private readonly repository: Repository<${entityNameCapitalized}>,
|
|
97
|
+
private readonly mapper: ${entityNameCapitalized}Mapper,
|
|
98
|
+
) {}
|
|
99
|
+
|
|
100
|
+
// create
|
|
101
|
+
async create(data: Create${entityNameCapitalized}Dto): Promise<${entityNameCapitalized}Entity> {
|
|
102
|
+
const toPersist = this.mapper.toPersistence(data);
|
|
103
|
+
const created = await this.repository.save(toPersist);
|
|
104
|
+
return this.mapper.toDomain(created);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// find by id
|
|
108
|
+
async findById(id: string): Promise<${entityNameCapitalized}Entity | null> {
|
|
109
|
+
const record = await this.repository.findOne({
|
|
110
|
+
where: { id },
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
if (!record) return null;
|
|
114
|
+
|
|
115
|
+
return this.mapper.toDomain(record);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
${extraMethods}
|
|
119
|
+
|
|
120
|
+
// update
|
|
121
|
+
async update(id: string, data: Update${entityNameCapitalized}Dto): Promise<${entityNameCapitalized}Entity> {
|
|
122
|
+
const toUpdate = this.mapper.toUpdatePersistence(data);
|
|
123
|
+
const updated = await this.repository.save({ ...toUpdate, id });
|
|
124
|
+
return this.mapper.toDomain(updated);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// find all
|
|
128
|
+
async findAll(): Promise<${entityNameCapitalized}Entity[]> {
|
|
129
|
+
const records = await this.repository.find();
|
|
130
|
+
return records.map(record => this.mapper.toDomain(record));
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// delete
|
|
134
|
+
async delete(id: string): Promise<void> {
|
|
135
|
+
await this.repository.delete(id);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
`,
|
|
139
|
+
});
|
|
140
|
+
break;
|
|
141
|
+
|
|
142
|
+
case "prisma":
|
|
143
|
+
await createFile({
|
|
144
|
+
path: `${entityPath}/infrastructure/repositories/${entityNameLower}.repository.ts`,
|
|
145
|
+
contente: `import { Injectable, NotFoundException } from '@nestjs/common';
|
|
146
|
+
import { PrismaService } from 'src/prisma/prisma.service';
|
|
147
|
+
import { Create${entityNameCapitalized}Dto, Update${entityNameCapitalized}Dto } from '${entityPath}/application/dtos/${entityNameLower}.dto';
|
|
148
|
+
import { I${entityNameCapitalized}Repository } from '${entityPath}/domain/interfaces/${entityNameLower}.repository.interface';
|
|
149
|
+
import { ${entityNameCapitalized}Entity } from '${entityPath}/domain/entities/${entityNameLower}.entity';
|
|
150
|
+
import { ${entityNameCapitalized}Mapper } from '${entityPath}/infrastructure/mappers/${entityNameLower}.mapper';
|
|
151
|
+
|
|
152
|
+
@Injectable()
|
|
153
|
+
export class ${entityNameCapitalized}Repository implements I${entityNameCapitalized}Repository {
|
|
154
|
+
constructor(
|
|
155
|
+
private readonly prisma: PrismaService,
|
|
156
|
+
private readonly mapper: ${entityNameCapitalized}Mapper,
|
|
157
|
+
) {}
|
|
158
|
+
|
|
159
|
+
// create
|
|
160
|
+
async create(data: Create${entityNameCapitalized}Dto): Promise<${entityNameCapitalized}Entity> {
|
|
161
|
+
const toPersist = this.mapper.toPersistence(data);
|
|
162
|
+
const created = await this.prisma.${entityNameLower}.create({ data: toPersist });
|
|
163
|
+
return this.mapper.toDomain(created);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// find by id
|
|
167
|
+
async findById(id: string): Promise<${entityNameCapitalized}Entity | null> {
|
|
168
|
+
const record = await this.prisma.${entityNameLower}.findUnique({
|
|
169
|
+
where: { id },
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
if (!record) return null;
|
|
173
|
+
|
|
174
|
+
return this.mapper.toDomain(record);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
${extraMethods}
|
|
178
|
+
|
|
179
|
+
// update
|
|
180
|
+
async update(id: string, data: Update${entityNameCapitalized}Dto): Promise<${entityNameCapitalized}Entity> {
|
|
181
|
+
const toUpdate = this.mapper.toUpdatePersistence(data);
|
|
182
|
+
const updated = await this.prisma.${entityNameLower}.update({
|
|
183
|
+
where: { id },
|
|
184
|
+
data: toUpdate,
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
return this.mapper.toDomain(updated);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// find all
|
|
191
|
+
async findAll(): Promise<${entityNameCapitalized}Entity[]> {
|
|
192
|
+
const records = await this.prisma.${entityNameLower}.findMany();
|
|
193
|
+
return records.map(record => this.mapper.toDomain(record));
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// delete
|
|
197
|
+
async delete(id: string): Promise<void> {
|
|
198
|
+
await this.prisma.${entityNameLower}.delete({
|
|
199
|
+
where: { id },
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
`,
|
|
204
|
+
});
|
|
205
|
+
break;
|
|
206
|
+
|
|
207
|
+
case "mongoose":
|
|
208
|
+
await createFile({
|
|
209
|
+
path: `${entityPath}/infrastructure/repositories/${entityNameLower}.repository.ts`,
|
|
210
|
+
contente: `import { Injectable, NotFoundException } from '@nestjs/common';
|
|
211
|
+
import { InjectModel } from '@nestjs/mongoose';
|
|
212
|
+
import { Model } from 'mongoose';
|
|
213
|
+
import { ${entityNameCapitalized}Entity } from '${entityPath}/domain/entities/${entityNameLower}.entity';
|
|
214
|
+
import { Create${entityNameCapitalized}Dto, Update${entityNameCapitalized}Dto } from '${entityPath}/application/dtos/${entityNameLower}.dto';
|
|
215
|
+
import { I${entityNameCapitalized}Repository } from '${entityPath}/domain/interfaces/${entityNameLower}.repository.interface';
|
|
216
|
+
import { ${entityNameCapitalized} } from '${entityPath}/infrastructure/persistence/mongoose/${entityNameLower}.schema';
|
|
217
|
+
import { ${entityNameCapitalized}Mapper } from '${entityPath}/infrastructure/mappers/${entityNameLower}.mapper';
|
|
218
|
+
|
|
219
|
+
@Injectable()
|
|
220
|
+
export class ${entityNameCapitalized}Repository implements I${entityNameCapitalized}Repository {
|
|
221
|
+
constructor(
|
|
222
|
+
@InjectModel(${entityNameCapitalized}.name)
|
|
223
|
+
private readonly model: Model<${entityNameCapitalized}>,
|
|
224
|
+
private readonly mapper: ${entityNameCapitalized}Mapper,
|
|
225
|
+
) {}
|
|
226
|
+
|
|
227
|
+
// create
|
|
228
|
+
async create(data: Create${entityNameCapitalized}Dto): Promise<${entityNameCapitalized}Entity> {
|
|
229
|
+
const toPersist = this.mapper.toPersistence(data);
|
|
230
|
+
const created = await this.model.create(toPersist);
|
|
231
|
+
return this.mapper.toDomain(created);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// find by id
|
|
235
|
+
async findById(id: string): Promise<${entityNameCapitalized}Entity | null> {
|
|
236
|
+
const record = await this.model.findById(id).exec();
|
|
237
|
+
|
|
238
|
+
if (!record) return null;
|
|
239
|
+
|
|
240
|
+
return this.mapper.toDomain(record);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
${extraMethods}
|
|
244
|
+
|
|
245
|
+
// update
|
|
246
|
+
async update(id: string, data: Update${entityNameCapitalized}Dto): Promise<${entityNameCapitalized}Entity> {
|
|
247
|
+
const toUpdate = this.mapper.toUpdatePersistence(data);
|
|
248
|
+
const updated = await this.model.findByIdAndUpdate(id, toUpdate, { new: true }).exec();
|
|
249
|
+
return this.mapper.toDomain(updated);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// find all
|
|
253
|
+
async findAll(): Promise<${entityNameCapitalized}Entity[]> {
|
|
254
|
+
const records = await this.model.find().exec();
|
|
255
|
+
return records.map(record => this.mapper.toDomain(record));
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// delete
|
|
259
|
+
async delete(id: string): Promise<void> {
|
|
260
|
+
await this.model.findByIdAndDelete(id).exec();
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
`,
|
|
264
|
+
});
|
|
265
|
+
break;
|
|
266
|
+
|
|
267
|
+
case "sequelize":
|
|
268
|
+
await createFile({
|
|
269
|
+
path: `${entityPath}/infrastructure/repositories/${entityNameLower}.repository.ts`,
|
|
270
|
+
contente: `import { Injectable, NotFoundException } from '@nestjs/common';
|
|
271
|
+
import { InjectModel } from '@nestjs/sequelize';
|
|
272
|
+
import { Model } from 'sequelize-typescript';
|
|
273
|
+
import { Create${entityNameCapitalized}Dto, Update${entityNameCapitalized}Dto } from '${entityPath}/application/dtos/${entityNameLower}.dto';
|
|
274
|
+
import { I${entityNameCapitalized}Repository } from '${entityPath}/domain/interfaces/${entityNameLower}.repository.interface';
|
|
275
|
+
import { ${entityNameCapitalized}Entity } from '${entityPath}/domain/entities/${entityNameLower}.entity';
|
|
276
|
+
import { ${entityNameCapitalized}Mapper } from '${entityPath}/infrastructure/mappers/${entityNameLower}.mapper';
|
|
277
|
+
|
|
278
|
+
@Injectable()
|
|
279
|
+
export class ${entityNameCapitalized}Repository implements I${entityNameCapitalized}Repository {
|
|
280
|
+
constructor(
|
|
281
|
+
@InjectModel(${entityNameCapitalized}Entity)
|
|
282
|
+
private readonly model: Model<${entityNameCapitalized}Entity>,
|
|
283
|
+
private readonly mapper: ${entityNameCapitalized}Mapper,
|
|
284
|
+
) {}
|
|
285
|
+
|
|
286
|
+
// create
|
|
287
|
+
async create(data: Create${entityNameCapitalized}Dto): Promise<${entityNameCapitalized}Entity> {
|
|
288
|
+
const toPersist = this.mapper.toPersistence(data);
|
|
289
|
+
const created = await this.model.create(toPersist);
|
|
290
|
+
return this.mapper.toDomain(created);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
// find by id
|
|
294
|
+
async findById(id: string): Promise<${entityNameCapitalized}Entity> {
|
|
295
|
+
const record = await this.model.findByPk(id);
|
|
296
|
+
|
|
297
|
+
if (!record) {
|
|
298
|
+
throw new NotFoundException(\`${entityNameCapitalized}Entity with id \${id} not found\`);
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
return this.mapper.toDomain(record);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
${extraMethods}
|
|
305
|
+
|
|
306
|
+
// update
|
|
307
|
+
async update(id: string, data: Update${entityNameCapitalized}Dto): Promise<${entityNameCapitalized}Entity> {
|
|
308
|
+
const toUpdate = this.mapper.toUpdatePersistence(data);
|
|
309
|
+
const updated = await this.model.update(toUpdate, { where: { id } });
|
|
310
|
+
return this.mapper.toDomain(updated);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
// find all
|
|
314
|
+
async findAll(): Promise<${entityNameCapitalized}Entity[]> {
|
|
315
|
+
const records = await this.model.find();
|
|
316
|
+
return records.map(record => this.mapper.toDomain(record));
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// delete
|
|
320
|
+
async delete(id: string): Promise<void> {
|
|
321
|
+
await this.model.destroy({ where: { id } });
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
`,
|
|
325
|
+
});
|
|
326
|
+
break;
|
|
327
|
+
|
|
328
|
+
default:
|
|
329
|
+
console.error("Unsupported ORM: " + orm);
|
|
330
|
+
break;
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
module.exports = { generateRepository };
|