`sql` in D1 batch causes "TypeError: Cannot read properties of undefined (reading 'bind')"

Using sql in D1 batch causes "TypeError: Cannot read properties of undefined (reading 'bind')"
const upsertSql = sql`insert into ${schema.subscriptions}
(id, status, plan, cycle_started_at, customer_id, created_at, updated_at)
values (${subscriptionData.id}, ${subscriptionData.status}, ${plan}, ${cycleStartedAt.getTime()}, ${subscriptionData.customerId}, ${createdAt.getTime()}, ${updatedAt.getTime()})
on conflict (${schema.subscriptions.id}) do update set status = ${subscriptionData.status}, plan = ${plan}, cycle_started_at = ${cycleStartedAt.getTime()}, updated_at = ${updatedAt.getTime()}
where ${updatedAt.getTime()} > ${schema.subscriptions.updatedAt}
on conflict (${schema.subscriptions.customerId}) do update set id = ${subscriptionData.id}, status = ${subscriptionData.status}, plan = ${plan}, cycle_started_at = ${cycleStartedAt.getTime()}, created_at = ${createdAt.getTime()}, updated_at = ${updatedAt.getTime()}
where ${createdAt.getTime()} > ${schema.subscriptions.createdAt}
returning *`;

const [[batchSubscription]] = await db.batch([
db.all<schema.Subscription>(upsertSql),
]);
const upsertSql = sql`insert into ${schema.subscriptions}
(id, status, plan, cycle_started_at, customer_id, created_at, updated_at)
values (${subscriptionData.id}, ${subscriptionData.status}, ${plan}, ${cycleStartedAt.getTime()}, ${subscriptionData.customerId}, ${createdAt.getTime()}, ${updatedAt.getTime()})
on conflict (${schema.subscriptions.id}) do update set status = ${subscriptionData.status}, plan = ${plan}, cycle_started_at = ${cycleStartedAt.getTime()}, updated_at = ${updatedAt.getTime()}
where ${updatedAt.getTime()} > ${schema.subscriptions.updatedAt}
on conflict (${schema.subscriptions.customerId}) do update set id = ${subscriptionData.id}, status = ${subscriptionData.status}, plan = ${plan}, cycle_started_at = ${cycleStartedAt.getTime()}, created_at = ${createdAt.getTime()}, updated_at = ${updatedAt.getTime()}
where ${createdAt.getTime()} > ${schema.subscriptions.createdAt}
returning *`;

const [[batchSubscription]] = await db.batch([
db.all<schema.Subscription>(upsertSql),
]);
Running the sql outside of batch works fine.
const [subscription] = await db.all<schema.Subscription>(upsertSql);
const [subscription] = await db.all<schema.Subscription>(upsertSql);
Has anybody gotten sql to work inside of D1 batch?
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server