Refactor drizzle and db folder
This commit is contained in:
parent
e96dfb2d74
commit
c493ac342b
31 changed files with 22 additions and 23 deletions
109
lib/db/schema.ts
Normal file
109
lib/db/schema.ts
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
import { InferSelectModel } from 'drizzle-orm';
|
||||
import {
|
||||
pgTable,
|
||||
varchar,
|
||||
timestamp,
|
||||
json,
|
||||
uuid,
|
||||
text,
|
||||
primaryKey,
|
||||
foreignKey,
|
||||
boolean,
|
||||
} from 'drizzle-orm/pg-core';
|
||||
|
||||
export const user = pgTable('User', {
|
||||
id: uuid('id').primaryKey().notNull().defaultRandom(),
|
||||
email: varchar('email', { length: 64 }).notNull(),
|
||||
password: varchar('password', { length: 64 }),
|
||||
});
|
||||
|
||||
export type User = InferSelectModel<typeof user>;
|
||||
|
||||
export const chat = pgTable('Chat', {
|
||||
id: uuid('id').primaryKey().notNull().defaultRandom(),
|
||||
createdAt: timestamp('createdAt').notNull(),
|
||||
title: text('title').notNull(),
|
||||
userId: uuid('userId')
|
||||
.notNull()
|
||||
.references(() => user.id),
|
||||
});
|
||||
|
||||
export type Chat = InferSelectModel<typeof chat>;
|
||||
|
||||
export const message = pgTable('Message', {
|
||||
id: uuid('id').primaryKey().notNull().defaultRandom(),
|
||||
chatId: uuid('chatId')
|
||||
.notNull()
|
||||
.references(() => chat.id),
|
||||
role: varchar('role').notNull(),
|
||||
content: json('content').notNull(),
|
||||
createdAt: timestamp('createdAt').notNull(),
|
||||
});
|
||||
|
||||
export type Message = InferSelectModel<typeof message>;
|
||||
|
||||
export const vote = pgTable(
|
||||
'Vote',
|
||||
{
|
||||
chatId: uuid('chatId')
|
||||
.notNull()
|
||||
.references(() => chat.id),
|
||||
messageId: uuid('messageId')
|
||||
.notNull()
|
||||
.references(() => message.id),
|
||||
isUpvoted: boolean('isUpvoted').notNull(),
|
||||
},
|
||||
(table) => {
|
||||
return {
|
||||
pk: primaryKey({ columns: [table.chatId, table.messageId] }),
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
export type Vote = InferSelectModel<typeof vote>;
|
||||
|
||||
export const document = pgTable(
|
||||
'Document',
|
||||
{
|
||||
id: uuid('id').notNull().defaultRandom(),
|
||||
createdAt: timestamp('createdAt').notNull(),
|
||||
title: text('title').notNull(),
|
||||
content: text('content'),
|
||||
userId: uuid('userId')
|
||||
.notNull()
|
||||
.references(() => user.id),
|
||||
},
|
||||
(table) => {
|
||||
return {
|
||||
pk: primaryKey({ columns: [table.id, table.createdAt] }),
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
export type Document = InferSelectModel<typeof document>;
|
||||
|
||||
export const suggestion = pgTable(
|
||||
'Suggestion',
|
||||
{
|
||||
id: uuid('id').notNull().defaultRandom(),
|
||||
documentId: uuid('documentId').notNull(),
|
||||
documentCreatedAt: timestamp('documentCreatedAt').notNull(),
|
||||
originalText: text('originalText').notNull(),
|
||||
suggestedText: text('suggestedText').notNull(),
|
||||
description: text('description'),
|
||||
isResolved: boolean('isResolved').notNull().default(false),
|
||||
userId: uuid('userId')
|
||||
.notNull()
|
||||
.references(() => user.id),
|
||||
createdAt: timestamp('createdAt').notNull(),
|
||||
},
|
||||
(table) => ({
|
||||
pk: primaryKey({ columns: [table.id] }),
|
||||
documentRef: foreignKey({
|
||||
columns: [table.documentId, table.documentCreatedAt],
|
||||
foreignColumns: [document.id, document.createdAt],
|
||||
}),
|
||||
})
|
||||
);
|
||||
|
||||
export type Suggestion = InferSelectModel<typeof suggestion>;
|
||||
Loading…
Add table
Add a link
Reference in a new issue