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

feat(prisma): support pglite in developer environments #338

Closed
wants to merge 2 commits into from

Conversation

LoneRifle
Copy link
Contributor

Problem

In some local dev environments, we may not be able to connect to a postgres instance, for the following non-exhaustive reasons:

  • The machine is disallowed from connecting to postgres servers
  • The machine cannot run docker, and there is no practical way to procure a standalone postgres server
  • The environment is actually StackBlitz and is run completely in-browser

Solution

In such instances, we will allow for use of pglite, if NODE_ENV is development or test, and there is no DATABASE_URL specified. If both conditions hold, we inject a placeholder database url to placate prisma, and set up a PGLite adapter.

Testing evidence

 ✓ Starting...
 ✓ Ready in 1566ms
 ○ Compiling /sign-in ...
 ✓ Compiled /sign-in in 3.9s (2725 modules)
 GET /sign-in?callbackUrl=%2Fsign-in 200 in 4787ms
 ✓ Compiled /api/trpc/[trpc] in 255ms (300 modules)
{"level":40,"time":1728010001039,"pid":53386,"hostname":"localhost","msg":"DATABASE_URL not set, using pglite"}

@LoneRifle LoneRifle requested a review from karrui October 8, 2024 04:12
Copy link

vercel bot commented Oct 8, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
starter-kit ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 8, 2024 7:10am

@datadog-opengovsg
Copy link

datadog-opengovsg bot commented Oct 8, 2024

Datadog Report

Branch report: feat/prisma/pglite
Commit report: 45f5f94
Test service: starter-kit

✅ 0 Failed, 13 Passed, 0 Skipped, 10.03s Total Time

Copy link

@qin-guan qin-guan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keeping sane inside Pulau Tekong

src/env.mjs Show resolved Hide resolved
src/env.mjs Outdated Show resolved Hide resolved
src/server/prisma/index.ts Show resolved Hide resolved
src/server/prisma/index.ts Show resolved Hide resolved
Make pglite part of the core codebase, so that we can use it in
environments where postgres may not be available, such as in
browser-only environments like StackBlitz.

- Rename prisma -> prisma/index to host submodules
- Move pglite-related logic to pglite
  - Wrap actual init of PGlite as factory, and not
    within module load
- Rework vitest.setup to depend on new submodule
  - Keep resetDb, since that is only useful to testing
If `NODE_ENV` is set to `development`, but no `DATABASE_URL` is given,
we are likely in an environment which has no database, so fallback on
using pglite.
Copy link
Collaborator

@karrui karrui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if starter kit should be using pglite for development (and couple more code to the node environment). Does not seem like a useful thing for OGP folks. We want to stop tailoring this kit for external use at the expense of internal use!

@LoneRifle
Copy link
Contributor Author

LoneRifle commented Oct 9, 2024

I don't know if starter kit should be using pglite for development (and couple more code to the node environment). Does not seem like a useful thing for OGP folks. We want to stop tailoring this kit for external use at the expense of internal use!

@karrui but is it really at the expense of internal use?

Granted this PR (and a few more I have planned) would add a few more moving parts, mostly properties or logic driven by NODE_ENV and DATABASE_URL, but it could unlock quite a fair bit of opportunity. Consider eg being able to try out an open-sourced @opengovsg product based on Starter Kit within a locked down intranet environment, or a non-technical person being able to see what the product can do for their organisation by demoing it completely out of their browser

@karrui
Copy link
Collaborator

karrui commented Oct 9, 2024

I think the important thing for this starter kit is for it to be tight. Users already have to delete so many things to get it to a clean slate; and I want to reduce unnecessary code in the code base.

I'd rather you update the docs to add this as a possibility if one does not have the propensity to spin up a database for use.

However, I don't want to recommend people to use pglite in development either; you can't really connect to the db and explore the db as one would a real database.

@LoneRifle
Copy link
Contributor Author

I think the important thing for this starter kit is for it to be tight. Users already have to delete so many things to get it to a clean slate; and I want to reduce unnecessary code in the code base.

I'd rather you update the docs to add this as a possibility if one does not have the propensity to spin up a database for use.

Okay, that's unfortunate but fair. I'll see how I can have this documented.

@LoneRifle LoneRifle closed this Oct 9, 2024
@LoneRifle LoneRifle deleted the feat/prisma/pglite branch October 9, 2024 08:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants