drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
21.52k stars 487 forks source link

[FEATURE]: transform/alias raw row properties #2392

Open sakulstra opened 1 month ago

sakulstra commented 1 month ago

Describe what you want

Hello, i'm currently using drizzle, but must rely on native postgres-js for features not currently supported by drizzle (e.g. triggers/function, publications, subscriptions).

While this works kinda fine, an issue i frequently face is the lack of transformation with this raw postgres usage.

Let assume i have a users table with: lastName: text(last_name) When fetching data via drizzle last_name would be aliased to lastName and i can access the property via value.lastName on the result.

When i do e.g. a subscription via raw postgres-js

await client.subscribe("insert:users", (row) => {
    console.log(row.last_name); // <-- unaliased
});

the row properties will be unaliased and all the helper functions i built around the drizzle types will break.

Does drizzle somehow expose this transform / alias? I couldn't find anything on the docs.

I imagine a schema.transformToAliased(rawValue) to map from unaliased to aliased object properties would be quite helpful to workaround shortcomings on more complex db functionality not currently supported by drizzle.

HarrisonJackson commented 1 week ago

This happens using supabase frequently.

I want to use supabase realtime but the events from it send back the raw rows. It would be great if I could run them through some helper function from drizzle so the typings match.