aidenwallis / kysely-d1

D1 dialect for Kysely
MIT License
206 stars 12 forks source link

Rest operator on bind will fail to execute on bigger tables #23

Open vikiival opened 11 months ago

vikiival commented 11 months ago

https://github.com/aidenwallis/kysely-d1/blob/ba48112890fd91035fb51b93dc1301e9f8e4329d/src/index.ts#L104

Context

For a few hours I am trying to debug a non trivial problem

D1_ERROR: too many SQL variables

I found that generation of kysely-d1 is using the rest operator .bind(...data) therefore from array of objects i get one single array which is flat. From this point D1 will fail because I have suddenly 100+ params

To reproduce

create a big table

export interface items {
  a: string;
  b: string;
  c: string;
  d: string;
  created_at: Generated<string | null>;
  e: string;
  f: Generated<number | null>;
  id: string;
  g: string | null;
  h: string;
  i: string | null;
  j: string;
  k: string;
  l: string;
  m: string;
  n: string;
}

try to do a insert with 10 items Which produces sql like this

insert into "items" ("a", "b", "c", "d", "e", "f", "id", "g", "h", "i", "j", "k", "l", "m", "n") values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?), (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)

and params will be

Array(135) [ ]

To fix

Most possible adapt https://developers.cloudflare.com/d1/platform/client-api/#batch-statements ?

aidenwallis commented 11 months ago

Interesting.. is this something you feel comfortable cutting a PR for? Else I can take a look next week (am travelling for the rest of the week)