nodejs-structure-cli 1.0.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.
Files changed (243) hide show
  1. package/README.md +32 -0
  2. package/bin/index.js +143 -0
  3. package/lib/generator.js +145 -0
  4. package/lib/modules/app-setup.js +479 -0
  5. package/lib/modules/caching-setup.js +76 -0
  6. package/lib/modules/config-files.js +151 -0
  7. package/lib/modules/database-setup.js +116 -0
  8. package/lib/modules/kafka-setup.js +249 -0
  9. package/lib/modules/project-setup.js +32 -0
  10. package/lib/prompts.js +128 -0
  11. package/package.json +66 -0
  12. package/templates/clean-architecture/js/src/domain/models/User.js.ejs +11 -0
  13. package/templates/clean-architecture/js/src/errors/ApiError.js +14 -0
  14. package/templates/clean-architecture/js/src/errors/BadRequestError.js +11 -0
  15. package/templates/clean-architecture/js/src/errors/BadRequestError.spec.js.ejs +22 -0
  16. package/templates/clean-architecture/js/src/errors/NotFoundError.js +11 -0
  17. package/templates/clean-architecture/js/src/errors/NotFoundError.spec.js.ejs +22 -0
  18. package/templates/clean-architecture/js/src/index.js.ejs +56 -0
  19. package/templates/clean-architecture/js/src/infrastructure/config/env.js.ejs +47 -0
  20. package/templates/clean-architecture/js/src/infrastructure/log/logger.js +36 -0
  21. package/templates/clean-architecture/js/src/infrastructure/log/logger.spec.js.ejs +63 -0
  22. package/templates/clean-architecture/js/src/infrastructure/repositories/UserRepository.js.ejs +88 -0
  23. package/templates/clean-architecture/js/src/infrastructure/repositories/UserRepository.spec.js.ejs +142 -0
  24. package/templates/clean-architecture/js/src/infrastructure/webserver/middleware/errorMiddleware.js +30 -0
  25. package/templates/clean-architecture/js/src/infrastructure/webserver/server.js.ejs +93 -0
  26. package/templates/clean-architecture/js/src/infrastructure/webserver/swagger.js.ejs +6 -0
  27. package/templates/clean-architecture/js/src/interfaces/controllers/userController.js.ejs +190 -0
  28. package/templates/clean-architecture/js/src/interfaces/controllers/userController.spec.js.ejs +234 -0
  29. package/templates/clean-architecture/js/src/interfaces/graphql/context.js.ejs +13 -0
  30. package/templates/clean-architecture/js/src/interfaces/graphql/context.spec.js.ejs +31 -0
  31. package/templates/clean-architecture/js/src/interfaces/graphql/index.js.ejs +5 -0
  32. package/templates/clean-architecture/js/src/interfaces/graphql/resolvers/index.js.ejs +6 -0
  33. package/templates/clean-architecture/js/src/interfaces/graphql/resolvers/user.resolvers.js.ejs +27 -0
  34. package/templates/clean-architecture/js/src/interfaces/graphql/resolvers/user.resolvers.spec.js.ejs +66 -0
  35. package/templates/clean-architecture/js/src/interfaces/graphql/typeDefs/index.js.ejs +6 -0
  36. package/templates/clean-architecture/js/src/interfaces/graphql/typeDefs/user.types.js.ejs +19 -0
  37. package/templates/clean-architecture/js/src/interfaces/routes/api.js.ejs +17 -0
  38. package/templates/clean-architecture/js/src/interfaces/routes/api.spec.js.ejs +38 -0
  39. package/templates/clean-architecture/js/src/usecases/CreateUser.js.ejs +14 -0
  40. package/templates/clean-architecture/js/src/usecases/CreateUser.spec.js.ejs +51 -0
  41. package/templates/clean-architecture/js/src/usecases/DeleteUser.js +11 -0
  42. package/templates/clean-architecture/js/src/usecases/DeleteUser.spec.js.ejs +47 -0
  43. package/templates/clean-architecture/js/src/usecases/GetAllUsers.js +12 -0
  44. package/templates/clean-architecture/js/src/usecases/GetAllUsers.spec.js.ejs +61 -0
  45. package/templates/clean-architecture/js/src/usecases/UpdateUser.js.ejs +11 -0
  46. package/templates/clean-architecture/js/src/usecases/UpdateUser.spec.js.ejs +48 -0
  47. package/templates/clean-architecture/js/src/utils/errorMessages.js +14 -0
  48. package/templates/clean-architecture/js/src/utils/httpCodes.js +9 -0
  49. package/templates/clean-architecture/ts/src/config/env.ts.ejs +46 -0
  50. package/templates/clean-architecture/ts/src/config/swagger.ts.ejs +6 -0
  51. package/templates/clean-architecture/ts/src/domain/user.ts.ejs +9 -0
  52. package/templates/clean-architecture/ts/src/errors/ApiError.ts +15 -0
  53. package/templates/clean-architecture/ts/src/errors/BadRequestError.spec.ts.ejs +22 -0
  54. package/templates/clean-architecture/ts/src/errors/BadRequestError.ts +9 -0
  55. package/templates/clean-architecture/ts/src/errors/NotFoundError.spec.ts.ejs +22 -0
  56. package/templates/clean-architecture/ts/src/errors/NotFoundError.ts +9 -0
  57. package/templates/clean-architecture/ts/src/index.ts.ejs +144 -0
  58. package/templates/clean-architecture/ts/src/infrastructure/log/logger.spec.ts.ejs +63 -0
  59. package/templates/clean-architecture/ts/src/infrastructure/log/logger.ts +36 -0
  60. package/templates/clean-architecture/ts/src/infrastructure/repositories/UserRepository.spec.ts.ejs +175 -0
  61. package/templates/clean-architecture/ts/src/infrastructure/repositories/userRepository.ts.ejs +125 -0
  62. package/templates/clean-architecture/ts/src/interfaces/controllers/userController.spec.ts.ejs +331 -0
  63. package/templates/clean-architecture/ts/src/interfaces/controllers/userController.ts.ejs +208 -0
  64. package/templates/clean-architecture/ts/src/interfaces/graphql/context.spec.ts.ejs +32 -0
  65. package/templates/clean-architecture/ts/src/interfaces/graphql/context.ts.ejs +17 -0
  66. package/templates/clean-architecture/ts/src/interfaces/graphql/index.ts.ejs +3 -0
  67. package/templates/clean-architecture/ts/src/interfaces/graphql/resolvers/index.ts.ejs +4 -0
  68. package/templates/clean-architecture/ts/src/interfaces/graphql/resolvers/user.resolvers.spec.ts.ejs +68 -0
  69. package/templates/clean-architecture/ts/src/interfaces/graphql/resolvers/user.resolvers.ts.ejs +29 -0
  70. package/templates/clean-architecture/ts/src/interfaces/graphql/typeDefs/index.ts.ejs +4 -0
  71. package/templates/clean-architecture/ts/src/interfaces/graphql/typeDefs/user.types.ts.ejs +17 -0
  72. package/templates/clean-architecture/ts/src/interfaces/routes/userRoutes.spec.ts.ejs +40 -0
  73. package/templates/clean-architecture/ts/src/interfaces/routes/userRoutes.ts.ejs +18 -0
  74. package/templates/clean-architecture/ts/src/usecases/createUser.spec.ts.ejs +51 -0
  75. package/templates/clean-architecture/ts/src/usecases/createUser.ts.ejs +11 -0
  76. package/templates/clean-architecture/ts/src/usecases/deleteUser.spec.ts.ejs +47 -0
  77. package/templates/clean-architecture/ts/src/usecases/deleteUser.ts +9 -0
  78. package/templates/clean-architecture/ts/src/usecases/getAllUsers.spec.ts.ejs +63 -0
  79. package/templates/clean-architecture/ts/src/usecases/getAllUsers.ts +10 -0
  80. package/templates/clean-architecture/ts/src/usecases/updateUser.spec.ts.ejs +48 -0
  81. package/templates/clean-architecture/ts/src/usecases/updateUser.ts.ejs +10 -0
  82. package/templates/clean-architecture/ts/src/utils/errorMessages.ts +12 -0
  83. package/templates/clean-architecture/ts/src/utils/errorMiddleware.ts.ejs +27 -0
  84. package/templates/clean-architecture/ts/src/utils/httpCodes.ts +7 -0
  85. package/templates/common/.cursorrules.ejs +60 -0
  86. package/templates/common/.dockerignore +12 -0
  87. package/templates/common/.env.example.ejs +60 -0
  88. package/templates/common/.gitattributes +46 -0
  89. package/templates/common/.gitlab-ci.yml.ejs +86 -0
  90. package/templates/common/.lintstagedrc +6 -0
  91. package/templates/common/.prettierrc +7 -0
  92. package/templates/common/.snyk.ejs +45 -0
  93. package/templates/common/Dockerfile +73 -0
  94. package/templates/common/Jenkinsfile.ejs +87 -0
  95. package/templates/common/README.md.ejs +148 -0
  96. package/templates/common/_github/workflows/ci.yml.ejs +46 -0
  97. package/templates/common/_github/workflows/security.yml.ejs +36 -0
  98. package/templates/common/_gitignore +5 -0
  99. package/templates/common/_husky/pre-commit +4 -0
  100. package/templates/common/caching/clean/js/CreateUser.js.ejs +29 -0
  101. package/templates/common/caching/clean/js/DeleteUser.js.ejs +27 -0
  102. package/templates/common/caching/clean/js/GetAllUsers.js.ejs +37 -0
  103. package/templates/common/caching/clean/js/UpdateUser.js.ejs +27 -0
  104. package/templates/common/caching/clean/ts/createUser.ts.ejs +27 -0
  105. package/templates/common/caching/clean/ts/deleteUser.ts.ejs +24 -0
  106. package/templates/common/caching/clean/ts/getAllUsers.ts.ejs +34 -0
  107. package/templates/common/caching/clean/ts/updateUser.ts.ejs +25 -0
  108. package/templates/common/caching/js/memoryCache.js.ejs +60 -0
  109. package/templates/common/caching/js/memoryCache.spec.js.ejs +101 -0
  110. package/templates/common/caching/js/redisClient.js.ejs +75 -0
  111. package/templates/common/caching/js/redisClient.spec.js.ejs +147 -0
  112. package/templates/common/caching/ts/memoryCache.spec.ts.ejs +102 -0
  113. package/templates/common/caching/ts/memoryCache.ts.ejs +73 -0
  114. package/templates/common/caching/ts/redisClient.spec.ts.ejs +157 -0
  115. package/templates/common/caching/ts/redisClient.ts.ejs +89 -0
  116. package/templates/common/database/js/database.js.ejs +19 -0
  117. package/templates/common/database/js/database.spec.js.ejs +56 -0
  118. package/templates/common/database/js/models/User.js.ejs +91 -0
  119. package/templates/common/database/js/models/User.js.mongoose.ejs +35 -0
  120. package/templates/common/database/js/models/User.spec.js.ejs +94 -0
  121. package/templates/common/database/js/mongoose.js.ejs +33 -0
  122. package/templates/common/database/js/mongoose.spec.js.ejs +43 -0
  123. package/templates/common/database/ts/database.spec.ts.ejs +56 -0
  124. package/templates/common/database/ts/database.ts.ejs +21 -0
  125. package/templates/common/database/ts/models/User.spec.ts.ejs +100 -0
  126. package/templates/common/database/ts/models/User.ts.ejs +102 -0
  127. package/templates/common/database/ts/models/User.ts.mongoose.ejs +34 -0
  128. package/templates/common/database/ts/mongoose.spec.ts.ejs +42 -0
  129. package/templates/common/database/ts/mongoose.ts.ejs +28 -0
  130. package/templates/common/docker-compose.yml.ejs +159 -0
  131. package/templates/common/ecosystem.config.js.ejs +40 -0
  132. package/templates/common/eslint.config.mjs.ejs +77 -0
  133. package/templates/common/health/js/healthRoute.js.ejs +50 -0
  134. package/templates/common/health/js/healthRoute.spec.js.ejs +70 -0
  135. package/templates/common/health/ts/healthRoute.spec.ts.ejs +76 -0
  136. package/templates/common/health/ts/healthRoute.ts.ejs +49 -0
  137. package/templates/common/jest.config.js.ejs +32 -0
  138. package/templates/common/jest.e2e.config.js.ejs +8 -0
  139. package/templates/common/kafka/js/config/kafka.js +9 -0
  140. package/templates/common/kafka/js/config/kafka.spec.js.ejs +27 -0
  141. package/templates/common/kafka/js/messaging/baseConsumer.js.ejs +30 -0
  142. package/templates/common/kafka/js/messaging/baseConsumer.spec.js.ejs +58 -0
  143. package/templates/common/kafka/js/messaging/userEventSchema.js.ejs +12 -0
  144. package/templates/common/kafka/js/messaging/userEventSchema.spec.js.ejs +27 -0
  145. package/templates/common/kafka/js/messaging/welcomeEmailConsumer.js.ejs +44 -0
  146. package/templates/common/kafka/js/messaging/welcomeEmailConsumer.spec.js.ejs +86 -0
  147. package/templates/common/kafka/js/services/kafkaService.js.ejs +93 -0
  148. package/templates/common/kafka/js/services/kafkaService.spec.js.ejs +106 -0
  149. package/templates/common/kafka/js/utils/kafkaEvents.js.ejs +7 -0
  150. package/templates/common/kafka/ts/config/kafka.spec.ts.ejs +27 -0
  151. package/templates/common/kafka/ts/config/kafka.ts +7 -0
  152. package/templates/common/kafka/ts/messaging/baseConsumer.spec.ts.ejs +50 -0
  153. package/templates/common/kafka/ts/messaging/baseConsumer.ts.ejs +27 -0
  154. package/templates/common/kafka/ts/messaging/userEventSchema.spec.ts.ejs +51 -0
  155. package/templates/common/kafka/ts/messaging/userEventSchema.ts.ejs +12 -0
  156. package/templates/common/kafka/ts/messaging/welcomeEmailConsumer.spec.ts.ejs +86 -0
  157. package/templates/common/kafka/ts/messaging/welcomeEmailConsumer.ts.ejs +38 -0
  158. package/templates/common/kafka/ts/services/kafkaService.spec.ts.ejs +81 -0
  159. package/templates/common/kafka/ts/services/kafkaService.ts.ejs +95 -0
  160. package/templates/common/kafka/ts/utils/kafkaEvents.ts.ejs +5 -0
  161. package/templates/common/migrate-mongo-config.js.ejs +31 -0
  162. package/templates/common/migrations/init.js.ejs +23 -0
  163. package/templates/common/package.json.ejs +137 -0
  164. package/templates/common/prompts/add-feature.md.ejs +26 -0
  165. package/templates/common/prompts/project-context.md.ejs +43 -0
  166. package/templates/common/prompts/troubleshoot.md.ejs +28 -0
  167. package/templates/common/public/css/style.css +147 -0
  168. package/templates/common/scripts/run-e2e.js.ejs +63 -0
  169. package/templates/common/shutdown/js/gracefulShutdown.js.ejs +65 -0
  170. package/templates/common/shutdown/js/gracefulShutdown.spec.js.ejs +149 -0
  171. package/templates/common/shutdown/ts/gracefulShutdown.spec.ts.ejs +179 -0
  172. package/templates/common/shutdown/ts/gracefulShutdown.ts.ejs +59 -0
  173. package/templates/common/sonar-project.properties.ejs +27 -0
  174. package/templates/common/src/config/auth.js.ejs +19 -0
  175. package/templates/common/src/config/auth.ts.ejs +19 -0
  176. package/templates/common/src/controllers/authController.js.ejs +101 -0
  177. package/templates/common/src/controllers/authController.ts.ejs +101 -0
  178. package/templates/common/src/middleware/auth.js.ejs +20 -0
  179. package/templates/common/src/middleware/auth.ts.ejs +25 -0
  180. package/templates/common/src/middleware/upload.js.ejs +31 -0
  181. package/templates/common/src/middleware/upload.ts.ejs +32 -0
  182. package/templates/common/src/routes/authRoutes.js.ejs +20 -0
  183. package/templates/common/src/routes/authRoutes.ts.ejs +20 -0
  184. package/templates/common/src/tests/e2e/e2e.users.test.js.ejs +120 -0
  185. package/templates/common/src/tests/e2e/e2e.users.test.ts.ejs +120 -0
  186. package/templates/common/src/utils/errorMiddleware.spec.js.ejs +79 -0
  187. package/templates/common/src/utils/errorMiddleware.spec.ts.ejs +94 -0
  188. package/templates/common/swagger.yml.ejs +118 -0
  189. package/templates/common/tsconfig.json +23 -0
  190. package/templates/common/views/ejs/index.ejs +55 -0
  191. package/templates/common/views/pug/index.pug +40 -0
  192. package/templates/db/mysql/V1__Initial_Setup.sql.ejs +10 -0
  193. package/templates/db/postgres/V1__Initial_Setup.sql.ejs +10 -0
  194. package/templates/mvc/js/src/config/env.js.ejs +46 -0
  195. package/templates/mvc/js/src/config/swagger.js.ejs +6 -0
  196. package/templates/mvc/js/src/controllers/userController.js.ejs +288 -0
  197. package/templates/mvc/js/src/controllers/userController.spec.js.ejs +481 -0
  198. package/templates/mvc/js/src/errors/ApiError.js +14 -0
  199. package/templates/mvc/js/src/errors/BadRequestError.js +11 -0
  200. package/templates/mvc/js/src/errors/BadRequestError.spec.js.ejs +22 -0
  201. package/templates/mvc/js/src/errors/NotFoundError.js +11 -0
  202. package/templates/mvc/js/src/errors/NotFoundError.spec.js.ejs +22 -0
  203. package/templates/mvc/js/src/graphql/context.js.ejs +7 -0
  204. package/templates/mvc/js/src/graphql/context.spec.js.ejs +29 -0
  205. package/templates/mvc/js/src/graphql/index.js.ejs +5 -0
  206. package/templates/mvc/js/src/graphql/resolvers/index.js.ejs +6 -0
  207. package/templates/mvc/js/src/graphql/resolvers/user.resolvers.js.ejs +25 -0
  208. package/templates/mvc/js/src/graphql/resolvers/user.resolvers.spec.js.ejs +64 -0
  209. package/templates/mvc/js/src/graphql/typeDefs/index.js.ejs +6 -0
  210. package/templates/mvc/js/src/graphql/typeDefs/user.types.js.ejs +19 -0
  211. package/templates/mvc/js/src/index.js.ejs +141 -0
  212. package/templates/mvc/js/src/routes/api.js.ejs +15 -0
  213. package/templates/mvc/js/src/routes/api.spec.js.ejs +41 -0
  214. package/templates/mvc/js/src/utils/errorMessages.js +14 -0
  215. package/templates/mvc/js/src/utils/errorMiddleware.js +29 -0
  216. package/templates/mvc/js/src/utils/httpCodes.js +9 -0
  217. package/templates/mvc/js/src/utils/logger.js +40 -0
  218. package/templates/mvc/js/src/utils/logger.spec.js.ejs +63 -0
  219. package/templates/mvc/ts/src/config/env.ts.ejs +45 -0
  220. package/templates/mvc/ts/src/config/swagger.ts.ejs +6 -0
  221. package/templates/mvc/ts/src/controllers/userController.spec.ts.ejs +481 -0
  222. package/templates/mvc/ts/src/controllers/userController.ts.ejs +292 -0
  223. package/templates/mvc/ts/src/errors/ApiError.ts +15 -0
  224. package/templates/mvc/ts/src/errors/BadRequestError.spec.ts.ejs +22 -0
  225. package/templates/mvc/ts/src/errors/BadRequestError.ts +9 -0
  226. package/templates/mvc/ts/src/errors/NotFoundError.spec.ts.ejs +27 -0
  227. package/templates/mvc/ts/src/errors/NotFoundError.ts +9 -0
  228. package/templates/mvc/ts/src/graphql/context.spec.ts.ejs +30 -0
  229. package/templates/mvc/ts/src/graphql/context.ts.ejs +12 -0
  230. package/templates/mvc/ts/src/graphql/index.ts.ejs +3 -0
  231. package/templates/mvc/ts/src/graphql/resolvers/index.ts.ejs +4 -0
  232. package/templates/mvc/ts/src/graphql/resolvers/user.resolvers.spec.ts.ejs +68 -0
  233. package/templates/mvc/ts/src/graphql/resolvers/user.resolvers.ts.ejs +29 -0
  234. package/templates/mvc/ts/src/graphql/typeDefs/index.ts.ejs +4 -0
  235. package/templates/mvc/ts/src/graphql/typeDefs/user.types.ts.ejs +17 -0
  236. package/templates/mvc/ts/src/index.ts.ejs +157 -0
  237. package/templates/mvc/ts/src/routes/api.spec.ts.ejs +59 -0
  238. package/templates/mvc/ts/src/routes/api.ts.ejs +17 -0
  239. package/templates/mvc/ts/src/utils/errorMessages.ts +12 -0
  240. package/templates/mvc/ts/src/utils/errorMiddleware.ts.ejs +27 -0
  241. package/templates/mvc/ts/src/utils/httpCodes.ts +7 -0
  242. package/templates/mvc/ts/src/utils/logger.spec.ts.ejs +63 -0
  243. package/templates/mvc/ts/src/utils/logger.ts +36 -0
