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

Migrate squel to kysely #922

Open
wants to merge 15 commits into
base: main
Choose a base branch
from

Conversation

ygarg465
Copy link

@ygarg465 ygarg465 commented Feb 6, 2024

This PR is just a PoC of migrating squel to kysely for now.

Log.debug('Reading from unsynced table ' + this._tableName)

const query = db.selectFrom(this._tableName)
const addFieldSelectionP = this.addFieldSelection.bind(this, i, selectWhereFields ? identificationFields : [])
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: could you add back the comment right before this line to explain what it does:

// only select the fields provided in `i.select` and the ones in `i.where`

return query.select(fields)
}

castBigIntToText(field: string) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Here also, put back the comment that clarifies why this is needed:

/**
   * Casts a field to TEXT if it is of type BigInt
   * because not all adapters deal well with BigInts
   * (e.g. better-sqlite3 requires BigInt support to be enabled
   *       but then all integers are returned as BigInt...)
   * The DAL will convert the string into a BigInt in the `fromSqlite` function from `../conversions/sqlite.ts`.
   */

const db = new Kysely<any>({
dialect: {
createAdapter() {
return new PostgresAdapter();
Copy link
Contributor

Choose a reason for hiding this comment

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

We should use SQLite adapter, introspector and compiler.
The squel version used PG because of a feature of squel that only worked when using the Postgres flavour.
But for Kysely this should not be needed.

@ygarg465
Copy link
Author

Tests are updated a little bit to match syntax generated by kysely.

  1. SQL keywords are in lower case
  2. table and column names are in double quotes
  3. parameters are tested separately instead of inline

@ygarg465
Copy link
Author

ygarg465 commented Feb 22, 2024

A problem I am currently facing to complete this is migrating the method makeBooleanFilter.

With sequel library we were just concatenating the statements but this does not work very easily in kysely.

The "OR" statements needs to be add by calling or method which does fit very well in the current style of code written here.

There is another way of using raw SQL which does support parameters automatically however I am unable to implement that.

@kevin-dp
Copy link
Contributor

Hi @ygarg465, what exactly is the problem you're facing with makeBooleanFilter? Looking at the code you do have an implementation for makeBooleanFilter. Is that code working? Or is that code copied from how it was done in squeel and not working with Kysely?

@ygarg465
Copy link
Author

Hey @kevin-dp, yes that is the code copied from squel implementation and is not working with Kysely.

The problem is that all the or statements need to be added by calling the or method of the Kysely's respective object which does not fill well in the current implementation style of the code.

And there is another way to achieve this which is raw SQL, that is I am unable to understand and implement.

@kevin-dp
Copy link
Contributor

@ygarg465 Do you understand the changes that are needed to make it work with or? I'm fine with changing to a better approach to build these queries (using Kysely). I would avoid dropping down to raw SQL as i don't think that will mix well with the rest of the builder that uses Kysely. Unfortunately, I don't have the capacity to help on the implementation of the builder right now.

@ygarg465
Copy link
Author

@kevin-dp I have to look at the implementation again for or as it's been some time since I have worked with the library.
I will get back to you with a implementation for the same that you can review

@ygarg465 ygarg465 marked this pull request as ready for review March 22, 2024 04:06
# Conflicts:
#	clients/typescript/src/client/execution/db.ts
#	clients/typescript/src/client/execution/transactionalDB.ts
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.

None yet

2 participants