BlinkDB Logo blinkDB

one()

Retrieves one entity from the table.

This is similar to first() in the sense that it retrieves only the first matching entity, but:

  • it throws an exception if no entity was found
  • it throws an exception if more than one entity matched the filter

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"
  }
});
ParameterDescription
tableThe table created by createTable() .
filterOrIdEither a filter or an id. The item returned will be the first one to match. See filters.

Retrieve by primary id

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");

Retrieve by filter

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"
  }
});