-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateFullModule.sh
executable file
·152 lines (110 loc) · 4.86 KB
/
CreateFullModule.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/bin/bash
read -p "Enter Module Name - Module name MUST BE lowercase hypen separated:
" MODULE_NAME
read -p "Enter Capitalized Module Name:
" MODULE_NAME_UPPERCASE
echo -e "\n-------------------------------------------------------------"
echo -e " Nest Module & Controller Creation "
echo -e "-------------------------------------------------------------\n"
read -p "If a module named '${MODULE_NAME}' already exists it will be removed. Continue (Y) Exit (N)
" CONSENT
if [ $CONSENT == 'Y' ]
then
echo -e "\n--------- Creating ${MODULE_NAME} ---------\n"
else
echo "Exiting..."
exit 0
fi
nest generate module ${MODULE_NAME}
nest generate controller ${MODULE_NAME}
cd src/$MODULE_NAME
rm -rf src/${MODULE_NAME}/${MODULE_NAME}.module.ts
echo -e "\n Updating module.ts \n"
cat << EOF > ${MODULE_NAME}.module.ts
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ${MODULE_NAME_UPPERCASE}Controller } from './${MODULE_NAME}.controller';
import { ${MODULE_NAME_UPPERCASE}Repository } from './${MODULE_NAME}.repository';
import { ${MODULE_NAME} as ${MODULE_NAME_UPPERCASE} } from 'src/interfaces/entities/${MODULE_NAME}';
@Module({
imports: [TypeOrmModule.forFeature([${MODULE_NAME_UPPERCASE}, ${MODULE_NAME_UPPERCASE}Repository])],
controllers: [${MODULE_NAME_UPPERCASE}Controller]
})
export class ${MODULE_NAME_UPPERCASE}Module { }
EOF
echo -e "\n Updating cotroller.ts file \n"
cat << EOF > ${MODULE_NAME}.controller.ts
import { Controller, UseGuards, Get, Post, Body, Param, Delete, Put } from '@nestjs/common';
import { ApiUseTags } from '@nestjs/swagger';
import { InjectRepository } from '@nestjs/typeorm';
import { ${MODULE_NAME_UPPERCASE}Repository } from 'src/${MODULE_NAME}/${MODULE_NAME}.repository';
import ${MODULE_NAME_UPPERCASE}Dto from 'src/${MODULE_NAME}/${MODULE_NAME}.dto';
// @UseGuards(AuthGuard('jwt'))
@ApiUseTags('${MODULE_NAME}')
@Controller('${MODULE_NAME}')
export class ${MODULE_NAME_UPPERCASE}Controller {
constructor(
@InjectRepository(${MODULE_NAME_UPPERCASE}Repository) private readonly ${MODULE_NAME}Repository: ${MODULE_NAME_UPPERCASE}Repository,
) { }
@Get(':id')
findOne(@Param('id') id: number) {
return this.${MODULE_NAME}Repository.findOne${MODULE_NAME_UPPERCASE}(id)
}
@Post()
create(@Body() ${MODULE_NAME}Dto: ${MODULE_NAME_UPPERCASE}Dto) {
return this.${MODULE_NAME}Repository.create(${MODULE_NAME}Dto);
}
@Put(':id')
update(@Param('id') id: number, @Body() ${MODULE_NAME}Dto: ${MODULE_NAME_UPPERCASE}Dto) {
return this.${MODULE_NAME}Repository.update${MODULE_NAME_UPPERCASE}(id, ${MODULE_NAME}Dto);
}
@Delete(':id')
remove(@Param('id') id: number) {
return this.${MODULE_NAME}Repository.remove${MODULE_NAME_UPPERCASE}(id);
}
}
EOF
echo -e "\n-------------------------------------------------------------"
echo " Nest Repository & DTO Creation "
echo -e "-------------------------------------------------------------\n"
touch ${MODULE_NAME}.repository.ts
touch ${MODULE_NAME}.dto.ts
echo "CREATE /src/${MODULE_NAME}/${MODULE_NAME}.repository.ts "
echo -e "CREATE /src/${MODULE_NAME}/${MODULE_NAME}.dto.ts \n"
cat << EOF > ${MODULE_NAME}.repository.ts
import { EntityRepository, Repository } from 'typeorm';
import ${MODULE_NAME_UPPERCASE}Dto from 'src/${MODULE_NAME}/${MODULE_NAME}.dto';
import { ${MODULE_NAME} as ${MODULE_NAME_UPPERCASE} } from 'src/interfaces/entities/${MODULE_NAME}';
// Now, to manipulate the ${MODULE_NAME} objects, you need to create a repository ${MODULE_NAME}/${MODULE_NAME}.repository.ts.
@EntityRepository(${MODULE_NAME_UPPERCASE})
export class ${MODULE_NAME_UPPERCASE}Repository extends Repository<${MODULE_NAME_UPPERCASE}> {
create${MODULE_NAME_UPPERCASE} = async (${MODULE_NAME}: ${MODULE_NAME_UPPERCASE}Dto) => {
return await this.save(${MODULE_NAME});
};
findOne${MODULE_NAME_UPPERCASE} = async (id: number) => {
return this.findOneOrFail(id);
};
update${MODULE_NAME_UPPERCASE} = async (id: number, ${MODULE_NAME}: ${MODULE_NAME_UPPERCASE}Dto) => {
return this.save({ ...${MODULE_NAME}, id: Number(id) });
};
remove${MODULE_NAME_UPPERCASE} = async (id: number) => {
await this.findOneOrFail(id);
return this.delete(id);
};
}
EOF
cat << EOF > ${MODULE_NAME}.dto.ts
import { ApiModelProperty } from '@nestjs/swagger';
export default class ${MODULE_NAME_UPPERCASE}Dto {
@ApiModelProperty()
readonly
}
EOF
echo -e "\n Files have been created. \n \n Please update the dto file with the necessary fields from the interface file.\n"
# Loop through the interface file and dynamically add the properties to the dto file
# while IFS='ManyToOne' read -ra FILE; do
# for i in "${FILE[@]}"; do
# # process "$i"
# echo "i ---- ${i]"
# done
# done <<< "$DTO_PROPERTY_ARRAY"