65 lines
1.7 KiB
TypeScript
Raw Normal View History

2024-04-16 18:19:34 -04:00
import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core";
import { createId } from "@paralleldrive/cuid2";
2024-05-01 08:20:08 -04:00
import { relations, 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(),
2024-05-01 01:29:16 -04:00
image: text("image"),
2024-04-16 18:19:34 -04: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-04-16 18:19:34 -04:00
export const sandbox = sqliteTable("sandbox", {
id: text("id")
.$defaultFn(() => createId())
.primaryKey()
.unique(),
name: text("name").notNull(),
type: text("type", { enum: ["react", "node"] }).notNull(),
2024-04-27 16:22:35 -04:00
visibility: text("visibility", { enum: ["public", "private"] }),
2024-04-16 18:19:34 -04:00
userId: text("user_id")
.notNull()
.references(() => user.id),
});
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-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-05-01 00:08:10 -04: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],
}),
}));