create-origo-app 0.1.0 → 0.1.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.
Files changed (133) hide show
  1. package/README.md +315 -0
  2. package/dist/base/backend/express/.env +3 -0
  3. package/dist/base/backend/express/javascript/src/app.js +25 -0
  4. package/dist/base/backend/express/javascript/src/config/env.js +10 -0
  5. package/dist/base/backend/express/javascript/src/routes/index.js +18 -0
  6. package/dist/base/backend/express/javascript/src/server.js +28 -0
  7. package/dist/base/backend/express/manifest.js +61 -0
  8. package/dist/base/backend/express/typescript/src/app.ts +25 -0
  9. package/dist/base/backend/express/typescript/src/config/env.ts +10 -0
  10. package/dist/base/backend/express/typescript/src/routes/index.ts +18 -0
  11. package/dist/base/backend/express/typescript/src/server.ts +28 -0
  12. package/dist/base/backend/express/typescript/tsconfig.server.json +16 -0
  13. package/dist/base/backend/fastify/javascript/src/app.js +16 -0
  14. package/dist/base/backend/fastify/javascript/src/routes/index.js +18 -0
  15. package/dist/base/backend/fastify/javascript/src/server.js +27 -0
  16. package/dist/base/backend/fastify/manifest.js +61 -0
  17. package/dist/base/backend/fastify/typescript/src/app.ts +16 -0
  18. package/dist/base/backend/fastify/typescript/src/routes/index.ts +20 -0
  19. package/dist/base/backend/fastify/typescript/src/server.ts +27 -0
  20. package/dist/base/frontend/react/app/App.css +308 -0
  21. package/dist/base/frontend/react/app/App.tsx +59 -0
  22. package/dist/base/frontend/react/app/assets/react.svg +1 -0
  23. package/dist/base/frontend/react/app/assets/vite.svg +1 -0
  24. package/dist/base/frontend/react/app/components/Counter.tsx +16 -0
  25. package/dist/base/frontend/react/app/components/OrigoLogo.tsx +32 -0
  26. package/dist/base/frontend/react/app/components/PingButton.tsx +45 -0
  27. package/dist/base/frontend/react/app/index.css +27 -0
  28. package/dist/base/frontend/react/app/main.tsx +17 -0
  29. package/dist/base/frontend/react/app/vite-env.d.ts +2 -0
  30. package/dist/base/frontend/react/html/index.html +23 -0
  31. package/dist/base/frontend/react/manifest.js +180 -0
  32. package/dist/base/frontend/react/styles/bootstrap/App.tsx +61 -0
  33. package/dist/base/frontend/react/styles/bootstrap/Counter.tsx +16 -0
  34. package/dist/base/frontend/react/styles/bootstrap/PingButton.tsx +52 -0
  35. package/dist/base/frontend/react/styles/bootstrap/index.css +174 -0
  36. package/dist/base/frontend/react/styles/sass/App.scss +231 -0
  37. package/dist/base/frontend/react/styles/sass/App.tsx +59 -0
  38. package/dist/base/frontend/react/styles/sass/PingButton.scss +33 -0
  39. package/dist/base/frontend/react/styles/sass/PingButton.tsx +47 -0
  40. package/dist/base/frontend/react/styles/sass/index.scss +30 -0
  41. package/dist/base/frontend/react/styles/sass/main.tsx +17 -0
  42. package/dist/base/frontend/react/styles/tailwind/App.tsx +66 -0
  43. package/dist/base/frontend/react/styles/tailwind/Counter.tsx +16 -0
  44. package/dist/base/frontend/react/styles/tailwind/PingButton.tsx +52 -0
  45. package/dist/base/frontend/shared/favicon.ico +0 -0
  46. package/dist/base/frontend/shared/origo-logo.svg +6 -0
  47. package/dist/commands/create.js +195 -10
  48. package/dist/commands/index.js +6 -1
  49. package/dist/composer/ast/arrays.js +12 -0
  50. package/dist/composer/ast/config.js +13 -0
  51. package/dist/composer/ast/context.js +73 -0
  52. package/dist/composer/ast/expressions.js +6 -0
  53. package/dist/composer/ast/imports.js +33 -0
  54. package/dist/composer/ast/index.js +6 -0
  55. package/dist/composer/ast/objects.js +32 -0
  56. package/dist/config/architectures.js +5 -0
  57. package/dist/config/backend.js +9 -0
  58. package/dist/config/frontend.js +17 -0
  59. package/dist/config/fullstack.js +30 -0
  60. package/dist/core/copy-template.js +57 -0
  61. package/dist/core/dependency-resolver.js +43 -0
  62. package/dist/core/install-dependencies.js +10 -0
  63. package/dist/core/path-safety.js +22 -0
  64. package/dist/core/project-target.js +40 -0
  65. package/dist/core/version-resolver.js +137 -0
  66. package/dist/features/frontend/styling/tailwind/dependencies.js +10 -0
  67. package/dist/features/frontend/styling/tailwind/feature.js +11 -0
  68. package/dist/features/frontend/styling/tailwind/generator.js +13 -0
  69. package/dist/features/frontend/styling/tailwind/index.js +3 -0
  70. package/dist/features/frontend/styling/tailwind/integrations/index.js +11 -0
  71. package/dist/features/frontend/styling/tailwind/integrations/rsbuild.js +3 -0
  72. package/dist/features/frontend/styling/tailwind/integrations/vite.js +17 -0
  73. package/dist/features/frontend/styling/tailwind/integrations/webpack.js +3 -0
  74. package/dist/features/frontend/styling/tailwind/templates/tailwind-css.js +1 -0
  75. package/dist/features/frontend/styling/tailwind/templates/theme.js +39 -0
  76. package/dist/features/frontend/styling/tailwind/versions/v3.js +19 -0
  77. package/dist/features/frontend/styling/tailwind/versions/v4.js +11 -0
  78. package/dist/features/frontend/ui/shadcn/dependencies.js +8 -0
  79. package/dist/features/frontend/ui/shadcn/feature.js +11 -0
  80. package/dist/features/frontend/ui/shadcn/generator.js +13 -0
  81. package/dist/features/frontend/ui/shadcn/index.js +3 -0
  82. package/dist/features/frontend/ui/shadcn/patches/tsconfig.js +22 -0
  83. package/dist/features/frontend/ui/shadcn/patches/vite-config.js +35 -0
  84. package/dist/features/frontend/ui/shadcn/templates/components-json.js +23 -0
  85. package/dist/features/frontend/ui/shadcn/templates/lib-utils.js +14 -0
  86. package/dist/features/index.js +29 -0
  87. package/dist/generators/backend/base-loader.js +33 -0
  88. package/dist/generators/backend/context.js +54 -0
  89. package/dist/generators/backend/generate.js +72 -0
  90. package/dist/generators/backend/index.js +4 -0
  91. package/dist/generators/backend/resolvers/database.js +1277 -0
  92. package/dist/generators/frontend/base-loader.js +23 -0
  93. package/dist/generators/frontend/context.js +118 -0
  94. package/dist/generators/frontend/css.js +34 -0
  95. package/dist/generators/frontend/file-writer.js +37 -0
  96. package/dist/generators/frontend/generate.js +46 -0
  97. package/dist/generators/frontend/index.js +4 -0
  98. package/dist/generators/frontend/resolvers/base.js +24 -0
  99. package/dist/generators/frontend/resolvers/bundler.js +13 -0
  100. package/dist/generators/frontend/resolvers/index.js +5 -0
  101. package/dist/generators/frontend/resolvers/language.js +55 -0
  102. package/dist/generators/frontend/resolvers/profile.js +41 -0
  103. package/dist/generators/frontend/resolvers/typescript.js +29 -0
  104. package/dist/generators/frontend/template.js +3 -0
  105. package/dist/generators/frontend/validate.js +31 -0
  106. package/dist/generators/package-json/build-package-json.js +33 -0
  107. package/dist/generators/package-json/index.js +2 -0
  108. package/dist/generators/package-json/types.js +1 -0
  109. package/dist/generators/package-json/write-package-json.js +9 -0
  110. package/dist/prompts/architecture.js +5 -0
  111. package/dist/prompts/compatibility.js +18 -0
  112. package/dist/prompts/custom-fullstack.js +10 -0
  113. package/dist/prompts/custom-stack.js +12 -0
  114. package/dist/prompts/database-package.js +15 -0
  115. package/dist/prompts/project-name.js +25 -0
  116. package/dist/prompts/tech-stack.js +54 -0
  117. package/dist/registry/bundlers.js +4 -0
  118. package/dist/registry/databases.js +36 -0
  119. package/dist/registry/features/development.js +40 -0
  120. package/dist/registry/features/quality.js +33 -0
  121. package/dist/registry/features/testing.js +36 -0
  122. package/dist/registry/features/ui.js +37 -0
  123. package/dist/registry/frameworks.js +9 -0
  124. package/dist/registry/packages.js +231 -0
  125. package/dist/registry/routing.js +12 -0
  126. package/dist/registry/runtimes.js +7 -0
  127. package/dist/registry/styling.js +7 -0
  128. package/dist/types/project.js +1 -0
  129. package/dist/ui/banner.js +0 -1
  130. package/dist/ui/checkbox.js +51 -0
  131. package/package.json +36 -4
  132. package/dist/cli.js +0 -22
  133. package/dist/ui/multiselect.js +0 -22
