feat: bring-your-own-pg-client™ (browser support) #782
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Adds a
PostgresMetaBase
class that allows you to bring-your-own-pg-client™. Instead of depending onpg
, the base class accepts customquery()
andend()
functions allowing you to choose how these methods are implemented.The existing
PostgresMeta
class now extendsPostgresMetaBase
and implementsquery()
andend()
usingpg
exactly as before without breaking changes.Why?
Browser support 🤓 ElectricSQL's pglite gives us a working Postgres instance in the browser (via wasm). Offering a platform-agnostic version of this lib means we can use it in any environment including the browser.
Example using PGlite
Other important notes
In order to make this platform-agnostic, all code needed to be pure TS (no native deps). We do 3 things:
Introduce
PostgresMetaBase
class which doesn't import thepg
dependencyCreate a new entrypoint
base.ts
with a package export at/base
. This means you canimport { PostgresMetaBase } from '@supabase/postgres-meta/base'
without importingpg
.Importing
@supabase/postgres-meta
will continue to usepg
as before without breaking changes.Create custom loader for
.sql
files that live under./src/lib/sql
. Previously these were loaded using Node'sfs
API. Now they are imported directly likeimport tablesSql from './sql/tables.sql'
.This custom import is accomplished by adding a lightweight bundler to the build step:
tsup
(esbuild
bundler under the hood). So nowtsup
/esbuild
run the build process instead of puretsc
. I did my best to make sure thedist
outputs were consistent with previoustsc
builds, but worth double checking (there are some differences, like bundling into single files). Also outputs both ESM and CJS outputs which should provide more flexibility to consumers.All tests continue to pass. Only change needed was adding a custom
vitest
plugin to load.sql
files, similar to what we do withtsup
.