(params: {
pascal: string;
filePrefix: string;
singular: string;
})
| 205 | const writeResourceFiles = async (params: { |
| 206 | pascal: string; |
| 207 | filePrefix: string; |
| 208 | singular: string; |
| 209 | }): Promise<void> => { |
| 210 | const { pascal, filePrefix, singular } = params; |
| 211 | const dirPath = fileURLToPath( |
| 212 | new URL(`../../src/api/${filePrefix}/`, import.meta.url) |
| 213 | ); |
| 214 | |
| 215 | if (existsSync(dirPath)) { |
| 216 | console.error( |
| 217 | `Directory src/api/${filePrefix}/ already exists. Remove it or pick another name.` |
| 218 | ); |
| 219 | process.exit(1); |
| 220 | } |
| 221 | |
| 222 | await mkdir(dirPath, { recursive: true }); |
| 223 | |
| 224 | const createSchemaName = `Create${singular}Schema`; |
| 225 | const responseName = `${singular}Response`; |
| 226 | |
| 227 | await Bun.write( |
| 228 | `${dirPath}/${filePrefix}.schemas.ts`, |
| 229 | `import { t } from "elysia"; |
| 230 | |
| 231 | export const ${createSchemaName} = t.Object({ |
| 232 | name: t.String({ minLength: 1, maxLength: 100 }), |
| 233 | }); |
| 234 | |
| 235 | export const ${responseName} = t.Object({ |
| 236 | id: t.String(), |
| 237 | userId: t.String(), |
| 238 | name: t.String(), |
| 239 | createdAt: t.String({ format: "date-time" }), |
| 240 | updatedAt: t.String({ format: "date-time" }), |
| 241 | }); |
| 242 | ` |
| 243 | ); |
| 244 | |
| 245 | await Bun.write( |
| 246 | `${dirPath}/${filePrefix}.types.ts`, |
| 247 | `import type { InferInsertModel, InferSelectModel } from "drizzle-orm"; |
| 248 | import type { ${filePrefix} } from "../../clients/postgres/schema"; |
| 249 | |
| 250 | export type I${singular} = InferSelectModel<typeof ${filePrefix}>; |
| 251 | export type ICreate${singular}Data = InferInsertModel<typeof ${filePrefix}>; |
| 252 | export type IUpdate${singular}Data = Partial<InferInsertModel<typeof ${filePrefix}>>; |
| 253 | ` |
| 254 | ); |
| 255 | |
| 256 | const auditKey = `${singular.toUpperCase()}_CREATED`; |
| 257 | const singularLower = singular.toLowerCase(); |
| 258 | |
| 259 | await Bun.write( |
| 260 | `${dirPath}/${filePrefix}.service.ts`, |
| 261 | `import { eq } from "drizzle-orm"; |
| 262 | import { db } from "../../clients/postgres"; |
| 263 | import { ${filePrefix} } from "../../clients/postgres/schema"; |
| 264 | import { AUDIT_ACTIONS, auditLogService } from "../../lib/audit-log"; |
no test coverage detected