Skip to content
This repository was archived by the owner on Mar 1, 2026. It is now read-only.
Merged
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
5 changes: 4 additions & 1 deletion packages/orm/src/client/crud/dialects/postgresql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,10 @@ export class PostgresCrudDialect<Schema extends SchemaDef> extends BaseCrudDiale
value: unknown,
) {
return match(operation)
.with('array_contains', () => sql<SqlBool>`${lhs} @> ${sql.val(JSON.stringify([value]))}::jsonb`)
.with('array_contains', () => {
const v = Array.isArray(value) ? value : [value];
return sql<SqlBool>`${lhs} @> ${sql.val(JSON.stringify(v))}::jsonb`;
})
.with('array_starts_with', () =>
this.eb(
this.eb.fn('jsonb_extract_path', [lhs, this.eb.val('0')]),
Expand Down
14 changes: 11 additions & 3 deletions packages/orm/src/client/crud/dialects/sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { AnyNullClass, DbNullClass, JsonNullClass } from '../../../common-types'
import type { BuiltinType, FieldDef, GetModels, SchemaDef } from '../../../schema';
import { DELEGATE_JOINED_FIELD_PREFIX } from '../../constants';
import type { FindArgs } from '../../crud-types';
import { createInternalError } from '../../errors';
import { createInternalError, createNotSupportedError } from '../../errors';
import {
getDelegateDescendantModels,
getManyToManyRelation,
Expand Down Expand Up @@ -374,7 +374,15 @@ export class SqliteCrudDialect<Schema extends SchemaDef> extends BaseCrudDialect
value: unknown,
) {
return match(operation)
.with('array_contains', () => sql<any>`EXISTS (SELECT 1 FROM jsonb_each(${lhs}) WHERE value = ${value})`)
.with('array_contains', () => {
if (Array.isArray(value)) {
throw createNotSupportedError(
'SQLite "array_contains" only supports checking for a single value, not an array of values',
);
} else {
return sql<any>`EXISTS (SELECT 1 FROM jsonb_each(${lhs}) WHERE value = ${value})`;
}
})
Comment thread
ymc9 marked this conversation as resolved.
.with('array_starts_with', () =>
this.eb(this.eb.fn('json_extract', [lhs, this.eb.val('$[0]')]), '=', value),
)
Expand All @@ -390,7 +398,7 @@ export class SqliteCrudDialect<Schema extends SchemaDef> extends BaseCrudDialect
) {
return this.eb.exists(
this.eb
.selectFrom(this.eb.fn('json_each', [receiver]).as('$items'))
.selectFrom(this.eb.fn('jsonb_each', [receiver]).as('$items'))
.select(this.eb.lit(1).as('$t'))
.where(buildFilter(this.eb.ref('$items.value'))),
);
Expand Down
13 changes: 13 additions & 0 deletions tests/e2e/orm/client-api/json-filter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,19 @@ describe('Json filter tests', () => {
}),
).resolves.toMatchObject({ data: { tags: ['typescript', 'react', 'node'] } });

if ((db.$schema.provider.type as any) === 'postgresql') {
await expect(
db.foo.findFirst({
where: {
data: {
path: '$.tags',
array_contains: ['react'],
},
},
}),
).resolves.toMatchObject({ data: { tags: ['typescript', 'react', 'node'] } });
}
Comment thread
ymc9 marked this conversation as resolved.

await expect(
db.foo.findFirst({
where: {
Expand Down