fetchOne
fetchOne
is similar to fetch()
but will return only the first entity in the result set or null
if the result set is empty. For example:
await client.insert('employees', { id: 'Fry', name: 'Philip J. Fry' });
await client.insert('employees', { id: 'Leela', name: 'Turanga Leela' });
const query = client.query('employees').build();
const result = await client.fetchOne(query, options); // { name: 'Philip J. Fry' }
const queryEmpty = client
.query('employees')
.where('name', '=', 'Bender')
.build();
const resultEmpty = await client.fetchOne(queryEmpty); // null
This is a convenient shorthand for using the limit parameter .limit(1)
and extracting the result from the result set.