feat: schema updates
- added additional items to users and sandbox tables - added a random username generator
This commit is contained in:
@ -282,14 +282,26 @@ export default {
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
email: z.string().email(),
|
||||
username: z.string(),
|
||||
avatarUrl: z.string().optional(),
|
||||
createdAt: z.string().optional(),
|
||||
generations: z.number().optional(),
|
||||
})
|
||||
|
||||
const body = await request.json()
|
||||
const { id, name, email } = userSchema.parse(body)
|
||||
const { id, name, email, username, avatarUrl, createdAt, generations } = userSchema.parse(body)
|
||||
|
||||
const res = await db
|
||||
.insert(user)
|
||||
.values({ id, name, email })
|
||||
.values({
|
||||
id,
|
||||
name,
|
||||
email,
|
||||
username,
|
||||
avatarUrl,
|
||||
createdAt: createdAt ? new Date(createdAt) : new Date(),
|
||||
generations,
|
||||
})
|
||||
.returning()
|
||||
.get()
|
||||
return json({ res })
|
||||
@ -303,6 +315,20 @@ export default {
|
||||
} else {
|
||||
return methodNotAllowed
|
||||
}
|
||||
} else if (path === "/api/user/check-username") {
|
||||
if (method === "GET") {
|
||||
const params = url.searchParams
|
||||
const username = params.get("username")
|
||||
|
||||
if (!username) return invalidRequest
|
||||
|
||||
const exists = await db.query.user.findFirst({
|
||||
where: (user, { eq }) => eq(user.username, username)
|
||||
})
|
||||
|
||||
return json({ exists: !!exists })
|
||||
}
|
||||
return methodNotAllowed
|
||||
} else return notFound
|
||||
},
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
import { createId } from "@paralleldrive/cuid2"
|
||||
import { relations } from "drizzle-orm"
|
||||
import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core"
|
||||
import { sql } from "drizzle-orm"
|
||||
|
||||
export const user = sqliteTable("user", {
|
||||
id: text("id")
|
||||
@ -9,7 +10,10 @@ export const user = sqliteTable("user", {
|
||||
.unique(),
|
||||
name: text("name").notNull(),
|
||||
email: text("email").notNull(),
|
||||
image: text("image"),
|
||||
username: text("username").notNull().unique(),
|
||||
avatarUrl: text("avatarUrl"),
|
||||
createdAt: integer("createdAt", { mode: "timestamp_ms" })
|
||||
.default(sql`CURRENT_TIMESTAMP`),
|
||||
generations: integer("generations").default(0),
|
||||
})
|
||||
|
||||
@ -28,10 +32,13 @@ export const sandbox = sqliteTable("sandbox", {
|
||||
name: text("name").notNull(),
|
||||
type: text("type").notNull(),
|
||||
visibility: text("visibility", { enum: ["public", "private"] }),
|
||||
createdAt: integer("createdAt", { mode: "timestamp_ms" }),
|
||||
createdAt: integer("createdAt", { mode: "timestamp_ms" })
|
||||
.default(sql`CURRENT_TIMESTAMP`),
|
||||
userId: text("user_id")
|
||||
.notNull()
|
||||
.references(() => user.id),
|
||||
likeCount: integer("likeCount").default(0),
|
||||
viewCount: integer("viewCount").default(0),
|
||||
})
|
||||
|
||||
export type Sandbox = typeof sandbox.$inferSelect
|
||||
|
Reference in New Issue
Block a user