useMany()

Retrieves entities from the table. If a filter is supplied, blinkDB will only return matching items - if not given, all items are returned.

const Component = () => {
  const userTable = useTable((model: Model) => model.users);
  // Retrieve all users named Bob
  const { data: bobUsers } = await useMany(userTable, {
    where: {
      name: "Bob"
    }
  });
  ...
}
Parameter Description
table The table created by createTable() .
filter Optional. If given, items returned will match the filter. See filters.

Query State

useMany() is asynchronous, which means the query doesn’t return your values immediately. Instead, it returns an object containing the current query state, the data (if present), and zero or one error (if your query threw an error).

type QueryResult<T> {
  // The items if successful,
  // or `undefined` if the query is not done yet
  data: T[]|undefined;
  // An error object if the query has produced an error
  error: Error|undefined;
  // The current state of the query
  state: "loading"|"done"|"error";
}

You can use the loading state to display loading indicators while you wait for your query to complete:

const Component = () => {
  const userTable = useTable((model: Model) => model.users);
  const { data: users, state, error } = await useMany(userTable);

  return (
    <>
      {state === "error" && <span>An error occurred: {error}</span>}
      {state === "loading" && <span>Loading...</span>}
      {state === "done" && users.map(u => (
        <div>{u.name}</div>
      ))}
    </>
  );
}

Retrieve all items

If no second parameter is supplied, useMany() will return all items in the table.

// Retrieve all users
const { data: users } = await useMany(userTable);

Retrieve items that match a filter

If a filter is provided as the second parameter, useMany() returns all items that match the filter.

// Retrieve all users named Bob
const { data: bobUsers } = await useMany(userTable, {
  where: {
    name: "Bob"
  }
});
blinkDB © 2023