Skip to content

Returning

The returning field allows you to return data after inserts/updates have been performed while in the same query

Returning a single field

const qb = new D1QB(env.DB)

const inserted = await qb.insert({
  tableName: 'employees',
  data: {
    name: 'Joe',
    role: 'manager',
    department: 'store',
  },
  returning: 'id',
})

console.log(`Joe just got the employee id: ${inserted.results.id}`)

Returning all fields

const qb = new D1QB(env.DB)

const inserted = await qb.insert({
  tableName: 'employees',
  data: {
    name: 'Joe',
    role: 'manager',
    department: 'store',
  },
  returning: '*',
})

Returning multiple fields

const qb = new D1QB(env.DB)

const inserted = await qb.insert({
  tableName: 'employees',
  data: {
    name: 'Joe',
    role: 'manager',
    department: 'store',
  },
  returning: ['id', 'salary'],
})