Skip to content
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
13 changes: 11 additions & 2 deletions drizzle-orm/src/sql/expressions/conditions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,25 @@ export function or(
/**
* Negate the meaning of an expression using the `not` keyword.
*
* A condition that is equal to `undefined` is passed through as `undefined`,
* so `not` composes with `and`/`or`, which may return `undefined`.
*
* ## Examples
*
* ```ts
* // Select cars _not_ made by GM or Ford.
* db.select().from(cars)
* .where(not(inArray(cars.make, ['GM', 'Ford'])))
*
* // Composes with `and`/`or`, which can return `undefined`.
* db.select().from(cars)
* .where(not(and(eq(cars.make, 'GM'), eq(cars.year, 1950))))
* ```
*/
export function not(condition: SQLWrapper): SQL {
return sql`not ${condition}`;
export function not(condition: SQLWrapper): SQL;
export function not(condition: SQLWrapper | undefined): SQL | undefined;
export function not(condition: SQLWrapper | undefined): SQL | undefined {
return condition === undefined ? undefined : sql`not ${condition}`;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions drizzle-orm/type-tests/geldb/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,8 @@ const allOperators = await db
ne(users.id, 1),
or(eq(users.id, 1), ne(users.id, 1)),
not(eq(users.id, 1)),
not(and(eq(users.id, 1), ne(users.id, 1))),
not(or(eq(users.id, 1), ne(users.id, 1))),
gt(users.id, 1),
gte(users.id, 1),
lt(users.id, 1),
Expand Down
2 changes: 2 additions & 0 deletions drizzle-orm/type-tests/mysql/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,8 @@ const allOperators = await db
ne(users.id, 1),
or(eq(users.id, 1), ne(users.id, 1)),
not(eq(users.id, 1)),
not(and(eq(users.id, 1), ne(users.id, 1))),
not(or(eq(users.id, 1), ne(users.id, 1))),
gt(users.id, 1),
gte(users.id, 1),
lt(users.id, 1),
Expand Down
2 changes: 2 additions & 0 deletions drizzle-orm/type-tests/pg/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,8 @@ const allOperators = await db
ne(users.id, 1),
or(eq(users.id, 1), ne(users.id, 1)),
not(eq(users.id, 1)),
not(and(eq(users.id, 1), ne(users.id, 1))),
not(or(eq(users.id, 1), ne(users.id, 1))),
gt(users.id, 1),
gte(users.id, 1),
lt(users.id, 1),
Expand Down
2 changes: 2 additions & 0 deletions drizzle-orm/type-tests/singlestore/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,8 @@ const allOperators = await db
ne(users.id, 1),
or(eq(users.id, 1), ne(users.id, 1)),
not(eq(users.id, 1)),
not(and(eq(users.id, 1), ne(users.id, 1))),
not(or(eq(users.id, 1), ne(users.id, 1))),
gt(users.id, 1),
gte(users.id, 1),
lt(users.id, 1),
Expand Down
2 changes: 2 additions & 0 deletions drizzle-orm/type-tests/sqlite/select.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,8 @@ const allOperators = db
ne(users.id, 1),
or(eq(users.id, 1), ne(users.id, 1)),
not(eq(users.id, 1)),
not(and(eq(users.id, 1), ne(users.id, 1))),
not(or(eq(users.id, 1), ne(users.id, 1))),
gt(users.id, 1),
gte(users.id, 1),
lt(users.id, 1),
Expand Down