Skip to content

Commit e2cc72f

Browse files
committed
all types tests for bytea, bytea RQBv2 fix, array contained, contains, overlaps functions for RQBv2 filters
1 parent ec0aae1 commit e2cc72f

8 files changed

Lines changed: 78 additions & 6 deletions

File tree

drizzle-orm/src/pg-core/columns/bytea.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ export class PgByteaBuilder<T extends ColumnBuilderBaseConfig<'buffer', 'PgBytea
3131
export class PgBytea<T extends ColumnBaseConfig<'buffer', 'PgBytea'>> extends PgColumn<T> {
3232
static override readonly [entityKind]: string = 'PgBytea';
3333

34+
override mapFromDriverValue(value: Buffer | string): Buffer {
35+
if (Buffer.isBuffer(value)) return value;
36+
37+
// Remove '\x'
38+
const trimmed = value.slice(2, value.length);
39+
return Buffer.from(trimmed, 'hex');
40+
}
41+
3442
getSQLType(): string {
3543
return 'bytea';
3644
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -926,7 +926,8 @@ export class PgDialect {
926926
case 'PgBigSerial64':
927927
case 'PgTimestampString':
928928
case 'PgGeometry':
929-
case 'PgGeometryObject': {
929+
case 'PgGeometryObject':
930+
case 'PgBytea': {
930931
return sql`${name}::text${sql.raw(arrVal).if(arrVal)} as ${sql.identifier(key)}`;
931932
}
932933
default: {

drizzle-orm/src/relations.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import { entityKind, is } from './entity.ts';
77
import { DrizzleError } from './errors.ts';
88
import {
99
and,
10+
arrayContained,
11+
arrayContains,
12+
arrayOverlaps,
1013
asc,
1114
between,
1215
desc,
@@ -406,6 +409,9 @@ export const operators = {
406409
gte,
407410
ilike,
408411
inArray,
412+
arrayContains,
413+
arrayContained,
414+
arrayOverlaps,
409415
isNull,
410416
isNotNull,
411417
like,
@@ -901,6 +907,9 @@ export interface RelationFieldsFilterInternals<T> {
901907
lte?: T | Placeholder | undefined;
902908
in?: (T | Placeholder)[] | Placeholder | undefined;
903909
notIn?: (T | Placeholder)[] | Placeholder | undefined;
910+
arrayContains?: (T extends Array<infer E> ? (E | Placeholder)[] : T) | Placeholder | undefined;
911+
arrayContained?: (T extends Array<infer E> ? (E | Placeholder)[] : T) | Placeholder | undefined;
912+
arrayOverlaps?: (T extends Array<infer E> ? (E | Placeholder)[] : T) | Placeholder | undefined;
904913
like?: string | Placeholder | undefined;
905914
ilike?: string | Placeholder | undefined;
906915
notLike?: string | Placeholder | undefined;
@@ -979,9 +988,10 @@ export type TableFilter<
979988
: Assume<TTable, Table>['_']['columns'],
980989
> =
981990
& {
982-
[K in keyof TColumns as K extends keyof TableFilterCommons ? never : K]?: TColumns[K] extends Column
983-
? RelationsFieldFilter<TColumns[K]['_']['data']>
984-
: RelationsFieldFilter<unknown>;
991+
[K in keyof TColumns as K extends keyof TableFilterCommons ? never : K]?:
992+
| (TColumns[K] extends Column ? RelationsFieldFilter<TColumns[K]['_']['data']>
993+
: RelationsFieldFilter<unknown>)
994+
| undefined;
985995
}
986996
& TableFilterCommons<TTable, TColumns>;
987997

drizzle-orm/tests/rqb-builders.test.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import { relationsFilterToSQL, relationsOrderToSQL } from '~/relations';
55
import type { OrderBy, OrderByOperators, TableFilter } from '~/relations';
66
import {
77
and,
8+
arrayContained,
9+
arrayContains,
10+
arrayOverlaps,
811
asc,
912
desc,
1013
eq,
@@ -32,12 +35,13 @@ import type { Simplify, ValueOrArray } from '~/utils';
3235
const table = pgTable('test', {
3336
string: text(),
3437
number: integer(),
38+
arr: integer().array(),
3539
date: date('date', {
3640
mode: 'date',
3741
}),
3842
});
3943

40-
const buildFilter = <TTable extends Table>(table: TTable, filter: TableFilter<typeof table>) =>
44+
const buildFilter = <TTable extends Table>(table: TTable, filter: TableFilter<TTable>) =>
4145
relationsFilterToSQL(table, filter as TableFilter);
4246

4347
describe('Filters', () => {
@@ -48,7 +52,6 @@ describe('Filters', () => {
4852
date: new Date(),
4953
});
5054
buildFilter(table, {
51-
// @ts-expect-error
5255
date: sql.placeholder('date'),
5356
});
5457
buildFilter(table, {
@@ -170,6 +173,30 @@ describe('Filters', () => {
170173
})).toStrictEqual(and(and(notInArray(table.number, [2, 3]))));
171174
});
172175

176+
test('arrayContains', () => {
177+
expect(buildFilter(table, {
178+
arr: {
179+
arrayContains: [2, 3],
180+
},
181+
})).toStrictEqual(and(and(arrayContains(table.arr, [2, 3]))));
182+
});
183+
184+
test('arrayContained', () => {
185+
expect(buildFilter(table, {
186+
arr: {
187+
arrayContained: [2, 3],
188+
},
189+
})).toStrictEqual(and(and(arrayContained(table.arr, [2, 3]))));
190+
});
191+
192+
test('arrayOverlaps', () => {
193+
expect(buildFilter(table, {
194+
arr: {
195+
arrayOverlaps: [2, 3],
196+
},
197+
})).toStrictEqual(and(and(arrayOverlaps(table.arr, [2, 3]))));
198+
});
199+
173200
test('isNotNull', () => {
174201
expect(buildFilter(table, {
175202
number: {

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import {
3939
bigint,
4040
bigserial,
4141
boolean,
42+
bytea,
4243
char,
4344
cidr,
4445
date,
@@ -126,6 +127,7 @@ const allTypesTable = pgTable('all_types', {
126127
mode: 'bigint',
127128
}),
128129
bool: boolean('bool'),
130+
bytea: bytea('bytea'),
129131
char: char('char'),
130132
cidr: cidr('cidr'),
131133
date: date('date', {
@@ -190,6 +192,7 @@ const allTypesTable = pgTable('all_types', {
190192
mode: 'bigint',
191193
}).array(),
192194
arrbool: boolean('arrbool').array(),
195+
arrbytea: bytea('arrbytea').array(),
193196
arrchar: char('arrchar').array(),
194197
arrcidr: cidr('arrcidr').array(),
195198
arrdate: date('arrdate', {
@@ -5730,6 +5733,7 @@ export function tests() {
57305733
"bigint53" bigint,
57315734
"bigint64" bigint,
57325735
"bool" boolean,
5736+
"bytea" bytea,
57335737
"char" char,
57345738
"cidr" "cidr",
57355739
"date" date,
@@ -5764,6 +5768,7 @@ export function tests() {
57645768
"arrbigint53" bigint[],
57655769
"arrbigint64" bigint[],
57665770
"arrbool" boolean[],
5771+
"arrbytea" bytea[],
57675772
"arrchar" char[],
57685773
"arrcidr" "cidr"[],
57695774
"arrdate" date[],
@@ -5804,6 +5809,7 @@ export function tests() {
58045809
bigserial53: 9007199254740991,
58055810
bigserial64: 5044565289845416380n,
58065811
bool: true,
5812+
bytea: Buffer.from('BYTES'),
58075813
char: 'c',
58085814
cidr: '2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128',
58095815
inet: '192.168.0.1/24',
@@ -5850,6 +5856,7 @@ export function tests() {
58505856
arrbigint53: [9007199254740991],
58515857
arrbigint64: [5044565289845416380n],
58525858
arrbool: [true],
5859+
arrbytea: [Buffer.from('BYTES')],
58535860
arrchar: ['c'],
58545861
arrcidr: ['2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128'],
58555862
arrinet: ['192.168.0.1/24'],
@@ -5905,6 +5912,7 @@ export function tests() {
59055912
bigint53: number | null;
59065913
bigint64: bigint | null;
59075914
bool: boolean | null;
5915+
bytea: Buffer | null;
59085916
char: string | null;
59095917
cidr: string | null;
59105918
date: Date | null;
@@ -5946,6 +5954,7 @@ export function tests() {
59465954
arrbigint53: number[] | null;
59475955
arrbigint64: bigint[] | null;
59485956
arrbool: boolean[] | null;
5957+
arrbytea: Buffer[] | null;
59495958
arrchar: string[] | null;
59505959
arrcidr: string[] | null;
59515960
arrdate: Date[] | null;
@@ -5990,6 +5999,7 @@ export function tests() {
59905999
bigint53: 9007199254740991,
59916000
bigint64: 5044565289845416380n,
59926001
bool: true,
6002+
bytea: Buffer.from('BYTES'),
59936003
char: 'c',
59946004
cidr: '2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128',
59956005
date: new Date('2025-03-12T00:00:00.000Z'),
@@ -6024,6 +6034,7 @@ export function tests() {
60246034
arrbigint53: [9007199254740991],
60256035
arrbigint64: [5044565289845416380n],
60266036
arrbool: [true],
6037+
arrbytea: [Buffer.from('BYTES')],
60276038
arrchar: ['c'],
60286039
arrcidr: ['2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128'],
60296040
arrdate: [new Date('2025-03-12T00:00:00.000Z')],

integration-tests/tests/relational/pg.postgresjs.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12729,6 +12729,7 @@ test('alltypes', async () => {
1272912729
"bigint53" bigint,
1273012730
"bigint64" bigint,
1273112731
"bool" boolean,
12732+
"bytea" bytea,
1273212733
"char" char,
1273312734
"cidr" "cidr",
1273412735
"date" date,
@@ -12763,6 +12764,7 @@ test('alltypes', async () => {
1276312764
"arrbigint53" bigint[],
1276412765
"arrbigint64" bigint[],
1276512766
"arrbool" boolean[],
12767+
"arrbytea" bytea[],
1276612768
"arrchar" char[],
1276712769
"arrcidr" "cidr"[],
1276812770
"arrdate" date[],
@@ -12808,6 +12810,7 @@ test('alltypes', async () => {
1280812810
bigserial53: 9007199254740991,
1280912811
bigserial64: 5044565289845416380n,
1281012812
bool: true,
12813+
bytea: Buffer.from('BYTES'),
1281112814
char: 'c',
1281212815
cidr: '2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128',
1281312816
inet: '192.168.0.1/24',
@@ -12854,6 +12857,7 @@ test('alltypes', async () => {
1285412857
arrbigint53: [9007199254740991],
1285512858
arrbigint64: [5044565289845416380n],
1285612859
arrbool: [true],
12860+
arrbytea: [Buffer.from('BYTES')],
1285712861
arrchar: ['c'],
1285812862
arrcidr: ['2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128'],
1285912863
arrinet: ['192.168.0.1/24'],
@@ -12922,6 +12926,7 @@ test('alltypes', async () => {
1292212926
bigint53: 9007199254740991,
1292312927
bigint64: 5044565289845416380n,
1292412928
bool: true,
12929+
bytea: Buffer.from('BYTES'),
1292512930
char: 'c',
1292612931
cidr: '2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128',
1292712932
date: new Date('2025-03-12T00:00:00.000Z'),
@@ -12956,6 +12961,7 @@ test('alltypes', async () => {
1295612961
arrbigint53: [9007199254740991],
1295712962
arrbigint64: [5044565289845416380n],
1295812963
arrbool: [true],
12964+
arrbytea: [Buffer.from('BYTES')],
1295912965
arrchar: ['c'],
1296012966
arrcidr: ['2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128'],
1296112967
arrdate: [new Date('2025-03-12T00:00:00.000Z')],

integration-tests/tests/relational/pg.schema.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
bigint,
66
bigserial,
77
boolean,
8+
bytea,
89
char,
910
cidr,
1011
date,
@@ -204,6 +205,7 @@ export const allTypesTable = pgTable('all_types', {
204205
mode: 'bigint',
205206
}),
206207
bool: boolean(),
208+
bytea: bytea(),
207209
char: char(),
208210
cidr: cidr(),
209211
date: date({
@@ -268,6 +270,7 @@ export const allTypesTable = pgTable('all_types', {
268270
mode: 'bigint',
269271
}).array(),
270272
arrbool: boolean().array(),
273+
arrbytea: bytea().array(),
271274
arrchar: char().array(),
272275
arrcidr: cidr().array(),
273276
arrdate: date({

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12912,6 +12912,7 @@ test('alltypes', async () => {
1291212912
"bigint53" bigint,
1291312913
"bigint64" bigint,
1291412914
"bool" boolean,
12915+
"bytea" bytea,
1291512916
"char" char,
1291612917
"cidr" "cidr",
1291712918
"date" date,
@@ -12946,6 +12947,7 @@ test('alltypes', async () => {
1294612947
"arrbigint53" bigint[],
1294712948
"arrbigint64" bigint[],
1294812949
"arrbool" boolean[],
12950+
"arrbytea" bytea[],
1294912951
"arrchar" char[],
1295012952
"arrcidr" "cidr"[],
1295112953
"arrdate" date[],
@@ -12991,6 +12993,7 @@ test('alltypes', async () => {
1299112993
bigserial53: 9007199254740991,
1299212994
bigserial64: 5044565289845416380n,
1299312995
bool: true,
12996+
bytea: Buffer.from('BYTES'),
1299412997
char: 'c',
1299512998
cidr: '2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128',
1299612999
inet: '192.168.0.1/24',
@@ -13037,6 +13040,7 @@ test('alltypes', async () => {
1303713040
arrbigint53: [9007199254740991],
1303813041
arrbigint64: [5044565289845416380n],
1303913042
arrbool: [true],
13043+
arrbytea: [Buffer.from('BYTES')],
1304013044
arrchar: ['c'],
1304113045
arrcidr: ['2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128'],
1304213046
arrinet: ['192.168.0.1/24'],
@@ -13105,6 +13109,7 @@ test('alltypes', async () => {
1310513109
bigint53: 9007199254740991,
1310613110
bigint64: 5044565289845416380n,
1310713111
bool: true,
13112+
bytea: Buffer.from('BYTES'),
1310813113
char: 'c',
1310913114
cidr: '2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128',
1311013115
date: new Date('2025-03-12T00:00:00.000Z'),
@@ -13139,6 +13144,7 @@ test('alltypes', async () => {
1313913144
arrbigint53: [9007199254740991],
1314013145
arrbigint64: [5044565289845416380n],
1314113146
arrbool: [true],
13147+
arrbytea: [Buffer.from('BYTES')],
1314213148
arrchar: ['c'],
1314313149
arrcidr: ['2001:4f8:3:ba:2e0:81ff:fe22:d1f1/128'],
1314413150
arrdate: [new Date('2025-03-12T00:00:00.000Z')],

0 commit comments

Comments
 (0)