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

Improve batch types #2012

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

Conversation

DmitryScaletta
Copy link

It's currently impossible to pass dynamic arrays to db.batch function.
But sometimes it can be useful.

Before

const batchResponse0 = await db.batch(
  ['foo', 'bar'].map((channel) =>
    db.query.messages.findMany({
      where: eq(messages.channel, channel),
      limit: 100,
    }),
  ),
);
/*
Error: Argument of type 
'SQLiteRelationalQuery<"async", {
  id: number;
  channel: string;
  message: string;
  timestamp: Date;
}[]>[]'
is not assignable to parameter of type 
'readonly [BatchItem<"sqlite">, ...BatchItem<"sqlite">[]]'.
Source provides no match for required element at position 0 in target.
*/

After

import { eq } from 'drizzle-orm';
import { drizzle } from 'drizzle-orm/libsql';
import { text, integer, sqliteTable } from 'drizzle-orm/sqlite-core';
import { createClient } from '@libsql/client';

export const messages = sqliteTable('message', {
  id: integer('id').primaryKey(),
  channel: text('channel').notNull(),
  message: text('message', { length: 512 }).notNull(),
  timestamp: integer('timestamp', { mode: 'timestamp' }).notNull(),
});

const client = createClient({ url: '', authToken: '' });
const db = drizzle(client, { schema: { messages } });

const batchResponse0 = await db.batch(
  ['foo', 'bar'].map((channel) =>
    db.query.messages.findMany({
      where: eq(messages.channel, channel),
      limit: 100,
    }),
  ),
);
/*
const batchResponse0: {
  id: number;
  channel: string;
  message: string;
  timestamp: Date;
}[][]
*/

// tuples still work
const batchResponse1 = await db.batch([
  db.query.messages.findMany({}),
  db.query.messages.findFirst(),
]);
/*
const batchResponse1: [{
  id: number;
  channel: string;
  message: string;
  timestamp: Date;
}[], {
  id: number;
  channel: string;
  message: string;
  timestamp: Date;
} | undefined]
*/

const arr2: ReturnType<typeof db.query.messages.findFirst>[] = [];
const batchResponse2 = await db.batch(arr2);
/*
const batchResponse2: ({
  id: number;
  channel: string;
  message: string;
  timestamp: Date;
} | undefined)[]
*/

const batchResponse3 = await db.batch(
  [1, 2].map((id) =>
    db.query.messages.findFirst({ where: eq(messages.id, id) }),
  ),
);
/*
const batchResponse3: ({
  id: number;
  timestamp: Date;
  channel: string;
  message: string;
} | undefined)[]
*/

const arr4 = [];
arr4.push(db.query.messages.findFirst());
arr4.push(db.query.messages.findMany());
const batchResponse4 = await db.batch(arr4);
/*
const batchResponse4: ({
  id: number;
  channel: string;
  message: string;
  timestamp: Date;
} | {
  id: number;
  channel: string;
  message: string;
  timestamp: Date;
}[] | undefined)[]
*/

I couldn't run tests but they should work.

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

1 participant