Skip to content

Conversation

@jaskfla
Copy link
Contributor

@jaskfla jaskfla commented Oct 16, 2025

We often try a find(), occasionally a count(), and check whether it’s null or 0, respectively just to check existence of a record

> db = new TupaiaDatabase()

> await db.exists('user_account', { first_name: { comparator: 'ilike', comparisonValue: 'jasper' } })
// knex:query SELECT EXISTS((select * from "user_account" where "first_name" ilike $1)); undefined +37s
true

> await db.exists('user_account', { first_name: { comparator: 'ilike', comparisonValue: 'asdf' } })
// knex:query SELECT EXISTS((select * from "user_account" where "first_name" ilike $1)); undefined +4s
false

> await db.exists('project', { code: 'explore' })
// knex:query SELECT EXISTS((select * from "project" where "code" = $1)); undefined +4s
true

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @jaskfla, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a new exists method across the database and database model classes. This change provides a more explicit, readable, and potentially more performant way to determine if a record or set of records exists in the database, streamlining common patterns that previously involved find() and checking for null or count() and checking for 0.

Highlights

  • New exists method for database models: Introduced an async exists method to DatabaseModel that delegates the call to the underlying database instance, providing a cleaner API for checking record existence.
  • Database-level exists implementation: Implemented the async exists method in TupaiaDatabase to efficiently check for record existence using a SELECT EXISTS(?) SQL query, which is generally more performant than COUNT(*) when only existence is needed.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces an exists method to both the Database and DatabaseModel classes, providing a more readable and efficient way to check for the existence of a record. The implementation involves using SELECT EXISTS(?) in the database layer and exposing this functionality through the model. This change aims to simplify code and improve readability when checking for record existence.

Copy link
Contributor

@chris-bes chris-bes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just 1 suggestion

/**
* @returns {Promise<boolean>}
*/
async exists(...args) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we could move this to the BaseDatabase?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

100% agree! But it doesn't exist on the base branch (this PR is off dev)

Unless you’re itching to use this (I have been), feel free to leave this PR for my return and I will integrate it into BaseDatabase and resolve merge conflicts

const innerQuery = this.find(...args);
const [{ exists }] = await this.executeSql('SELECT EXISTS(?);', [innerQuery]);
return exists;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Race condition: Unready DB breaks subquery.

The exists method doesn't await this.find() before passing it to executeSql. When the database connection isn't ready, find returns a Promise-wrapped query via queryWhenConnected, not a Knex query builder. Passing this Promise to connection.raw() as a binding will fail because Knex expects a query builder object for subquery bindings, creating a race condition on startup.

Fix in Cursor Fix in Web

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants