porsager / postgres

Postgres.js - The Fastest full featured PostgreSQL client for Node.js, Deno, Bun and CloudFlare
The Unlicense
7.38k stars 267 forks source link

convert a specific column when reading or writing to the database #825

Open janiu-001 opened 6 months ago

janiu-001 commented 6 months ago

Dear expert,

Do the postgres support the similar functionality like the typeorm transformer? We have a request to convert the column value before updating/querying

export interface ColumnOptions extends ColumnCommonOptions {
    /**
     * Specifies a value transformer that is to be used to (un)marshal
     * this column when reading or writing to the database.
     */
    transformer?: ValueTransformer | ValueTransformer[];
}

i noticed the postgres also support transformer, but seem to a global change for all the tables, and the to is commented

    /** Transforms incoming and outgoing row values */
    value?: ((value: any) => any) | {
      /** Transform function for values in result rows */
      from?: ((value: unknown, column: postgres.Column<string>) => any) | undefined;
      // to?: ((value: unknown) => any) | undefined; // unused
    } | undefined;
janiu-001 commented 6 months ago

Dear expert:

Any update?

porsager commented 6 months ago

You should have the info needed to make such functionality using transform, but that said, this is not an ORM, but a postgres driver embracing being as close to PostgreSQL as possible, so I'd recommend doing what you want to in a DB level trigger instead if possible?