Skip to content

Commit

Permalink
Merge pull request #3 from joshxfi/feat/migrate-db
Browse files Browse the repository at this point in the history
feat: migrate db
  • Loading branch information
joshxfi authored Apr 23, 2024
2 parents 5653f6d + 09ffd43 commit 33f4d88
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 48 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ yarn-error.log*
# typescript
*.tsbuildinfo
next-env.d.ts

dev.*
Binary file modified bun.lockb
Binary file not shown.
18 changes: 0 additions & 18 deletions docker-compose.yml

This file was deleted.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
"lint": "next lint",
"generate:db": "prisma generate",
"push:db": "prisma db push",
"docker:down": "docker-compose down",
"docker:up": "docker-compose up -d",
"postinstall": "prisma generate"
},
"dependencies": {
"@hookform/resolvers": "^3.3.4",
"@libsql/client": "^0.6.0",
"@next-auth/prisma-adapter": "^1.0.7",
"@nextui-org/react": "^2.2.10",
"@pothos/core": "^3.41.0",
"@pothos/plugin-prisma": "^3.65.0",
"@prisma/adapter-libsql": "^5.12.1",
"@prisma/client": "^5.12.1",
"@radix-ui/react-alert-dialog": "^1.0.5",
"@radix-ui/react-avatar": "^1.0.4",
Expand All @@ -41,7 +41,6 @@
"clsx": "^2.1.0",
"cmdk": "^1.0.0",
"date-fns": "^3.3.1",
"framer-motion": "^11.0.8",
"gql.tada": "^1.5.6",
"graphql": "^16.8.1",
"graphql-scalars": "^1.22.5",
Expand Down
157 changes: 157 additions & 0 deletions prisma/migrations/20240423033748_init/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
-- CreateTable
CREATE TABLE "User" (
"id" TEXT NOT NULL PRIMARY KEY,
"name" TEXT,
"email" TEXT,
"emailVerified" DATETIME,
"image" TEXT,
"username" TEXT,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME,
"bio" TEXT
);

-- CreateTable
CREATE TABLE "Account" (
"id" TEXT NOT NULL PRIMARY KEY,
"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
);

-- CreateTable
CREATE TABLE "Organization" (
"id" TEXT NOT NULL PRIMARY KEY,
"name" TEXT NOT NULL,
"image" TEXT,
"bio" TEXT NOT NULL,
"username" TEXT NOT NULL,
"isApproved" BOOLEAN NOT NULL DEFAULT false,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME,
"ownerId" TEXT NOT NULL
);

-- CreateTable
CREATE TABLE "Donation" (
"id" TEXT NOT NULL PRIMARY KEY,
"amount" INTEGER NOT NULL,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME,
"orgId" TEXT NOT NULL,
"userId" TEXT NOT NULL
);

-- CreateTable
CREATE TABLE "Session" (
"id" TEXT NOT NULL PRIMARY KEY,
"sessionToken" TEXT NOT NULL,
"userId" TEXT NOT NULL,
"expires" DATETIME NOT NULL
);

-- CreateTable
CREATE TABLE "VerificationToken" (
"identifier" TEXT NOT NULL,
"token" TEXT NOT NULL,
"expires" DATETIME NOT NULL
);

-- CreateTable
CREATE TABLE "Post" (
"id" TEXT NOT NULL PRIMARY KEY,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL,
"content" TEXT NOT NULL,
"imgUrl" TEXT,
"sdg" TEXT,
"authorId" TEXT,
"orgId" TEXT,
"parentId" TEXT
);

-- CreateTable
CREATE TABLE "Upvote" (
"id" TEXT NOT NULL PRIMARY KEY,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"userId" TEXT NOT NULL,
"postId" TEXT NOT NULL
);

-- CreateTable
CREATE TABLE "Tag" (
"id" TEXT NOT NULL PRIMARY KEY,
"name" TEXT NOT NULL,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);

-- CreateTable
CREATE TABLE "_PostToTag" (
"A" TEXT NOT NULL,
"B" TEXT NOT NULL
);

-- CreateIndex
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");

-- CreateIndex
CREATE UNIQUE INDEX "User_username_key" ON "User"("username");

-- CreateIndex
CREATE INDEX "Account_userId_idx" ON "Account"("userId");

-- CreateIndex
CREATE UNIQUE INDEX "Account_provider_providerAccountId_key" ON "Account"("provider", "providerAccountId");

-- CreateIndex
CREATE UNIQUE INDEX "Organization_username_key" ON "Organization"("username");

-- CreateIndex
CREATE INDEX "Organization_ownerId_idx" ON "Organization"("ownerId");

-- CreateIndex
CREATE INDEX "Donation_orgId_idx" ON "Donation"("orgId");

-- CreateIndex
CREATE INDEX "Donation_userId_idx" ON "Donation"("userId");

-- CreateIndex
CREATE UNIQUE INDEX "Session_sessionToken_key" ON "Session"("sessionToken");

-- CreateIndex
CREATE INDEX "Session_userId_idx" ON "Session"("userId");

-- CreateIndex
CREATE UNIQUE INDEX "VerificationToken_token_key" ON "VerificationToken"("token");

-- CreateIndex
CREATE UNIQUE INDEX "VerificationToken_identifier_token_key" ON "VerificationToken"("identifier", "token");

-- CreateIndex
CREATE INDEX "Post_parentId_createdAt_idx" ON "Post"("parentId", "createdAt" DESC);

-- CreateIndex
CREATE INDEX "Post_orgId_parentId_createdAt_idx" ON "Post"("orgId", "parentId", "createdAt" DESC);

-- CreateIndex
CREATE INDEX "Post_authorId_parentId_createdAt_idx" ON "Post"("authorId", "parentId", "createdAt" DESC);

-- CreateIndex
CREATE INDEX "Upvote_postId_idx" ON "Upvote"("postId");

-- CreateIndex
CREATE UNIQUE INDEX "Tag_name_key" ON "Tag"("name");

-- CreateIndex
CREATE UNIQUE INDEX "_PostToTag_AB_unique" ON "_PostToTag"("A", "B");

-- CreateIndex
CREATE INDEX "_PostToTag_B_index" ON "_PostToTag"("B");
3 changes: 3 additions & 0 deletions prisma/migrations/migration_lock.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "sqlite"
44 changes: 18 additions & 26 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
generator client {
provider = "prisma-client-js"
previewFeatures = ["driverAdapters"]
}

generator pothos {
provider = "prisma-pothos-types"
}

datasource db {
provider = "postgresql"
url = env("POSTGRES_PRISMA_URL")
provider = "sqlite"
url = "file:./dev.db"
relationMode = "prisma"
}

model User {
id String @id @default(cuid())
id String @id @default(cuid())
name String?
email String? @unique
email String? @unique
emailVerified DateTime?
image String?
username String? @unique
createdAt DateTime @default(now())
updatedAt DateTime? @updatedAt
role Role @default(USER)
bio String? @db.VarChar(255)
posts Post[]
accounts Account[]
sessions Session[]
orgs Organization[]
donations Donation[]
username String? @unique
createdAt DateTime @default(now())
updatedAt DateTime? @updatedAt
bio String?
posts Post[]
accounts Account[]
sessions Session[]
orgs Organization[]
donations Donation[]
}

model Account {
Expand All @@ -37,12 +36,12 @@ model Account {
type String
provider String
providerAccountId String
refresh_token String? @db.Text
access_token String? @db.Text
refresh_token String?
access_token String?
expires_at Int?
token_type String?
scope String?
id_token String? @db.Text
id_token String?
session_state String?
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
Expand Down Expand Up @@ -106,8 +105,7 @@ model Post {
id String @id
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
content String @db.VarChar(500)
isAnonymous Boolean @default(false)
content String
imgUrl String?
upvotes Upvote[]
tags Tag[]
Expand Down Expand Up @@ -145,9 +143,3 @@ model Tag {
createdAt DateTime @default(now())
posts Post[]
}

enum Role {
USER
MODERATOR
ADMIN
}
11 changes: 10 additions & 1 deletion src/lib/db.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
import { PrismaClient } from "@prisma/client";
import { PrismaLibSQL } from "@prisma/adapter-libsql";
import { createClient } from "@libsql/client";

const libsql = createClient({
url: `${process.env.TURSO_DATABASE_URL}`,
authToken: `${process.env.TURSO_AUTH_TOKEN}`,
});

const adapter = new PrismaLibSQL(libsql);

const prismaClientSingleton = () => {
return new PrismaClient();
return new PrismaClient({ adapter });
};

declare global {
Expand Down

0 comments on commit 33f4d88

Please sign in to comment.