Skip to content

Commit

Permalink
Merge pull request #178 from omsimos/dev
Browse files Browse the repository at this point in the history
Migrate database to libSQL/Turso
  • Loading branch information
joshxfi authored Apr 8, 2024
2 parents 91c08ea + 6dbf33c commit ccdd18c
Show file tree
Hide file tree
Showing 36 changed files with 631 additions and 360 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
- name: Setup node
uses: actions/setup-node@v1
with:
node-version: 16
node-version: 18

- name: Install dependencies
run: yarn
Expand All @@ -31,4 +31,6 @@ jobs:
run: yarn lint

- name: Build project
env:
TURSO_DATABASE_URL: ${{ secrets.TURSO_DATABASE_URL }}
run: yarn build
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ yarn-error.log*

# turbo
.turbo
.vercel
8 changes: 4 additions & 4 deletions apps/web/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ const withPWA = require('next-pwa')({

module.exports = withPWA({
reactStrictMode: true,
compiler: {
removeConsole: process.env.NODE_ENV === 'production',
},
transpilePackages: ['@umamin/db', '@umamin/generated'],
// compiler: {
// removeConsole: process.env.NODE_ENV === 'production',
// },
transpilePackages: ['@umamin/generated'],
modularizeImports: {
'date-fns': {
transform: 'date-fns/{{member}}',
Expand Down
10 changes: 7 additions & 3 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@
"start": "next start",
"lint": "next lint",
"clean": "rimraf .next/ .turbo/ node_modules/",
"clean:cache": "rimraf .next/ .turbo/"
"clean:cache": "rimraf .next/ .turbo/",
"postinstall": "prisma generate"
},
"dependencies": {
"@apollo/server": "^4.9.4",
"@apollo/server-plugin-response-cache": "^4.1.3",
"@as-integrations/next": "^2.0.2",
"@hcaptcha/react-hcaptcha": "^1.4.4",
"@headlessui/react": "^1.7.15",
"@libsql/client": "^0.6.0",
"@next-auth/prisma-adapter": "^1.0.5",
"@prisma/adapter-libsql": "^5.12.1",
"@prisma/client": "^5.12.1",
"@tanstack/react-query": "^5.4.3",
"@umamin/db": "*",
"@umamin/generated": "*",
"@vercel/og": "^0.5.20",
"bcrypt": "^5.1.0",
Expand All @@ -34,7 +37,7 @@
"lru-cache": "^10.0.0",
"nanoid": "^4.0.0",
"next": "13.4.12",
"next-auth": "^4.18.8",
"next-auth": "4.24.6",
"next-pwa": "^5.6.0",
"next-seo": "^5.4.0",
"nprogress": "^0.2.0",
Expand Down Expand Up @@ -74,6 +77,7 @@
"postcss": "^8.4.14",
"prettier": "^2.7.1",
"prettier-plugin-tailwindcss": "^0.1.11",
"prisma": "^5.12.1",
"tailwindcss": "^3.1.4",
"typescript": "4.7.4"
}
Expand Down
Binary file added apps/web/prisma/dev.db
Binary file not shown.
Binary file added apps/web/prisma/dev.db-journal
Binary file not shown.
90 changes: 90 additions & 0 deletions apps/web/prisma/migrations/20240407070104_init/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
-- 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 "User" (
"id" TEXT NOT NULL PRIMARY KEY,
"name" TEXT,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"email" TEXT,
"emailVerified" DATETIME,
"username" TEXT,
"password" TEXT,
"image" TEXT,
"message" TEXT NOT NULL DEFAULT 'Send me an anonymous message!'
);

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

-- CreateTable
CREATE TABLE "Message" (
"id" TEXT NOT NULL PRIMARY KEY,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL,
"content" TEXT NOT NULL,
"receiverMsg" TEXT NOT NULL,
"isOpened" BOOLEAN NOT NULL DEFAULT false,
"reply" TEXT,
"clue" TEXT,
"senderId" TEXT,
"receiverUsername" TEXT,
"receiverId" TEXT NOT NULL
);

-- CreateTable
CREATE TABLE "GlobalMessage" (
"id" TEXT NOT NULL PRIMARY KEY,
"createdAt" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" DATETIME NOT NULL,
"content" TEXT NOT NULL,
"isAnonymous" BOOLEAN NOT NULL DEFAULT true,
"userId" TEXT
);

-- 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 "User_email_key" ON "User"("email");

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

-- CreateIndex
CREATE INDEX "User_email_username_idx" ON "User"("email", "username");

-- 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 "Message_senderId_createdAt_idx" ON "Message"("senderId", "createdAt" DESC);

-- CreateIndex
CREATE INDEX "Message_receiverId_createdAt_idx" ON "Message"("receiverId", "createdAt" DESC);

-- CreateIndex
CREATE INDEX "GlobalMessage_userId_updatedAt_idx" ON "GlobalMessage"("userId", "updatedAt" DESC);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- DropIndex
DROP INDEX "GlobalMessage_userId_updatedAt_idx";

-- CreateIndex
CREATE INDEX "GlobalMessage_userId_idx" ON "GlobalMessage"("userId");

-- CreateIndex
CREATE INDEX "GlobalMessage_updatedAt_idx" ON "GlobalMessage"("updatedAt" DESC);
3 changes: 3 additions & 0 deletions apps/web/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"
94 changes: 94 additions & 0 deletions apps/web/prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
provider = "prisma-client-js"
previewFeatures = ["driverAdapters"]
}

datasource db {
provider = "sqlite"
url = "file:./dev.db"
relationMode = "prisma"
}

model Account {
id String @id @default(cuid())
userId String
type String
provider String
providerAccountId String
refresh_token String?
access_token String?
expires_at Int?
token_type String?
scope String?
id_token String?
session_state String?
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@unique([provider, providerAccountId])
@@index([userId])
}

model User {
id String @id @default(cuid())
name String?
createdAt DateTime @default(now())
email String? @unique
emailVerified DateTime?
username String? @unique
password String?
image String?
message String @default("Send me an anonymous message!")
accounts Account[]
sentMessages Message[] @relation("sentMessages")
sentGlobalMessages GlobalMessage[] @relation("sentGlobalMessages")
receivedMessages Message[] @relation("receivedMessages")
@@index([email, username])
}

model VerificationToken {
identifier String
token String @unique
expires DateTime
@@unique([identifier, token])
}

model Message {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
content String
receiverMsg String
isOpened Boolean @default(false)
reply String?
clue String?
senderId String?
sender User? @relation(name: "sentMessages", fields: [senderId], references: [id])
receiverUsername String?
receiverId String
receiver User @relation(name: "receivedMessages", fields: [receiverId], references: [id], onDelete: Cascade)
@@index([senderId, createdAt(sort: Desc)])
@@index([receiverId, createdAt(sort: Desc)])
}

model GlobalMessage {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
content String
isAnonymous Boolean @default(true)
userId String?
user User? @relation(name: "sentGlobalMessages", fields: [userId], references: [id], onDelete: Cascade)
@@index([userId])
@@index([updatedAt(sort: Desc)])
}
30 changes: 16 additions & 14 deletions apps/web/src/components/Create.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,28 @@ const AdContainer = dynamic(() => import('@/components/AdContainer'), {
});

export const Create = () => {
const { refetchUser } = useInboxContext();
const { user, refetchUser } = useInboxContext();
const { mutate } = useMutation({ mutationFn: editUsername });
const [username, setUsername] = useState('');

const handleSubmit: React.FormEventHandler = (e) => {
e.preventDefault();

mutate(
{ username },
{
onSuccess: (data) => {
if (data.editUsername.error) {
toast.error(data.editUsername.error);
return;
}

refetchUser();
},
}
);
if (user?.id) {
mutate(
{ userId: user.id, username },
{
onSuccess: (data) => {
if (data.editUsername.error) {
toast.error(data.editUsername.error);
return;
}

refetchUser();
},
}
);
}
setUsername('');
};

Expand Down
41 changes: 22 additions & 19 deletions apps/web/src/components/Dialog/SendGlobal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,27 @@ export const SendGlobalModal = ({

const handleSend: React.FormEventHandler = (e) => {
e.preventDefault();
mutate(
{ input: { content: message, isAnonymous } },
{
onSuccess: (data) => {
if (data.sendGlobalMessage.error) {
toast.error(data.sendGlobalMessage.error);
return;
}

setMessageData(data.sendGlobalMessage.data);
toast.success('Message posted');
if (user?.id) {
mutate(
{ input: { userId: user.id, content: message, isAnonymous } },
{
onSuccess: (data) => {
if (data.sendGlobalMessage.error) {
toast.error(data.sendGlobalMessage.error);
return;
}

setTimeout(() => {
setMessage('');
}, 500);
},
}
);
setMessageData(data.sendGlobalMessage.data);
toast.success('Message posted');

setTimeout(() => {
setMessage('');
}, 500);
},
}
);
}

setIsOpen(false);
};
Expand All @@ -58,12 +61,12 @@ export const SendGlobalModal = ({
className='msg-card flex flex-col space-y-4 p-6'
>
<div>
<div className='flex gap-x-2 mb-4 items-center'>
<div className='mb-4 flex items-center gap-x-2'>
<ImageFill
alt='profile picture'
src={user?.image}
unoptimized
className='border-secondary-100 h-[40px] w-[40px] object-cover rounded-full border flex-none'
className='border-secondary-100 h-[40px] w-[40px] flex-none rounded-full border object-cover'
/>
<div>
<p>{user?.username}</p>
Expand All @@ -84,7 +87,7 @@ export const SendGlobalModal = ({
/>
</div>

<button type='submit' className='rounded py-2 bg-primary-200'>
<button type='submit' className='bg-primary-200 rounded py-2'>
Post
</button>

Expand Down
Loading

0 comments on commit ccdd18c

Please sign in to comment.