Skip to content

arrayField

const arrayField: (input) => KnexFieldSpec;

Defined in: filters/knex.ts:129

Allow-list a public field as an array filter over a column named by a column string ("tags" or "posts.tags"). The SQL depends on dialect: a native array with @>/<@/&& on Postgres, a JSON column with JSON_CONTAINS/JSON_OVERLAPS on MySQL, and json_each(…) subqueries on SQLite.

Parameter Type
input { column: string; }
input.column string

KnexFieldSpec

import type { ColumnFilter } from "@xtandard/lib/filters";
import { arrayField, buildWhereSql } from "@xtandard/lib/filters/knex";
const spec = { tags: arrayField({ column: "posts.tags" }) };
const filters: ColumnFilter[] = [
{ field: "tags", filter: { kind: "array", operator: "arrayOverlaps", values: ["ts", "js"] } },
];
buildWhereSql({ spec, filters });
// → { sql: '"posts"."tags" && ?', bindings: [["ts", "js"]] }
buildWhereSql({ spec, filters, dialect: "mysql" });
// → { sql: "JSON_OVERLAPS(`posts`.`tags`, ?)", bindings: ['["ts","js"]'] }