@@ -0,0 +1,1277 @@
1
+ const commonEnv = [
2
+ "PORT=3000",
3
+ "NODE_ENV=development",
4
+ "CLIENT_URL=http://localhost:5173",
5
+ ];
6
+ const mongoEnv = [
7
+ "MONGODB_URL=mongodb://localhost:27017",
8
+ "MONGODB_DB_NAME=origo_db",
9
+ "MONGODB_COLLECTION_NAME=todos",
10
+ ];
11
+ const postgresEnv = [
12
+ "DATABASE_URL=postgresql://postgres:postgres@localhost:5432/origo_db",
13
+ "POSTGRES_HOST=localhost",
14
+ "POSTGRES_PORT=5432",
15
+ "POSTGRES_DB=origo_db",
16
+ "POSTGRES_USER=postgres",
17
+ "POSTGRES_PASSWORD=postgres",
18
+ "POSTGRES_TABLE=todos",
19
+ ];
20
+ function sourceFile(language, folder, name, typescript, javascript) {
21
+ return {
22
+ path: `src/${folder}/${name}.${language === "typescript" ? "ts" : "js"}`,
23
+ content: language === "typescript" ? typescript : javascript,
24
+ };
25
+ }
26
+ function databaseConfig(language, typescript, javascript) {
27
+ return sourceFile(language, "config", "database", typescript, javascript);
28
+ }
29
+ function todoModel(language, typescript, javascript) {
30
+ return sourceFile(language, "models", "todo", typescript, javascript);
31
+ }
32
+ function todoService(language, typescript, javascript) {
33
+ return sourceFile(language, "services", "todos", typescript, javascript);
34
+ }
35
+ function expressRoute(language) {
36
+ return sourceFile(language, "routes", "todos", [
37
+ 'import { Router } from "express";',
38
+ "",
39
+ "import {",
40
+ " createTodo,",
41
+ " deleteTodo,",
42
+ " getTodo,",
43
+ " listTodos,",
44
+ " updateTodo,",
45
+ '} from "../services/todos.js";',
46
+ "",
47
+ "export const todoRouter = Router();",
48
+ "",
49
+ "function errorMessage(error: unknown): string {",
50
+ ' return error instanceof Error ? error.message : "Unexpected error";',
51
+ "}",
52
+ "",
53
+ 'todoRouter.get("/", async (_request, response) => {',
54
+ " try {",
55
+ " response.json(await listTodos());",
56
+ " } catch (error) {",
57
+ " response.status(500).json({ message: errorMessage(error) });",
58
+ " }",
59
+ "});",
60
+ "",
61
+ 'todoRouter.get("/:id", async (request, response) => {',
62
+ " try {",
63
+ " response.json(await getTodo(request.params.id));",
64
+ " } catch (error) {",
65
+ " response.status(500).json({ message: errorMessage(error) });",
66
+ " }",
67
+ "});",
68
+ "",
69
+ 'todoRouter.post("/", async (request, response) => {',
70
+ " try {",
71
+ " response.status(201).json(await createTodo(request.body));",
72
+ " } catch (error) {",
73
+ " response.status(500).json({ message: errorMessage(error) });",
74
+ " }",
75
+ "});",
76
+ "",
77
+ 'todoRouter.put("/:id", async (request, response) => {',
78
+ " try {",
79
+ " response.json(await updateTodo(request.params.id, request.body));",
80
+ " } catch (error) {",
81
+ " response.status(500).json({ message: errorMessage(error) });",
82
+ " }",
83
+ "});",
84
+ "",
85
+ 'todoRouter.delete("/:id", async (request, response) => {',
86
+ " try {",
87
+ " response.json(await deleteTodo(request.params.id));",
88
+ " } catch (error) {",
89
+ " response.status(500).json({ message: errorMessage(error) });",
90
+ " }",
91
+ "});",
92
+ ].join("\n"), [
93
+ 'import { Router } from "express";',
94
+ "",
95
+ "import {",
96
+ " createTodo,",
97
+ " deleteTodo,",
98
+ " getTodo,",
99
+ " listTodos,",
100
+ " updateTodo,",
101
+ '} from "../services/todos.js";',
102
+ "",
103
+ "export const todoRouter = Router();",
104
+ "",
105
+ "function errorMessage(error) {",
106
+ ' return error instanceof Error ? error.message : "Unexpected error";',
107
+ "}",
108
+ "",
109
+ 'todoRouter.get("/", async (_request, response) => {',
110
+ " try {",
111
+ " response.json(await listTodos());",
112
+ " } catch (error) {",
113
+ " response.status(500).json({ message: errorMessage(error) });",
114
+ " }",
115
+ "});",
116
+ "",
117
+ 'todoRouter.get("/:id", async (request, response) => {',
118
+ " try {",
119
+ " response.json(await getTodo(request.params.id));",
120
+ " } catch (error) {",
121
+ " response.status(500).json({ message: errorMessage(error) });",
122
+ " }",
123
+ "});",
124
+ "",
125
+ 'todoRouter.post("/", async (request, response) => {',
126
+ " try {",
127
+ " response.status(201).json(await createTodo(request.body));",
128
+ " } catch (error) {",
129
+ " response.status(500).json({ message: errorMessage(error) });",
130
+ " }",
131
+ "});",
132
+ "",
133
+ 'todoRouter.put("/:id", async (request, response) => {',
134
+ " try {",
135
+ " response.json(await updateTodo(request.params.id, request.body));",
136
+ " } catch (error) {",
137
+ " response.status(500).json({ message: errorMessage(error) });",
138
+ " }",
139
+ "});",
140
+ "",
141
+ 'todoRouter.delete("/:id", async (request, response) => {',
142
+ " try {",
143
+ " response.json(await deleteTodo(request.params.id));",
144
+ " } catch (error) {",
145
+ " response.status(500).json({ message: errorMessage(error) });",
146
+ " }",
147
+ "});",
148
+ ].join("\n"));
149
+ }
150
+ function fastifyRoute(language) {
151
+ return sourceFile(language, "routes", "todos", [
152
+ 'import type { FastifyInstance } from "fastify";',
153
+ "",
154
+ "import {",
155
+ " createTodo,",
156
+ " deleteTodo,",
157
+ " getTodo,",
158
+ " listTodos,",
159
+ " updateTodo,",
160
+ '} from "../services/todos.js";',
161
+ "",
162
+ "function errorMessage(error: unknown): string {",
163
+ ' return error instanceof Error ? error.message : "Unexpected error";',
164
+ "}",
165
+ "",
166
+ "export async function registerTodoRoutes(app: FastifyInstance): Promise<void> {",
167
+ ' app.get("/", async (_request, reply) => {',
168
+ " try {",
169
+ " return await listTodos();",
170
+ " } catch (error) {",
171
+ " reply.code(500);",
172
+ " return { message: errorMessage(error) };",
173
+ " }",
174
+ " });",
175
+ "",
176
+ ' app.get("/:id", async (request, reply) => {',
177
+ " try {",
178
+ ' const { id } = request.params as { id: string };',
179
+ " return await getTodo(id);",
180
+ " } catch (error) {",
181
+ " reply.code(500);",
182
+ " return { message: errorMessage(error) };",
183
+ " }",
184
+ " });",
185
+ "",
186
+ ' app.post("/", async (request, reply) => {',
187
+ " try {",
188
+ ' const todo = await createTodo(request.body as { name: string; description?: string });',
189
+ " reply.code(201);",
190
+ " return todo;",
191
+ " } catch (error) {",
192
+ " reply.code(500);",
193
+ " return { message: errorMessage(error) };",
194
+ " }",
195
+ " });",
196
+ "",
197
+ ' app.put("/:id", async (request, reply) => {',
198
+ " try {",
199
+ ' const { id } = request.params as { id: string };',
200
+ " return await updateTodo(",
201
+ " id,",
202
+ ' request.body as { name?: string; description?: string }',
203
+ " );",
204
+ " } catch (error) {",
205
+ " reply.code(500);",
206
+ " return { message: errorMessage(error) };",
207
+ " }",
208
+ " });",
209
+ "",
210
+ ' app.delete("/:id", async (request, reply) => {',
211
+ " try {",
212
+ ' const { id } = request.params as { id: string };',
213
+ " return await deleteTodo(id);",
214
+ " } catch (error) {",
215
+ " reply.code(500);",
216
+ " return { message: errorMessage(error) };",
217
+ " }",
218
+ " });",
219
+ "}",
220
+ ].join("\n"), [
221
+ "import {",
222
+ " createTodo,",
223
+ " deleteTodo,",
224
+ " getTodo,",
225
+ " listTodos,",
226
+ " updateTodo,",
227
+ '} from "../services/todos.js";',
228
+ "",
229
+ "function errorMessage(error) {",
230
+ ' return error instanceof Error ? error.message : "Unexpected error";',
231
+ "}",
232
+ "",
233
+ "export async function registerTodoRoutes(app) {",
234
+ ' app.get("/", async (_request, reply) => {',
235
+ " try {",
236
+ " return await listTodos();",
237
+ " } catch (error) {",
238
+ " reply.code(500);",
239
+ " return { message: errorMessage(error) };",
240
+ " }",
241
+ " });",
242
+ "",
243
+ ' app.get("/:id", async (request, reply) => {',
244
+ " try {",
245
+ " return await getTodo(request.params.id);",
246
+ " } catch (error) {",
247
+ " reply.code(500);",
248
+ " return { message: errorMessage(error) };",
249
+ " }",
250
+ " });",
251
+ "",
252
+ ' app.post("/", async (request, reply) => {',
253
+ " try {",
254
+ " const todo = await createTodo(request.body);",
255
+ " reply.code(201);",
256
+ " return todo;",
257
+ " } catch (error) {",
258
+ " reply.code(500);",
259
+ " return { message: errorMessage(error) };",
260
+ " }",
261
+ " });",
262
+ "",
263
+ ' app.put("/:id", async (request, reply) => {',
264
+ " try {",
265
+ " return await updateTodo(request.params.id, request.body);",
266
+ " } catch (error) {",
267
+ " reply.code(500);",
268
+ " return { message: errorMessage(error) };",
269
+ " }",
270
+ " });",
271
+ "",
272
+ ' app.delete("/:id", async (request, reply) => {',
273
+ " try {",
274
+ " return await deleteTodo(request.params.id);",
275
+ " } catch (error) {",
276
+ " reply.code(500);",
277
+ " return { message: errorMessage(error) };",
278
+ " }",
279
+ " });",
280
+ "}",
281
+ ].join("\n"));
282
+ }
283
+ function mongoDriverSetup(language, framework) {
284
+ return [
285
+ databaseConfig(language, [
286
+ 'import { MongoClient, type Collection, type Db, type Document } from "mongodb";',
287
+ "",
288
+ 'const url = process.env.MONGODB_URL ?? "mongodb://localhost:27017";',
289
+ 'const databaseName = process.env.MONGODB_DB_NAME ?? "origo_db";',
290
+ 'const collectionName = process.env.MONGODB_COLLECTION_NAME ?? "todos";',
291
+ "",
292
+ "let client: MongoClient | undefined;",
293
+ "",
294
+ "export async function connectToDatabase(): Promise<Db> {",
295
+ " if (!client) {",
296
+ " client = new MongoClient(url);",
297
+ " }",
298
+ "",
299
+ " await client.connect();",
300
+ " return client.db(databaseName);",
301
+ "}",
302
+ "",
303
+ "export async function getCollection(): Promise<Collection<Document>> {",
304
+ " const database = await connectToDatabase();",
305
+ " return database.collection(collectionName);",
306
+ "}",
307
+ "",
308
+ "export async function closeDatabase(): Promise<void> {",
309
+ " await client?.close();",
310
+ " client = undefined;",
311
+ "}",
312
+ ].join("\n"), [
313
+ 'import { MongoClient } from "mongodb";',
314
+ "",
315
+ 'const url = process.env.MONGODB_URL ?? "mongodb://localhost:27017";',
316
+ 'const databaseName = process.env.MONGODB_DB_NAME ?? "origo_db";',
317
+ 'const collectionName = process.env.MONGODB_COLLECTION_NAME ?? "todos";',
318
+ "",
319
+ "let client;",
320
+ "",
321
+ "export async function connectToDatabase() {",
322
+ " if (!client) {",
323
+ " client = new MongoClient(url);",
324
+ " }",
325
+ "",
326
+ " await client.connect();",
327
+ " return client.db(databaseName);",
328
+ "}",
329
+ "",
330
+ "export async function getCollection() {",
331
+ " const database = await connectToDatabase();",
332
+ " return database.collection(collectionName);",
333
+ "}",
334
+ "",
335
+ "export async function closeDatabase() {",
336
+ " await client?.close();",
337
+ " client = undefined;",
338
+ "}",
339
+ ].join("\n")),
340
+ todoModel(language, [
341
+ 'import type { ObjectId } from "mongodb";',
342
+ "",
343
+ "export interface TodoDocument {",
344
+ " _id?: ObjectId;",
345
+ " name: string;",
346
+ " description: string;",
347
+ " createdAt: Date;",
348
+ "}",
349
+ "",
350
+ "export interface CreateTodoInput {",
351
+ " name: string;",
352
+ " description?: string;",
353
+ "}",
354
+ "",
355
+ "export interface UpdateTodoInput {",
356
+ " name?: string;",
357
+ " description?: string;",
358
+ "}",
359
+ ].join("\n"), [
360
+ "export const todoSchema = {",
361
+ " name: String,",
362
+ " description: String,",
363
+ " createdAt: Date,",
364
+ "};",
365
+ "",
366
+ "export function createTodoDocument(input) {",
367
+ " return {",
368
+ " name: input.name,",
369
+ ' description: input.description ?? "",',
370
+ " createdAt: new Date(),",
371
+ " };",
372
+ "}",
373
+ ].join("\n")),
374
+ todoService(language, [
375
+ 'import { ObjectId } from "mongodb";',
376
+ "",
377
+ 'import { getCollection } from "../config/database.js";',
378
+ 'import type { CreateTodoInput, UpdateTodoInput } from "../models/todo.js";',
379
+ "",
380
+ "function objectId(id: string): ObjectId {",
381
+ ' if (!ObjectId.isValid(id)) throw new Error("Invalid todo id");',
382
+ " return new ObjectId(id);",
383
+ "}",
384
+ "",
385
+ "export async function listTodos() {",
386
+ " const collection = await getCollection();",
387
+ " return collection.find().sort({ createdAt: -1 }).toArray();",
388
+ "}",
389
+ "",
390
+ "export async function getTodo(id: string) {",
391
+ " const collection = await getCollection();",
392
+ " const todo = await collection.findOne({ _id: objectId(id) });",
393
+ ' if (!todo) throw new Error("Todo not found");',
394
+ " return todo;",
395
+ "}",
396
+ "",
397
+ "export async function createTodo(input: CreateTodoInput) {",
398
+ ' if (!input.name?.trim()) throw new Error("Name is required");',
399
+ " const collection = await getCollection();",
400
+ " const todo = {",
401
+ " name: input.name,",
402
+ ' description: input.description ?? "",',
403
+ " createdAt: new Date(),",
404
+ " };",
405
+ "",
406
+ " await collection.insertOne(todo);",
407
+ " return todo;",
408
+ "}",
409
+ "",
410
+ "export async function updateTodo(id: string, input: UpdateTodoInput) {",
411
+ " const collection = await getCollection();",
412
+ " const result = await collection.updateOne(",
413
+ " { _id: objectId(id) },",
414
+ " { $set: input }",
415
+ " );",
416
+ ' if (result.matchedCount === 0) throw new Error("Todo not found");',
417
+ " return getTodo(id);",
418
+ "}",
419
+ "",
420
+ "export async function deleteTodo(id: string) {",
421
+ " const collection = await getCollection();",
422
+ " const result = await collection.deleteOne({ _id: objectId(id) });",
423
+ ' if (result.deletedCount === 0) throw new Error("Todo not found");',
424
+ " return { deleted: true };",
425
+ "}",
426
+ ].join("\n"), [
427
+ 'import { ObjectId } from "mongodb";',
428
+ "",
429
+ 'import { getCollection } from "../config/database.js";',
430
+ "",
431
+ "function objectId(id) {",
432
+ ' if (!ObjectId.isValid(id)) throw new Error("Invalid todo id");',
433
+ " return new ObjectId(id);",
434
+ "}",
435
+ "",
436
+ "export async function listTodos() {",
437
+ " const collection = await getCollection();",
438
+ " return collection.find().sort({ createdAt: -1 }).toArray();",
439
+ "}",
440
+ "",
441
+ "export async function getTodo(id) {",
442
+ " const collection = await getCollection();",
443
+ " const todo = await collection.findOne({ _id: objectId(id) });",
444
+ ' if (!todo) throw new Error("Todo not found");',
445
+ " return todo;",
446
+ "}",
447
+ "",
448
+ "export async function createTodo(input) {",
449
+ ' if (!input.name?.trim()) throw new Error("Name is required");',
450
+ " const collection = await getCollection();",
451
+ " const todo = {",
452
+ " name: input.name,",
453
+ ' description: input.description ?? "",',
454
+ " createdAt: new Date(),",
455
+ " };",
456
+ "",
457
+ " await collection.insertOne(todo);",
458
+ " return todo;",
459
+ "}",
460
+ "",
461
+ "export async function updateTodo(id, input) {",
462
+ " const collection = await getCollection();",
463
+ " const result = await collection.updateOne({ _id: objectId(id) }, { $set: input });",
464
+ ' if (result.matchedCount === 0) throw new Error("Todo not found");',
465
+ " return getTodo(id);",
466
+ "}",
467
+ "",
468
+ "export async function deleteTodo(id) {",
469
+ " const collection = await getCollection();",
470
+ " const result = await collection.deleteOne({ _id: objectId(id) });",
471
+ ' if (result.deletedCount === 0) throw new Error("Todo not found");',
472
+ " return { deleted: true };",
473
+ "}",
474
+ ].join("\n")),
475
+ framework === "express" ? expressRoute(language) : fastifyRoute(language),
476
+ ];
477
+ }
478
+ function mongooseSetup(language, framework) {
479
+ return [
480
+ databaseConfig(language, [
481
+ 'import mongoose from "mongoose";',
482
+ "",
483
+ 'const url = process.env.MONGODB_URL ?? "mongodb://localhost:27017";',
484
+ "",
485
+ "export async function connectToDatabase(): Promise<typeof mongoose> {",
486
+ ' mongoose.set("strictQuery", true);',
487
+ " return mongoose.connect(url);",
488
+ "}",
489
+ "",
490
+ "export async function closeDatabase(): Promise<void> {",
491
+ " await mongoose.disconnect();",
492
+ "}",
493
+ ].join("\n"), [
494
+ 'import mongoose from "mongoose";',
495
+ "",
496
+ 'const url = process.env.MONGODB_URL ?? "mongodb://localhost:27017";',
497
+ "",
498
+ "export async function connectToDatabase() {",
499
+ ' mongoose.set("strictQuery", true);',
500
+ " return mongoose.connect(url);",
501
+ "}",
502
+ "",
503
+ "export async function closeDatabase() {",
504
+ " await mongoose.disconnect();",
505
+ "}",
506
+ ].join("\n")),
507
+ todoModel(language, [
508
+ 'import mongoose, { type Model } from "mongoose";',
509
+ "",
510
+ "export interface TodoDocument {",
511
+ " name: string;",
512
+ " description: string;",
513
+ " createdAt: Date;",
514
+ " updatedAt: Date;",
515
+ "}",
516
+ "",
517
+ "export interface CreateTodoInput {",
518
+ " name: string;",
519
+ " description?: string;",
520
+ "}",
521
+ "",
522
+ "export interface UpdateTodoInput {",
523
+ " name?: string;",
524
+ " description?: string;",
525
+ "}",
526
+ "",
527
+ "const collectionName = process.env.MONGODB_COLLECTION_NAME ?? \"todos\";",
528
+ "",
529
+ "const todoSchema = new mongoose.Schema<TodoDocument>(",
530
+ " {",
531
+ " name: { type: String, required: true, trim: true },",
532
+ ' description: { type: String, default: "" },',
533
+ " },",
534
+ " { timestamps: true, collection: collectionName }",
535
+ ");",
536
+ "",
537
+ "export const TodoModel =",
538
+ " (mongoose.models.Todo as Model<TodoDocument>) ??",
539
+ " mongoose.model<TodoDocument>(\"Todo\", todoSchema);",
540
+ ].join("\n"), [
541
+ 'import mongoose from "mongoose";',
542
+ "",
543
+ 'const collectionName = process.env.MONGODB_COLLECTION_NAME ?? "todos";',
544
+ "",
545
+ "const todoSchema = new mongoose.Schema(",
546
+ " {",
547
+ " name: { type: String, required: true, trim: true },",
548
+ ' description: { type: String, default: "" },',
549
+ " },",
550
+ " { timestamps: true, collection: collectionName }",
551
+ ");",
552
+ "",
553
+ 'export const TodoModel = mongoose.models.Todo ?? mongoose.model("Todo", todoSchema);',
554
+ ].join("\n")),
555
+ todoService(language, [
556
+ 'import { TodoModel } from "../models/todo.js";',
557
+ 'import type { CreateTodoInput, UpdateTodoInput } from "../models/todo.js";',
558
+ "",
559
+ "export async function listTodos() {",
560
+ " return TodoModel.find().sort({ createdAt: -1 });",
561
+ "}",
562
+ "",
563
+ "export async function getTodo(id: string) {",
564
+ " const todo = await TodoModel.findById(id);",
565
+ ' if (!todo) throw new Error("Todo not found");',
566
+ " return todo;",
567
+ "}",
568
+ "",
569
+ "export async function createTodo(input: CreateTodoInput) {",
570
+ ' if (!input.name?.trim()) throw new Error("Name is required");',
571
+ " return TodoModel.create({",
572
+ " name: input.name,",
573
+ ' description: input.description ?? "",',
574
+ " });",
575
+ "}",
576
+ "",
577
+ "export async function updateTodo(id: string, input: UpdateTodoInput) {",
578
+ " const todo = await TodoModel.findByIdAndUpdate(id, input, {",
579
+ " new: true,",
580
+ " runValidators: true,",
581
+ " });",
582
+ ' if (!todo) throw new Error("Todo not found");',
583
+ " return todo;",
584
+ "}",
585
+ "",
586
+ "export async function deleteTodo(id: string) {",
587
+ " const todo = await TodoModel.findByIdAndDelete(id);",
588
+ ' if (!todo) throw new Error("Todo not found");',
589
+ " return { deleted: true };",
590
+ "}",
591
+ ].join("\n"), [
592
+ 'import { TodoModel } from "../models/todo.js";',
593
+ "",
594
+ "export async function listTodos() {",
595
+ " return TodoModel.find().sort({ createdAt: -1 });",
596
+ "}",
597
+ "",
598
+ "export async function getTodo(id) {",
599
+ " const todo = await TodoModel.findById(id);",
600
+ ' if (!todo) throw new Error("Todo not found");',
601
+ " return todo;",
602
+ "}",
603
+ "",
604
+ "export async function createTodo(input) {",
605
+ ' if (!input.name?.trim()) throw new Error("Name is required");',
606
+ " return TodoModel.create({",
607
+ " name: input.name,",
608
+ ' description: input.description ?? "",',
609
+ " });",
610
+ "}",
611
+ "",
612
+ "export async function updateTodo(id, input) {",
613
+ " const todo = await TodoModel.findByIdAndUpdate(id, input, {",
614
+ " new: true,",
615
+ " runValidators: true,",
616
+ " });",
617
+ ' if (!todo) throw new Error("Todo not found");',
618
+ " return todo;",
619
+ "}",
620
+ "",
621
+ "export async function deleteTodo(id) {",
622
+ " const todo = await TodoModel.findByIdAndDelete(id);",
623
+ ' if (!todo) throw new Error("Todo not found");',
624
+ " return { deleted: true };",
625
+ "}",
626
+ ].join("\n")),
627
+ framework === "express" ? expressRoute(language) : fastifyRoute(language),
628
+ ];
629
+ }
630
+ function postgresDriverSetup(language, framework) {
631
+ return [
632
+ databaseConfig(language, [
633
+ 'import { Pool, type PoolClient } from "pg";',
634
+ "",
635
+ 'const connectionString = process.env.DATABASE_URL ?? "postgresql://postgres:postgres@localhost:5432/origo_db";',
636
+ "",
637
+ "export const pool = new Pool({ connectionString });",
638
+ "",
639
+ "export async function connectToDatabase(): Promise<Pool> {",
640
+ " const client: PoolClient = await pool.connect();",
641
+ "",
642
+ " try {",
643
+ ' await client.query("SELECT 1");',
644
+ " } finally {",
645
+ " client.release();",
646
+ " }",
647
+ "",
648
+ " return pool;",
649
+ "}",
650
+ "",
651
+ "export async function closeDatabase(): Promise<void> {",
652
+ " await pool.end();",
653
+ "}",
654
+ ].join("\n"), [
655
+ 'import { Pool } from "pg";',
656
+ "",
657
+ 'const connectionString = process.env.DATABASE_URL ?? "postgresql://postgres:postgres@localhost:5432/origo_db";',
658
+ "",
659
+ "export const pool = new Pool({ connectionString });",
660
+ "",
661
+ "export async function connectToDatabase() {",
662
+ " const client = await pool.connect();",
663
+ "",
664
+ " try {",
665
+ ' await client.query("SELECT 1");',
666
+ " } finally {",
667
+ " client.release();",
668
+ " }",
669
+ "",
670
+ " return pool;",
671
+ "}",
672
+ "",
673
+ "export async function closeDatabase() {",
674
+ " await pool.end();",
675
+ "}",
676
+ ].join("\n")),
677
+ todoModel(language, [
678
+ 'const tableName = process.env.POSTGRES_TABLE ?? "todos";',
679
+ "",
680
+ 'export const TODO_TABLE = tableName.replace(/[^a-zA-Z0-9_]/g, "") || "todos";',
681
+ "",
682
+ "export interface TodoRecord {",
683
+ " id: number;",
684
+ " name: string;",
685
+ " description: string | null;",
686
+ " created_at: Date;",
687
+ "}",
688
+ "",
689
+ "export interface CreateTodoInput {",
690
+ " name: string;",
691
+ " description?: string;",
692
+ "}",
693
+ "",
694
+ "export interface UpdateTodoInput {",
695
+ " name?: string;",
696
+ " description?: string;",
697
+ "}",
698
+ ].join("\n"), [
699
+ 'const tableName = process.env.POSTGRES_TABLE ?? "todos";',
700
+ "",
701
+ 'export const TODO_TABLE = tableName.replace(/[^a-zA-Z0-9_]/g, "") || "todos";',
702
+ ].join("\n")),
703
+ todoService(language, [
704
+ 'import { pool } from "../config/database.js";',
705
+ 'import { TODO_TABLE } from "../models/todo.js";',
706
+ 'import type { CreateTodoInput, UpdateTodoInput } from "../models/todo.js";',
707
+ "",
708
+ "function todoId(id: string): number {",
709
+ " const value = Number(id);",
710
+ ' if (!Number.isInteger(value) || value <= 0) throw new Error("Invalid todo id");',
711
+ " return value;",
712
+ "}",
713
+ "",
714
+ "export async function ensureTodoTable(): Promise<void> {",
715
+ " await pool.query(`CREATE TABLE IF NOT EXISTS ${TODO_TABLE} (",
716
+ " id SERIAL PRIMARY KEY,",
717
+ " name TEXT NOT NULL,",
718
+ " description TEXT,",
719
+ " created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()",
720
+ " )`);",
721
+ "}",
722
+ "",
723
+ "export async function listTodos() {",
724
+ " await ensureTodoTable();",
725
+ " const result = await pool.query(`SELECT id, name, description, created_at FROM ${TODO_TABLE} ORDER BY id DESC`);",
726
+ " return result.rows;",
727
+ "}",
728
+ "",
729
+ "export async function getTodo(id: string) {",
730
+ " await ensureTodoTable();",
731
+ " const result = await pool.query(",
732
+ " `SELECT id, name, description, created_at FROM ${TODO_TABLE} WHERE id = $1`,",
733
+ " [todoId(id)]",
734
+ " );",
735
+ " const todo = result.rows[0];",
736
+ ' if (!todo) throw new Error("Todo not found");',
737
+ " return todo;",
738
+ "}",
739
+ "",
740
+ "export async function createTodo(input: CreateTodoInput) {",
741
+ ' if (!input.name?.trim()) throw new Error("Name is required");',
742
+ " await ensureTodoTable();",
743
+ " const result = await pool.query(",
744
+ " `INSERT INTO ${TODO_TABLE} (name, description) VALUES ($1, $2) RETURNING id, name, description, created_at`,",
745
+ ' [input.name, input.description ?? null]',
746
+ " );",
747
+ " return result.rows[0];",
748
+ "}",
749
+ "",
750
+ "export async function updateTodo(id: string, input: UpdateTodoInput) {",
751
+ " await ensureTodoTable();",
752
+ " const fields: string[] = [];",
753
+ " const values: unknown[] = [];",
754
+ "",
755
+ " if (input.name !== undefined) {",
756
+ " fields.push(`name = $${fields.length + 1}`);",
757
+ " values.push(input.name);",
758
+ " }",
759
+ "",
760
+ " if (input.description !== undefined) {",
761
+ " fields.push(`description = $${fields.length + 1}`);",
762
+ " values.push(input.description);",
763
+ " }",
764
+ "",
765
+ " if (!fields.length) {",
766
+ " return getTodo(id);",
767
+ " }",
768
+ "",
769
+ " values.push(todoId(id));",
770
+ " const result = await pool.query(",
771
+ " `UPDATE ${TODO_TABLE} SET ${fields.join(\", \")} WHERE id = $${values.length} RETURNING id, name, description, created_at`,",
772
+ " values",
773
+ " );",
774
+ " const todo = result.rows[0];",
775
+ ' if (!todo) throw new Error("Todo not found");',
776
+ " return todo;",
777
+ "}",
778
+ "",
779
+ "export async function deleteTodo(id: string) {",
780
+ " await ensureTodoTable();",
781
+ " const result = await pool.query(`DELETE FROM ${TODO_TABLE} WHERE id = $1`, [todoId(id)]);",
782
+ ' if (!result.rowCount) throw new Error("Todo not found");',
783
+ " return { deleted: true };",
784
+ "}",
785
+ ].join("\n"), [
786
+ 'import { pool } from "../config/database.js";',
787
+ 'import { TODO_TABLE } from "../models/todo.js";',
788
+ "",
789
+ "function todoId(id) {",
790
+ " const value = Number(id);",
791
+ ' if (!Number.isInteger(value) || value <= 0) throw new Error("Invalid todo id");',
792
+ " return value;",
793
+ "}",
794
+ "",
795
+ "export async function ensureTodoTable() {",
796
+ " await pool.query(`CREATE TABLE IF NOT EXISTS ${TODO_TABLE} (",
797
+ " id SERIAL PRIMARY KEY,",
798
+ " name TEXT NOT NULL,",
799
+ " description TEXT,",
800
+ " created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()",
801
+ " )`);",
802
+ "}",
803
+ "",
804
+ "export async function listTodos() {",
805
+ " await ensureTodoTable();",
806
+ " const result = await pool.query(`SELECT id, name, description, created_at FROM ${TODO_TABLE} ORDER BY id DESC`);",
807
+ " return result.rows;",
808
+ "}",
809
+ "",
810
+ "export async function getTodo(id) {",
811
+ " await ensureTodoTable();",
812
+ " const result = await pool.query(",
813
+ " `SELECT id, name, description, created_at FROM ${TODO_TABLE} WHERE id = $1`,",
814
+ " [todoId(id)]",
815
+ " );",
816
+ " const todo = result.rows[0];",
817
+ ' if (!todo) throw new Error("Todo not found");',
818
+ " return todo;",
819
+ "}",
820
+ "",
821
+ "export async function createTodo(input) {",
822
+ ' if (!input.name?.trim()) throw new Error("Name is required");',
823
+ " await ensureTodoTable();",
824
+ " const result = await pool.query(",
825
+ " `INSERT INTO ${TODO_TABLE} (name, description) VALUES ($1, $2) RETURNING id, name, description, created_at`,",
826
+ ' [input.name, input.description ?? null]',
827
+ " );",
828
+ " return result.rows[0];",
829
+ "}",
830
+ "",
831
+ "export async function updateTodo(id, input) {",
832
+ " await ensureTodoTable();",
833
+ " const fields = [];",
834
+ " const values = [];",
835
+ "",
836
+ " if (input.name !== undefined) {",
837
+ " fields.push(`name = $${fields.length + 1}`);",
838
+ " values.push(input.name);",
839
+ " }",
840
+ "",
841
+ " if (input.description !== undefined) {",
842
+ " fields.push(`description = $${fields.length + 1}`);",
843
+ " values.push(input.description);",
844
+ " }",
845
+ "",
846
+ " if (!fields.length) {",
847
+ " return getTodo(id);",
848
+ " }",
849
+ "",
850
+ " values.push(todoId(id));",
851
+ " const result = await pool.query(",
852
+ " `UPDATE ${TODO_TABLE} SET ${fields.join(\", \")} WHERE id = $${values.length} RETURNING id, name, description, created_at`,",
853
+ " values",
854
+ " );",
855
+ " const todo = result.rows[0];",
856
+ ' if (!todo) throw new Error("Todo not found");',
857
+ " return todo;",
858
+ "}",
859
+ "",
860
+ "export async function deleteTodo(id) {",
861
+ " await ensureTodoTable();",
862
+ " const result = await pool.query(`DELETE FROM ${TODO_TABLE} WHERE id = $1`, [todoId(id)]);",
863
+ ' if (!result.rowCount) throw new Error("Todo not found");',
864
+ " return { deleted: true };",
865
+ "}",
866
+ ].join("\n")),
867
+ framework === "express" ? expressRoute(language) : fastifyRoute(language),
868
+ ];
869
+ }
870
+ function prismaSetup(language, framework) {
871
+ const adapterImport = 'import { PrismaPg } from "@prisma/adapter-pg";';
872
+ const clientImport = 'import { PrismaClient } from "@prisma/client";';
873
+ const connectionString = 'const connectionString = process.env.DATABASE_URL ?? "postgresql://postgres:postgres@localhost:5432/origo_db";';
874
+ const adapter = "const adapter = new PrismaPg({ connectionString });";
875
+ const client = "export const prisma = new PrismaClient({ adapter });";
876
+ return [
877
+ databaseConfig(language, [
878
+ adapterImport,
879
+ clientImport,
880
+ "",
881
+ connectionString,
882
+ adapter,
883
+ client,
884
+ "",
885
+ "export async function connectToDatabase(): Promise<PrismaClient> {",
886
+ " await prisma.$connect();",
887
+ " return prisma;",
888
+ "}",
889
+ "",
890
+ "export async function closeDatabase(): Promise<void> {",
891
+ " await prisma.$disconnect();",
892
+ "}",
893
+ ].join("\n"), [
894
+ adapterImport,
895
+ clientImport,
896
+ "",
897
+ connectionString,
898
+ adapter,
899
+ client,
900
+ "",
901
+ "export async function connectToDatabase() {",
902
+ " await prisma.$connect();",
903
+ " return prisma;",
904
+ "}",
905
+ "",
906
+ "export async function closeDatabase() {",
907
+ " await prisma.$disconnect();",
908
+ "}",
909
+ ].join("\n")),
910
+ todoModel(language, [
911
+ 'export type { Todo } from "@prisma/client";',
912
+ "",
913
+ "export interface CreateTodoInput {",
914
+ " name: string;",
915
+ " description?: string;",
916
+ "}",
917
+ "",
918
+ "export interface UpdateTodoInput {",
919
+ " name?: string;",
920
+ " description?: string;",
921
+ "}",
922
+ ].join("\n"), ["export const todoModel = \"Todo\";"].join("\n")),
923
+ todoService(language, [
924
+ 'import { prisma } from "../config/database.js";',
925
+ 'import type { CreateTodoInput, UpdateTodoInput } from "../models/todo.js";',
926
+ "",
927
+ "function todoId(id: string): number {",
928
+ " const value = Number(id);",
929
+ ' if (!Number.isInteger(value) || value <= 0) throw new Error("Invalid todo id");',
930
+ " return value;",
931
+ "}",
932
+ "",
933
+ "export async function listTodos() {",
934
+ " return prisma.todo.findMany({ orderBy: { id: \"desc\" } });",
935
+ "}",
936
+ "",
937
+ "export async function getTodo(id: string) {",
938
+ " const todo = await prisma.todo.findUnique({ where: { id: todoId(id) } });",
939
+ ' if (!todo) throw new Error("Todo not found");',
940
+ " return todo;",
941
+ "}",
942
+ "",
943
+ "export async function createTodo(input: CreateTodoInput) {",
944
+ ' if (!input.name?.trim()) throw new Error("Name is required");',
945
+ " return prisma.todo.create({",
946
+ " data: {",
947
+ " name: input.name,",
948
+ ' description: input.description ?? "",',
949
+ " },",
950
+ " });",
951
+ "}",
952
+ "",
953
+ "export async function updateTodo(id: string, input: UpdateTodoInput) {",
954
+ " return prisma.todo.update({",
955
+ " where: { id: todoId(id) },",
956
+ " data: input,",
957
+ " });",
958
+ "}",
959
+ "",
960
+ "export async function deleteTodo(id: string) {",
961
+ " await prisma.todo.delete({ where: { id: todoId(id) } });",
962
+ " return { deleted: true };",
963
+ "}",
964
+ ].join("\n"), [
965
+ 'import { prisma } from "../config/database.js";',
966
+ "",
967
+ "function todoId(id) {",
968
+ " const value = Number(id);",
969
+ ' if (!Number.isInteger(value) || value <= 0) throw new Error("Invalid todo id");',
970
+ " return value;",
971
+ "}",
972
+ "",
973
+ "export async function listTodos() {",
974
+ " return prisma.todo.findMany({ orderBy: { id: \"desc\" } });",
975
+ "}",
976
+ "",
977
+ "export async function getTodo(id) {",
978
+ " const todo = await prisma.todo.findUnique({ where: { id: todoId(id) } });",
979
+ ' if (!todo) throw new Error("Todo not found");',
980
+ " return todo;",
981
+ "}",
982
+ "",
983
+ "export async function createTodo(input) {",
984
+ ' if (!input.name?.trim()) throw new Error("Name is required");',
985
+ " return prisma.todo.create({",
986
+ " data: {",
987
+ " name: input.name,",
988
+ ' description: input.description ?? "",',
989
+ " },",
990
+ " });",
991
+ "}",
992
+ "",
993
+ "export async function updateTodo(id, input) {",
994
+ " return prisma.todo.update({",
995
+ " where: { id: todoId(id) },",
996
+ " data: input,",
997
+ " });",
998
+ "}",
999
+ "",
1000
+ "export async function deleteTodo(id) {",
1001
+ " await prisma.todo.delete({ where: { id: todoId(id) } });",
1002
+ " return { deleted: true };",
1003
+ "}",
1004
+ ].join("\n")),
1005
+ {
1006
+ path: "prisma/schema.prisma",
1007
+ content: [
1008
+ "generator client {",
1009
+ ' provider = "prisma-client-js"',
1010
+ "}",
1011
+ "",
1012
+ "datasource db {",
1013
+ ' provider = "postgresql"',
1014
+ "}",
1015
+ "",
1016
+ "model Todo {",
1017
+ " id Int @id @default(autoincrement())",
1018
+ " name String",
1019
+ " description String?",
1020
+ " createdAt DateTime @default(now())",
1021
+ ' @@map("todos")',
1022
+ "}",
1023
+ ].join("\n"),
1024
+ },
1025
+ {
1026
+ path: "prisma.config.ts",
1027
+ content: [
1028
+ 'import { defineConfig, env } from "prisma/config";',
1029
+ "",
1030
+ "export default defineConfig({",
1031
+ ' schema: "prisma/schema.prisma",',
1032
+ " migrations: {",
1033
+ ' url: env("DATABASE_URL"),',
1034
+ " },",
1035
+ "});",
1036
+ ].join("\n"),
1037
+ },
1038
+ framework === "express" ? expressRoute(language) : fastifyRoute(language),
1039
+ ];
1040
+ }
1041
+ function sequelizeSetup(language, framework) {
1042
+ return [
1043
+ databaseConfig(language, [
1044
+ 'import { Sequelize } from "sequelize";',
1045
+ "",
1046
+ 'const connectionString = process.env.DATABASE_URL ?? "postgresql://postgres:postgres@localhost:5432/origo_db";',
1047
+ "",
1048
+ 'export const sequelize = new Sequelize(connectionString, { logging: false });',
1049
+ "",
1050
+ "export async function connectToDatabase(): Promise<Sequelize> {",
1051
+ " await sequelize.authenticate();",
1052
+ " return sequelize;",
1053
+ "}",
1054
+ "",
1055
+ "export async function closeDatabase(): Promise<void> {",
1056
+ " await sequelize.close();",
1057
+ "}",
1058
+ ].join("\n"), [
1059
+ 'import { Sequelize } from "sequelize";',
1060
+ "",
1061
+ 'const connectionString = process.env.DATABASE_URL ?? "postgresql://postgres:postgres@localhost:5432/origo_db";',
1062
+ "",
1063
+ 'export const sequelize = new Sequelize(connectionString, { logging: false });',
1064
+ "",
1065
+ "export async function connectToDatabase() {",
1066
+ " await sequelize.authenticate();",
1067
+ " return sequelize;",
1068
+ "}",
1069
+ "",
1070
+ "export async function closeDatabase() {",
1071
+ " await sequelize.close();",
1072
+ "}",
1073
+ ].join("\n")),
1074
+ todoModel(language, [
1075
+ 'import { DataTypes, Model, type Sequelize } from "sequelize";',
1076
+ 'import { sequelize } from "../config/database.js";',
1077
+ "",
1078
+ "export interface CreateTodoInput {",
1079
+ " name: string;",
1080
+ " description?: string;",
1081
+ "}",
1082
+ "",
1083
+ "export interface UpdateTodoInput {",
1084
+ " name?: string;",
1085
+ " description?: string;",
1086
+ "}",
1087
+ "",
1088
+ "export class TodoModel extends Model {",
1089
+ " declare id: number;",
1090
+ " declare name: string;",
1091
+ " declare description: string | null;",
1092
+ " declare readonly createdAt: Date;",
1093
+ " declare readonly updatedAt: Date;",
1094
+ "}",
1095
+ "",
1096
+ "TodoModel.init(",
1097
+ " {",
1098
+ " name: { type: DataTypes.STRING, allowNull: false },",
1099
+ " description: { type: DataTypes.TEXT, allowNull: true },",
1100
+ " },",
1101
+ " {",
1102
+ " sequelize,",
1103
+ ' tableName: process.env.POSTGRES_TABLE ?? "todos",',
1104
+ " timestamps: true,",
1105
+ " }",
1106
+ ");",
1107
+ ].join("\n"), [
1108
+ 'import { DataTypes, Model } from "sequelize";',
1109
+ 'import { sequelize } from "../config/database.js";',
1110
+ "",
1111
+ "export class TodoModel extends Model {}",
1112
+ "",
1113
+ "TodoModel.init(",
1114
+ " {",
1115
+ " name: { type: DataTypes.STRING, allowNull: false },",
1116
+ " description: { type: DataTypes.TEXT, allowNull: true },",
1117
+ " },",
1118
+ " {",
1119
+ " sequelize,",
1120
+ ' tableName: process.env.POSTGRES_TABLE ?? "todos",',
1121
+ " timestamps: true,",
1122
+ " }",
1123
+ ");",
1124
+ ].join("\n")),
1125
+ todoService(language, [
1126
+ 'import { TodoModel } from "../models/todo.js";',
1127
+ 'import type { CreateTodoInput, UpdateTodoInput } from "../models/todo.js";',
1128
+ "",
1129
+ "function todoId(id: string): number {",
1130
+ " const value = Number(id);",
1131
+ ' if (!Number.isInteger(value) || value <= 0) throw new Error("Invalid todo id");',
1132
+ " return value;",
1133
+ "}",
1134
+ "",
1135
+ "export async function listTodos() {",
1136
+ " await TodoModel.sync();",
1137
+ " return TodoModel.findAll({ order: [[\"id\", \"DESC\"]] });",
1138
+ "}",
1139
+ "",
1140
+ "export async function getTodo(id: string) {",
1141
+ " await TodoModel.sync();",
1142
+ " const todo = await TodoModel.findByPk(todoId(id));",
1143
+ ' if (!todo) throw new Error("Todo not found");',
1144
+ " return todo;",
1145
+ "}",
1146
+ "",
1147
+ "export async function createTodo(input: CreateTodoInput) {",
1148
+ ' if (!input.name?.trim()) throw new Error("Name is required");',
1149
+ " await TodoModel.sync();",
1150
+ " return TodoModel.create({",
1151
+ " name: input.name,",
1152
+ ' description: input.description ?? "",',
1153
+ " });",
1154
+ "}",
1155
+ "",
1156
+ "export async function updateTodo(id: string, input: UpdateTodoInput) {",
1157
+ " await TodoModel.sync();",
1158
+ " const todo = await TodoModel.findByPk(todoId(id));",
1159
+ ' if (!todo) throw new Error("Todo not found");',
1160
+ " await todo.update(input);",
1161
+ " return todo;",
1162
+ "}",
1163
+ "",
1164
+ "export async function deleteTodo(id: string) {",
1165
+ " await TodoModel.sync();",
1166
+ " const todo = await TodoModel.findByPk(todoId(id));",
1167
+ ' if (!todo) throw new Error("Todo not found");',
1168
+ " await todo.destroy();",
1169
+ " return { deleted: true };",
1170
+ "}",
1171
+ ].join("\n"), [
1172
+ 'import { TodoModel } from "../models/todo.js";',
1173
+ "",
1174
+ "function todoId(id) {",
1175
+ " const value = Number(id);",
1176
+ ' if (!Number.isInteger(value) || value <= 0) throw new Error("Invalid todo id");',
1177
+ " return value;",
1178
+ "}",
1179
+ "",
1180
+ "export async function listTodos() {",
1181
+ " await TodoModel.sync();",
1182
+ ' return TodoModel.findAll({ order: [["id", "DESC"]] });',
1183
+ "}",
1184
+ "",
1185
+ "export async function getTodo(id) {",
1186
+ " await TodoModel.sync();",
1187
+ " const todo = await TodoModel.findByPk(todoId(id));",
1188
+ ' if (!todo) throw new Error("Todo not found");',
1189
+ " return todo;",
1190
+ "}",
1191
+ "",
1192
+ "export async function createTodo(input) {",
1193
+ ' if (!input.name?.trim()) throw new Error("Name is required");',
1194
+ " await TodoModel.sync();",
1195
+ " return TodoModel.create({",
1196
+ " name: input.name,",
1197
+ ' description: input.description ?? "",',
1198
+ " });",
1199
+ "}",
1200
+ "",
1201
+ "export async function updateTodo(id, input) {",
1202
+ " await TodoModel.sync();",
1203
+ " const todo = await TodoModel.findByPk(todoId(id));",
1204
+ ' if (!todo) throw new Error("Todo not found");',
1205
+ " await todo.update(input);",
1206
+ " return todo;",
1207
+ "}",
1208
+ "",
1209
+ "export async function deleteTodo(id) {",
1210
+ " await TodoModel.sync();",
1211
+ " const todo = await TodoModel.findByPk(todoId(id));",
1212
+ ' if (!todo) throw new Error("Todo not found");',
1213
+ " await todo.destroy();",
1214
+ " return { deleted: true };",
1215
+ "}",
1216
+ ].join("\n")),
1217
+ framework === "express" ? expressRoute(language) : fastifyRoute(language),
1218
+ ];
1219
+ }
1220
+ export function resolveDatabaseSetup(language, framework, technologies) {
1221
+ const id = technologies.has("mongoose")
1222
+ ? "mongoose"
1223
+ : technologies.has("mongodb-driver") || technologies.has("mongodb")
1224
+ ? "mongodb-driver"
1225
+ : technologies.has("prisma")
1226
+ ? "prisma"
1227
+ : technologies.has("sequelize")
1228
+ ? "sequelize"
1229
+ : technologies.has("postgresql-driver") ||
1230
+ technologies.has("postgresql")
1231
+ ? "postgresql-driver"
1232
+ : "none";
1233
+ const noRoutes = {
1234
+ serverImport: "",
1235
+ serverConnect: "",
1236
+ routeImport: "",
1237
+ routeRegister: "",
1238
+ };
1239
+ if (id === "none") {
1240
+ return {
1241
+ id,
1242
+ env: commonEnv,
1243
+ files: [],
1244
+ scripts: {},
1245
+ ...noRoutes,
1246
+ };
1247
+ }
1248
+ const isMongo = id === "mongodb-driver" || id === "mongoose";
1249
+ const scripts = id === "prisma"
1250
+ ? {
1251
+ "db:generate": "prisma generate",
1252
+ "db:push": "prisma db push",
1253
+ }
1254
+ : {};
1255
+ return {
1256
+ id,
1257
+ env: [...commonEnv, ...(isMongo ? mongoEnv : postgresEnv)],
1258
+ files: id === "mongodb-driver"
1259
+ ? mongoDriverSetup(language, framework)
1260
+ : id === "mongoose"
1261
+ ? mongooseSetup(language, framework)
1262
+ : id === "postgresql-driver"
1263
+ ? postgresDriverSetup(language, framework)
1264
+ : id === "prisma"
1265
+ ? prismaSetup(language, framework)
1266
+ : sequelizeSetup(language, framework),
1267
+ scripts,
1268
+ serverImport: 'const { connectToDatabase } = await import("./config/database.js");',
1269
+ serverConnect: "await connectToDatabase();",
1270
+ routeImport: framework === "express"
1271
+ ? 'import { todoRouter } from "./routes/todos.js";'
1272
+ : 'import { registerTodoRoutes } from "./routes/todos.js";',
1273
+ routeRegister: framework === "express"
1274
+ ? ' app.use("/api/todos", todoRouter);'
1275
+ : ' await app.register(registerTodoRoutes, { prefix: "/api/todos" });',
1276
+ };
1277
+ }