Skip to content
VibORM
Esc
navigateopen⌘Jpreview
On this page

Array Filters

Filter operators for array scalars

Array scalars (s.string().array(), s.int().array(), etc.) work identically on every database.

Operators

Operator Description
has Array contains value
hasEvery Array contains all values
hasSome Array contains any value
isEmpty Array is empty
equals Exact array match (order matters)

has

Check if array contains a single value:

// Has "featured" tag
where: {
  tags: { has: "featured" },
}

// Has specific role
where: {
  roles: { has: "ADMIN" },
}

has: null never matches any row (SQL NULL = element is never true), matching Prisma semantics.

hasEvery

Check if array contains ALL specified values:

// Has both "tech" and "tutorial"
where: {
  tags: { hasEvery: ["tech", "tutorial"] },
}

// User has all required permissions
where: {
  permissions: { hasEvery: ["read", "write", "delete"] },
}

hasSome

Check if array contains ANY of the specified values:

// Has "tech" OR "science" OR "programming"
where: {
  tags: { hasSome: ["tech", "science", "programming"] },
}

// User has at least one admin permission
where: {
  permissions: { hasSome: ["admin", "superuser"] },
}

isEmpty

Check if array is empty:

// No tags
where: {
  tags: { isEmpty: true },
}

// Has at least one tag
where: {
  tags: { isEmpty: false },
}

Updating Arrays

// Add to array
await client.post.update({
  where: { id: "post_123" },
  data: {
    tags: { push: "new-tag" },
  },
});

// Add several values (each appended as its own element)
await client.post.update({
  where: { id: "post_123" },
  data: {
    tags: { push: ["tag-a", "tag-b"] },
  },
});

// Prepend instead of append
await client.post.update({
  where: { id: "post_123" },
  data: {
    tags: { unshift: "first-tag" },
  },
});

// Set entire array
await client.post.update({
  where: { id: "post_123" },
  data: {
    tags: { set: ["tag1", "tag2", "tag3"] },
  },
});

JSON Columns as an Alternative

If you need more flexibility than the array operators above (e.g. arrays of objects, or ad hoc JSON structures), you can still model a field as raw JSON and use the JSON filter operators instead:

// Schema
const post = s.model({
  id: s.string().id(),
  metadata: s.json().default([]),
});

// Query with JSON operators (array_contains only matches array values)
where: {
  metadata: {
    array_contains: "featured",
  },
}

Was this page helpful?