Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]: calling $count on a replica using withReplicas throws a TypeError #4039

Open
1 task done
AndrewJo opened this issue Jan 30, 2025 · 0 comments
Open
1 task done
Labels
bug Something isn't working

Comments

@AndrewJo
Copy link
Contributor

Report hasn't been filed before.

  • I have verified that the bug I'm about to report hasn't been filed before.

What version of drizzle-orm are you using?

0.39.1

What version of drizzle-kit are you using?

0.30.4

Other packages

No response

Describe the Bug

When resolving $count on a client with replicas, it results in a TypeError with message: Cannot read properties of undefined (reading 'session')"

const db = withReplicas(primaryDb, [replicaDb]);
// This line throws TypeError
const postCount = await db.$count(posts);

The stack traces back to this line where this is undefined because the this context is likely not bound properly:

return new PgCountBuilder({ source, filters, session: this.session });

I'm wondering if this is because calling withReplica returns a plain object without the associated session object.

return {
...primary,
update,
insert,
delete: $delete,
execute,
transaction,
refreshMaterializedView,
$primary: primary,
select,
selectDistinct,
selectDistinctOn,
$count,
$with,
with: _with,
get query() {
return getReplica(replicas).query;
},
};

Or whether calling getReplicas.$count(...args) is not binding the underlying replica PgDatabase instance to this.

const $count: Q['$count'] = (...args: [any]) => getReplica(replicas).$count(...args);

If this is indeed the issue, the bug most likely exists for all dialects and not just Postgres.

I'm beginning to wonder if withReplica needs to return a class that extends PgDatabase (or equivalent in each dialect) that contains $primary property among others.

This is something that didn't grab my attention when I submitted #3952.

@AndriiSherman, @Sukairo-02: I'll be happy to submit a PR based on the outcomes of the discussion here.

@AndrewJo AndrewJo added the bug Something isn't working label Jan 30, 2025
@AndrewJo AndrewJo changed the title [BUG]: calling $count on a replica using withReplicas results in TypeError [BUG]: calling $count on a replica using withReplicas throws a TypeError Jan 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant