feat: implement CRUD operations for drones with validation and server functions
This commit is contained in:
parent
653a9b2dc5
commit
f951fe6629
@ -3,7 +3,6 @@ import {
|
||||
boolean,
|
||||
decimal,
|
||||
integer,
|
||||
jsonb,
|
||||
pgPolicy,
|
||||
pgTable,
|
||||
serial,
|
||||
@ -18,7 +17,7 @@ export const drones = pgTable("drones", {
|
||||
id: serial("id").primaryKey().notNull(),
|
||||
model: varchar("model", { length: 100 }),
|
||||
brand: varchar("brand", { length: 100 }),
|
||||
type: jsonb("type")
|
||||
// type: jsonb("type")
|
||||
}).enableRLS()
|
||||
|
||||
// === certs ===
|
||||
|
||||
67
src/lib/server/drones.ts
Normal file
67
src/lib/server/drones.ts
Normal file
@ -0,0 +1,67 @@
|
||||
import { createServerFn } from "@tanstack/react-start"
|
||||
import { asc, eq } from "drizzle-orm"
|
||||
import { db } from "@/integrations/drizzle"
|
||||
import { drones } from "@/integrations/drizzle/db/schema"
|
||||
import { dronesSchema, paginatedDronesSchema } from "../validation/drones"
|
||||
|
||||
const insertDrones = createServerFn()
|
||||
.inputValidator(dronesSchema)
|
||||
.handler(async ({ data }) => {
|
||||
await db
|
||||
.insert(drones)
|
||||
.values({
|
||||
model: data.model,
|
||||
brand: data.brand
|
||||
})
|
||||
.returning()
|
||||
})
|
||||
|
||||
const editDrone = createServerFn()
|
||||
.inputValidator(
|
||||
dronesSchema.pick({
|
||||
model: true,
|
||||
brand: true,
|
||||
id: true
|
||||
})
|
||||
)
|
||||
.handler(async ({ data }) => {
|
||||
await db
|
||||
.update(drones)
|
||||
.set({
|
||||
model: data.model,
|
||||
brand: data.brand
|
||||
})
|
||||
.where(eq(drones.id, data.id))
|
||||
})
|
||||
|
||||
const deleteDrones = createServerFn({
|
||||
method: "POST"
|
||||
})
|
||||
.inputValidator(
|
||||
dronesSchema.pick({
|
||||
id: true
|
||||
})
|
||||
)
|
||||
.handler(async ({ data }) => {
|
||||
return await db.delete(drones).where(eq(drones.id, data.id))
|
||||
})
|
||||
|
||||
const getAllDrones = createServerFn({
|
||||
method: "POST"
|
||||
})
|
||||
.inputValidator(paginatedDronesSchema)
|
||||
.handler(async ({ data }) => {
|
||||
return db
|
||||
.select()
|
||||
.from(drones)
|
||||
.limit(data.limit)
|
||||
.offset((data.page - 1) * data.limit)
|
||||
})
|
||||
|
||||
|
||||
export const serverDrones = {
|
||||
insertDrones,
|
||||
editDrone,
|
||||
getAllDrones,
|
||||
deleteDrones
|
||||
}
|
||||
@ -4,17 +4,13 @@ import { db } from "@/integrations/drizzle"
|
||||
import { places as placesSchema } from "@/integrations/drizzle/db/schema"
|
||||
import { paginatedPlacesSchema, placeSchema } from "../validation/places"
|
||||
|
||||
export const insertUserPlace = createServerFn({
|
||||
method: "POST"
|
||||
})
|
||||
export const insertUserPlace = createServerFn()
|
||||
.inputValidator(placeSchema)
|
||||
.handler(async ({ data }) => {
|
||||
await db.insert(placesSchema).values(data).returning()
|
||||
})
|
||||
|
||||
export const editUserPlace = createServerFn({
|
||||
method: "POST"
|
||||
})
|
||||
export const editUserPlace = createServerFn()
|
||||
.inputValidator(
|
||||
placeSchema.pick({
|
||||
hidden_place: true,
|
||||
|
||||
12
src/lib/validation/drones.ts
Normal file
12
src/lib/validation/drones.ts
Normal file
@ -0,0 +1,12 @@
|
||||
import z from "zod"
|
||||
|
||||
export const dronesSchema = z.object({
|
||||
model: z.string(),
|
||||
brand: z.string(),
|
||||
id: z.number()
|
||||
})
|
||||
|
||||
export const paginatedDronesSchema = z.object({
|
||||
page: z.number().min(1).default(1),
|
||||
limit: z.number().min(1).max(100).default(10)
|
||||
})
|
||||
Loading…
Reference in New Issue
Block a user