kysely icon indicating copy to clipboard operation
kysely copied to clipboard

Doing bulk queries with .raw() or something similar

Open ipb26 opened this issue 3 years ago • 0 comments

Right now I'm doing a couple of bulk queries (user input) using the sqlite client directly.

I'd like to incorporate this into kysely so i can hide my connection object entirely, and take advantage of kysely for locking/transactions. However I have 2 issues:

  • Kysely only executes the first query I pass in using .raw().execute(). Standard behavior for sqlite with the methods the driver calls on the sqlite object.
  • Even if this did work (I could modify the driver) the return types would not match. Sqlite kicks back an object that looks something like this for bulk queries (using exec()):

{ columns: string[], values: any[][] }[]

This obviously has 2 issues. 1 is that it may contain more than one result set, whereas kysely only expects one. The other is that if I run a query and need to know the column names, this will work, whereas kysely's standard type is an array of objects, so I won't know the column names if the query has no results.

Is this something you'd consider incorporating? Maybe a separate bulk() method, similar to the raw method but with a different response type. As far as I can tell it could be done, but would require modifying all of the drivers to either change the response type to { columns: string[], values: any[][] }[] and then have kysely convert it to object[] outside of the driver for regular queries, or adding a separate executeBulkQuery() method to each driver's DatabaseConnection. Not sure if it's worth it, but otherwise anyone who wants to do bulk queries has to move the connection/locking/transaction logic outside of the kysely driver.

I'm also not sure what format postgres and mysql give back by default so that may be an issue as well.

ipb26 avatar Dec 31 '21 09:12 ipb26