Skip to content

Add @effect/sql-pglite package#4692

Closed
evelant wants to merge 9 commits into
Effect-TS:next-minorfrom
evelant:feat/sql-pglite
Closed

Add @effect/sql-pglite package#4692
evelant wants to merge 9 commits into
Effect-TS:next-minorfrom
evelant:feat/sql-pglite

Conversation

@evelant
Copy link
Copy Markdown
Contributor

@evelant evelant commented Apr 3, 2025

Type

  • Refactor
  • Feature
  • Bug Fix
  • Optimization
  • Documentation Update

Description

Add @effect/sql-pglite package. An @effect/sql driver for PGlite, single user postgresql in the browser or node.

Related

  • Related Issue #
  • Closes #

@github-project-automation github-project-automation Bot moved this to Discussion Ongoing in PR Backlog Apr 3, 2025
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Apr 3, 2025

🦋 Changeset detected

Latest commit: cb5dfd6

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@effect/sql-pglite Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@effect-bot effect-bot force-pushed the next-minor branch 5 times, most recently from 02d328c to 1ea11b9 Compare April 4, 2025 15:48
@evelant
Copy link
Copy Markdown
Contributor Author

evelant commented Apr 4, 2025

Not sure why the lint and docgen failed, they pass on my branch locally.

@effect-bot effect-bot force-pushed the next-minor branch 19 times, most recently from 4cba33b to 5c9e640 Compare April 14, 2025 00:33
@effect-bot effect-bot force-pushed the next-minor branch 2 times, most recently from f265f41 to 0fd314b Compare April 14, 2025 01:59
@evelant
Copy link
Copy Markdown
Contributor Author

evelant commented May 1, 2025

Thanks for the feedback! I've had to put down my side project using pglite for now but as soon as I can find time I'll update the PR. As for the naming it's extra confusing because PGlite uses PGlite not Pglite or PgLite. I think my fingers just typed PgLite out of habit haha. I think you're right, probably best to change it to Pglite to match Sqlite. Ah I see how the formatting got a little off too -- prettier plugin in vscode auto-formatting on save.

@evelant
Copy link
Copy Markdown
Contributor Author

evelant commented Jun 3, 2025

I finally got around to cleaning this up. Sorry for the long delay!

const pgDumpMigrations = runPgDump(["--column-inserts", "--data-only", `--table=${table}`])

