From c493ac342b5e34a0414e96d2f8ad19efbf1b20df Mon Sep 17 00:00:00 2001 From: Jared Palmer Date: Fri, 15 Nov 2024 10:13:21 -0500 Subject: [PATCH] Refactor drizzle and db folder --- app/(auth)/actions.ts | 2 +- app/(auth)/auth.ts | 2 +- app/(chat)/api/chat/route.ts | 4 ++-- app/(chat)/api/document/route.ts | 2 +- app/(chat)/api/history/route.ts | 2 +- app/(chat)/api/suggestions/route.ts | 2 +- app/(chat)/api/vote/route.ts | 2 +- app/(chat)/chat/[id]/page.tsx | 2 +- components/custom/block.tsx | 2 +- components/custom/chat.tsx | 2 +- components/custom/editor.tsx | 2 +- components/custom/message-actions.tsx | 2 +- components/custom/message.tsx | 2 +- components/custom/sidebar-history.tsx | 2 +- components/custom/use-block-stream.tsx | 2 +- components/custom/version-footer.tsx | 2 +- drizzle.config.ts | 4 ++-- {db => lib/db}/migrate.ts | 0 lib/{drizzle => db/migrations}/0000_keen_devos.sql | 0 lib/{drizzle => db/migrations}/0001_sparkling_blue_marvel.sql | 0 lib/{drizzle => db/migrations}/0002_wandering_riptide.sql | 0 lib/{drizzle => db/migrations}/meta/0000_snapshot.json | 0 lib/{drizzle => db/migrations}/meta/0001_snapshot.json | 0 lib/{drizzle => db/migrations}/meta/0002_snapshot.json | 0 lib/{drizzle => db/migrations}/meta/_journal.json | 0 {db => lib/db}/queries.ts | 0 {db => lib/db}/schema.ts | 0 lib/editor/suggestions.tsx | 2 +- lib/utils.ts | 2 +- package.json | 2 +- tsconfig.json | 1 - 31 files changed, 22 insertions(+), 23 deletions(-) rename {db => lib/db}/migrate.ts (100%) rename lib/{drizzle => db/migrations}/0000_keen_devos.sql (100%) rename lib/{drizzle => db/migrations}/0001_sparkling_blue_marvel.sql (100%) rename lib/{drizzle => db/migrations}/0002_wandering_riptide.sql (100%) rename lib/{drizzle => db/migrations}/meta/0000_snapshot.json (100%) rename lib/{drizzle => db/migrations}/meta/0001_snapshot.json (100%) rename lib/{drizzle => db/migrations}/meta/0002_snapshot.json (100%) rename lib/{drizzle => db/migrations}/meta/_journal.json (100%) rename {db => lib/db}/queries.ts (100%) rename {db => lib/db}/schema.ts (100%) diff --git a/app/(auth)/actions.ts b/app/(auth)/actions.ts index 66006c9..058e8af 100644 --- a/app/(auth)/actions.ts +++ b/app/(auth)/actions.ts @@ -2,7 +2,7 @@ import { z } from 'zod'; -import { createUser, getUser } from '@/db/queries'; +import { createUser, getUser } from '@/lib/db/queries'; import { signIn } from './auth'; diff --git a/app/(auth)/auth.ts b/app/(auth)/auth.ts index 1fa8401..d49fefe 100644 --- a/app/(auth)/auth.ts +++ b/app/(auth)/auth.ts @@ -2,7 +2,7 @@ import { compare } from 'bcrypt-ts'; import NextAuth, { User, Session } from 'next-auth'; import Credentials from 'next-auth/providers/credentials'; -import { getUser } from '@/db/queries'; +import { getUser } from '@/lib/db/queries'; import { authConfig } from './auth.config'; diff --git a/app/(chat)/api/chat/route.ts b/app/(chat)/api/chat/route.ts index 83b7dcd..f3b54f7 100644 --- a/app/(chat)/api/chat/route.ts +++ b/app/(chat)/api/chat/route.ts @@ -19,8 +19,8 @@ import { saveDocument, saveMessages, saveSuggestions, -} from '@/db/queries'; -import { Suggestion } from '@/db/schema'; +} from '@/lib/db/queries'; +import { Suggestion } from '@/lib/db/schema'; import { generateUUID, getMostRecentUserMessage, diff --git a/app/(chat)/api/document/route.ts b/app/(chat)/api/document/route.ts index f73572f..885d39c 100644 --- a/app/(chat)/api/document/route.ts +++ b/app/(chat)/api/document/route.ts @@ -3,7 +3,7 @@ import { deleteDocumentsByIdAfterTimestamp, getDocumentsById, saveDocument, -} from '@/db/queries'; +} from '@/lib/db/queries'; export async function GET(request: Request) { const { searchParams } = new URL(request.url); diff --git a/app/(chat)/api/history/route.ts b/app/(chat)/api/history/route.ts index 0c98e79..00d365b 100644 --- a/app/(chat)/api/history/route.ts +++ b/app/(chat)/api/history/route.ts @@ -1,5 +1,5 @@ import { auth } from '@/app/(auth)/auth'; -import { getChatsByUserId } from '@/db/queries'; +import { getChatsByUserId } from '@/lib/db/queries'; export async function GET() { const session = await auth(); diff --git a/app/(chat)/api/suggestions/route.ts b/app/(chat)/api/suggestions/route.ts index dc472c2..542bc61 100644 --- a/app/(chat)/api/suggestions/route.ts +++ b/app/(chat)/api/suggestions/route.ts @@ -1,5 +1,5 @@ import { auth } from '@/app/(auth)/auth'; -import { getSuggestionsByDocumentId } from '@/db/queries'; +import { getSuggestionsByDocumentId } from '@/lib/db/queries'; export async function GET(request: Request) { const { searchParams } = new URL(request.url); diff --git a/app/(chat)/api/vote/route.ts b/app/(chat)/api/vote/route.ts index 42b1701..87f78e7 100644 --- a/app/(chat)/api/vote/route.ts +++ b/app/(chat)/api/vote/route.ts @@ -1,5 +1,5 @@ import { auth } from '@/app/(auth)/auth'; -import { getVotesByChatId, voteMessage } from '@/db/queries'; +import { getVotesByChatId, voteMessage } from '@/lib/db/queries'; export async function GET(request: Request) { const { searchParams } = new URL(request.url); diff --git a/app/(chat)/chat/[id]/page.tsx b/app/(chat)/chat/[id]/page.tsx index 859e328..a99d718 100644 --- a/app/(chat)/chat/[id]/page.tsx +++ b/app/(chat)/chat/[id]/page.tsx @@ -5,7 +5,7 @@ import { notFound } from 'next/navigation'; import { DEFAULT_MODEL_NAME, models } from '@/ai/models'; import { auth } from '@/app/(auth)/auth'; import { Chat as PreviewChat } from '@/components/custom/chat'; -import { getChatById, getMessagesByChatId } from '@/db/queries'; +import { getChatById, getMessagesByChatId } from '@/lib/db/queries'; import { convertToUIMessages } from '@/lib/utils'; export default async function Page(props: { params: Promise }) { diff --git a/components/custom/block.tsx b/components/custom/block.tsx index d01bd4c..7321121 100644 --- a/components/custom/block.tsx +++ b/components/custom/block.tsx @@ -17,7 +17,7 @@ import { useWindowSize, } from 'usehooks-ts'; -import { Document, Suggestion, Vote } from '@/db/schema'; +import { Document, Suggestion, Vote } from '@/lib/db/schema'; import { fetcher } from '@/lib/utils'; import { DiffView } from './diffview'; diff --git a/components/custom/chat.tsx b/components/custom/chat.tsx index 3edd615..fa0565a 100644 --- a/components/custom/chat.tsx +++ b/components/custom/chat.tsx @@ -10,7 +10,7 @@ import { useWindowSize } from 'usehooks-ts'; import { ChatHeader } from '@/components/custom/chat-header'; import { PreviewMessage, ThinkingMessage } from '@/components/custom/message'; import { useScrollToBottom } from '@/components/custom/use-scroll-to-bottom'; -import { Vote } from '@/db/schema'; +import { Vote } from '@/lib/db/schema'; import { fetcher } from '@/lib/utils'; import { Block, UIBlock } from './block'; diff --git a/components/custom/editor.tsx b/components/custom/editor.tsx index 64822a4..6d657b8 100644 --- a/components/custom/editor.tsx +++ b/components/custom/editor.tsx @@ -6,7 +6,7 @@ import { EditorState } from 'prosemirror-state'; import { EditorView } from 'prosemirror-view'; import React, { memo, useEffect, useRef } from 'react'; -import { Suggestion } from '@/db/schema'; +import { Suggestion } from '@/lib/db/schema'; import { documentSchema, handleTransaction, diff --git a/components/custom/message-actions.tsx b/components/custom/message-actions.tsx index 902fa09..d88bae8 100644 --- a/components/custom/message-actions.tsx +++ b/components/custom/message-actions.tsx @@ -3,7 +3,7 @@ import { toast } from 'sonner'; import { useSWRConfig } from 'swr'; import { useCopyToClipboard } from 'usehooks-ts'; -import { Vote } from '@/db/schema'; +import { Vote } from '@/lib/db/schema'; import { getMessageIdFromAnnotations } from '@/lib/utils'; import { CopyIcon, ThumbDownIcon, ThumbUpIcon } from './icons'; diff --git a/components/custom/message.tsx b/components/custom/message.tsx index 702a7e1..137beb9 100644 --- a/components/custom/message.tsx +++ b/components/custom/message.tsx @@ -5,7 +5,7 @@ import cx from 'classnames'; import { motion } from 'framer-motion'; import { Dispatch, SetStateAction } from 'react'; -import { Vote } from '@/db/schema'; +import { Vote } from '@/lib/db/schema'; import { UIBlock } from './block'; import { DocumentToolCall, DocumentToolResult } from './document'; diff --git a/components/custom/sidebar-history.tsx b/components/custom/sidebar-history.tsx index cb6e01f..09ca8e2 100644 --- a/components/custom/sidebar-history.tsx +++ b/components/custom/sidebar-history.tsx @@ -34,7 +34,7 @@ import { SidebarMenuItem, useSidebar, } from '@/components/ui/sidebar'; -import { Chat } from '@/db/schema'; +import { Chat } from '@/lib/db/schema'; import { fetcher } from '@/lib/utils'; type GroupedChats = { diff --git a/components/custom/use-block-stream.tsx b/components/custom/use-block-stream.tsx index 8871fa1..bcc0454 100644 --- a/components/custom/use-block-stream.tsx +++ b/components/custom/use-block-stream.tsx @@ -2,7 +2,7 @@ import { JSONValue } from 'ai'; import { Dispatch, SetStateAction, useEffect, useState } from 'react'; import { useSWRConfig } from 'swr'; -import { Suggestion } from '@/db/schema'; +import { Suggestion } from '@/lib/db/schema'; import { UIBlock } from './block'; diff --git a/components/custom/version-footer.tsx b/components/custom/version-footer.tsx index bb30cb5..c257949 100644 --- a/components/custom/version-footer.tsx +++ b/components/custom/version-footer.tsx @@ -6,7 +6,7 @@ import { useState } from 'react'; import { useSWRConfig } from 'swr'; import { useWindowSize } from 'usehooks-ts'; -import { Document } from '@/db/schema'; +import { Document } from '@/lib/db/schema'; import { getDocumentTimestampByIndex } from '@/lib/utils'; import { UIBlock } from './block'; diff --git a/drizzle.config.ts b/drizzle.config.ts index f0537cd..ef25eb8 100644 --- a/drizzle.config.ts +++ b/drizzle.config.ts @@ -6,8 +6,8 @@ config({ }); export default defineConfig({ - schema: './db/schema.ts', - out: './lib/drizzle', + schema: './lib/db/schema.ts', + out: './lib/db/migrations', dialect: 'postgresql', dbCredentials: { url: process.env.POSTGRES_URL!, diff --git a/db/migrate.ts b/lib/db/migrate.ts similarity index 100% rename from db/migrate.ts rename to lib/db/migrate.ts diff --git a/lib/drizzle/0000_keen_devos.sql b/lib/db/migrations/0000_keen_devos.sql similarity index 100% rename from lib/drizzle/0000_keen_devos.sql rename to lib/db/migrations/0000_keen_devos.sql diff --git a/lib/drizzle/0001_sparkling_blue_marvel.sql b/lib/db/migrations/0001_sparkling_blue_marvel.sql similarity index 100% rename from lib/drizzle/0001_sparkling_blue_marvel.sql rename to lib/db/migrations/0001_sparkling_blue_marvel.sql diff --git a/lib/drizzle/0002_wandering_riptide.sql b/lib/db/migrations/0002_wandering_riptide.sql similarity index 100% rename from lib/drizzle/0002_wandering_riptide.sql rename to lib/db/migrations/0002_wandering_riptide.sql diff --git a/lib/drizzle/meta/0000_snapshot.json b/lib/db/migrations/meta/0000_snapshot.json similarity index 100% rename from lib/drizzle/meta/0000_snapshot.json rename to lib/db/migrations/meta/0000_snapshot.json diff --git a/lib/drizzle/meta/0001_snapshot.json b/lib/db/migrations/meta/0001_snapshot.json similarity index 100% rename from lib/drizzle/meta/0001_snapshot.json rename to lib/db/migrations/meta/0001_snapshot.json diff --git a/lib/drizzle/meta/0002_snapshot.json b/lib/db/migrations/meta/0002_snapshot.json similarity index 100% rename from lib/drizzle/meta/0002_snapshot.json rename to lib/db/migrations/meta/0002_snapshot.json diff --git a/lib/drizzle/meta/_journal.json b/lib/db/migrations/meta/_journal.json similarity index 100% rename from lib/drizzle/meta/_journal.json rename to lib/db/migrations/meta/_journal.json diff --git a/db/queries.ts b/lib/db/queries.ts similarity index 100% rename from db/queries.ts rename to lib/db/queries.ts diff --git a/db/schema.ts b/lib/db/schema.ts similarity index 100% rename from db/schema.ts rename to lib/db/schema.ts diff --git a/lib/editor/suggestions.tsx b/lib/editor/suggestions.tsx index 9a74936..e9bd850 100644 --- a/lib/editor/suggestions.tsx +++ b/lib/editor/suggestions.tsx @@ -4,7 +4,7 @@ import { Decoration, DecorationSet, EditorView } from 'prosemirror-view'; import { createRoot } from 'react-dom/client'; import { Suggestion as PreviewSuggestion } from '@/components/custom/suggestion'; -import { Suggestion } from '@/db/schema'; +import { Suggestion } from '@/lib/db/schema'; export interface UISuggestion extends Suggestion { selectionStart: number; diff --git a/lib/utils.ts b/lib/utils.ts index 828dd1b..0366981 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -8,7 +8,7 @@ import { import { clsx, type ClassValue } from 'clsx'; import { twMerge } from 'tailwind-merge'; -import { Message as DBMessage, Document } from '@/db/schema'; +import { Message as DBMessage, Document } from '@/lib/db/schema'; export function cn(...inputs: ClassValue[]) { return twMerge(clsx(inputs)); diff --git a/package.json b/package.json index a7f9c0f..b943a59 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "lint:fix": "next lint --fix", "format": "prettier --write .", "db:generate": "drizzle-kit generate", - "db:migrate": "npx tsx db/migrate.ts", + "db:migrate": "npx tsx lib/db/migrate.ts", "db:studio": "drizzle-kit studio" }, "dependencies": { diff --git a/tsconfig.json b/tsconfig.json index 42e476b..10c77bb 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -13,7 +13,6 @@ "isolatedModules": true, "jsx": "preserve", "incremental": true, - "baseUrl": ".", "plugins": [ { "name": "next"