2023-05-22 12:14:37 -04:00
|
|
|
import { ProviderType } from "@auth/nextjs/providers";
|
2023-06-02 11:15:04 -04:00
|
|
|
import { sql } from "@vercel/postgres";
|
|
|
|
|
import { relations } from "drizzle-orm";
|
|
|
|
|
import { integer, pgTable, primaryKey, text } from "drizzle-orm/pg-core";
|
|
|
|
|
import { drizzle } from "drizzle-orm/vercel-postgres";
|
2023-05-22 12:14:37 -04:00
|
|
|
|
2023-06-02 11:15:04 -04:00
|
|
|
// const connection = createPool({ connectionString: process.env.POSTGRES_URL });
|
|
|
|
|
|
|
|
|
|
export const users = pgTable("users", {
|
|
|
|
|
id: text("id").notNull().primaryKey(),
|
|
|
|
|
name: text("name"),
|
|
|
|
|
email: text("email").notNull(),
|
|
|
|
|
emailVerified: integer("emailVerified"),
|
|
|
|
|
image: text("image"),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const usersRelations = relations(users, ({ many }) => ({
|
|
|
|
|
chats: many(chats),
|
|
|
|
|
accounts: many(accounts),
|
|
|
|
|
}));
|
2023-05-22 12:14:37 -04:00
|
|
|
|
|
|
|
|
export const accounts = pgTable(
|
|
|
|
|
"accounts",
|
|
|
|
|
{
|
|
|
|
|
userId: text("userId")
|
|
|
|
|
.notNull()
|
|
|
|
|
.references(() => users.id, { onDelete: "cascade" }),
|
|
|
|
|
type: text("type").$type<ProviderType>().notNull(),
|
|
|
|
|
provider: text("provider").notNull(),
|
|
|
|
|
providerAccountId: text("providerAccountId").notNull(),
|
|
|
|
|
refresh_token: text("refresh_token"),
|
|
|
|
|
access_token: text("access_token"),
|
|
|
|
|
expires_at: integer("expires_at"),
|
|
|
|
|
token_type: text("token_type"),
|
|
|
|
|
scope: text("scope"),
|
|
|
|
|
id_token: text("id_token"),
|
|
|
|
|
session_state: text("session_state"),
|
|
|
|
|
},
|
2023-06-02 11:15:04 -04:00
|
|
|
(account) => ({
|
|
|
|
|
_: primaryKey(account.provider, account.providerAccountId),
|
|
|
|
|
})
|
2023-05-22 12:14:37 -04:00
|
|
|
);
|
|
|
|
|
|
2023-06-02 11:15:04 -04:00
|
|
|
export const sessions = pgTable("sessions", {
|
|
|
|
|
userId: text("userId")
|
|
|
|
|
.notNull()
|
|
|
|
|
.references(() => users.id, { onDelete: "cascade" }),
|
|
|
|
|
sessionToken: text("sessionToken").notNull().primaryKey(),
|
|
|
|
|
expires: integer("expires").notNull(),
|
|
|
|
|
});
|
2023-05-22 12:14:37 -04:00
|
|
|
|
|
|
|
|
export const verificationTokens = pgTable(
|
|
|
|
|
"verificationToken",
|
|
|
|
|
{
|
|
|
|
|
identifier: text("identifier").notNull(),
|
|
|
|
|
token: text("token").notNull(),
|
2023-06-02 11:15:04 -04:00
|
|
|
expires: integer("expires").notNull(),
|
2023-05-22 12:14:37 -04:00
|
|
|
},
|
2023-06-02 11:15:04 -04:00
|
|
|
(vt) => ({
|
|
|
|
|
_: primaryKey(vt.identifier, vt.token),
|
2023-05-22 12:14:37 -04:00
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
export type Schema = {
|
|
|
|
|
users: typeof users;
|
|
|
|
|
accounts: typeof accounts;
|
|
|
|
|
sessions: typeof sessions;
|
|
|
|
|
verificationTokens: typeof verificationTokens;
|
|
|
|
|
};
|
|
|
|
|
|
2023-06-02 11:15:04 -04:00
|
|
|
export const chats = pgTable("chats", {
|
|
|
|
|
id: text("id").notNull().primaryKey(),
|
|
|
|
|
title: text("role").notNull(),
|
|
|
|
|
userId: text("userId")
|
|
|
|
|
.notNull()
|
|
|
|
|
.references(() => users.id),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export const chatsRelations = relations(chats, ({ one }) => ({
|
|
|
|
|
user: one(users, {
|
|
|
|
|
fields: [chats.userId],
|
|
|
|
|
references: [users.id],
|
|
|
|
|
}),
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
export const db = drizzle(sql, {
|
|
|
|
|
schema: {
|
|
|
|
|
users,
|
|
|
|
|
accounts,
|
|
|
|
|
sessions,
|
|
|
|
|
verificationTokens,
|
|
|
|
|
chats,
|
|
|
|
|
chatsRelations,
|
|
|
|
|
usersRelations,
|
|
|
|
|
},
|
|
|
|
|
});
|
2023-05-22 12:14:37 -04:00
|
|
|
|
2023-06-02 11:15:04 -04:00
|
|
|
export type DbClient = typeof db;
|