electric-sql / pglite

Lightweight WASM Postgres with real-time, reactive bindings.
https://pglite.dev
Apache License 2.0
9.45k stars 204 forks source link

feat: add custom parser and serializer to PGlite options #397

Closed 0xOlias closed 1 week ago

0xOlias commented 1 month ago

This PR adds support for custom serializers to QueryOptions, and adds support for custom parsers and serializers to PGliteOptions. The change is non-breaking.

Our use case is using NUMERIC columns to store integers too large for BIGINT. We'd like these values to use the JS native BigInt rather than String, and it's easiest if this happens at the driver layer.

import { NUMERIC, PGlite, type PGliteOptions } from "@electric-sql/pglite";

export function createPglite(options: PGliteOptions) {
  return new PGlite({
    serializers: { [NUMERIC]: (x: string | number | bigint) => x.toString() },
    parsers: { [NUMERIC]: (x: string) => BigInt(x) },
    ...options,
  });
}

Verified locally in an integration test with our framework.

0xOlias commented 3 weeks ago

@samwillis waiting on review. Please let me know if I can make this easier to review in any way, or if you have a different direction in mind than the one proposed here.

For what it's worth, our use case would be unblocked just by adding serializers to QueryOptions - don't strictly need the instance-wide parsers/serializers.

samwillis commented 3 weeks ago

Hey @0xOlias, apologies for the delay. I'll aim to take a proper look in the next couple of days. Massively appreciate you picking this up!

0xOlias commented 2 weeks ago

As a heads up, we unblocked ourselves by moving the parse/serialize logic to the query builder/ORM layer. Feel free to close, though I suspect others will ultimately want this.