@@ -0,0 +1,56 @@
1
+ jest.mock('sequelize', () => {
2
+ const mSequelize = jest.fn(() => ({
3
+ authenticate: jest.fn().mockResolvedValue(true),
4
+ define: jest.fn(),
5
+ }));
6
+ return { Sequelize: mSequelize };
7
+ });
8
+
9
+ jest.mock('dotenv', () => ({
10
+ config: jest.fn()
11
+ }));
12
+
13
+ describe('Database Configuration', () => {
14
+ beforeEach(() => {
15
+ jest.clearAllMocks();
16
+ jest.resetModules();
17
+
18
+ // Clean environment
19
+ const envVars = ['DB_NAME', 'DB_USER', 'DB_PASSWORD', 'DB_HOST', 'DB_PORT'];
20
+ envVars.forEach(v => delete process.env[v]);
21
+ });
22
+
23
+ it('should initialize Sequelize with environment variables', () => {
24
+ const { Sequelize: SequelizeMock } = require('sequelize');
25
+ process.env.DB_NAME = 'testdb';
26
+ process.env.DB_USER = 'testuser';
27
+ process.env.DB_PASSWORD = 'testpassword';
28
+ process.env.DB_HOST = 'localhost';
29
+ process.env.DB_PORT = '5432';
30
+
31
+ require('<% if (architecture === "MVC") { %>@/config/database<% } else { %>@/infrastructure/database/database<% } %>');
32
+
33
+ expect(SequelizeMock).toHaveBeenCalledWith(
34
+ 'testdb',
35
+ 'testuser',
36
+ 'testpassword',
37
+ expect.objectContaining({
38
+ host: 'localhost',
39
+ port: 5432
40
+ })
41
+ );
42
+ });
43
+
44
+ it('should initialize Sequelize with default values when env vars are missing', () => {
45
+ const { Sequelize: SequelizeMock } = require('sequelize');
46
+ delete process.env.DB_NAME;
47
+ delete process.env.DB_USER;
48
+ delete process.env.DB_PASSWORD;
49
+ delete process.env.DB_HOST;
50
+ delete process.env.DB_PORT;
51
+
52
+ require('<% if (architecture === "MVC") { %>@/config/database<% } else { %>@/infrastructure/database/database<% } %>');
53
+
54
+ expect(SequelizeMock).toHaveBeenCalledTimes(1);
55
+ });
56
+ });
@@ -0,0 +1,91 @@
1
+ <% if (database === 'None') { -%>
2
+ class UserModel {
3
+ static mockData = [];
4
+
5
+ static async find() {
6
+ return this.mockData;
7
+ }
8
+
9
+ static async findAll() {
10
+ return this.mockData;
11
+ }
12
+
13
+ static async create(data) {
14
+ const { id, ...rest } = data;
15
+ const newUser = { id: String(this.mockData.length + 1), ...rest };
16
+ this.mockData.push(newUser);
17
+ return newUser;
18
+ }
19
+
20
+ static async findByPk(id) {
21
+ return this.mockData.find((u) => u.id === String(id));
22
+ }
23
+
24
+ static async update(id, data) {
25
+ const user = await this.findByPk(id);
26
+ if (user) {
27
+ Object.assign(user, data);
28
+ }
29
+ return user;
30
+ }
31
+
32
+ static async destroy(id) {
33
+ const index = this.mockData.findIndex((u) => u.id === String(id));
34
+ if (index !== -1) {
35
+ this.mockData.splice(index, 1);
36
+ return true;
37
+ }
38
+ return false;
39
+ }
40
+ }
41
+
42
+ module.exports = UserModel;
43
+ <% } else { -%>
44
+ const { DataTypes, Model } = require('sequelize');
45
+ <% if (architecture === 'MVC') { %>const sequelize = require('../config/database');<% } else { %>const sequelize = require('../database');<% } %>
46
+
47
+ class User extends Model {}
48
+
49
+ User.init(
50
+ {
51
+ id: {
52
+ type: DataTypes.INTEGER,
53
+ autoIncrement: true,
54
+ primaryKey: true,
55
+ },
56
+ name: {
57
+ type: DataTypes.STRING,
58
+ allowNull: false,
59
+ },
60
+ email: {
61
+ type: DataTypes.STRING,
62
+ allowNull: false,
63
+ unique: true,
64
+ },
65
+ <%_ if (auth && auth !== 'None') { _%>
66
+ password: {
67
+ type: DataTypes.STRING,
68
+ allowNull: false,
69
+ },
70
+ <%_ } _%>
71
+ <%_ if (includeMulter) { _%>
72
+ imageUrl: {
73
+ type: DataTypes.STRING,
74
+ allowNull: true,
75
+ },
76
+ <%_ } _%>
77
+ deletedAt: {
78
+ type: DataTypes.DATE,
79
+ allowNull: true,
80
+ },
81
+ },
82
+ {
83
+ sequelize,
84
+ tableName: 'users',
85
+ underscored: true,
86
+ paranoid: true,
87
+ }
88
+ );
89
+
90
+ module.exports = User;
91
+ <% } -%>
@@ -0,0 +1,35 @@
1
+ const mongoose = require('mongoose');
2
+
3
+ const UserSchema = new mongoose.Schema({
4
+ name: {
5
+ type: String,
6
+ required: true
7
+ },
8
+ email: {
9
+ type: String,
10
+ required: true,
11
+ unique: true
12
+ },
13
+ <%_ if (auth && auth !== 'None') { _%>
14
+ password: {
15
+ type: String,
16
+ required: true
17
+ },
18
+ <%_ } _%>
19
+ <%_ if (includeMulter) { _%>
20
+ imageUrl: {
21
+ type: String,
22
+ default: null
23
+ },
24
+ <%_ } _%>
25
+ createdAt: {
26
+ type: Date,
27
+ default: Date.now
28
+ },
29
+ deletedAt: {
30
+ type: Date,
31
+ default: null
32
+ }
33
+ });
34
+
35
+ module.exports = mongoose.model('User', UserSchema);
@@ -0,0 +1,94 @@
1
+ <% if (database === 'MongoDB') { -%>
2
+ const mongoose = require('mongoose');
3
+
4
+ jest.mock('mongoose', () => {
5
+ const mSchema = jest.fn();
6
+ const mModel = {
7
+ find: jest.fn(),
8
+ create: jest.fn(),
9
+ };
10
+ return {
11
+ Schema: mSchema,
12
+ model: jest.fn().mockReturnValue(mModel),
13
+ };
14
+ });
15
+ <% } else if (database === 'None') { -%>
16
+ // No external dependencies to mock for None DB
17
+ <% } else { -%>
18
+ const { Model } = require('sequelize');
19
+
20
+ jest.mock('sequelize', () => {
21
+ const mDataTypes = {
22
+ INTEGER: 'INTEGER',
23
+ STRING: 'STRING',
24
+ };
25
+ const mModel = class {
26
+ static init = jest.fn();
27
+ static findAll = jest.fn();
28
+ static create = jest.fn();
29
+ };
30
+ return { DataTypes: mDataTypes, Model: mModel };
31
+ });
32
+
33
+ <% if (architecture === 'MVC') { -%>
34
+ jest.mock('@/config/database', () => ({}));
35
+ <% } else { -%>
36
+ jest.mock('@/infrastructure/database/database', () => ({}));
37
+ <% } -%>
38
+ <% } -%>
39
+
40
+ describe('User Model', () => {
41
+ beforeEach(() => {
42
+ jest.clearAllMocks();
43
+ });
44
+
45
+ it('should be defined and initialized', () => {
46
+ const User = require('<% if (architecture === "MVC") { %>@/models/User<% } else { %>@/infrastructure/database/models/User<% } %>');
47
+ expect(User).toBeDefined();
48
+ <% if (database === 'MongoDB') { %>
49
+ expect(mongoose.model).toHaveBeenCalled();
50
+ <% } else if (database === 'None') { %>
51
+ // Test mockData is initialized
52
+ expect(User.mockData).toEqual([]);
53
+ <% } else { %>
54
+ // Sequelize init is called on the class
55
+ expect(Model.init).toHaveBeenCalled();
56
+ <% } %>
57
+ });
58
+
59
+ it('should handle model operations', async () => {
60
+ const User = require('<% if (architecture === "MVC") { %>@/models/User<% } else { %>@/infrastructure/database/models/User<% } %>');
61
+ const data = { name: 'Test', email: 'test@example.com' };
62
+
63
+ <% if (database === 'MongoDB') { %>
64
+ User.create.mockResolvedValue({ id: '1', ...data });
65
+ User.find.mockResolvedValue([{ id: '1', ...data }]);
66
+
67
+ const user = await User.create(data);
68
+ expect(user.name).toBe(data.name);
69
+ expect(await User.find()).toBeDefined();
70
+ <% } else if (database === 'None') { %>
71
+ const user = await User.create(data);
72
+ expect(user.name).toBe(data.name);
73
+ expect(await User.find()).toBeDefined();
74
+ expect(await User.findAll()).toBeDefined();
75
+
76
+ const found = await User.findByPk(user.id);
77
+ expect(found.id).toBe(user.id);
78
+
79
+ const updated = await User.update(user.id, { name: 'New Name' });
80
+ expect(updated.name).toBe('New Name');
81
+
82
+ const deleted = await User.destroy(user.id);
83
+ expect(deleted).toBe(true);
84
+ expect(await User.findByPk(user.id)).toBeUndefined();
85
+ <% } else { %>
86
+ User.create.mockResolvedValue({ id: 1, ...data });
87
+ User.findAll.mockResolvedValue([{ id: 1, ...data }]);
88
+
89
+ const user = await User.create(data);
90
+ expect(user.name).toBe(data.name);
91
+ expect(await User.findAll()).toBeDefined();
92
+ <% } %>
93
+ });
94
+ });
@@ -0,0 +1,33 @@
1
+ const mongoose = require('mongoose');
2
+
3
+ let logger;
4
+ <% if (architecture === 'MVC') { %>
5
+ logger = require('../utils/logger');
6
+ <% } else { %>
7
+ logger = require('../log/logger');
8
+ <% } %>
9
+ const connectDB = async () => {
10
+ const dbHost = process.env.DB_HOST || 'localhost';
11
+ const dbPort = process.env.DB_PORT || '27017';
12
+ const dbName = process.env.DB_NAME || '<%= dbName %>';
13
+ const mongoURI = process.env.MONGO_URI || `mongodb://${dbHost}:${dbPort}/${dbName}`;
14
+
15
+ let retries = 5;
16
+ while (retries) {
17
+ try {
18
+ await mongoose.connect(mongoURI, {
19
+ useNewUrlParser: true,
20
+ useUnifiedTopology: true
21
+ });
22
+ logger.info('MongoDB Connected...');
23
+ break;
24
+ } catch (err) {
25
+ logger.error('MongoDB connection failed:', err);
26
+ retries -= 1;
27
+ logger.info(`Retries left: ${retries}. Waiting 5s...`);
28
+ await new Promise(res => setTimeout(res, 5000));
29
+ }
30
+ }
31
+ };
32
+
33
+ module.exports = connectDB; // Export function to call in index.js
@@ -0,0 +1,43 @@
1
+ jest.mock('mongoose', () => ({
2
+ connect: jest.fn().mockResolvedValue(true),
3
+ }));
4
+
5
+ const mockLogger = {
6
+ info: jest.fn(),
7
+ error: jest.fn(),
8
+ };
9
+
10
+ jest.mock('<% if (architecture === "MVC") { %>@/utils/logger<% } else { %>@/infrastructure/log/logger<% } %>', () => mockLogger);
11
+
12
+ describe('Mongoose Configuration', () => {
13
+ beforeEach(() => {
14
+ jest.clearAllMocks();
15
+ jest.resetModules();
16
+ });
17
+
18
+ it('should call mongoose.connect with correct parameters', async () => {
19
+ const connectDB = require('<% if (architecture === "MVC") { %>@/config/database<% } else { %>@/infrastructure/database/database<% } %>');
20
+ const mongoose = require('mongoose');
21
+ await connectDB();
22
+ expect(mongoose.connect).toHaveBeenCalledWith(
23
+ expect.stringContaining('mongodb://'),
24
+ expect.any(Object)
25
+ );
26
+ });
27
+
28
+ it('should handle connection failure and retry', async () => {
29
+ const connectDB = require('<% if (architecture === "MVC") { %>@/config/database<% } else { %>@/infrastructure/database/database<% } %>');
30
+ const mongoose = require('mongoose');
31
+ mongoose.connect
32
+ .mockRejectedValueOnce(new Error('Connection failed'))
33
+ .mockResolvedValueOnce(true);
34
+
35
+ const timeoutSpy = jest.spyOn(global, 'setTimeout').mockImplementation(cb => cb());
36
+
37
+ await connectDB();
38
+
39
+ expect(mockLogger.error).toHaveBeenCalled();
40
+ expect(mongoose.connect).toHaveBeenCalledTimes(2);
41
+ timeoutSpy.mockRestore();
42
+ });
43
+ });
@@ -0,0 +1,56 @@
1
+ jest.mock('sequelize', () => {
2
+ const mSequelize = jest.fn(() => ({
3
+ authenticate: jest.fn().mockResolvedValue(true),
4
+ define: jest.fn(),
5
+ }));
6
+ return { Sequelize: mSequelize };
7
+ });
8
+
9
+ jest.mock('dotenv', () => ({
10
+ config: jest.fn()
11
+ }));
12
+
13
+ describe('Database Configuration', () => {
14
+ beforeEach(() => {
15
+ jest.clearAllMocks();
16
+ jest.resetModules();
17
+
18
+ // Clean environment
19
+ const envVars = ['DB_NAME', 'DB_USER', 'DB_PASSWORD', 'DB_HOST', 'DB_PORT'];
20
+ envVars.forEach(v => delete process.env[v]);
21
+ });
22
+
23
+ it('should initialize Sequelize with environment variables', () => {
24
+ const { Sequelize: SequelizeMock } = require('sequelize');
25
+ process.env.DB_NAME = 'testdb';
26
+ process.env.DB_USER = 'testuser';
27
+ process.env.DB_PASSWORD = 'testpassword';
28
+ process.env.DB_HOST = 'localhost';
29
+ process.env.DB_PORT = '5432';
30
+
31
+ require('<% if (architecture === "MVC") { %>@/config/database<% } else { %>@/infrastructure/database/database<% } %>');
32
+
33
+ expect(SequelizeMock).toHaveBeenLastCalledWith(
34
+ 'testdb',
35
+ 'testuser',
36
+ 'testpassword',
37
+ expect.objectContaining({
38
+ host: 'localhost',
39
+ port: 5432
40
+ })
41
+ );
42
+ });
43
+
44
+ it('should initialize Sequelize with default values when env vars are missing', () => {
45
+ const { Sequelize: SequelizeMock } = require('sequelize');
46
+ delete process.env.DB_NAME;
47
+ delete process.env.DB_USER;
48
+ delete process.env.DB_PASSWORD;
49
+ delete process.env.DB_HOST;
50
+ delete process.env.DB_PORT;
51
+
52
+ require('<% if (architecture === "MVC") { %>@/config/database<% } else { %>@/infrastructure/database/database<% } %>');
53
+
54
+ expect(SequelizeMock).toHaveBeenCalledTimes(1);
55
+ });
56
+ });
@@ -0,0 +1,21 @@
1
+ import { Sequelize } from 'sequelize';
2
+ import dotenv from 'dotenv';
3
+
4
+ dotenv.config();
5
+
6
+ <% if (database === 'MySQL') { %>const dialect = 'mysql';<% } -%>
7
+ <% if (database === 'PostgreSQL') { %>const dialect = 'postgres';<% } -%>
8
+
9
+ const sequelize = new Sequelize(
10
+ process.env.DB_NAME || '<%= dbName %>',
11
+ process.env.DB_USER || '<% if (database === 'MySQL') { %>root<% } else { %>postgres<% } %>',
12
+ process.env.DB_PASSWORD || '<% if (database === 'MySQL') { %>root<% } else { %>root<% } %>',
13
+ {
14
+ host: process.env.DB_HOST || '127.0.0.1',
15
+ dialect: dialect,
16
+ logging: false,
17
+ port: parseInt(process.env.DB_PORT || '<% if (database === 'MySQL') { %>3306<% } else { %>5432<% } %>')
18
+ }
19
+ );
20
+
21
+ export default sequelize;
@@ -0,0 +1,100 @@
1
+ <% if (database === 'MongoDB') { -%>
2
+ import mongoose from 'mongoose';
3
+
4
+ jest.mock('mongoose', () => {
5
+ const mSchema = jest.fn();
6
+ const mModel = {
7
+ find: jest.fn(),
8
+ create: jest.fn(),
9
+ };
10
+ return {
11
+ Schema: mSchema,
12
+ model: jest.fn().mockReturnValue(mModel),
13
+ };
14
+ });
15
+ <% } else if (database === 'None') { -%>
16
+ // No external dependencies to mock for None DB
17
+ <% } else { -%>
18
+ import { Model } from 'sequelize';
19
+
20
+ jest.mock('sequelize', () => {
21
+ const mDataTypes = {
22
+ INTEGER: 'INTEGER',
23
+ STRING: 'STRING',
24
+ };
25
+ const mModel = class {
26
+ static init = jest.fn();
27
+ static findAll = jest.fn();
28
+ static create = jest.fn();
29
+ };
30
+ return { DataTypes: mDataTypes, Model: mModel };
31
+ });
32
+
33
+ <% if (architecture === 'MVC') { -%>
34
+ jest.mock('@/config/database', () => ({}));
35
+ <% } else { -%>
36
+ jest.mock('@/infrastructure/database/database', () => ({}));
37
+ <% } -%>
38
+ <% } -%>
39
+
40
+ describe('User Model', () => {
41
+ beforeEach(() => {
42
+ jest.clearAllMocks();
43
+ });
44
+
45
+ it('should be defined and initialized', () => {
46
+ const User = require('<% if (architecture === "MVC") { %>@/models/User<% } else { %>@/infrastructure/database/models/User<% } %>').default;
47
+ expect(User).toBeDefined();
48
+ <% if (database === 'MongoDB') { %>
49
+ expect(mongoose.model).toHaveBeenCalled();
50
+ <% } else if (database === 'None') { %>
51
+ // Test mockData is initialized
52
+ expect(User.mockData).toEqual([]);
53
+ <% } else { %>
54
+ // Sequelize init is called on the class
55
+ expect(Model.init).toHaveBeenCalled();
56
+ <% } %>
57
+ });
58
+
59
+ it('should handle model operations', async () => {
60
+ const User = require('<% if (architecture === "MVC") { %>@/models/User<% } else { %>@/infrastructure/database/models/User<% } %>').default;
61
+ const data = { name: 'Test', email: 'test@example.com' };
62
+
63
+ <% if (database === 'MongoDB') { %>
64
+ (User.create as jest.Mock).mockResolvedValue({ id: '1', ...data });
65
+ (User.find as jest.Mock).mockResolvedValue([{ id: '1', ...data }]);
66
+
67
+ const user = await User.create(data);
68
+ expect(user.name).toBe(data.name);
69
+ expect(await User.find()).toBeDefined();
70
+ <% } else if (database === 'None') { %>
71
+ const user = await User.create(data);
72
+ expect(user.name).toBe(data.name);
73
+ expect(await User.find()).toContain(user);
74
+ expect(await User.findAll()).toContain(user);
75
+
76
+ // findByPk
77
+ const found = await User.findByPk(user.id);
78
+ expect(found).toEqual(user);
79
+ expect(await User.findByPk('invalid')).toBeUndefined();
80
+
81
+ // update
82
+ const updated = await User.update(user.id, { name: 'Updated' });
83
+ expect(updated.name).toBe('Updated');
84
+ expect(await User.update('invalid', {})).toBeUndefined();
85
+
86
+ // destroy
87
+ const deleted = await User.destroy(user.id);
88
+ expect(deleted).toBe(true);
89
+ expect(await User.destroy('invalid')).toBe(false);
90
+ expect(await User.find()).not.toContain(user);
91
+ <% } else { %>
92
+ (User.create as jest.Mock).mockResolvedValue({ id: 1, ...data });
93
+ (User.findAll as jest.Mock).mockResolvedValue([{ id: 1, ...data }]);
94
+
95
+ const user = await User.create(data);
96
+ expect(user.name).toBe(data.name);
97
+ expect(await User.findAll()).toBeDefined();
98
+ <% } %>
99
+ });
100
+ });
@@ -0,0 +1,102 @@
1
+ export interface User {
2
+ id: string;
3
+ name: string;
4
+ email: string;
5
+ <% if (auth && auth !== 'None') { %>password?: string;<% } %>
6
+ <% if (includeMulter) { %>imageUrl?: string;<% } %>
7
+ }
8
+
9
+ export default class UserModel {
10
+ static mockData: User[] = [];
11
+
12
+ static async find() {
13
+ return this.mockData;
14
+ }
15
+
16
+ static async findAll() {
17
+ return this.mockData;
18
+ }
19
+
20
+ static async create(data: Omit<User, 'id'> & { id?: string | number | null }) {
21
+ const { id: _, ...rest } = data;
22
+ const newUser: User = { id: String(this.mockData.length + 1), ...rest };
23
+ this.mockData.push(newUser);
24
+ return newUser;
25
+ }
26
+
27
+ static async findByPk(id: string | number) {
28
+ return this.mockData.find((u) => u.id === String(id));
29
+ }
30
+
31
+ static async update(id: string | number, data: Partial<User>) {
32
+ const user = await this.findByPk(id);
33
+ if (user) {
34
+ Object.assign(user, data);
35
+ }
36
+ return user;
37
+ }
38
+
39
+ static async destroy(id: string | number) {
40
+ const index = this.mockData.findIndex((u) => u.id === String(id));
41
+ if (index !== -1) {
42
+ this.mockData.splice(index, 1);
43
+ return true;
44
+ }
45
+ return false;
46
+ }
47
+ }
48
+ <% } else { -%>
49
+ import { DataTypes, Model } from 'sequelize';
50
+ <% if (architecture === 'MVC') { %>import sequelize from '@/config/database';<% } else { %>import sequelize from '@/infrastructure/database/database';<% } %>
51
+
52
+ class User extends Model {
53
+ public id!: number;
54
+ public name!: string;
55
+ public email!: string;
56
+ <% if (auth && auth !== 'None') { %>public password!: string;<% } %>
57
+ <% if (includeMulter) { %>public imageUrl!: string;<% } %>
58
+ }
59
+
60
+ User.init(
61
+ {
62
+ id: {
63
+ type: DataTypes.INTEGER,
64
+ autoIncrement: true,
65
+ primaryKey: true,
66
+ },
67
+ name: {
68
+ type: DataTypes.STRING,
69
+ allowNull: false,
70
+ },
71
+ email: {
72
+ type: DataTypes.STRING,
73
+ allowNull: false,
74
+ unique: true,
75
+ },
76
+ <% if (auth && auth !== 'None') { %>
77
+ password: {
78
+ type: DataTypes.STRING,
79
+ allowNull: true,
80
+ },
81
+ <% } %>
82
+ <% if (includeMulter) { %>
83
+ imageUrl: {
84
+ type: DataTypes.STRING,
85
+ allowNull: true,
86
+ },
87
+ <% } %>
88
+ deletedAt: {
89
+ type: DataTypes.DATE,
90
+ allowNull: true,
91
+ },
92
+ },
93
+ {
94
+ sequelize,
95
+ tableName: 'users',
96
+ underscored: true,
97
+ paranoid: true,
98
+ }
99
+ );
100
+
101
+ export default User;
102
+ <% } -%>
@@ -0,0 +1,34 @@
1
+ import mongoose, { Schema, Document } from 'mongoose';
2
+
3
+ export interface IUser extends Document {
4
+ name: string;
5
+ email: string;
6
+ <% if (auth && auth !== 'None') { %>password?: string;<% } %>
7
+ <% if (includeMulter) { %>imageUrl?: string;<% } %>
8
+ createdAt: Date;
9
+ deletedAt?: Date | null;
10
+ }
11
+
12
+ const UserSchema: Schema = new Schema({
13
+ name: {
14
+ type: String,
15
+ required: true
16
+ },
17
+ email: {
18
+ type: String,
19
+ required: true,
20
+ unique: true
21
+ },
22
+ <% if (auth && auth !== 'None') { %>password: { type: String },<% } %>
23
+ <% if (includeMulter) { %>imageUrl: { type: String },<% } %>
24
+ createdAt: {
25
+ type: Date,
26
+ default: Date.now
27
+ },
28
+ deletedAt: {
29
+ type: Date,
30
+ default: null
31
+ }
32
+ });
33
+
34
+ export default mongoose.model<IUser>('User', UserSchema);