const pgDumpAll = Effect.map(
Effect.all([pgDumpSchema, pgDumpMigrations], { concurrency: 2 }),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Will setting concurrency: 2 here have any effect? Isn't dumping the database a blocking operation?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The PgliteMigrator is a direct copy of the PgMigrator just with commands switched out for Pglite instead of Pg. I'm not sure why the original PgMigrator does it this way TBH.

Comment thread packages/sql-pglite/src/PgLiteMigrator.ts Outdated
Comment thread packages/sql-pglite/src/PgLiteMigrator.ts Outdated
@evelant
Copy link
Copy Markdown
Contributor Author

evelant commented Jun 18, 2025

Any more changes needed or can this be merged now?

@nikelborm
Copy link
Copy Markdown
Contributor

I think you probably have to ask Tim for a review manually. Because on my PR, where I had a lot of changes to SQL packages, he was automatically attached as per the CODEOWNERS file, while it seems this automation doesn't work when you are adding a completely new SQL-related package. Maybe you have to even add a new row there

Comment thread packages/sql-pglite/src/PgLiteClient.ts Outdated
@nikelborm
Copy link
Copy Markdown
Contributor

Fixed failing CI in evelant/effect#3

image

@evelant
Copy link
Copy Markdown
Contributor Author

evelant commented Jul 22, 2025

@tim-smart Whenever you get a chance I'd appreciate your feedback on this so I can make any necessary changes, thanks!

Comment thread packages/sql-pglite/src/PgLiteMigrator.ts
Comment thread packages/sql-pglite/src/PgLiteClient.ts Outdated
executeValues(sql: string, params: ReadonlyArray<Primitive>) {
// PGlite doesn't have a values() method like postgres.js
// We'll just return the regular query results
return this.execute(sql, params, (r) => Object.values(r))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think you will need an .map here? The results will be an array?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Maybe I was confused about what executeValues should return. Is it supposed to be an array of arrays of values? That's what this should do since the function param is the row transform, it transforms each row object into an array of values.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

r will be an array of objects here, so you will need to map over each item.

Comment thread packages/sql-pglite/src/PgLiteClient.ts Outdated
) {
// PGlite doesn't have a cursor method like postgres.js
// We'll fetch all results at once and convert to a stream
return Stream.fromEffect(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You can use Stream.fromIterableEffect

Comment thread packages/sql-pglite/src/PgLiteClient.ts Outdated
constructor(private readonly pg: PGlite) {}

private run(query: Promise<any>) {
return Effect.async<ReadonlyArray<any>, SqlError>((resume) => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You can switch to tryPromise here

Comment thread packages/sql-pglite/src/PgLiteClient.ts Outdated
extensions: options.extensions ? (client as any) : ({} as any),
listen: (channel: string) =>
Stream.asyncPush<string, SqlError>((emit) =>
Effect.tryPromise({
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You would use Effect.acquireRelease here. I think this would immediately unsubscribe.

Comment thread packages/sql-pglite/src/PgLiteClient.ts Outdated
Effect.tryPromise({
try: () => client.query(`NOTIFY ${channel}, '${payload}'`),
catch: (cause) => new SqlError({ cause, message: "Failed to notify" })
}).pipe(Effect.map(() => void 0))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
}).pipe(Effect.map(() => void 0))
}).pipe(Effect.asVoid)

Comment on lines +16 to +18

test("should work", () => expect(true))
describe("PgliteClient", () => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
test("should work", () => expect(true))
describe("PgliteClient", () => {
describe("PgliteClient", () => {

@bwbuchanan
Copy link
Copy Markdown

This is especially needed now that @effect/sql-pg uses pg instead of postgres, and has lost support for the socket configuration option, which I was previously using to integrate with pglite for integration-testing database code.

@evelant
Copy link
Copy Markdown
Contributor Author

evelant commented Nov 4, 2025

Sorry I dropped the ball on finishing this PR. I'll pick it up again ASAP.

@thewilkybarkid
Copy link
Copy Markdown
Contributor

Anything we could help with? 🙂

mikearnaldi and others added 2 commits December 13, 2025 05:00
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Tim Smart <hello@timsmart.co>
@evelant
Copy link
Copy Markdown
Contributor Author

evelant commented Dec 17, 2025

Sorry for taking forever to get back to this. I've been extremely busy. I cleaned everything up, addressed the review comments, rebased on main, and updated everything to match latest changes in sql. @tim-smart if you would take another look I'd appreciate it!

This comment was marked as resolved.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

hmm, I'm not sure how that happened! Thanks for pointing it out, I'll try to clean it up, although I'm not sure of the best way to do so. Maybe I should copy the pglite changes to a new branch and open a new PR that's clean.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hmm yeah IDK what happened here. My history looks clean locally. The commits relating to pglite are clean. It seems that the most recent 5 commits from main are somehow included in the PR after I rebased. I'm not sure why.

Copy link
Copy Markdown
Contributor

@nikelborm nikelborm Dec 17, 2025

Choose a reason for hiding this comment

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

This is because your PR is opened against next-minor branch, and should have been rebased against it. git rebase -i interactive mode is very helpful in such situations, and you can easily remove a few commits.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ah. I rebased from main but there are commits there that aren't in next-minor yet so they got picked up. I'm not sure what the right thing is here. Should I make a new PR with the pglite stuff on top of next-minor instead of main?

Copy link
Copy Markdown
Contributor

@nikelborm nikelborm Dec 17, 2025

Choose a reason for hiding this comment

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

No need to make a new branch. Just make git rebase -i HEAD~12 and inside the interactive editor, delete rows with commits you don't want

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ah got it, fixed

@bwbuchanan
Copy link
Copy Markdown

Could this use PgJson instead of PgliteJson for the custom sql type, and align the type so that the argument is unknown instead of [unknown] to enable drop-in compatibility with @effect/sql-pg?

Also, it seems that PgArray was recently removed from @effect/sql-pg, so PgliteArray may now also be redundant?

@evelant
Copy link
Copy Markdown
Contributor Author

evelant commented Mar 19, 2026

I've somewhat put this on hold as I'm not sure if adding a pglite lib to core effect really makes sense. Pglite is a very useful idea but the progress of the project has been glacial and I'm not sure if it is stable and supported enough to warrant a package in effect.

@bwbuchanan
Copy link
Copy Markdown

pglite is essential to the test suite of a codebase I maintain, because it would drastically increase the test run time to spin up a "real" Postgres contanier.

I dropped your code from this PR directly into my repo, which made it possible to upgrade effect past the point where pg became the backend for @effect/sql-pg. Claude was then able to one-shot a port to effect v4 beta.

Unfortunately this is far enough outside of my usual responsibilities that I can't allocate any time to helping with this PR right now, but for anyone else in the same situation of needing to test their Postgres-dependent effect code, I would suggest pointing your coding agent at this PR and asking it to sort it for you.

@evelant
Copy link
Copy Markdown
Contributor Author

evelant commented Mar 20, 2026

Heh of course I made that comment yesterday only to find that electric-sql has just finished their big rework of PGlite. It may be a lot more stable/usable now. I'll try to bring this PR up to speed again soon.

@evelant
Copy link
Copy Markdown
Contributor Author

evelant commented May 15, 2026

It looks like someone took this and patched it up for v4 and it's now merged, thanks @blntrsz! Going to close this since v4 now has @effect/sql-pglite.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

9 participants