Alt drizzle
This commit is contained in:
parent
030b23985d
commit
17bfd0cac1
10 changed files with 89 additions and 581 deletions
|
|
@ -1,6 +1,7 @@
|
||||||
import { auth } from "@/auth";
|
import { auth } from "@/auth";
|
||||||
import { chats, db } from "@/lib/db/schema";
|
import { chats, db } from "@/lib/db/schema";
|
||||||
import { openai } from "@/lib/openai";
|
import { openai } from "@/lib/openai";
|
||||||
|
import { nanoid } from "@/lib/utils";
|
||||||
import { OpenAIStream, StreamingTextResponse } from "ai-connector";
|
import { OpenAIStream, StreamingTextResponse } from "ai-connector";
|
||||||
|
|
||||||
export const runtime = "edge";
|
export const runtime = "edge";
|
||||||
|
|
@ -31,11 +32,24 @@ export const POST = auth(async function POST(req: Request) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const title = json.messages[0].content.substring(0, 20);
|
const title = json.messages[0].content.substring(0, 20);
|
||||||
await db.insert(chats).values({
|
const payload = {
|
||||||
id: crypto.randomUUID(),
|
id: json.id ?? crypto.randomUUID(),
|
||||||
title,
|
title,
|
||||||
userId: (req as any).auth?.user?.email,
|
userId: (req as any).auth?.user?.email,
|
||||||
});
|
messages: json.messages.concat({
|
||||||
|
content: completion,
|
||||||
|
role: "assistant",
|
||||||
|
id: nanoid(),
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
await db
|
||||||
|
.insert(chats)
|
||||||
|
.values(payload)
|
||||||
|
.onConflictDoUpdate({
|
||||||
|
target: json.id,
|
||||||
|
set: payload,
|
||||||
|
})
|
||||||
|
.execute();
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
19
auth.ts
19
auth.ts
|
|
@ -2,13 +2,6 @@ import NextAuth from "@auth/nextjs";
|
||||||
import GitHub from "@auth/nextjs/providers/github";
|
import GitHub from "@auth/nextjs/providers/github";
|
||||||
import { NextResponse } from "next/server";
|
import { NextResponse } from "next/server";
|
||||||
|
|
||||||
import {
|
|
||||||
db,
|
|
||||||
users,
|
|
||||||
accounts,
|
|
||||||
sessions,
|
|
||||||
verificationTokens,
|
|
||||||
} from "./lib/db/schema";
|
|
||||||
import { PgAdapter } from "./lib/db";
|
import { PgAdapter } from "./lib/db";
|
||||||
|
|
||||||
export const {
|
export const {
|
||||||
|
|
@ -16,12 +9,12 @@ export const {
|
||||||
auth,
|
auth,
|
||||||
CSRF_experimental,
|
CSRF_experimental,
|
||||||
} = NextAuth({
|
} = NextAuth({
|
||||||
adapter: PgAdapter(db, {
|
// adapter: PgAdapter(db, {
|
||||||
accounts,
|
// accounts,
|
||||||
users,
|
// users,
|
||||||
sessions,
|
// sessions,
|
||||||
verificationTokens,
|
// verificationTokens,
|
||||||
}),
|
// }),
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
providers: [GitHub],
|
providers: [GitHub],
|
||||||
session: { strategy: "jwt" },
|
session: { strategy: "jwt" },
|
||||||
|
|
|
||||||
|
|
@ -1,49 +0,0 @@
|
||||||
CREATE TABLE IF NOT EXISTS "accounts" (
|
|
||||||
"userId" text NOT NULL,
|
|
||||||
"type" text NOT NULL,
|
|
||||||
"provider" text NOT NULL,
|
|
||||||
"providerAccountId" text NOT NULL,
|
|
||||||
"refresh_token" text,
|
|
||||||
"access_token" text,
|
|
||||||
"expires_at" integer,
|
|
||||||
"token_type" text,
|
|
||||||
"scope" text,
|
|
||||||
"id_token" text,
|
|
||||||
"session_state" text
|
|
||||||
);
|
|
||||||
--> statement-breakpoint
|
|
||||||
ALTER TABLE "accounts" ADD CONSTRAINT "accounts_provider_providerAccountId" PRIMARY KEY("provider","providerAccountId");
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS "sessions" (
|
|
||||||
"userId" text NOT NULL,
|
|
||||||
"sessionToken" text PRIMARY KEY NOT NULL,
|
|
||||||
"expires" integer NOT NULL
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS "users" (
|
|
||||||
"id" text PRIMARY KEY NOT NULL,
|
|
||||||
"name" text,
|
|
||||||
"email" text NOT NULL,
|
|
||||||
"emailVerified" integer,
|
|
||||||
"image" text
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS "verificationToken" (
|
|
||||||
"identifier" text NOT NULL,
|
|
||||||
"token" text NOT NULL,
|
|
||||||
"expires" integer NOT NULL
|
|
||||||
);
|
|
||||||
--> statement-breakpoint
|
|
||||||
ALTER TABLE "verificationToken" ADD CONSTRAINT "verificationToken_identifier_token" PRIMARY KEY("identifier","token");
|
|
||||||
|
|
||||||
DO $$ BEGIN
|
|
||||||
ALTER TABLE "accounts" ADD CONSTRAINT "accounts_userId_users_id_fk" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE cascade ON UPDATE no action;
|
|
||||||
EXCEPTION
|
|
||||||
WHEN duplicate_object THEN null;
|
|
||||||
END $$;
|
|
||||||
|
|
||||||
DO $$ BEGIN
|
|
||||||
ALTER TABLE "sessions" ADD CONSTRAINT "sessions_userId_users_id_fk" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE cascade ON UPDATE no action;
|
|
||||||
EXCEPTION
|
|
||||||
WHEN duplicate_object THEN null;
|
|
||||||
END $$;
|
|
||||||
6
lib/db/migrations/0000_fair_groot.sql
Normal file
6
lib/db/migrations/0000_fair_groot.sql
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
CREATE TABLE IF NOT EXISTS "chats" (
|
||||||
|
"id" text PRIMARY KEY NOT NULL,
|
||||||
|
"role" text NOT NULL,
|
||||||
|
"userId" text NOT NULL,
|
||||||
|
"messages" json DEFAULT '{}'::json NOT NULL
|
||||||
|
);
|
||||||
1
lib/db/migrations/0001_naive_gunslinger.sql
Normal file
1
lib/db/migrations/0001_naive_gunslinger.sql
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
ALTER TABLE "chats" DROP COLUMN IF EXISTS "messages";
|
||||||
|
|
@ -1,11 +0,0 @@
|
||||||
CREATE TABLE IF NOT EXISTS "chats" (
|
|
||||||
"id" text PRIMARY KEY NOT NULL,
|
|
||||||
"role" text NOT NULL,
|
|
||||||
"userId" text NOT NULL
|
|
||||||
);
|
|
||||||
|
|
||||||
DO $$ BEGIN
|
|
||||||
ALTER TABLE "chats" ADD CONSTRAINT "chats_userId_users_id_fk" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE no action ON UPDATE no action;
|
|
||||||
EXCEPTION
|
|
||||||
WHEN duplicate_object THEN null;
|
|
||||||
END $$;
|
|
||||||
|
|
@ -1,149 +1,11 @@
|
||||||
{
|
{
|
||||||
"version": "5",
|
"version": "5",
|
||||||
"dialect": "pg",
|
"dialect": "pg",
|
||||||
"id": "c6cb6121-2dc1-49be-8bd9-62f1c0c55939",
|
"id": "3bddf21a-46a3-4ee8-8b41-bb79cb80d904",
|
||||||
"prevId": "00000000-0000-0000-0000-000000000000",
|
"prevId": "00000000-0000-0000-0000-000000000000",
|
||||||
"tables": {
|
"tables": {
|
||||||
"accounts": {
|
"chats": {
|
||||||
"name": "accounts",
|
"name": "chats",
|
||||||
"schema": "",
|
|
||||||
"columns": {
|
|
||||||
"userId": {
|
|
||||||
"name": "userId",
|
|
||||||
"type": "text",
|
|
||||||
"primaryKey": false,
|
|
||||||
"notNull": true
|
|
||||||
},
|
|
||||||
"type": {
|
|
||||||
"name": "type",
|
|
||||||
"type": "text",
|
|
||||||
"primaryKey": false,
|
|
||||||
"notNull": true
|
|
||||||
},
|
|
||||||
"provider": {
|
|
||||||
"name": "provider",
|
|
||||||
"type": "text",
|
|
||||||
"primaryKey": false,
|
|
||||||
"notNull": true
|
|
||||||
},
|
|
||||||
"providerAccountId": {
|
|
||||||
"name": "providerAccountId",
|
|
||||||
"type": "text",
|
|
||||||
"primaryKey": false,
|
|
||||||
"notNull": true
|
|
||||||
},
|
|
||||||
"refresh_token": {
|
|
||||||
"name": "refresh_token",
|
|
||||||
"type": "text",
|
|
||||||
"primaryKey": false,
|
|
||||||
"notNull": false
|
|
||||||
},
|
|
||||||
"access_token": {
|
|
||||||
"name": "access_token",
|
|
||||||
"type": "text",
|
|
||||||
"primaryKey": false,
|
|
||||||
"notNull": false
|
|
||||||
},
|
|
||||||
"expires_at": {
|
|
||||||
"name": "expires_at",
|
|
||||||
"type": "integer",
|
|
||||||
"primaryKey": false,
|
|
||||||
"notNull": false
|
|
||||||
},
|
|
||||||
"token_type": {
|
|
||||||
"name": "token_type",
|
|
||||||
"type": "text",
|
|
||||||
"primaryKey": false,
|
|
||||||
"notNull": false
|
|
||||||
},
|
|
||||||
"scope": {
|
|
||||||
"name": "scope",
|
|
||||||
"type": "text",
|
|
||||||
"primaryKey": false,
|
|
||||||
"notNull": false
|
|
||||||
},
|
|
||||||
"id_token": {
|
|
||||||
"name": "id_token",
|
|
||||||
"type": "text",
|
|
||||||
"primaryKey": false,
|
|
||||||
"notNull": false
|
|
||||||
},
|
|
||||||
"session_state": {
|
|
||||||
"name": "session_state",
|
|
||||||
"type": "text",
|
|
||||||
"primaryKey": false,
|
|
||||||
"notNull": false
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"indexes": {},
|
|
||||||
"foreignKeys": {
|
|
||||||
"accounts_userId_users_id_fk": {
|
|
||||||
"name": "accounts_userId_users_id_fk",
|
|
||||||
"tableFrom": "accounts",
|
|
||||||
"tableTo": "users",
|
|
||||||
"columnsFrom": [
|
|
||||||
"userId"
|
|
||||||
],
|
|
||||||
"columnsTo": [
|
|
||||||
"id"
|
|
||||||
],
|
|
||||||
"onDelete": "cascade",
|
|
||||||
"onUpdate": "no action"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"compositePrimaryKeys": {
|
|
||||||
"accounts_provider_providerAccountId": {
|
|
||||||
"name": "accounts_provider_providerAccountId",
|
|
||||||
"columns": [
|
|
||||||
"provider",
|
|
||||||
"providerAccountId"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"sessions": {
|
|
||||||
"name": "sessions",
|
|
||||||
"schema": "",
|
|
||||||
"columns": {
|
|
||||||
"userId": {
|
|
||||||
"name": "userId",
|
|
||||||
"type": "text",
|
|
||||||
"primaryKey": false,
|
|
||||||
"notNull": true
|
|
||||||
},
|
|
||||||
"sessionToken": {
|
|
||||||
"name": "sessionToken",
|
|
||||||
"type": "text",
|
|
||||||
"primaryKey": true,
|
|
||||||
"notNull": true
|
|
||||||
},
|
|
||||||
"expires": {
|
|
||||||
"name": "expires",
|
|
||||||
"type": "integer",
|
|
||||||
"primaryKey": false,
|
|
||||||
"notNull": true
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"indexes": {},
|
|
||||||
"foreignKeys": {
|
|
||||||
"sessions_userId_users_id_fk": {
|
|
||||||
"name": "sessions_userId_users_id_fk",
|
|
||||||
"tableFrom": "sessions",
|
|
||||||
"tableTo": "users",
|
|
||||||
"columnsFrom": [
|
|
||||||
"userId"
|
|
||||||
],
|
|
||||||
"columnsTo": [
|
|
||||||
"id"
|
|
||||||
],
|
|
||||||
"onDelete": "cascade",
|
|
||||||
"onUpdate": "no action"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"compositePrimaryKeys": {}
|
|
||||||
},
|
|
||||||
"users": {
|
|
||||||
"name": "users",
|
|
||||||
"schema": "",
|
"schema": "",
|
||||||
"columns": {
|
"columns": {
|
||||||
"id": {
|
"id": {
|
||||||
|
|
@ -152,69 +14,29 @@
|
||||||
"primaryKey": true,
|
"primaryKey": true,
|
||||||
"notNull": true
|
"notNull": true
|
||||||
},
|
},
|
||||||
"name": {
|
"role": {
|
||||||
"name": "name",
|
"name": "role",
|
||||||
"type": "text",
|
|
||||||
"primaryKey": false,
|
|
||||||
"notNull": false
|
|
||||||
},
|
|
||||||
"email": {
|
|
||||||
"name": "email",
|
|
||||||
"type": "text",
|
"type": "text",
|
||||||
"primaryKey": false,
|
"primaryKey": false,
|
||||||
"notNull": true
|
"notNull": true
|
||||||
},
|
},
|
||||||
"emailVerified": {
|
"userId": {
|
||||||
"name": "emailVerified",
|
"name": "userId",
|
||||||
"type": "integer",
|
|
||||||
"primaryKey": false,
|
|
||||||
"notNull": false
|
|
||||||
},
|
|
||||||
"image": {
|
|
||||||
"name": "image",
|
|
||||||
"type": "text",
|
"type": "text",
|
||||||
"primaryKey": false,
|
"primaryKey": false,
|
||||||
"notNull": false
|
"notNull": true
|
||||||
|
},
|
||||||
|
"messages": {
|
||||||
|
"name": "messages",
|
||||||
|
"type": "json",
|
||||||
|
"primaryKey": false,
|
||||||
|
"notNull": true,
|
||||||
|
"default": "'{}'::json"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"indexes": {},
|
"indexes": {},
|
||||||
"foreignKeys": {},
|
"foreignKeys": {},
|
||||||
"compositePrimaryKeys": {}
|
"compositePrimaryKeys": {}
|
||||||
},
|
|
||||||
"verificationToken": {
|
|
||||||
"name": "verificationToken",
|
|
||||||
"schema": "",
|
|
||||||
"columns": {
|
|
||||||
"identifier": {
|
|
||||||
"name": "identifier",
|
|
||||||
"type": "text",
|
|
||||||
"primaryKey": false,
|
|
||||||
"notNull": true
|
|
||||||
},
|
|
||||||
"token": {
|
|
||||||
"name": "token",
|
|
||||||
"type": "text",
|
|
||||||
"primaryKey": false,
|
|
||||||
"notNull": true
|
|
||||||
},
|
|
||||||
"expires": {
|
|
||||||
"name": "expires",
|
|
||||||
"type": "integer",
|
|
||||||
"primaryKey": false,
|
|
||||||
"notNull": true
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"indexes": {},
|
|
||||||
"foreignKeys": {},
|
|
||||||
"compositePrimaryKeys": {
|
|
||||||
"verificationToken_identifier_token": {
|
|
||||||
"name": "verificationToken_identifier_token",
|
|
||||||
"columns": [
|
|
||||||
"identifier",
|
|
||||||
"token"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"enums": {},
|
"enums": {},
|
||||||
|
|
|
||||||
|
|
@ -1,106 +1,9 @@
|
||||||
{
|
{
|
||||||
"version": "5",
|
"version": "5",
|
||||||
"dialect": "pg",
|
"dialect": "pg",
|
||||||
"id": "ad52915b-d697-4935-9373-97dca163dd91",
|
"id": "e8eef642-fc25-4737-8e75-5ad1e8c53b84",
|
||||||
"prevId": "c6cb6121-2dc1-49be-8bd9-62f1c0c55939",
|
"prevId": "3bddf21a-46a3-4ee8-8b41-bb79cb80d904",
|
||||||
"tables": {
|
"tables": {
|
||||||
"accounts": {
|
|
||||||
"name": "accounts",
|
|
||||||
"schema": "",
|
|
||||||
"columns": {
|
|
||||||
"userId": {
|
|
||||||
"name": "userId",
|
|
||||||
"type": "text",
|
|
||||||
"primaryKey": false,
|
|
||||||
"notNull": true
|
|
||||||
},
|
|
||||||
"type": {
|
|
||||||
"name": "type",
|
|
||||||
"type": "text",
|
|
||||||
"primaryKey": false,
|
|
||||||
"notNull": true
|
|
||||||
},
|
|
||||||
"provider": {
|
|
||||||
"name": "provider",
|
|
||||||
"type": "text",
|
|
||||||
"primaryKey": false,
|
|
||||||
"notNull": true
|
|
||||||
},
|
|
||||||
"providerAccountId": {
|
|
||||||
"name": "providerAccountId",
|
|
||||||
"type": "text",
|
|
||||||
"primaryKey": false,
|
|
||||||
"notNull": true
|
|
||||||
},
|
|
||||||
"refresh_token": {
|
|
||||||
"name": "refresh_token",
|
|
||||||
"type": "text",
|
|
||||||
"primaryKey": false,
|
|
||||||
"notNull": false
|
|
||||||
},
|
|
||||||
"access_token": {
|
|
||||||
"name": "access_token",
|
|
||||||
"type": "text",
|
|
||||||
"primaryKey": false,
|
|
||||||
"notNull": false
|
|
||||||
},
|
|
||||||
"expires_at": {
|
|
||||||
"name": "expires_at",
|
|
||||||
"type": "integer",
|
|
||||||
"primaryKey": false,
|
|
||||||
"notNull": false
|
|
||||||
},
|
|
||||||
"token_type": {
|
|
||||||
"name": "token_type",
|
|
||||||
"type": "text",
|
|
||||||
"primaryKey": false,
|
|
||||||
"notNull": false
|
|
||||||
},
|
|
||||||
"scope": {
|
|
||||||
"name": "scope",
|
|
||||||
"type": "text",
|
|
||||||
"primaryKey": false,
|
|
||||||
"notNull": false
|
|
||||||
},
|
|
||||||
"id_token": {
|
|
||||||
"name": "id_token",
|
|
||||||
"type": "text",
|
|
||||||
"primaryKey": false,
|
|
||||||
"notNull": false
|
|
||||||
},
|
|
||||||
"session_state": {
|
|
||||||
"name": "session_state",
|
|
||||||
"type": "text",
|
|
||||||
"primaryKey": false,
|
|
||||||
"notNull": false
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"indexes": {},
|
|
||||||
"foreignKeys": {
|
|
||||||
"accounts_userId_users_id_fk": {
|
|
||||||
"name": "accounts_userId_users_id_fk",
|
|
||||||
"tableFrom": "accounts",
|
|
||||||
"tableTo": "users",
|
|
||||||
"columnsFrom": [
|
|
||||||
"userId"
|
|
||||||
],
|
|
||||||
"columnsTo": [
|
|
||||||
"id"
|
|
||||||
],
|
|
||||||
"onDelete": "cascade",
|
|
||||||
"onUpdate": "no action"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"compositePrimaryKeys": {
|
|
||||||
"accounts_provider_providerAccountId": {
|
|
||||||
"name": "accounts_provider_providerAccountId",
|
|
||||||
"columns": [
|
|
||||||
"provider",
|
|
||||||
"providerAccountId"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"chats": {
|
"chats": {
|
||||||
"name": "chats",
|
"name": "chats",
|
||||||
"schema": "",
|
"schema": "",
|
||||||
|
|
@ -125,137 +28,8 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"indexes": {},
|
"indexes": {},
|
||||||
"foreignKeys": {
|
|
||||||
"chats_userId_users_id_fk": {
|
|
||||||
"name": "chats_userId_users_id_fk",
|
|
||||||
"tableFrom": "chats",
|
|
||||||
"tableTo": "users",
|
|
||||||
"columnsFrom": [
|
|
||||||
"userId"
|
|
||||||
],
|
|
||||||
"columnsTo": [
|
|
||||||
"id"
|
|
||||||
],
|
|
||||||
"onDelete": "no action",
|
|
||||||
"onUpdate": "no action"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"compositePrimaryKeys": {}
|
|
||||||
},
|
|
||||||
"sessions": {
|
|
||||||
"name": "sessions",
|
|
||||||
"schema": "",
|
|
||||||
"columns": {
|
|
||||||
"userId": {
|
|
||||||
"name": "userId",
|
|
||||||
"type": "text",
|
|
||||||
"primaryKey": false,
|
|
||||||
"notNull": true
|
|
||||||
},
|
|
||||||
"sessionToken": {
|
|
||||||
"name": "sessionToken",
|
|
||||||
"type": "text",
|
|
||||||
"primaryKey": true,
|
|
||||||
"notNull": true
|
|
||||||
},
|
|
||||||
"expires": {
|
|
||||||
"name": "expires",
|
|
||||||
"type": "integer",
|
|
||||||
"primaryKey": false,
|
|
||||||
"notNull": true
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"indexes": {},
|
|
||||||
"foreignKeys": {
|
|
||||||
"sessions_userId_users_id_fk": {
|
|
||||||
"name": "sessions_userId_users_id_fk",
|
|
||||||
"tableFrom": "sessions",
|
|
||||||
"tableTo": "users",
|
|
||||||
"columnsFrom": [
|
|
||||||
"userId"
|
|
||||||
],
|
|
||||||
"columnsTo": [
|
|
||||||
"id"
|
|
||||||
],
|
|
||||||
"onDelete": "cascade",
|
|
||||||
"onUpdate": "no action"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"compositePrimaryKeys": {}
|
|
||||||
},
|
|
||||||
"users": {
|
|
||||||
"name": "users",
|
|
||||||
"schema": "",
|
|
||||||
"columns": {
|
|
||||||
"id": {
|
|
||||||
"name": "id",
|
|
||||||
"type": "text",
|
|
||||||
"primaryKey": true,
|
|
||||||
"notNull": true
|
|
||||||
},
|
|
||||||
"name": {
|
|
||||||
"name": "name",
|
|
||||||
"type": "text",
|
|
||||||
"primaryKey": false,
|
|
||||||
"notNull": false
|
|
||||||
},
|
|
||||||
"email": {
|
|
||||||
"name": "email",
|
|
||||||
"type": "text",
|
|
||||||
"primaryKey": false,
|
|
||||||
"notNull": true
|
|
||||||
},
|
|
||||||
"emailVerified": {
|
|
||||||
"name": "emailVerified",
|
|
||||||
"type": "integer",
|
|
||||||
"primaryKey": false,
|
|
||||||
"notNull": false
|
|
||||||
},
|
|
||||||
"image": {
|
|
||||||
"name": "image",
|
|
||||||
"type": "text",
|
|
||||||
"primaryKey": false,
|
|
||||||
"notNull": false
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"indexes": {},
|
|
||||||
"foreignKeys": {},
|
"foreignKeys": {},
|
||||||
"compositePrimaryKeys": {}
|
"compositePrimaryKeys": {}
|
||||||
},
|
|
||||||
"verificationToken": {
|
|
||||||
"name": "verificationToken",
|
|
||||||
"schema": "",
|
|
||||||
"columns": {
|
|
||||||
"identifier": {
|
|
||||||
"name": "identifier",
|
|
||||||
"type": "text",
|
|
||||||
"primaryKey": false,
|
|
||||||
"notNull": true
|
|
||||||
},
|
|
||||||
"token": {
|
|
||||||
"name": "token",
|
|
||||||
"type": "text",
|
|
||||||
"primaryKey": false,
|
|
||||||
"notNull": true
|
|
||||||
},
|
|
||||||
"expires": {
|
|
||||||
"name": "expires",
|
|
||||||
"type": "integer",
|
|
||||||
"primaryKey": false,
|
|
||||||
"notNull": true
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"indexes": {},
|
|
||||||
"foreignKeys": {},
|
|
||||||
"compositePrimaryKeys": {
|
|
||||||
"verificationToken_identifier_token": {
|
|
||||||
"name": "verificationToken_identifier_token",
|
|
||||||
"columns": [
|
|
||||||
"identifier",
|
|
||||||
"token"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"enums": {},
|
"enums": {},
|
||||||
|
|
|
||||||
|
|
@ -5,15 +5,15 @@
|
||||||
{
|
{
|
||||||
"idx": 0,
|
"idx": 0,
|
||||||
"version": "5",
|
"version": "5",
|
||||||
"when": 1685716228240,
|
"when": 1685719783900,
|
||||||
"tag": "0000_clammy_freak",
|
"tag": "0000_fair_groot",
|
||||||
"breakpoints": false
|
"breakpoints": false
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"idx": 1,
|
"idx": 1,
|
||||||
"version": "5",
|
"version": "5",
|
||||||
"when": 1685716937211,
|
"when": 1685719963202,
|
||||||
"tag": "0001_petite_sue_storm",
|
"tag": "0001_naive_gunslinger",
|
||||||
"breakpoints": false
|
"breakpoints": false
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|
|
||||||
118
lib/db/schema.ts
118
lib/db/schema.ts
|
|
@ -1,98 +1,56 @@
|
||||||
import { ProviderType } from "@auth/nextjs/providers";
|
|
||||||
import { sql } from "@vercel/postgres";
|
import { sql } from "@vercel/postgres";
|
||||||
import { relations } from "drizzle-orm";
|
import { pgTable, text } from "drizzle-orm/pg-core";
|
||||||
import { integer, pgTable, primaryKey, text } from "drizzle-orm/pg-core";
|
|
||||||
import { drizzle } from "drizzle-orm/vercel-postgres";
|
import { drizzle } from "drizzle-orm/vercel-postgres";
|
||||||
|
|
||||||
// const connection = createPool({ connectionString: process.env.POSTGRES_URL });
|
// 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),
|
|
||||||
}));
|
|
||||||
|
|
||||||
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"),
|
|
||||||
},
|
|
||||||
(account) => ({
|
|
||||||
_: primaryKey(account.provider, account.providerAccountId),
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
export const sessions = pgTable("sessions", {
|
|
||||||
userId: text("userId")
|
|
||||||
.notNull()
|
|
||||||
.references(() => users.id, { onDelete: "cascade" }),
|
|
||||||
sessionToken: text("sessionToken").notNull().primaryKey(),
|
|
||||||
expires: integer("expires").notNull(),
|
|
||||||
});
|
|
||||||
|
|
||||||
export const verificationTokens = pgTable(
|
|
||||||
"verificationToken",
|
|
||||||
{
|
|
||||||
identifier: text("identifier").notNull(),
|
|
||||||
token: text("token").notNull(),
|
|
||||||
expires: integer("expires").notNull(),
|
|
||||||
},
|
|
||||||
(vt) => ({
|
|
||||||
_: primaryKey(vt.identifier, vt.token),
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
export type Schema = {
|
|
||||||
users: typeof users;
|
|
||||||
accounts: typeof accounts;
|
|
||||||
sessions: typeof sessions;
|
|
||||||
verificationTokens: typeof verificationTokens;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const chats = pgTable("chats", {
|
export const chats = pgTable("chats", {
|
||||||
id: text("id").notNull().primaryKey(),
|
id: text("id").notNull().primaryKey(),
|
||||||
title: text("role").notNull(),
|
title: text("role").notNull(),
|
||||||
userId: text("userId")
|
userId: text("userId").notNull(),
|
||||||
.notNull()
|
// messages: json("messages").notNull().default({}),
|
||||||
.references(() => users.id),
|
// .references(() => users.id),
|
||||||
});
|
});
|
||||||
|
|
||||||
export const chatsRelations = relations(chats, ({ one }) => ({
|
// export const messages = pgTable("messages", {
|
||||||
user: one(users, {
|
// id: text("id").notNull().primaryKey(),
|
||||||
fields: [chats.userId],
|
// title: text("role").notNull(),
|
||||||
references: [users.id],
|
// chatId: text("chatId")
|
||||||
}),
|
// .notNull()
|
||||||
}));
|
// .references(() => chats.id),
|
||||||
|
// });
|
||||||
|
|
||||||
|
// export const chatsRelations = relations(chats, ({ many }) => ({
|
||||||
|
// // user: one(users, {
|
||||||
|
// // fields: [chats.userId],
|
||||||
|
// // references: [users.id],
|
||||||
|
// // }),
|
||||||
|
// messages: many(messages),
|
||||||
|
// }));
|
||||||
|
|
||||||
|
// export const messagesRelations = relations(messages, ({ one }) => ({
|
||||||
|
// chat: one(chats, {
|
||||||
|
// fields: [messages.chatId],
|
||||||
|
// references: [chats.id],
|
||||||
|
// }),
|
||||||
|
// }));
|
||||||
|
|
||||||
export const db = drizzle(sql, {
|
export const db = drizzle(sql, {
|
||||||
schema: {
|
schema: {
|
||||||
users,
|
// users,
|
||||||
accounts,
|
// accounts,
|
||||||
sessions,
|
// sessions,
|
||||||
verificationTokens,
|
// verificationTokens,
|
||||||
chats,
|
chats,
|
||||||
chatsRelations,
|
// chatsRelations,
|
||||||
usersRelations,
|
// messages,
|
||||||
|
// messagesRelations,
|
||||||
|
// usersRelations,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
export type DbClient = typeof db;
|
export type DbClient = typeof db;
|
||||||
|
|
||||||
|
export type Schema = {
|
||||||
|
chats: typeof chats;
|
||||||
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue