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

Astro DB Batch Query Type Mismatch #11865

Open
1 task
lorenzolewis opened this issue Aug 28, 2024 · 2 comments
Open
1 task

Astro DB Batch Query Type Mismatch #11865

lorenzolewis opened this issue Aug 28, 2024 · 2 comments
Labels
- P2: nice to have Not breaking anything but nice to have (priority) ecosystem: upstream Upstream package has issue

Comments

@lorenzolewis
Copy link
Contributor

Astro Info

Astro                    v4.14.6
Node                     v20.16.0
System                   macOS (arm64)
Package Manager          pnpm
Output                   static
Adapter                  none
Integrations             astro:db
                         @astrojs/db/file-url

If this issue only occurs in one browser, which browser is a problem?

N/A

Describe the Bug

Following the instructions in https://docs.astro.build/en/guides/astro-db/#batch-transactions you will receive the following error:

Argument of type 'SQLiteInsertBase<Table<"User", { user: { type: "text"; schema: { unique: false; deprecated: false; name: "user"; collection: "User"; primaryKey: false; optional: false; }; }; }>, "async", ResultSet, undefined, false, never>[]' is not assignable to parameter of type 'readonly [BatchItem<"sqlite">, ...BatchItem<"sqlite">[]]'.
  Source provides no match for required element at position 0 in target.ts(2345)

In the included repro check out src/pages/index.astro for a quick implementation of this.

What's the expected result?

There should be no TS error here.

Link to Minimal Reproducible Example

https://github.com/lorenzolewis/gullible-houston

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Aug 28, 2024
@Fryuni
Copy link
Member

Fryuni commented Aug 29, 2024

This is a problem with the types from Drizzle, it expects the argument for .batch() to be ensured at the type level to not be empty ([U, ...U[]], an array of U follow by any number of Us).

When you make a normal array in TS it will have the type of array, allowed to be empty. By giving this guarantee to the batch method it will not complain:

  const statements = [];
  for (let i = 0; i < 10; i++) {
    statements.push(db.insert(Item).values({ id: i, name: 'foo' }));
  }

  const [head, ...tail] = statements;
  if (head) {
    await db.batch([
      head,
      ...tail,
    ]);
  }

It seems to be working as they intended, requiring the caller to ensure that the array is not empty. The weirdness of this DX is something that can be pointed out as an issue on Drizzle.

@Fryuni
Copy link
Member

Fryuni commented Aug 29, 2024

This is the code defining the signature for the non-empty array as the argument:
https://github.com/drizzle-team/drizzle-orm/blob/c8359a16fff4b05aff09445edd63fc65a7430ce9/drizzle-orm/src/libsql/driver.ts#L25-L27

It seems to be like that ever since the support to batch API was added to the libSQL driver drizzle-team/drizzle-orm@a571331

@Fryuni Fryuni added ecosystem: upstream Upstream package has issue - P2: nice to have Not breaking anything but nice to have (priority) and removed needs triage Issue needs to be triaged labels Aug 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P2: nice to have Not breaking anything but nice to have (priority) ecosystem: upstream Upstream package has issue
Projects
None yet
Development

No branches or pull requests

2 participants