postgres icon indicating copy to clipboard operation
postgres copied to clipboard

convert a specific column when reading or writing to the database

Open janiu-001 opened this issue 1 year ago • 2 comments

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 avatar Mar 13 '24 23:03 janiu-001

Dear expert:

Any update?

janiu-001 avatar Mar 21 '24 00:03 janiu-001

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?

porsager avatar Mar 21 '24 06:03 porsager