electric-sql / pglite

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

Top-level type parser #315

Open kyscott18 opened 2 months ago

kyscott18 commented 2 months ago

It would be great to be able to set a custom type parser for postgres data types at an instance level in addition to the query level. I'd be happy to work on this pr, but wanted to get some input on how it should look first.

API

import { PGlite, types } from '@electric-sql/pglite';

const pg = await PGlite.create({
 parsers: {
    [types.TEXT]: (value) => value.toUpperCase(),
  },
});

// or 

const pg = await PGlite.create();
pg.setParser(types.TEXT, (value) => value.toUpperCase());

It should probably be one or the other, which one do you think fits in best?

Relevant examples

node-postgres

var types = require('pg').types;
types.setTypeParser(20, function(val) {
  return parseInt(val, 10)
});

drizzle-orm

const users = pgTable('users', {
  // set custom column type with ".$type"
  id: serial('id').$type<UserId>().primaryKey(),
});