Skip to content

Commit

Permalink
Setup Postgres with Drizzle ORM
Browse files Browse the repository at this point in the history
  • Loading branch information
ruchernchong committed Dec 23, 2024
1 parent d5e961e commit a28bb27
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CLOUDFLARE_API_TOKEN=
MONGODB_URI=
DATABASE_URL=
SG_CARS_TRENDS_API_TOKEN=

UPSTASH_REDIS_REST_URL=
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@neondatabase/serverless": "^0.10.4",
"@upstash/ratelimit": "^2.0.3",
"@upstash/redis": "^1.34.0",
"date-fns": "^2.30.0",
"drizzle-orm": "^0.38.2",
"hono": "^4.6.5",
"mongodb": "6.8.0",
"sst": "3.3.59"
Expand Down
186 changes: 186 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 4 additions & 17 deletions src/config/db.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
import * as mongodb from "mongodb";
import { neon } from "@neondatabase/serverless";
import { drizzle } from "drizzle-orm/neon-http";
import { Resource } from "sst";

const MongoClient = mongodb.MongoClient;

let cachedDb: mongodb.Db | null = null;

const connectToDatabase = async (): Promise<mongodb.Db> => {
if (cachedDb) {
return cachedDb;
}

const client = await MongoClient.connect(Resource.MONGODB_URI.value);
cachedDb = client.db("main");

return cachedDb;
};

const db = await connectToDatabase();
const sql = neon(Resource.DATABASE_URL.value);
const db = drizzle(sql);

export default db;
7 changes: 5 additions & 2 deletions sst.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ export default $config({
};
},
async run() {
const MONGODB_URI = new sst.Secret("MONGODB_URI", process.env.MONGODB_URI);
const DATABASE_URL = new sst.Secret(
"DATABASE_URL",
process.env.DATABASE_URL,
);
const SG_CARS_TRENDS_API_TOKEN = new sst.Secret(
"SG_CARS_TRENDS_API_TOKEN",
process.env.SG_CARS_TRENDS_API_TOKEN,
Expand All @@ -60,7 +63,7 @@ export default $config({

const { url } = new sst.aws.Function("Hono", {
link: [
MONGODB_URI,
DATABASE_URL,
SG_CARS_TRENDS_API_TOKEN,
UPSTASH_REDIS_REST_TOKEN,
UPSTASH_REDIS_REST_URL,
Expand Down

0 comments on commit a28bb27

Please sign in to comment.