import { createId } from "@paralleldrive/cuid2";
import { relations, sql } from "drizzle-orm";
import { sqliteTableCreator, text } from "drizzle-orm/sqlite-core";
export const createTable = sqliteTableCreator((name) => `myproject_${name}`);
export const users = createTable("users", {
id: text("id", { length: 128 }).primaryKey(),
name: text("name"),
});
export const usersRelations = relations(users, ({ many }) => ({
lists: many(lists),
}));
export const lists = createTable("lists", {
id: text("id", { length: 128 })
.$defaultFn(() => createId())
.primaryKey(),
userId: text("userId", { length: 128 }).notNull(),
name: text("name", { length: 64 }).notNull(),
description: text("description", { length: 256 }),
createdAt: text("created_at")
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
readOnlyId: text("readOnlyId", { length: 128 }).$defaultFn(() => createId()),
});
export const listsRelations = relations(lists, ({ one }) => ({
author: one(users, {
fields: [lists.userId],
references: [users.id],
}),
}));
export const items = createTable("items", {
id: text("id", { length: 128 })
.$defaultFn(() => createId())
.primaryKey(),
listId: text("listId", { length: 128 })
.references(() => lists.id, { onDelete: "cascade" })
.notNull(),
sourceLink: text("sourceLink", { length: 256 }),
tagLink: text("tagLink", { length: 256 }),
name: text("name", { length: 64 }).notNull(),
description: text("description", { length: 256 }),
createdAt: text("created_at")
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
});
export const itemsRelations = relations(items, ({ one }) => ({
author: one(lists, {
fields: [items.listId],
references: [lists.id],
}),
}));