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');
const result = await client.fetchOne(query, options); // { name: 'Philip J. Fry' }
const queryEmpty = client.query('employees').Where('name', '=', 'Bender');
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.