Drizzle TeamDT
Drizzle Teamโ€ข2y ago
eCrow

Convenient Function to map SQL Column keys to JS Column keys

I have a magic sql query that returns an entire row after an update. I need to remap the column keys from the SQL Table to the keys I defined on the pgTable in JS since they aren't the same. Is there a convenient function to do that?

Minimal example:

The table definition.
export const myTable = pgTable('my_table', {
  idInJS: uuid('id_in_sql').primaryKey().defaultRandom(),
  nameInJS: varchar('name_in_sql'),
});


The update query (Drizzle ORM does not have the from clause for the update builder... See https://github.com/drizzle-team/drizzle-orm/issues/2304)
const chunks: SQL[] = [];
chunks.push(tx.update(myTable).set({ nameInJS: 'John Doe' }).getSQL());
chunks.push(sql`from ${anotherTable}`);
chunks.push(sql`where ${eq(myTable.idInJS, anotherTable.idInJS)}`);
chunks.push(sql`returning *`);
const updateSql = sql.join(chunks, sql.raw(' '));

const { rows } = await tx.execute(updateSql);
/**
 * rows = [ { id_in_sql: 0, name_in_sql: 'John Doe' } ]
 *
 * Need: [ { idInJS: 0, nameInJS: 'John Doe' } ]
 */


Is there a way to conveniently get the JS keys from executing magic sql?
Thank you.
GitHub
Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too ๐Ÿ˜…
  • Issues ยท drizzle-team/drizzle-orm
Was this page helpful?