Skip to content

Commit 9ad42c2

Browse files
committed
Merge branch 'main' of github.com:drizzle-team/drizzle-orm into kit
2 parents 201b45c + 55231b0 commit 9ad42c2

File tree

12 files changed

+217
-15
lines changed

12 files changed

+217
-15
lines changed

changelogs/drizzle-orm/0.32.1.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
- Fix typings for indexes and allow creating indexes on 3+ columns mixing columns and expressions - thanks @lbguilherme!
2+
- Added support for "limit 0" in all dialects - closes [#2011](https://github.com/drizzle-team/drizzle-orm/issues/2011) - thanks @sillvva!
3+
- Make inArray and notInArray accept empty list, closes [#1295](https://github.com/drizzle-team/drizzle-orm/issues/1295) - thanks @RemiPeruto!
4+
- fix typo in lt typedoc - thanks @dalechyn!
5+
- fix wrong example in README.md - thanks @7flash!

drizzle-orm/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "drizzle-orm",
3-
"version": "0.32.0",
3+
"version": "0.32.1",
44
"description": "Drizzle ORM package for SQL databases",
55
"type": "module",
66
"scripts": {

drizzle-orm/src/mysql-core/dialect.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,9 @@ export class MySqlDialect {
326326
groupBySql = sql` group by ${sql.join(groupBy, sql`, `)}`;
327327
}
328328

329-
const limitSql = limit ? sql` limit ${limit}` : undefined;
329+
const limitSql = typeof limit === 'object' || (typeof limit === 'number' && limit >= 0)
330+
? sql` limit ${limit}`
331+
: undefined;
330332

331333
const offsetSql = offset ? sql` offset ${offset}` : undefined;
332334

@@ -403,7 +405,9 @@ export class MySqlDialect {
403405
orderBySql = sql` order by ${sql.join(orderByValues, sql`, `)} `;
404406
}
405407

406-
const limitSql = limit ? sql` limit ${limit}` : undefined;
408+
const limitSql = typeof limit === 'object' || (typeof limit === 'number' && limit >= 0)
409+
? sql` limit ${limit}`
410+
: undefined;
407411

408412
const operatorChunk = sql.raw(`${type} ${isAll ? 'all ' : ''}`);
409413

drizzle-orm/src/pg-core/dialect.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,9 @@ export class PgDialect {
357357
groupBySql = sql` group by ${sql.join(groupBy, sql`, `)}`;
358358
}
359359

360-
const limitSql = limit ? sql` limit ${limit}` : undefined;
360+
const limitSql = typeof limit === 'object' || (typeof limit === 'number' && limit >= 0)
361+
? sql` limit ${limit}`
362+
: undefined;
361363

362364
const offsetSql = offset ? sql` offset ${offset}` : undefined;
363365

@@ -443,7 +445,9 @@ export class PgDialect {
443445
orderBySql = sql` order by ${sql.join(orderByValues, sql`, `)} `;
444446
}
445447

446-
const limitSql = limit ? sql` limit ${limit}` : undefined;
448+
const limitSql = typeof limit === 'object' || (typeof limit === 'number' && limit >= 0)
449+
? sql` limit ${limit}`
450+
: undefined;
447451

448452
const operatorChunk = sql.raw(`${type} ${isAll ? 'all ' : ''}`);
449453

drizzle-orm/src/pg-core/indexes.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export class IndexBuilderOn {
111111

112112
constructor(private unique: boolean, private name?: string) {}
113113

114-
on(...columns: [Partial<ExtraConfigColumn> | SQL, ...Partial<ExtraConfigColumn>[] | SQL[]]): IndexBuilder {
114+
on(...columns: [Partial<ExtraConfigColumn> | SQL, ...Partial<ExtraConfigColumn | SQL>[]]): IndexBuilder {
115115
return new IndexBuilder(
116116
columns.map((it) => {
117117
if (is(it, SQL)) {
@@ -128,7 +128,7 @@ export class IndexBuilderOn {
128128
);
129129
}
130130

131-
onOnly(...columns: [Partial<ExtraConfigColumn | SQL>, ...Partial<ExtraConfigColumn>[] | SQL[]]): IndexBuilder {
131+
onOnly(...columns: [Partial<ExtraConfigColumn | SQL>, ...Partial<ExtraConfigColumn | SQL>[]]): IndexBuilder {
132132
return new IndexBuilder(
133133
columns.map((it) => {
134134
if (is(it, SQL)) {
@@ -158,7 +158,7 @@ export class IndexBuilderOn {
158158
*/
159159
using(
160160
method: PgIndexMethod,
161-
...columns: [Partial<ExtraConfigColumn | SQL>, ...Partial<ExtraConfigColumn>[] | SQL[]]
161+
...columns: [Partial<ExtraConfigColumn | SQL>, ...Partial<ExtraConfigColumn | SQL>[]]
162162
): IndexBuilder {
163163
return new IndexBuilder(
164164
columns.map((it) => {

drizzle-orm/src/sql/expressions/conditions.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ export const gte: BinaryOperator = (left: SQLWrapper, right: unknown): SQL => {
228228
* .where(lt(cars.year, 2000))
229229
* ```
230230
*
231-
* @see lte for greater-than-or-equal
231+
* @see lte for less-than-or-equal
232232
*/
233233
export const lt: BinaryOperator = (left: SQLWrapper, right: unknown): SQL => {
234234
return sql`${left} < ${bindIfParam(right, left)}`;
@@ -289,7 +289,7 @@ export function inArray(
289289
): SQL {
290290
if (Array.isArray(values)) {
291291
if (values.length === 0) {
292-
throw new Error('inArray requires at least one value');
292+
return sql`false`;
293293
}
294294
return sql`${column} in ${values.map((v) => bindIfParam(v, column))}`;
295295
}
@@ -335,7 +335,7 @@ export function notInArray(
335335
): SQL {
336336
if (Array.isArray(values)) {
337337
if (values.length === 0) {
338-
throw new Error('notInArray requires at least one value');
338+
return sql`true`;
339339
}
340340
return sql`${column} not in ${values.map((v) => bindIfParam(v, column))}`;
341341
}

drizzle-orm/src/sqlite-core/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ import Database from 'better-sqlite3';
178178
const sqlite = new Database('sqlite.db');
179179
const db: BetterSQLite3Database = drizzle(sqlite);
180180

181-
const result: User[] = db.select().from(users).all();
181+
const result: User[] = await db.select().from(users).all();
182182

183183
const insertUser = (user: InsertUser) => {
184184
return db.insert(users).values(user).run()

drizzle-orm/src/sqlite-core/dialect.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,9 @@ export abstract class SQLiteDialect {
295295

296296
const orderBySql = orderByList.length > 0 ? sql` order by ${sql.join(orderByList)}` : undefined;
297297

298-
const limitSql = limit ? sql` limit ${limit}` : undefined;
298+
const limitSql = typeof limit === 'object' || (typeof limit === 'number' && limit >= 0)
299+
? sql` limit ${limit}`
300+
: undefined;
299301

300302
const offsetSql = offset ? sql` offset ${offset}` : undefined;
301303

@@ -362,7 +364,9 @@ export abstract class SQLiteDialect {
362364
orderBySql = sql` order by ${sql.join(orderByValues, sql`, `)}`;
363365
}
364366

365-
const limitSql = limit ? sql` limit ${limit}` : undefined;
367+
const limitSql = typeof limit === 'object' || (typeof limit === 'number' && limit >= 0)
368+
? sql` limit ${limit}`
369+
: undefined;
366370

367371
const operatorChunk = sql.raw(`${type} ${isAll ? 'all ' : ''}`);
368372

integration-tests/tests/mysql/mysql-common.ts

+53
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
max,
1919
min,
2020
Name,
21+
notInArray,
2122
placeholder,
2223
sql,
2324
sum,
@@ -534,6 +535,34 @@ export function tests(driver?: string) {
534535
expect(users).toEqual([{ name: 'JOHN' }]);
535536
});
536537

538+
test('select with empty array in inArray', async (ctx) => {
539+
const { db } = ctx.mysql;
540+
541+
await db.insert(usersTable).values([{ name: 'John' }, { name: 'Jane' }, { name: 'Jane' }]);
542+
const result = await db
543+
.select({
544+
name: sql`upper(${usersTable.name})`,
545+
})
546+
.from(usersTable)
547+
.where(inArray(usersTable.id, []));
548+
549+
expect(result).toEqual([]);
550+
});
551+
552+
test('select with empty array in notInArray', async (ctx) => {
553+
const { db } = ctx.mysql;
554+
555+
await db.insert(usersTable).values([{ name: 'John' }, { name: 'Jane' }, { name: 'Jane' }]);
556+
const result = await db
557+
.select({
558+
name: sql`upper(${usersTable.name})`,
559+
})
560+
.from(usersTable)
561+
.where(notInArray(usersTable.id, []));
562+
563+
expect(result).toEqual([{ name: 'JOHN' }, { name: 'JANE' }, { name: 'JANE' }]);
564+
});
565+
537566
test('select distinct', async (ctx) => {
538567
const { db } = ctx.mysql;
539568

@@ -3486,4 +3515,28 @@ export function tests(driver?: string) {
34863515
await db.execute(sql`drop view ${newYorkers1}`);
34873516
});
34883517
});
3518+
3519+
test('limit 0', async (ctx) => {
3520+
const { db } = ctx.mysql;
3521+
3522+
await db.insert(usersTable).values({ name: 'John' });
3523+
const users = await db
3524+
.select()
3525+
.from(usersTable)
3526+
.limit(0);
3527+
3528+
expect(users).toEqual([]);
3529+
});
3530+
3531+
test('limit -1', async (ctx) => {
3532+
const { db } = ctx.mysql;
3533+
3534+
await db.insert(usersTable).values({ name: 'John' });
3535+
const users = await db
3536+
.select()
3537+
.from(usersTable)
3538+
.limit(-1);
3539+
3540+
expect(users.length).toBeGreaterThan(0);
3541+
});
34893542
}

integration-tests/tests/pg/awsdatapi.test.ts

+27-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import 'dotenv/config';
22

33
import { RDSDataClient } from '@aws-sdk/client-rds-data';
44
import * as dotenv from 'dotenv';
5-
import { asc, eq, sql, TransactionRollbackError } from 'drizzle-orm';
5+
import { asc, eq, inArray, notInArray, sql, TransactionRollbackError } from 'drizzle-orm';
66
import type { AwsDataApiPgDatabase } from 'drizzle-orm/aws-data-api/pg';
77
import { drizzle } from 'drizzle-orm/aws-data-api/pg';
88
import { migrate } from 'drizzle-orm/aws-data-api/pg/migrator';
@@ -105,6 +105,32 @@ test('select sql', async () => {
105105
expect(users).toEqual([{ name: 'JOHN' }]);
106106
});
107107

108+
test('select with empty array in inArray', async () => {
109+
await db.insert(usersTable).values([
110+
{ name: 'John' },
111+
{ name: 'Jane' },
112+
{ name: 'Jane' },
113+
]);
114+
const users = await db.select({
115+
name: sql`upper(${usersTable.name})`,
116+
}).from(usersTable).where(inArray(usersTable.id, []));
117+
118+
expect(users).toEqual([]);
119+
});
120+
121+
test('select with empty array in notInArray', async () => {
122+
await db.insert(usersTable).values([
123+
{ name: 'John' },
124+
{ name: 'Jane' },
125+
{ name: 'Jane' },
126+
]);
127+
const result = await db.select({
128+
name: sql`upper(${usersTable.name})`,
129+
}).from(usersTable).where(notInArray(usersTable.id, []));
130+
131+
expect(result).toEqual([{ name: 'John' }, { name: 'Jane' }, { name: 'Jane' }]);
132+
});
133+
108134
test('select typed sql', async () => {
109135
await db.insert(usersTable).values({ name: 'John' });
110136
const users = await db.select({

integration-tests/tests/pg/pg-common.ts

+53
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
lt,
2222
max,
2323
min,
24+
notInArray,
2425
or,
2526
SQL,
2627
sql,
@@ -539,6 +540,34 @@ export function tests() {
539540
expect(users).toEqual([{ name: 'JOHN' }]);
540541
});
541542

543+
test('select with empty array in inArray', async (ctx) => {
544+
const { db } = ctx.pg;
545+
546+
await db.insert(usersTable).values([{ name: 'John' }, { name: 'Jane' }, { name: 'Jane' }]);
547+
const result = await db
548+
.select({
549+
name: sql`upper(${usersTable.name})`,
550+
})
551+
.from(usersTable)
552+
.where(inArray(usersTable.id, []));
553+
554+
expect(result).toEqual([]);
555+
});
556+
557+
test('select with empty array in notInArray', async (ctx) => {
558+
const { db } = ctx.pg;
559+
560+
await db.insert(usersTable).values([{ name: 'John' }, { name: 'Jane' }, { name: 'Jane' }]);
561+
const result = await db
562+
.select({
563+
name: sql`upper(${usersTable.name})`,
564+
})
565+
.from(usersTable)
566+
.where(notInArray(usersTable.id, []));
567+
568+
expect(result).toEqual([{ name: 'JOHN' }, { name: 'JANE' }, { name: 'JANE' }]);
569+
});
570+
542571
test('$default function', async (ctx) => {
543572
const { db } = ctx.pg;
544573

@@ -4428,5 +4457,29 @@ export function tests() {
44284457

44294458
await db.execute(sql`drop materialized view ${newYorkers1}`);
44304459
});
4460+
4461+
test('limit 0', async (ctx) => {
4462+
const { db } = ctx.pg;
4463+
4464+
await db.insert(usersTable).values({ name: 'John' });
4465+
const users = await db
4466+
.select()
4467+
.from(usersTable)
4468+
.limit(0);
4469+
4470+
expect(users).toEqual([]);
4471+
});
4472+
4473+
test('limit -1', async (ctx) => {
4474+
const { db } = ctx.pg;
4475+
4476+
await db.insert(usersTable).values({ name: 'John' });
4477+
const users = await db
4478+
.select()
4479+
.from(usersTable)
4480+
.limit(-1);
4481+
4482+
expect(users.length).toBeGreaterThan(0);
4483+
});
44314484
});
44324485
}

0 commit comments

Comments
 (0)