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

Feat: allow empty arrays to passed to inArray and notInArray #1664

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions drizzle-orm/src/sql/expressions/conditions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ export function inArray(
): SQL {
if (Array.isArray(values)) {
if (values.length === 0) {
throw new Error('inArray requires at least one value');
return sql`false`;
}
return sql`${column} in ${values.map((v) => bindIfParam(v, column))}`;
}
Expand Down Expand Up @@ -335,7 +335,7 @@ export function notInArray(
): SQL {
if (Array.isArray(values)) {
if (values.length === 0) {
throw new Error('notInArray requires at least one value');
return sql`true`;
}
return sql`${column} not in ${values.map((v) => bindIfParam(v, column))}`;
}
Expand Down
23 changes: 23 additions & 0 deletions integration-tests/tests/libsql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
min,
Name,
name,
notInArray,
placeholder,
sql,
sum,
Expand Down Expand Up @@ -956,6 +957,28 @@ test.serial('select with group by complex query', async (t) => {
t.deepEqual(result, [{ name: 'Jane' }]);
});

test.serial('select with empty array in inArray', async (t) => {
const { db } = t.context;

await db.insert(usersTable).values([{ name: 'John' }, { name: 'Jane' }, { name: 'Jane' }]);

const result = await db.select({ name: usersTable.name }).from(usersTable)
.where(inArray(usersTable.id, []));

t.deepEqual(result, []);
});

test.serial('select with empty array in notInArray', async (t) => {
const { db } = t.context;

await db.insert(usersTable).values([{ name: 'John' }, { name: 'Jane' }, { name: 'Jane' }]);

const result = await db.select({ name: usersTable.name }).from(usersTable)
.where(notInArray(usersTable.id, []));

t.deepEqual(result, [{ name: 'John' }, { name: 'Jane' }, { name: 'Jane' }]);
});

test.serial('build query', async (t) => {
const { db } = t.context;

Expand Down
23 changes: 23 additions & 0 deletions integration-tests/tests/mysql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
max,
min,
Name,
notInArray,
placeholder,
sql,
sum,
Expand Down Expand Up @@ -760,6 +761,28 @@ test.serial('select with group by as column + sql', async (t) => {
t.deepEqual(result, [{ name: 'John' }, { name: 'Jane' }, { name: 'Jane' }]);
});

test.serial('select with empty array in inArray', async (t) => {
const { db } = t.context;

await db.insert(usersTable).values([{ name: 'John' }, { name: 'Jane' }, { name: 'Jane' }]);

const result = await db.select({ name: usersTable.name }).from(usersTable)
.where(inArray(usersTable.id, []));

t.deepEqual(result, []);
});

test.serial('select with empty array in notInArray', async (t) => {
const { db } = t.context;

await db.insert(usersTable).values([{ name: 'John' }, { name: 'Jane' }, { name: 'Jane' }]);

const result = await db.select({ name: usersTable.name }).from(usersTable)
.where(notInArray(usersTable.id, []));

t.deepEqual(result, [{ name: 'John' }, { name: 'Jane' }, { name: 'Jane' }]);
});

test.serial('select with group by complex query', async (t) => {
const { db } = t.context;

Expand Down
23 changes: 23 additions & 0 deletions integration-tests/tests/pg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
max,
min,
name,
notInArray,
placeholder,
type SQL,
sql,
Expand Down Expand Up @@ -1728,6 +1729,28 @@ test.serial('array types', async (t) => {
t.deepEqual(res, values);
});

test.serial('select with empty array in inArray', async (t) => {
const { db } = t.context;

await db.insert(usersTable).values([{ name: 'John' }, { name: 'Jane' }, { name: 'Jane' }]);

const result = await db.select({ name: usersTable.name }).from(usersTable)
.where(inArray(usersTable.id, []));

t.deepEqual(result, []);
});

test.serial('select with empty array in notInArray', async (t) => {
const { db } = t.context;

await db.insert(usersTable).values([{ name: 'John' }, { name: 'Jane' }, { name: 'Jane' }]);

const result = await db.select({ name: usersTable.name }).from(usersTable)
.where(notInArray(usersTable.id, []));

t.deepEqual(result, [{ name: 'John' }, { name: 'Jane' }, { name: 'Jane' }]);
});

test.serial('select for ...', (t) => {
const { db } = t.context;

Expand Down
23 changes: 23 additions & 0 deletions integration-tests/tests/postgres.js.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
lt,
Name,
name,
notInArray,
placeholder,
type SQL,
sql,
Expand Down Expand Up @@ -1318,6 +1319,28 @@ test.serial('select count w/ custom mapper', async (t) => {
t.deepEqual(res, [{ count: 2 }]);
});

test.serial('select with empty array in inArray', async (t) => {
const { db } = t.context;

await db.insert(usersTable).values([{ name: 'John' }, { name: 'Jane' }, { name: 'Jane' }]);

const result = await db.select({ name: usersTable.name }).from(usersTable)
.where(inArray(usersTable.id, []));

t.deepEqual(result, []);
});

test.serial('select with empty array in notInArray', async (t) => {
const { db } = t.context;

await db.insert(usersTable).values([{ name: 'John' }, { name: 'Jane' }, { name: 'Jane' }]);

const result = await db.select({ name: usersTable.name }).from(usersTable)
.where(notInArray(usersTable.id, []));

t.deepEqual(result, [{ name: 'John' }, { name: 'Jane' }, { name: 'Jane' }]);
});

test.serial('select for ...', (t) => {
const { db } = t.context;

Expand Down