Skip to content

Commit

Permalink
inArray and notInArray methods
Browse files Browse the repository at this point in the history
Make inArray and notInArray methods accept an empty list as their second parameter
  • Loading branch information
RemiPeruto committed Jul 22, 2024
1 parent d01cd7f commit 8f97370
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 3 deletions.
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
29 changes: 29 additions & 0 deletions integration-tests/tests/mysql/mysql-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
gt,
gte,
inArray,
notInArray,
lt,
max,
min,
Expand Down Expand Up @@ -534,6 +535,34 @@ export function tests(driver?: string) {
expect(users).toEqual([{ name: 'JOHN' }]);
});

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

await db.insert(usersTable).values([{ name: 'John' }, { name: 'Jane' }, { name: 'Jane' }]);
const result = await db
.select({
name: sql`upper(${usersTable.name})`,
})
.from(usersTable)
.where(inArray(usersTable.id, []));

expect(result).toEqual([]);
});

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

await db.insert(usersTable).values([{ name: 'John' }, { name: 'Jane' }, { name: 'Jane' }]);
const result = await db
.select({
name: sql`upper(${usersTable.name})`,
})
.from(usersTable)
.where(notInArray(usersTable.id, []));

expect(result).toEqual([{ name: 'JOHN' }, { name: 'JANE' }, { name: 'JANE' }]);
});

test('select distinct', async (ctx) => {
const { db } = ctx.mysql;

Expand Down
20 changes: 19 additions & 1 deletion integration-tests/tests/pg/awsdatapi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'dotenv/config';

import { RDSDataClient } from '@aws-sdk/client-rds-data';
import * as dotenv from 'dotenv';
import { asc, eq, sql, TransactionRollbackError } from 'drizzle-orm';
import {asc, eq, inArray, notInArray, sql, TransactionRollbackError} from 'drizzle-orm';
import type { AwsDataApiPgDatabase } from 'drizzle-orm/aws-data-api/pg';
import { drizzle } from 'drizzle-orm/aws-data-api/pg';
import { migrate } from 'drizzle-orm/aws-data-api/pg/migrator';
Expand Down Expand Up @@ -105,6 +105,24 @@ test('select sql', async () => {
expect(users).toEqual([{ name: 'JOHN' }]);
});

test('select with empty array in inArray', async () => {
await db.insert(usersTable).values([{ name: 'John' }, { name: 'Jane' }, { name: 'Jane' }]);
const users = await db.select({
name: sql`upper(${usersTable.name})`,
}).from(usersTable).where(inArray(usersTable.id, []));

expect(users).toEqual([]);
});

test('select with empty array in notInArray', async () => {
await db.insert(usersTable).values([{ name: 'John' }, { name: 'Jane' }, { name: 'Jane' }]);
const result = await db.select({
name: sql`upper(${usersTable.name})`,
}).from(usersTable).where(notInArray(usersTable.id, []));

expect(result).toEqual([{ name: 'John' }, { name: 'Jane' }, { name: 'Jane' }]);
});

test('select typed sql', async () => {
await db.insert(usersTable).values({ name: 'John' });
const users = await db.select({
Expand Down
29 changes: 29 additions & 0 deletions integration-tests/tests/pg/pg-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
gte,
ilike,
inArray,
notInArray,
lt,
max,
min,
Expand Down Expand Up @@ -539,6 +540,34 @@ export function tests() {
expect(users).toEqual([{ name: 'JOHN' }]);
});

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

await db.insert(usersTable).values([{ name: 'John' }, { name: 'Jane' }, { name: 'Jane' }]);
const result = await db
.select({
name: sql`upper(${usersTable.name})`,
})
.from(usersTable)
.where(inArray(usersTable.id, []));

expect(result).toEqual([]);
});

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

await db.insert(usersTable).values([{ name: 'John' }, { name: 'Jane' }, { name: 'Jane' }]);
const result = await db
.select({
name: sql`upper(${usersTable.name})`,
})
.from(usersTable)
.where(notInArray(usersTable.id, []));

expect(result).toEqual([{ name: 'JOHN' }, { name: 'JANE' }, { name: 'JANE' }]);
});

test('$default function', async (ctx) => {
const { db } = ctx.pg;

Expand Down
29 changes: 29 additions & 0 deletions integration-tests/tests/sqlite/sqlite-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
gt,
gte,
inArray,
notInArray,
lt,
max,
min,
Expand Down Expand Up @@ -371,6 +372,34 @@ export function tests() {
expect(users).toEqual([{ name: 'JOHN' }]);
});

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

await db.insert(usersTable).values([{ name: 'John' }, { name: 'Jane' }, { name: 'Jane' }]);
const result = await db
.select({
name: sql`upper(${usersTable.name})`,
})
.from(usersTable)
.where(inArray(usersTable.id, []));

expect(result).toEqual([]);
});

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

await db.insert(usersTable).values([{ name: 'John' }, { name: 'Jane' }, { name: 'Jane' }]);
const result = await db
.select({
name: sql`upper(${usersTable.name})`,
})
.from(usersTable)
.where(notInArray(usersTable.id, []));

expect(result).toEqual([{ name: 'JOHN' }, { name: 'JANE' }, { name: 'JANE' }]);
});

test('select distinct', async (ctx) => {
const { db } = ctx.sqlite;

Expand Down

0 comments on commit 8f97370

Please # to comment.