codelilac
DTDrizzle Team
•Created by codelilac on 6/24/2024 in #help
Struggling to use Zod with Drizzle... Type errors around |undefined
import { serial, text, timestamp, pgTable } from "drizzle-orm/pg-core";
import { createInsertSchema, createSelectSchema } from "drizzle-zod";
import { z } from "zod";
export const users = pgTable("users", {
id: serial("id"),
name: text("name"),
email: text("email"),
password: text("password"),
role: text("role").$type<"admin" | "user">(),
createdAt: timestamp("created_at"),
updatedAt: timestamp("updated_at"),
});
export const insertUserSchema = createInsertSchema(users, {
role: z.enum(["admin", "user"])
});
//This is supposed to be what we get from the POST request, it omits ID and createdAt/updatedAt fields
export const apiInsertUserSchema = insertUserSchema.omit({ id: true, createdAt: true, updatedAt: true });
export const selectUserSchema = createSelectSchema(users);
import { serial, text, timestamp, pgTable } from "drizzle-orm/pg-core";
import { createInsertSchema, createSelectSchema } from "drizzle-zod";
import { z } from "zod";
export const users = pgTable("users", {
id: serial("id"),
name: text("name"),
email: text("email"),
password: text("password"),
role: text("role").$type<"admin" | "user">(),
createdAt: timestamp("created_at"),
updatedAt: timestamp("updated_at"),
});
export const insertUserSchema = createInsertSchema(users, {
role: z.enum(["admin", "user"])
});
//This is supposed to be what we get from the POST request, it omits ID and createdAt/updatedAt fields
export const apiInsertUserSchema = insertUserSchema.omit({ id: true, createdAt: true, updatedAt: true });
export const selectUserSchema = createSelectSchema(users);
import type { FastifyInstance } from "fastify";
// Import drizzle schema
import { users, insertUserSchema, apiInsertUserSchema } from "@api/db/schemas";
import { db } from "@api/db";
import { Logger } from "@api/utils";
export const testRoutes = (fastify: FastifyInstance, _: unknown, done: () => void) => {
fastify.get("/", async (request, response) => {
const vists = 0;
console.log(request);
response.send({
hello: "world",
vists,
});
});
fastify.get("/users", async (request, response) => {
const allUsers = await db.select().from(users);
console.log(users);
response.send(allUsers);
});
fastify.post("/users", async (request, response) => {
console.log(request);
try {
let parsedRequest = apiInsertUserSchema.parse(request.body);
console.log("request body: ", request.body);
console.log("parsed request: ", parsedRequest);
await db.insert(users).values(parsedRequest);
} catch (error) {
console.log(error);
Logger.error("POST /users", `${error}`);
response.status(400).send({ error: "Invalid request body" });
}
});
import type { FastifyInstance } from "fastify";
// Import drizzle schema
import { users, insertUserSchema, apiInsertUserSchema } from "@api/db/schemas";
import { db } from "@api/db";
import { Logger } from "@api/utils";
export const testRoutes = (fastify: FastifyInstance, _: unknown, done: () => void) => {
fastify.get("/", async (request, response) => {
const vists = 0;
console.log(request);
response.send({
hello: "world",
vists,
});
});
fastify.get("/users", async (request, response) => {
const allUsers = await db.select().from(users);
console.log(users);
response.send(allUsers);
});
fastify.post("/users", async (request, response) => {
console.log(request);
try {
let parsedRequest = apiInsertUserSchema.parse(request.body);
console.log("request body: ", request.body);
console.log("parsed request: ", parsedRequest);
await db.insert(users).values(parsedRequest);
} catch (error) {
console.log(error);
Logger.error("POST /users", `${error}`);
response.status(400).send({ error: "Invalid request body" });
}
});
8 replies