77 lines
2.1 KiB
TypeScript
Raw Normal View History

2024-10-24 12:44:38 -06:00
import { createId } from "@paralleldrive/cuid2"
import { relations } from "drizzle-orm"
import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core"
import { sql } from "drizzle-orm"
2024-04-16 18:19:34 -04:00
export const user = sqliteTable("user", {
id: text("id")
.$defaultFn(() => createId())
.primaryKey()
.unique(),
name: text("name").notNull(),
email: text("email").notNull(),
username: text("username").notNull().unique(),
avatarUrl: text("avatarUrl"),
createdAt: integer("createdAt", { mode: "timestamp_ms" })
.default(sql`CURRENT_TIMESTAMP`),
generations: integer("generations").default(0),
2024-10-24 12:44:38 -06:00
})
2024-04-16 18:19:34 -04:00
2024-10-24 12:44:38 -06:00
export type User = typeof user.$inferSelect
export const userRelations = relations(user, ({ many }) => ({
2024-05-01 01:29:16 -04:00
sandbox: many(sandbox),
usersToSandboxes: many(usersToSandboxes),
2024-10-24 12:44:38 -06:00
}))
2024-04-16 18:19:34 -04:00
export const sandbox = sqliteTable("sandbox", {
id: text("id")
.$defaultFn(() => createId())
.primaryKey()
.unique(),
name: text("name").notNull(),
2024-08-18 06:52:41 -07:00
type: text("type").notNull(),
2024-04-27 16:22:35 -04:00
visibility: text("visibility", { enum: ["public", "private"] }),
createdAt: integer("createdAt", { mode: "timestamp_ms" })
.default(sql`CURRENT_TIMESTAMP`),
2024-04-16 18:19:34 -04:00
userId: text("user_id")
.notNull()
.references(() => user.id),
likeCount: integer("likeCount").default(0),
viewCount: integer("viewCount").default(0),
2024-10-24 12:44:38 -06:00
})
2024-10-24 12:44:38 -06:00
export type Sandbox = typeof sandbox.$inferSelect
2024-05-01 00:08:10 -04:00
export const sandboxRelations = relations(sandbox, ({ one, many }) => ({
author: one(user, {
fields: [sandbox.userId],
references: [user.id],
2024-05-01 00:08:10 -04:00
}),
2024-05-01 01:29:16 -04:00
usersToSandboxes: many(usersToSandboxes),
2024-10-24 12:44:38 -06:00
}))
2024-05-01 00:08:10 -04:00
export const usersToSandboxes = sqliteTable("users_to_sandboxes", {
2024-05-01 01:29:16 -04:00
userId: text("userId")
2024-05-01 00:08:10 -04:00
.notNull()
.references(() => user.id),
2024-05-01 01:29:16 -04:00
sandboxId: text("sandboxId")
2024-05-01 00:08:10 -04:00
.notNull()
.references(() => sandbox.id),
2024-05-01 08:20:08 -04:00
sharedOn: integer("sharedOn", { mode: "timestamp_ms" }),
2024-10-24 12:44:38 -06:00
})
2024-05-01 00:08:10 -04:00
2024-10-24 12:44:38 -06:00
export const usersToSandboxesRelations = relations(
usersToSandboxes,
({ one }) => ({
group: one(sandbox, {
fields: [usersToSandboxes.sandboxId],
references: [sandbox.id],
}),
user: one(user, {
fields: [usersToSandboxes.userId],
references: [user.id],
}),
})
)