magic sql column naming

Ok, here's a doozy: One of the things i like best about drizzle vs prisma is the fact that we can keep the standard naming conventions at the database level and still get the objects with cool camelCase properties. For example:
export const Purchases = pgTable('purchases', {
id: bigserial('id', { mode: 'number' }).primaryKey(),
websiteId: bigint('website_id', { mode: 'number' }),
experimentId: bigint('experiment_id', { mode: 'number' }),
variantId: bigint('variant_id', { mode: 'number' }),
from: bigint('from', { mode: 'number' }),
currencyCode: varchar('currency_code', { length: 4 }),
purchases: integer('purchases'),
revenue: bigint('revenue', { mode: 'number' }),
});
export const Purchases = pgTable('purchases', {
id: bigserial('id', { mode: 'number' }).primaryKey(),
websiteId: bigint('website_id', { mode: 'number' }),
experimentId: bigint('experiment_id', { mode: 'number' }),
variantId: bigint('variant_id', { mode: 'number' }),
from: bigint('from', { mode: 'number' }),
currencyCode: varchar('currency_code', { length: 4 }),
purchases: integer('purchases'),
revenue: bigint('revenue', { mode: 'number' }),
});
Yesterday, i was working on a custom query, so i decided to use the magical sql operator, provided by drizzle ORM.
sql`
SELECT
${Purchases.variantId},
${Purchases.currencyCode},
${Purchases.from},
SUM(${Purchases.revenue}) AS revenue
FROM
${Purchases}
WHERE
${Purchases.variantId} IN (
Select ${Variants.id} From ${Variants} WHERE ${Variants.experimentId} = ${experimentId}
)
AND ${Purchases.currencyCode} = ${currency}
GROUP BY
${Purchases.variantId},
${Purchases.currencyCode},
${Purchases.from}
ORDER BY
${Purchases.from} ASC;
`;
sql`
SELECT
${Purchases.variantId},
${Purchases.currencyCode},
${Purchases.from},
SUM(${Purchases.revenue}) AS revenue
FROM
${Purchases}
WHERE
${Purchases.variantId} IN (
Select ${Variants.id} From ${Variants} WHERE ${Variants.experimentId} = ${experimentId}
)
AND ${Purchases.currencyCode} = ${currency}
GROUP BY
${Purchases.variantId},
${Purchases.currencyCode},
${Purchases.from}
ORDER BY
${Purchases.from} ASC;
`;
The problem here is that the resulting objects i get come with the database column naming: variant_id, currency_code ... Even if i try using AS, the return doesn't keep the capitalize characters in the column name
SELECT
${Purchases.variantId} as variantId,
${Purchases.currencyCode} as currencyCode,
SELECT
${Purchases.variantId} as variantId,
${Purchases.currencyCode} as currencyCode,
still gives me back:
{
variantid,
currencycode
}
{
variantid,
currencycode
}
My questions are: - Is there a way to make this work as expected? - if not, why should i use the drizzle magic sql operator, instead of just a normal call through the postgres driver, which would allow me to just write pure SQL instead of having to use: ${Purchases.variantId} ? TY
1 Reply
predaytor
predaytor7mo ago
same here
Want results from more Discord servers?
Add your server