Derive Elysia request validators from Drizzle tables via drizzle-typebox
This commit is contained in:
+8
-21
@@ -2,6 +2,7 @@ import { Elysia, t } from "elysia";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { db } from "../db";
|
||||
import { printers } from "../db/schema";
|
||||
import { PrinterInsertSchema } from "../db/validators";
|
||||
|
||||
export const printerRoutes = new Elysia({ prefix: "/printers" })
|
||||
.get("/", async ({ query }) => {
|
||||
@@ -21,28 +22,14 @@ export const printerRoutes = new Elysia({ prefix: "/printers" })
|
||||
.post(
|
||||
"/",
|
||||
async ({ body, set }) => {
|
||||
const [printer] = await db
|
||||
.insert(printers)
|
||||
.values({
|
||||
farmId: body.farmId,
|
||||
name: body.name,
|
||||
vendor: body.vendor,
|
||||
lanAddress: body.lanAddress,
|
||||
serialNumber: body.serialNumber,
|
||||
accessCode: body.accessCode,
|
||||
})
|
||||
.returning();
|
||||
// `body` is already validated + shaped against the Drizzle table by
|
||||
// PrinterInsertSchema, so it can go straight into `.values()` with no
|
||||
// hand-mapping of individual fields.
|
||||
const [printer] = await db.insert(printers).values(body).returning();
|
||||
set.status = 201;
|
||||
return printer;
|
||||
},
|
||||
{
|
||||
body: t.Object({
|
||||
farmId: t.String({ format: "uuid" }),
|
||||
name: t.String({ minLength: 1 }),
|
||||
vendor: t.Union([t.Literal("bambu"), t.Literal("prusa"), t.Literal("klipper")]),
|
||||
lanAddress: t.Optional(t.String()),
|
||||
serialNumber: t.Optional(t.String()),
|
||||
accessCode: t.Optional(t.String()),
|
||||
}),
|
||||
},
|
||||
// Derived from `printers` via drizzle-typebox — see src/db/validators.ts.
|
||||
// Add or rename a column in the schema and this validator updates itself.
|
||||
{ body: PrinterInsertSchema },
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user