Skip to content

Commit

Permalink
db: Migrate from prisma to drizzle
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedBassem committed Feb 23, 2024
1 parent bed5720 commit e234d35
Show file tree
Hide file tree
Showing 41 changed files with 2,177 additions and 975 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ jobs:
run: yarn lint
- name: Format
run: yarn format:check
- name: Prisma
run: yarn prisma:generate
- name: Build the workers package
working-directory: packages/workers
run: yarn typecheck
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ yarn-error.log*
next-env.d.ts

# The sqlite database
**/prisma/*dev.db*
**/dev.db
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ To mitigate those risks, you can do one of the following:
1. Limit access to trusted users
2. Let the browser traffic go through some VPN with restricted network policies.
3. Host the browser container outside of your network.
4. Use a hosted browser as a service (e.g. [browserless](browserless.io)). Note: I've never used them before.
4. Use a hosted browser as a service (e.g. [browserless](https://browserless.io)). Note: I've never used them before.

## Stack

- [NextJS](https://nextjs.org/) for the web app. Using app router.
- [Prisma](https://www.prisma.io/) for the database and its migrations.
- [Drizzle](https://orm.drizzle.team/) for the database and its migrations.
- [NextAuth](https://next-auth.js.org) for authentication.
- [tRPC](https://trpc.io) for client->server communication.
- [Puppeteer](https://pptr.dev/) for crawling the bookmarks.
Expand Down Expand Up @@ -89,7 +89,7 @@ You can turnup the whole development environment with:
Or if you have nodejs installed locally, you can do:

- `yarn install` in the root of the repo.
- `yarn prisma:migrate:dev` then `yarn prisma:generate` to generate prisma's data and run the database migrations.
- `yarn db:migrate` to run the db migrations.
- `yarn web` to start the web app.
- Access it over `http://localhost:3000`.
- `yarn workers` to start the crawler and the openai worker.
Expand All @@ -99,7 +99,7 @@ Or if you have nodejs installed locally, you can do:

### Codebase structure

- `packages/db`: Where prisma's schema lives. Shared between packages.
- `packages/db`: Where drizzle's schema lives. Shared between packages.
- `packages/shared`: Shared utilities and code between the workers and the web app.
- `packages/web`: Where the nextjs based web app lives.
- `packages/workers`: Where the background job workers (crawler and openai as of now) run.
Expand Down
8 changes: 1 addition & 7 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ ENV NEXT_TELEMETRY_DISABLED 1
ENV PUPPETEER_SKIP_DOWNLOAD true

RUN corepack enable && \
cd /app/packages/db && \
yarn dlx prisma generate && \
cd /app/packages/web/ && \
yarn install --immutable && \
yarn next experimental-compile
Expand Down Expand Up @@ -67,9 +65,7 @@ ENV PUPPETEER_SKIP_DOWNLOAD true

RUN corepack enable && \
cd /app/packages/workers && \
yarn workspaces focus --production && \
cd /app/packages/db && \
yarn dlx prisma generate
yarn workspaces focus --production


################# The workers ##############
Expand All @@ -92,8 +88,6 @@ COPY packages/workers packages/workers
COPY package.json yarn.lock .yarnrc.yml .
COPY --from=workers_builder /app/node_modules /app/node_modules

RUN corepack enable && cd packages/db && yarn dlx prisma generate

WORKDIR /app/packages/workers
USER root

Expand Down
2 changes: 1 addition & 1 deletion docker/docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ services:
command:
- /bin/sh
- -c
- "corepack enable && yarn install --immutable && yarn prisma:generate && yarn prisma:migrate:dev"
- "corepack enable && yarn install --immutable && yarn run db:migrate"

volumes:
redis:
Expand Down
2 changes: 1 addition & 1 deletion docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ services:
command:
- /bin/sh
- -c
- "cd /app/packages/db && yarn dlx prisma migrate deploy"
- "cd /app/packages/db && yarn run migrate"

volumes:
redis:
Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
"format": "prettier . --write",
"format:check": "prettier . --check",
"lint": "eslint .",
"prisma:migrate:dev": "yarn workspace @hoarder/db prisma migrate dev",
"prisma:generate": "yarn workspace @hoarder/db prisma generate",
"db:migrate": "yarn workspace @hoarder/db run migrate",
"db:studio": "yarn workspace @hoarder/db studio",
"workers": "yarn workspace @hoarder/workers run start",
"web": "yarn workspace @hoarder/web run dev",
"studio": "yarn workspace @hoarder/db prisma studio"
"web": "yarn workspace @hoarder/web run dev"
},
"dependencies": {
"@next/eslint-plugin-next": "^14.1.0",
Expand Down
10 changes: 10 additions & 0 deletions packages/db/drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import "dotenv/config";
import type { Config } from "drizzle-kit";
export default {
schema: "./schema.ts",
out: "./drizzle",
driver: "better-sqlite",
dbCredentials: {
url: process.env.DATABASE_URL || "",
},
} satisfies Config;
7 changes: 7 additions & 0 deletions packages/db/drizzle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import "dotenv/config";
import { drizzle } from "drizzle-orm/better-sqlite3";
import Database from "better-sqlite3";
import * as schema from "./schema";

const sqlite = new Database(process.env.DATABASE_URL);
export const db = drizzle(sqlite, { schema, logger: true });
92 changes: 92 additions & 0 deletions packages/db/drizzle/0000_luxuriant_johnny_blaze.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
CREATE TABLE `account` (
`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,
PRIMARY KEY(`provider`, `providerAccountId`),
FOREIGN KEY (`userId`) REFERENCES `user`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `apiKey` (
`id` text PRIMARY KEY NOT NULL,
`name` text NOT NULL,
`createdAt` integer NOT NULL,
`keyId` text NOT NULL,
`keyHash` text NOT NULL,
`userId` text NOT NULL,
FOREIGN KEY (`userId`) REFERENCES `user`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `bookmarkLinks` (
`id` text PRIMARY KEY NOT NULL,
`url` text NOT NULL,
`title` text,
`description` text,
`imageUrl` text,
`favicon` text,
`crawledAt` integer,
FOREIGN KEY (`id`) REFERENCES `bookmarks`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `bookmarkTags` (
`id` text PRIMARY KEY NOT NULL,
`name` text NOT NULL,
`createdAt` integer NOT NULL,
`userId` text NOT NULL,
FOREIGN KEY (`userId`) REFERENCES `user`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `bookmarks` (
`id` text PRIMARY KEY NOT NULL,
`createdAt` integer NOT NULL,
`archived` integer DEFAULT false NOT NULL,
`favourited` integer DEFAULT false NOT NULL,
`userId` text NOT NULL,
FOREIGN KEY (`userId`) REFERENCES `user`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `session` (
`sessionToken` text PRIMARY KEY NOT NULL,
`userId` text NOT NULL,
`expires` integer NOT NULL,
FOREIGN KEY (`userId`) REFERENCES `user`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `tagsOnBookmarks` (
`bookmarkId` text NOT NULL,
`tagId` text NOT NULL,
`attachedAt` text,
`attachedBy` text,
PRIMARY KEY(`bookmarkId`, `tagId`),
FOREIGN KEY (`bookmarkId`) REFERENCES `bookmarks`(`id`) ON UPDATE no action ON DELETE cascade,
FOREIGN KEY (`tagId`) REFERENCES `bookmarkTags`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `user` (
`id` text PRIMARY KEY NOT NULL,
`name` text NOT NULL,
`email` text NOT NULL,
`emailVerified` integer,
`image` text,
`password` text
);
--> statement-breakpoint
CREATE TABLE `verificationToken` (
`identifier` text NOT NULL,
`token` text NOT NULL,
`expires` integer NOT NULL,
PRIMARY KEY(`identifier`, `token`)
);
--> statement-breakpoint
CREATE UNIQUE INDEX `apiKey_name_unique` ON `apiKey` (`name`);--> statement-breakpoint
CREATE UNIQUE INDEX `apiKey_keyId_unique` ON `apiKey` (`keyId`);--> statement-breakpoint
CREATE UNIQUE INDEX `apiKey_name_userId_unique` ON `apiKey` (`name`,`userId`);--> statement-breakpoint
CREATE UNIQUE INDEX `bookmarkTags_userId_name_unique` ON `bookmarkTags` (`userId`,`name`);--> statement-breakpoint
CREATE UNIQUE INDEX `user_email_unique` ON `user` (`email`);
Loading

0 comments on commit e234d35

Please sign in to comment.