feat: bring-your-own-pg-client™ (browser support)#782
Conversation
|
This is great! 💯 We're also slowly moving all pg-meta functionality to the main Supabase repo, which is pure-JS and is simply a SQL builder - would that be a better place for this? There's a lot of work that still needs to be done for that though - feature parity, publishing the package somewhere, tests, the works. |
|
Or maybe we keep this feature here for now and switch to the pure-JS version once we reach parity? Wdyt? |
|
@soedirgo wow sorry for the super long delay (I somehow had this repo's notifications muted 😬). Good to know about the migration to My use case for this was in database.build in order to grab table schema from PGlite as JSON and feed into the schema visualizer. @SaxonF will be doing something similar (in-browser) with new AI onboarding screens we are working on. Do you think it should be as simple as migrating tables.sql to the new package? If so there's probably no need for this PR. Will the new package continue to be published as |
Adds a
PostgresMetaBaseclass 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
PostgresMetaclass now extendsPostgresMetaBaseand implementsquery()andend()usingpgexactly 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
PostgresMetaBaseclass which doesn't import thepgdependencyCreate a new entrypoint
base.tswith a package export at/base. This means you canimport { PostgresMetaBase } from '@supabase/postgres-meta/base'without importingpg.Importing
@supabase/postgres-metawill continue to usepgas before without breaking changes.Create custom loader for
.sqlfiles that live under./src/lib/sql. Previously these were loaded using Node'sfsAPI. 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(esbuildbundler under the hood). So nowtsup/esbuildrun the build process instead of puretsc. I did my best to make sure thedistoutputs were consistent with previoustscbuilds, 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
vitestplugin to load.sqlfiles, similar to what we do withtsup.