Skip to content

Where

The where parameter can receive multiple inputs and all of them will result in the same query: Currently due to a limitation in D1, there is only support for ordered parameters (but named parameters is in progress)

Simple where

const qb = new D1QB(env.DB)

const fetched = await qb.fetchAll({
  tableName: 'employees',
  fields: '*',
  where: {
    conditions: 'active = true',
  },
})

Where with multiple conditions

const qb = new D1QB(env.DB)

const fetched = await qb.fetchAll({
  tableName: 'employees',
  fields: '*',
  where: {
    conditions: ['active = true', 'department = "HR"'],
  },
})

Simple where with parameters

const qb = new D1QB(env.DB)

const fetched = await qb.fetchAll({
  tableName: 'employees',
  fields: '*',
  where: {
    conditions: 'department = ?1',
    params: ['HR'],
  },
})

Where with advanced conditions

const qb = new D1QB(env.DB)

async function countEmployees(department?: string): number {
  const conditions = []

  if (department) conditions.push('department = ?1')

  const fetched = await qb.fetchAll({
    tableName: 'employees',
    fields: 'count(*) as count',
    where: {
      conditions: conditions,
      params: ['HR'],
    },
  })

  return fetched.results.count
}

const allEmployees = await countEmployees()

const hrEmployees = await countEmployees('HR')