boredgame/src/db/schema/collections.ts

29 lines
870 B
TypeScript
Raw Normal View History

import { pgTable, text, timestamp, uuid } from 'drizzle-orm/pg-core';
import { createId as cuid2 } from '@paralleldrive/cuid2';
import { type InferSelectModel, relations } from 'drizzle-orm';
2024-07-21 19:05:48 +00:00
import usersTable from './users.table';
import { timestamps } from '../utils';
const collections = pgTable('collections', {
id: uuid('id').primaryKey().defaultRandom(),
cuid: text('cuid')
.unique()
.$defaultFn(() => cuid2()),
user_id: uuid('user_id')
.notNull()
2024-07-21 19:05:48 +00:00
.references(() => usersTable.id, { onDelete: 'cascade' }),
name: text('name').notNull().default('My Collection'),
...timestamps,
});
export const collection_relations = relations(collections, ({ one }) => ({
2024-07-21 19:05:48 +00:00
user: one(usersTable, {
fields: [collections.user_id],
2024-07-21 19:05:48 +00:00
references: [usersTable.id],
}),
}));
export type Collections = InferSelectModel<typeof collections>;
export default collections;