> ## Documentation Index
> Fetch the complete documentation index at: https://docs.bijection.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Interface: SelectableQueryInitializer<TableInfo>

> The entry point ctx.db.query(...) returns: upstream's initializer, with every query it produces still projectable.

[server](/api/modules/server).SelectableQueryInitializer

The entry point `ctx.db.query(...)` returns: upstream's initializer, with
every query it produces still projectable.

## Type parameters

| Name | Type |
| :- | :- |
| `TableInfo` | extends [`GenericTableInfo`](/api/modules/server#generictableinfo) |

## Hierarchy

* [`QueryInitializer`](/api/interfaces/server.QueryInitializer)\<`TableInfo`>

* [`SelectableQuery`](/api/interfaces/server.SelectableQuery)\<`TableInfo`>

  ↳ **`SelectableQueryInitializer`**

## Methods

### filter

▸ **filter**(`predicate`): [`SelectableQueryInitializer`](/api/interfaces/server.SelectableQueryInitializer)\<`TableInfo`>

Filter the query output, returning only the values for which `predicate` evaluates to true.

**Important:** Prefer using `.withIndex()` over `.filter()` whenever
possible. Filters scan all documents matched so far and discard non-matches,
while indexes efficiently skip non-matching documents. Define an index in
your schema for fields you filter on frequently.

#### Parameters

| Name | Type | Description |
| :- | :- | :- |
| `predicate` | (`q`: [`FilterBuilder`](/api/interfaces/server.FilterBuilder)\<`TableInfo`>) => [`ExpressionOrValue`](/api/modules/server#expressionorvalue)\<`boolean`> | An [Expression](/api/classes/server.Expression) constructed with the supplied [FilterBuilder](/api/interfaces/server.FilterBuilder) that specifies which documents to keep. |

#### Returns

[`SelectableQueryInitializer`](/api/interfaces/server.SelectableQueryInitializer)\<`TableInfo`>

* A new [OrderedQuery](/api/interfaces/server.OrderedQuery) with the given filter predicate applied.

#### Inherited from

[SelectableQuery](/api/interfaces/server.SelectableQuery).[filter](/api/interfaces/server.SelectableQuery#filter)

***

### paginate

▸ **paginate**(`paginationOpts`): `Promise`\<[`PaginationResult`](/api/interfaces/server.PaginationResult)\<[`DocumentByInfo`](/api/modules/server#documentbyinfo)\<`TableInfo`>>>

Load a page of `n` results and obtain a [Cursor](/api/modules/server#cursor) for loading more.

Note: If this is called from a reactive query function the number of
results may not match `paginationOpts.numItems`!

`paginationOpts.numItems` is only an initial value. After the first invocation,
`paginate` will return all items in the original query range. This ensures
that all pages will remain adjacent and non-overlapping.

#### Parameters

| Name | Type | Description |
| :- | :- | :- |
| `paginationOpts` | [`PaginationOptions`](/api/interfaces/server.PaginationOptions) | A [PaginationOptions](/api/interfaces/server.PaginationOptions) object containing the number of items to load and the cursor to start at. |

#### Returns

`Promise`\<[`PaginationResult`](/api/interfaces/server.PaginationResult)\<[`DocumentByInfo`](/api/modules/server#documentbyinfo)\<`TableInfo`>>>

A [PaginationResult](/api/interfaces/server.PaginationResult) containing the page of results and a
cursor to continue paginating.

#### Inherited from

[SelectableQuery](/api/interfaces/server.SelectableQuery).[paginate](/api/interfaces/server.SelectableQuery#paginate)

***

### collect

▸ **collect**(): `Promise`\<[`DocumentByInfo`](/api/modules/server#documentbyinfo)\<`TableInfo`>\[]>

Execute the query and return all of the results as an array.

**Warning:** This loads every matching document into memory. If the result
set can grow unbounded as your database grows, `.collect()` will eventually
cause performance problems or hit limits. Only use `.collect()` when the
result set is tightly bounded (e.g., a known small number of items).

Prefer `.first()`, `.unique()`, `.take(n)`, or `.paginate()` when the
result set may be large or unbounded. For processing many results without
loading all into memory, use the `Query` as an `AsyncIterable` with
`for await...of`.

#### Returns

`Promise`\<[`DocumentByInfo`](/api/modules/server#documentbyinfo)\<`TableInfo`>\[]>

* An array of all of the query's results.

#### Inherited from

[SelectableQuery](/api/interfaces/server.SelectableQuery).[collect](/api/interfaces/server.SelectableQuery#collect)

***

### take

▸ **take**(`n`): `Promise`\<[`DocumentByInfo`](/api/modules/server#documentbyinfo)\<`TableInfo`>\[]>

Execute the query and return the first `n` results.

#### Parameters

| Name | Type | Description |
| :- | :- | :- |
| `n` | `number` | The number of items to take. |

#### Returns

`Promise`\<[`DocumentByInfo`](/api/modules/server#documentbyinfo)\<`TableInfo`>\[]>

* An array of the first `n` results of the query (or less if the
  query doesn't have `n` results).

#### Inherited from

[SelectableQuery](/api/interfaces/server.SelectableQuery).[take](/api/interfaces/server.SelectableQuery#take)

***

### first

▸ **first**(): `Promise`\<`null` | [`DocumentByInfo`](/api/modules/server#documentbyinfo)\<`TableInfo`>>

Execute the query and return the first result if there is one.

#### Returns

`Promise`\<`null` | [`DocumentByInfo`](/api/modules/server#documentbyinfo)\<`TableInfo`>>

* The first value of the query or `null` if the query returned no results.

#### Inherited from

[SelectableQuery](/api/interfaces/server.SelectableQuery).[first](/api/interfaces/server.SelectableQuery#first)

***

### unique

▸ **unique**(): `Promise`\<`null` | [`DocumentByInfo`](/api/modules/server#documentbyinfo)\<`TableInfo`>>

Execute the query and return the singular result if there is one.

Use this when you expect exactly zero or one result, for example when
querying by a unique field. If the query matches more than one document,
this will throw an error.

**`Example`**

```typescript theme={"theme":{"light":"github-light-default","dark":"github-dark-default"}}
const user = await ctx.db
  .query("users")
  .withIndex("by_email", (q) => q.eq("email", "alice@example.com"))
  .unique();
```

**`Throws`**

Will throw an error if the query returns more than one result.

#### Returns

`Promise`\<`null` | [`DocumentByInfo`](/api/modules/server#documentbyinfo)\<`TableInfo`>>

* The single result returned from the query or null if none exists.

#### Inherited from

[SelectableQuery](/api/interfaces/server.SelectableQuery).[unique](/api/interfaces/server.SelectableQuery#unique)

***

### select

▸ **select**\<`Fields`>(`fields`): [`ProjectedQuery`](/api/interfaces/server.ProjectedQuery)\<`TableInfo`, `Fields`>

See [select](/api/interfaces/server.SelectableOrderedQuery#select).

#### Type parameters

| Name | Type |
| :- | :- |
| `Fields` | extends `string` |

#### Parameters

| Name | Type |
| :- | :- |
| `fields` | readonly `Fields`\[] |

#### Returns

[`ProjectedQuery`](/api/interfaces/server.ProjectedQuery)\<`TableInfo`, `Fields`>

#### Inherited from

[SelectableQuery](/api/interfaces/server.SelectableQuery).[select](/api/interfaces/server.SelectableQuery#select)

***

### order

▸ **order**(`order`): [`SelectableOrderedQuery`](/api/interfaces/server.SelectableOrderedQuery)\<`TableInfo`>

Define the order of the query output.

Use `"asc"` for an ascending order and `"desc"` for a descending order. If not specified, the order defaults to ascending.

#### Parameters

| Name | Type | Description |
| :- | :- | :- |
| `order` | `"asc"` \| `"desc"` | The order to return results in. |

#### Returns

[`SelectableOrderedQuery`](/api/interfaces/server.SelectableOrderedQuery)\<`TableInfo`>

#### Overrides

[SelectableQuery](/api/interfaces/server.SelectableQuery).[order](/api/interfaces/server.SelectableQuery#order)

***

### fullTableScan

▸ **fullTableScan**(): [`SelectableQuery`](/api/interfaces/server.SelectableQuery)\<`TableInfo`>

Query by reading all of the values out of this table.

This query's cost is relative to the size of the entire table, so this
should only be used on tables that will stay very small (say between a few
hundred and a few thousand documents) and are updated infrequently.

#### Returns

[`SelectableQuery`](/api/interfaces/server.SelectableQuery)\<`TableInfo`>

* The [Query](/api/interfaces/server.Query) that iterates over every document of the table.

#### Overrides

[QueryInitializer](/api/interfaces/server.QueryInitializer).[fullTableScan](/api/interfaces/server.QueryInitializer#fulltablescan)

***

### withIndex

▸ **withIndex**\<`IndexName`>(`indexName`, `indexRange?`): [`SelectableQuery`](/api/interfaces/server.SelectableQuery)\<`TableInfo`>

Query by reading documents from an index on this table.

This query's cost is relative to the number of documents that match the
index range expression.

Results will be returned in index order.

To learn about indexes, see [Indexes](/database/reading-data/indexes/indexes).

#### Type parameters

| Name | Type |
| :- | :- |
| `IndexName` | extends `string` \| `number` \| `symbol` |

#### Parameters

| Name | Type | Description |
| :- | :- | :- |
| `indexName` | `IndexName` | The name of the index to query. |
| `indexRange?` | (`q`: [`IndexRangeBuilder`](/api/interfaces/server.IndexRangeBuilder)\<[`DocumentByInfo`](/api/modules/server#documentbyinfo)\<`TableInfo`>, [`NamedIndex`](/api/modules/server#namedindex)\<`TableInfo`, `IndexName`>, `0`>) => [`IndexRange`](/api/classes/server.IndexRange) | An optional index range constructed with the supplied [IndexRangeBuilder](/api/interfaces/server.IndexRangeBuilder). An index range is a description of which documents Bijection should consider when running the query. If no index range is present, the query will consider all documents in the index. |

#### Returns

[`SelectableQuery`](/api/interfaces/server.SelectableQuery)\<`TableInfo`>

* The query that yields documents in the index.

#### Overrides

[QueryInitializer](/api/interfaces/server.QueryInitializer).[withIndex](/api/interfaces/server.QueryInitializer#withindex)

***

### withSearchIndex

▸ **withSearchIndex**\<`IndexName`>(`indexName`, `searchFilter`): [`SelectableOrderedQuery`](/api/interfaces/server.SelectableOrderedQuery)\<`TableInfo`>

Query by running a full text search against a search index.

Search queries must always search for some text within the index's
`searchField`. This query can optionally add equality filters for any
`filterFields` specified in the index.

Documents will be returned in relevance order based on how well they
match the search text.

To learn about full text search, see [Indexes](/search/text-search).

#### Type parameters

| Name | Type |
| :- | :- |
| `IndexName` | extends `string` \| `number` \| `symbol` |

#### Parameters

| Name | Type | Description |
| :- | :- | :- |
| `indexName` | `IndexName` | The name of the search index to query. |
| `searchFilter` | (`q`: [`SearchFilterBuilder`](/api/interfaces/server.SearchFilterBuilder)\<[`DocumentByInfo`](/api/modules/server#documentbyinfo)\<`TableInfo`>, [`NamedSearchIndex`](/api/modules/server#namedsearchindex)\<`TableInfo`, `IndexName`>>) => [`SearchFilter`](/api/classes/server.SearchFilter) | A search filter expression constructed with the supplied [SearchFilterBuilder](/api/interfaces/server.SearchFilterBuilder). This defines the full text search to run along with equality filtering to run within the search index. |

#### Returns

[`SelectableOrderedQuery`](/api/interfaces/server.SelectableOrderedQuery)\<`TableInfo`>

* A query that searches for matching documents, returning them
  in relevancy order.

#### Overrides

[QueryInitializer](/api/interfaces/server.QueryInitializer).[withSearchIndex](/api/interfaces/server.QueryInitializer#withsearchindex)

***

### \[asyncIterator]

▸ **\[asyncIterator]**(): `AsyncIterator`\<[`DocumentByInfo`](/api/modules/server#documentbyinfo)\<`TableInfo`>, `any`, `undefined`>

#### Returns

`AsyncIterator`\<[`DocumentByInfo`](/api/modules/server#documentbyinfo)\<`TableInfo`>, `any`, `undefined`>

#### Inherited from

[SelectableQuery](/api/interfaces/server.SelectableQuery).[\[asyncIterator\]](/api/interfaces/server.SelectableQuery#\[asynciterator])
