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
14 changes: 13 additions & 1 deletion packages/orm/src/client/crud/dialects/base-dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,12 @@ export abstract class BaseCrudDialect<Schema extends SchemaDef> {

switch (key) {
case 'equals': {
clauses.push(this.eb(receiver, '=', buildArray(value)));
if (isTypeDef(this.schema, fieldDef.type) && !isPrimitiveTypeDef(this.schema, fieldDef.type)) {
// a typed JSON array is stored as a single JSON value, not a native list
clauses.push(this.buildJsonArrayEqualityFilter(fieldRef, _value));
} else {
clauses.push(this.eb(receiver, '=', buildArray(value)));
}
break;
}

Expand Down Expand Up @@ -888,6 +893,13 @@ export abstract class BaseCrudDialect<Schema extends SchemaDef> {
}
}

/**
* Builds an exact, element-order-sensitive equality filter for a typed JSON array field.
*/
protected buildJsonArrayEqualityFilter(lhs: Expression<any>, rhs: unknown) {
return this.buildJsonValueFilterClause(lhs, rhs);
}

protected buildJsonEqualityFilter(lhs: Expression<any>, rhs: unknown) {
return this.buildValueFilter(lhs, 'Json', rhs);
}
Expand Down
7 changes: 7 additions & 0 deletions packages/orm/src/client/crud/dialects/mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,13 @@ export class MySqlCrudDialect<Schema extends SchemaDef> extends LateralJoinDiale
]);
}

protected override buildJsonArrayEqualityFilter(lhs: Expression<any>, rhs: unknown) {
// bi-directional JSON_CONTAINS ignores element order and duplicates, use MySQL's native JSON
// comparison instead: arrays are equal only if they have the same length and equal values at
// each position, while objects are compared regardless of key order
return sql<SqlBool>`${lhs} = CAST(${sql.val(JSON.stringify(rhs))} AS JSON)`;
}

protected override buildJsonPathSelection(receiver: Expression<any>, path: string | undefined) {
if (path) {
return this.eb.fn('JSON_EXTRACT', [receiver, this.eb.val(path)]);
Expand Down
45 changes: 45 additions & 0 deletions tests/regression/test/issue-2843.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { createTestClient, generateTsSchema } from '@zenstackhq/testtools';
import { describe, expect, it } from 'vitest';

describe('Regression for issue #2843', () => {
const schema = `
type Entry {
label String
}

model Example {
id Int @id
entries Entry[] @json
}
`;

it('filters a typed JSON array field with equals', async () => {
// ZModel only accepts list fields for PostgreSQL, so generate the schema for PostgreSQL and run it
// against the configured test provider to also cover the other dialects' JSON array comparison
const { schema: schemaDef } = await generateTsSchema(schema, 'postgresql');
const db: any = await createTestClient(schemaDef);

await db.example.create({ data: { id: 1, entries: [{ label: 'x' }] } });
await db.example.create({ data: { id: 2, entries: [{ label: 'y' }, { label: 'x' }] } });
await db.example.create({ data: { id: 3, entries: [] } });
await db.example.create({ data: { id: 4, entries: [{ label: 'x' }, { label: 'y' }] } });
await db.example.create({ data: { id: 5, entries: [{ label: 'x' }, { label: 'x' }] } });

await expect(db.example.findMany({ where: { entries: { equals: [{ label: 'x' }] } } })).resolves.toEqual([
{ id: 1, entries: [{ label: 'x' }] },
]);
await expect(
db.example.findMany({ where: { entries: { equals: [{ label: 'y' }, { label: 'x' }] } } }),
).resolves.toEqual([{ id: 2, entries: [{ label: 'y' }, { label: 'x' }] }]);
await expect(db.example.findMany({ where: { entries: { equals: [] } } })).resolves.toEqual([
{ id: 3, entries: [] },
]);
await expect(
db.example.findMany({ where: { entries: { equals: [{ label: 'x' }, { label: 'y' }] } } }),
).resolves.toEqual([{ id: 4, entries: [{ label: 'x' }, { label: 'y' }] }]);
await expect(
db.example.findMany({ where: { entries: { equals: [{ label: 'x' }, { label: 'x' }] } } }),
).resolves.toEqual([{ id: 5, entries: [{ label: 'x' }, { label: 'x' }] }]);
await expect(db.example.findMany({ where: { entries: { equals: [{ label: 'z' }] } } })).resolves.toEqual([]);
});
});
Loading