olup
olup
DTDrizzle Team
Created by olup on 11/8/2023 in #help
mapWith, any special things to make it work ?
thnaks for the great work, and the support
47 replies
DTDrizzle Team
Created by olup on 11/8/2023 in #help
mapWith, any special things to make it work ?
i'll wait for the update from to be implemented 😄
47 replies
DTDrizzle Team
Created by olup on 11/8/2023 in #help
mapWith, any special things to make it work ?
I was hoping I could access the mapping function from the model object but ok
47 replies
DTDrizzle Team
Created by olup on 11/8/2023 in #help
mapWith, any special things to make it work ?
so no way to map the returning statement to js object ?
47 replies
DTDrizzle Team
Created by olup on 11/8/2023 in #help
mapWith, any special things to make it work ?
a ok - so for this query I won't be able to use the query builder right ?
47 replies
DTDrizzle Team
Created by olup on 11/8/2023 in #help
mapWith, any special things to make it work ?
yes
47 replies
DTDrizzle Team
Created by olup on 11/8/2023 in #help
mapWith, any special things to make it work ?
the doc does not mention that, hence my confusion
47 replies
DTDrizzle Team
Created by olup on 11/8/2023 in #help
mapWith, any special things to make it work ?
any other trick than mapWith then ?
47 replies
DTDrizzle Team
Created by olup on 11/8/2023 in #help
mapWith, any special things to make it work ?
I take this as a compliment 😄
47 replies
DTDrizzle Team
Created by olup on 11/8/2023 in #help
mapWith, any special things to make it work ?
so if I retrun 5 cols, I can chain the mapWith calls, one for each col ?
47 replies
DTDrizzle Team
Created by olup on 11/8/2023 in #help
mapWith, any special things to make it work ?
you mean call mapWith repeatedly ?
47 replies
DTDrizzle Team
Created by olup on 11/8/2023 in #help
mapWith, any special things to make it work ?
// generate the derived table
const derivedTableLines: SQL[] = [];
for (const usage of usages) {
derivedTableLines.push(
sql`( ${usage.customerId}::uuid, ${usage.dataConsumed}::int, ${usage.zoneExternalReference} )`,
sql`,`,
);
}

derivedTableLines.pop(); // remove the last comma

// build the query
const dataConsumedColumn = sql.raw(CustomerPlanModel.dataConsumed.name);
const usageStartedAt = sql.raw(CustomerPlanModel.usageStartedAt.name);
const expirationDateColumn = sql.raw(CustomerPlanModel.expirationDate.name);
const usageLastUpdatedAtColumn = sql.raw(
CustomerPlanModel.usageLastUpdatedAt.name,
);
const query = sql.join([
sql`
UPDATE ${CustomerPlanModel}
SET
${dataConsumedColumn} = ${CustomerPlanModel.dataConsumed} + u.dataConsumed,
${usageStartedAt} = COALESCE(${CustomerPlanModel.usageStartedAt}, NOW()),
${expirationDateColumn} = COALESCE(${CustomerPlanModel.expirationDate}, NOW() + INTERVAL '1 DAY' * ${CustomerPlanModel.initialDuration}),
${usageLastUpdatedAtColumn} = NOW()

--- create a derived table with the values to update
FROM
(VALUES `,
...derivedTableLines,
sql`
) u(customerId, dataConsumed, zoneExternalReference)

WHERE ${CustomerPlanModel.customerId} = u.customerId
AND ${CustomerPlanModel.zoneExternalReference} = u.zoneExternalReference

RETURNING *;
`,
]);

// execute the query
const updatedPlans = await this.db
.execute<CustomerPlanModel>(query)
.catch((e) => {
const pgDialect = new PgDialect();
Logger.error('handleUsageEvent: error running the query', {
query: pgDialect.sqlToQuery(query),
});
throw e;
});
// generate the derived table
const derivedTableLines: SQL[] = [];
for (const usage of usages) {
derivedTableLines.push(
sql`( ${usage.customerId}::uuid, ${usage.dataConsumed}::int, ${usage.zoneExternalReference} )`,
sql`,`,
);
}

derivedTableLines.pop(); // remove the last comma

// build the query
const dataConsumedColumn = sql.raw(CustomerPlanModel.dataConsumed.name);
const usageStartedAt = sql.raw(CustomerPlanModel.usageStartedAt.name);
const expirationDateColumn = sql.raw(CustomerPlanModel.expirationDate.name);
const usageLastUpdatedAtColumn = sql.raw(
CustomerPlanModel.usageLastUpdatedAt.name,
);
const query = sql.join([
sql`
UPDATE ${CustomerPlanModel}
SET
${dataConsumedColumn} = ${CustomerPlanModel.dataConsumed} + u.dataConsumed,
${usageStartedAt} = COALESCE(${CustomerPlanModel.usageStartedAt}, NOW()),
${expirationDateColumn} = COALESCE(${CustomerPlanModel.expirationDate}, NOW() + INTERVAL '1 DAY' * ${CustomerPlanModel.initialDuration}),
${usageLastUpdatedAtColumn} = NOW()

--- create a derived table with the values to update
FROM
(VALUES `,
...derivedTableLines,
sql`
) u(customerId, dataConsumed, zoneExternalReference)

WHERE ${CustomerPlanModel.customerId} = u.customerId
AND ${CustomerPlanModel.zoneExternalReference} = u.zoneExternalReference

RETURNING *;
`,
]);

// execute the query
const updatedPlans = await this.db
.execute<CustomerPlanModel>(query)
.catch((e) => {
const pgDialect = new PgDialect();
Logger.error('handleUsageEvent: error running the query', {
query: pgDialect.sqlToQuery(query),
});
throw e;
});
47 replies
DTDrizzle Team
Created by olup on 11/8/2023 in #help
mapWith, any special things to make it work ?
there is no way to leveragle drizzle internal mapping when returning a sql query that we know are of a certain type ?
47 replies
DTDrizzle Team
Created by olup on 11/8/2023 in #help
mapWith, any special things to make it work ?
ok
47 replies
DTDrizzle Team
Created by olup on 11/8/2023 in #help
mapWith, any special things to make it work ?
I gues smapWith is not used for that
47 replies
DTDrizzle Team
Created by olup on 11/8/2023 in #help
mapWith, any special things to make it work ?
this kinda have to be in sql, and we use the returning statement
47 replies
DTDrizzle Team
Created by olup on 11/8/2023 in #help
mapWith, any special things to make it work ?
ok. What about another use cas - we do a complex update with a temp table and want to return the updated lines
47 replies
DTDrizzle Team
Created by olup on 11/8/2023 in #help
mapWith, any special things to make it work ?
What i am looking to get is a regular customerPlan with camelCase names
47 replies
DTDrizzle Team
Created by olup on 11/8/2023 in #help
mapWith, any special things to make it work ?
export const CustomerPlanModel = pgTable(
'customer_plan',
{
id: uuid('id').primaryKey().defaultRandom(),
initialDuration: integer('initial_duration'),
usageStartedAt: timestamp('usage_started_at'),
...
export const CustomerPlanModel = pgTable(
'customer_plan',
{
id: uuid('id').primaryKey().defaultRandom(),
initialDuration: integer('initial_duration'),
usageStartedAt: timestamp('usage_started_at'),
...
47 replies