MCPcopy Create free account
hub / github.com/boringstack-xyz/boringstack / writeResourceFiles

Function writeResourceFiles

apps/api/scripts/codegen/new-resource.ts:207–334  ·  view source on GitHub ↗
(params: {
  pascal: string;
  filePrefix: string;
  singular: string;
})

Source from the content-addressed store, hash-verified

205const 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
231export const ${createSchemaName} = t.Object({
232 name: t.String({ minLength: 1, maxLength: 100 }),
233});
234
235export 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";
248import type { ${filePrefix} } from "../../clients/postgres/schema";
249
250export type I${singular} = InferSelectModel<typeof ${filePrefix}>;
251export type ICreate${singular}Data = InferInsertModel<typeof ${filePrefix}>;
252export 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";
262import { db } from "../../clients/postgres";
263import { ${filePrefix} } from "../../clients/postgres/schema";
264import { AUDIT_ACTIONS, auditLogService } from "../../lib/audit-log";

Callers 1

new-resource.tsFile · 0.85

Calls 2

errorMethod · 0.80
writeMethod · 0.80

Tested by

no test coverage detected