queryDb
const
queryDb: {<TResultSchema
,TResult
>(queryInput
,options?
):LiveQueryDef
<TResult
>; <TResultSchema
,TResult
>(queryInput
,options?
):LiveQueryDef
<TResult
>; }
Defined in: packages/@livestore/livestore/src/live-queries/db-query.ts:74
NOTE queryDb
is only supposed to read data. Don’t use it to insert/update/delete data but use events instead.
When using contextual data when constructing the query, please make sure to include it in the deps
option.
Call Signature
Section titled “Call Signature”<
TResultSchema
,TResult
>(queryInput
,options?
):LiveQueryDef
<TResult
>
Type Parameters
Section titled “Type Parameters”TResultSchema
Section titled “TResultSchema”TResultSchema
TResult
Section titled “TResult”TResult
= TResultSchema
Parameters
Section titled “Parameters”queryInput
Section titled “queryInput”QueryInputRaw
<TResultSchema
, readonly any
[]> | QueryBuilder
<TResultSchema
, any
, any
>
options?
Section titled “options?”DepKey
label?
Section titled “label?”string
Used for debugging / devtools
(rows
) => TResult
Returns
Section titled “Returns”LiveQueryDef
<TResult
>
Call Signature
Section titled “Call Signature”<
TResultSchema
,TResult
>(queryInput
,options?
):LiveQueryDef
<TResult
>
Type Parameters
Section titled “Type Parameters”TResultSchema
Section titled “TResultSchema”TResultSchema
TResult
Section titled “TResult”TResult
= TResultSchema
Parameters
Section titled “Parameters”queryInput
Section titled “queryInput”(get
) => QueryInputRaw
<TResultSchema
, readonly any
[]> | (get
) => QueryBuilder
<TResultSchema
, any
, any
>
options?
Section titled “options?”DepKey
label?
Section titled “label?”string
Used for debugging / devtools
(rows
) => TResult
Returns
Section titled “Returns”LiveQueryDef
<TResult
>
Examples
Section titled “Examples”const todos$ = queryDb(tables.todos.where({ complete: true }))
// Group-by raw SQL queryconst colorCounts$ = queryDb({ query: sql`SELECT color, COUNT(*) as count FROM todos WHERE complete = ? GROUP BY color`, schema: Schema.Array(Schema.Struct({ color: Schema.String, count: Schema.Number, })), bindValues: [1],})
// Using contextual data when constructing the queryconst makeFilteredQuery = (filter: string) => queryDb(tables.todos.where({ title: { op: 'like', value: filter } }), { deps: [filter] })
const filteredTodos$ = makeFilteredQuery('buy coffee')