Skip to content

Commit

Permalink
Move to Turso
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKohler committed Mar 17, 2024
1 parent bb18336 commit 870f9f4
Show file tree
Hide file tree
Showing 6 changed files with 652 additions and 32 deletions.
4 changes: 2 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
DATABASE_URL="mysql://root:root@localhost:3306/dev"
SHADOW_DATABASE_URL="mysql://root@localhost:3309/shadowdb"
TURSO_DATABASE_URL=""
TURSO_AUTH_TOKEN=""
SESSION_SECRET="super-duper-s3cret"
MAPBOX_ACCESS_TOKEN="insert-me"
SMTP_HOST=""
Expand Down
18 changes: 16 additions & 2 deletions app/db.server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { PrismaClient } from "@prisma/client";
import { PrismaLibSQL } from "@prisma/adapter-libsql";
import { createClient } from "@libsql/client";

let prisma: PrismaClient;

Expand All @@ -11,10 +13,22 @@ declare global {
// create a new connection to the DB with every change either.
// in production we'll have a single connection to the DB.
if (process.env.NODE_ENV === "production") {
prisma = new PrismaClient();
const libsql = createClient({
url: `${process.env.TURSO_DATABASE_URL}`,
authToken: `${process.env.TURSO_AUTH_TOKEN}`,
});

const adapter = new PrismaLibSQL(libsql);
prisma = new PrismaClient({ adapter });
} else {
if (!global.__db__) {
global.__db__ = new PrismaClient();
const libsql = createClient({
url: `${process.env.TURSO_DATABASE_URL}`,
authToken: `${process.env.TURSO_AUTH_TOKEN}`,
});

const adapter = new PrismaLibSQL(libsql);
global.__db__ = new PrismaClient({ adapter });
}
prisma = global.__db__;
prisma.$connect();
Expand Down

0 comments on commit 870f9f4

Please sign in to comment.