Skip to content

Commit fa25bac

Browse files
authored
DB initial setup (#1)
1 parent 83f86ce commit fa25bac

File tree

12 files changed

+1236
-9
lines changed

12 files changed

+1236
-9
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,6 @@ node_modules
2020
.idea
2121

2222
# cli
23-
dist
23+
dist
24+
25+
data/db

.husky/pre-commit

100644100755
File mode changed.

docker-compose.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: "3"
2+
services:
3+
db:
4+
image: postgres:16
5+
environment:
6+
POSTGRES_PASSWORD: mysecretpassword
7+
ports:
8+
- "5432:5432"
9+
volumes:
10+
- ./data/db:/var/lib/postgresql/data

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
"account:import": "yarn workspace @se-2/hardhat account:import",
1414
"chain": "yarn hardhat:chain",
1515
"compile": "yarn hardhat:compile",
16+
"db:seed": "yarn workspace @se-2/nextjs db:seed",
1617
"deploy": "yarn hardhat:deploy",
18+
"drizzle-kit": "yarn workspace @se-2/nextjs drizzle-kit",
1719
"fork": "yarn hardhat:fork",
1820
"format": "yarn next:format && yarn hardhat:format",
1921
"generate": "yarn account:generate",

packages/nextjs/.env.development

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
POSTGRES_URL="postgresql://postgres:mysecretpassword@localhost:5432/postgres"

packages/nextjs/drizzle.config.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as dotenv from "dotenv";
2+
import { defineConfig } from "drizzle-kit";
3+
4+
dotenv.config({ path: ".env.development" });
5+
6+
export default defineConfig({
7+
schema: "./services/database/config/schema.ts",
8+
out: "./services/database/migrations",
9+
dialect: "postgresql",
10+
dbCredentials: {
11+
url: process.env.POSTGRES_URL as string,
12+
},
13+
});

packages/nextjs/package.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
"scripts": {
66
"build": "next build",
77
"check-types": "tsc --noEmit --incremental",
8+
"db:seed": "tsx services/database/seed.ts",
89
"dev": "next dev",
10+
"drizzle-kit": "drizzle-kit",
911
"format": "prettier --write . '!(node_modules|.next|contracts)/**/*'",
1012
"lint": "next lint",
1113
"serve": "next start",
@@ -20,12 +22,16 @@
2022
"@tanstack/react-query": "~5.59.15",
2123
"@uniswap/sdk-core": "~5.8.2",
2224
"@uniswap/v2-sdk": "~4.6.1",
25+
"@vercel/postgres": "^0.10.0",
2326
"blo": "~1.2.0",
2427
"burner-connector": "0.0.9",
2528
"daisyui": "4.12.10",
29+
"dotenv": "^16.4.7",
30+
"drizzle-orm": "^0.39.1",
2631
"next": "~14.2.11",
2732
"next-nprogress-bar": "~2.3.13",
2833
"next-themes": "~0.3.0",
34+
"pg": "^8.13.1",
2935
"qrcode.react": "~4.0.1",
3036
"react": "~18.3.1",
3137
"react-copy-to-clipboard": "~5.1.0",
@@ -39,17 +45,20 @@
3945
"devDependencies": {
4046
"@trivago/prettier-plugin-sort-imports": "~4.3.0",
4147
"@types/node": "~18.19.50",
48+
"@types/pg": "^8",
4249
"@types/react": "~18.3.5",
4350
"@typescript-eslint/eslint-plugin": "~5.40.0",
4451
"abitype": "1.0.6",
4552
"autoprefixer": "~10.4.20",
53+
"drizzle-kit": "^0.30.4",
4654
"eslint": "~8.57.1",
4755
"eslint-config-next": "~14.2.15",
4856
"eslint-config-prettier": "~8.10.0",
4957
"eslint-plugin-prettier": "~5.2.1",
5058
"postcss": "~8.4.45",
5159
"prettier": "~3.3.3",
5260
"tailwindcss": "~3.4.11",
61+
"tsx": "^4.19.2",
5362
"type-fest": "~4.26.1",
5463
"typescript": "<5.6.0",
5564
"vercel": "~39.1.3"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import * as schema from "./schema";
2+
import { sql } from "@vercel/postgres";
3+
import { drizzle } from "drizzle-orm/node-postgres";
4+
import { drizzle as drizzleVercel } from "drizzle-orm/vercel-postgres";
5+
import { Pool } from "pg";
6+
7+
let db: ReturnType<typeof drizzle<typeof schema>> | ReturnType<typeof drizzleVercel<typeof schema>>;
8+
9+
const VERCEL_DB_STRING = "verceldb";
10+
if (process.env.POSTGRES_URL?.includes(VERCEL_DB_STRING)) {
11+
db = drizzleVercel(sql, { schema });
12+
} else {
13+
const pool = new Pool({
14+
connectionString: process.env.POSTGRES_URL,
15+
});
16+
17+
db = drizzle(pool, { schema });
18+
19+
pool.on("error", err => {
20+
console.error("Unexpected error on idle client", err);
21+
process.exit(-1);
22+
});
23+
}
24+
25+
export { db };
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { sql } from "drizzle-orm";
2+
import { pgTable, timestamp, varchar } from "drizzle-orm/pg-core";
3+
4+
// TODO: Define the right schema.
5+
export const users = pgTable("users", {
6+
id: varchar("id", { length: 42 }).primaryKey(),
7+
creationTimestamp: timestamp("creation_timestamp").default(sql`now()`),
8+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { InferInsertModel } from "drizzle-orm";
2+
import { db } from "~~/services/database/config/postgresClient";
3+
import { users } from "~~/services/database/config/schema";
4+
5+
export type UserInsert = InferInsertModel<typeof users>;
6+
7+
export async function getAllUsers() {
8+
return await db.select().from(users);
9+
}
10+
export async function createUser(user: UserInsert) {
11+
return await db.insert(users).values(user);
12+
}

0 commit comments

Comments
 (0)