Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SQLite support #58

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ POSTGRES_USER=postgres
POSTGRES_DB=open-health
UPSTAGE_API_KEY=up_
OPENAI_API_KEY=sk-
# Example SQLite database URL
DATABASE_URL=sqlite:./dev.db
13 changes: 11 additions & 2 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,20 @@ services:
ports:
- "5432:5432"

sqlite:
image: nouchka/sqlite3
restart: always
volumes:
- sqlite_data:/var/lib/sqlite3/data
environment:
SQLITE_DB: ${SQLITE_DB}

app:
build:
context: .
dockerfile: Dockerfile
args:
- DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@database:5432/${POSTGRES_DB}
- DATABASE_URL=${DATABASE_URL}
depends_on:
- database
volumes:
Expand All @@ -27,8 +35,9 @@ services:
- "3000:3000"
restart: always
environment:
DATABASE_URL: postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@database:5432/${POSTGRES_DB}
DATABASE_URL: ${DATABASE_URL}

volumes:
postgres_data:
sqlite_data:
app_data:
2 changes: 1 addition & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ generator client {
}

datasource db {
provider = "postgresql"
provider = env("DATABASE_PROVIDER")
url = env("DATABASE_URL")
}

Expand Down
30 changes: 21 additions & 9 deletions src/lib/prisma.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
import {PrismaClient} from "@prisma/client";
import { PrismaClient } from "@prisma/client";

export function createPrismaClient() {
return new PrismaClient({
log: ['warn', 'error']
});
const databaseProvider = process.env.DATABASE_PROVIDER || "postgresql";
const databaseUrl = process.env.DATABASE_URL;

if (!databaseUrl) {
throw new Error("DATABASE_URL environment variable is not set");
}

return new PrismaClient({
log: ["warn", "error"],
datasources: {
db: {
url: databaseUrl,
},
},
});
}

declare const globalThis: {
prismaGlobal: ReturnType<typeof createPrismaClient>;
prismaGlobal: ReturnType<typeof createPrismaClient>;
} & typeof global;

const prisma = globalThis.prismaGlobal ?? createPrismaClient()
const prisma = globalThis.prismaGlobal ?? createPrismaClient();

export default prisma
export {Prisma} from "@prisma/client";
export default prisma;
export { Prisma } from "@prisma/client";

if (process.env.NODE_ENV !== 'production') globalThis.prismaGlobal = prisma
if (process.env.NODE_ENV !== "production") globalThis.prismaGlobal = prisma;