Retrieves one entity from the table.
This is similar to first()
in the sense that it retrieves only the first matching entity, but:
This makes it perfect for retrieving entities by their primary id, which are unique by default.
let alice: User;
// Retrieve directly by id
alice = await one(userTable, "alice-uuid");
// Retrieve with filter (equivalent to the line above!)
alice = await one(userTable, {
where: {
id: "alice-uuid"
}
});
Parameter | Description |
---|---|
table
|
The table created by createTable() .
|
filterOrId
| Either a filter or an id. The item returned will be the first one to match. See filters. |
In case an id is given as the second parameter, one()
will return either the entity with a matching primary id,
or throw as detailed above.
const alice = await one(userTable, "alice-uuid");
If the second parameter is a filter, one()
returns the item that matches the filter,
or throws if no or more than one item is available.
const alice = await one(userTable, {
where: {
id: "alice-uuid"
}
});