Skip to main content

QueryItems

Store.query()

Create a query request.

import { DynamoDBClient } from '@aws-cdk/client-dynamodb'
import { Store } from '@declanprice/dynostore'

const store = new Store('customers', new new DynamoDBClient())

const query = store.query<CustomerItem>()

pk(path, value)

Provide the partition item key to be used.

query.pk('id', '123');

sk(condition)

Provide an optional sort key condition, sort key conditions supports the following conditions.

query.sk(eq('sk', 'Customer'))
query.sk(lt('sk', 2))
query.sk(lte('sk', 2))
query.sk(gt('sk', 2))
query.sk(gte('sk', 2))
query.sk(beginsWith('sk', 'Cus'))
query.sk(between('sk', 1, 2))

using(index)

Use a specific index.

query.using('indexName')

filter(...conditions)

Provide any combination of supported dynamodb conditions, multiple conditions must be seperated by either and(), or() conditions otherwise an error will be thrown.

query.filter(
group(
gt('age', 2),
and(),
lt('age', 30)
)
or(),
eq('age', 33)
)

project(projectionString)

Project specific fields to be returned.

query.project('id,fistName,lastName')

limit(count)

Restrict the number of items to be returned.

query.limit(100)

startAt(key)

Provide a starting key for pagination purposes, if the previous query returns a valid lastKey item you can pass it to the next query.

query.startAt({id: '123'})

sort('asc' | 'desc')

Provide an optional sort direction in ascending or descending order, the default is ascending.

query.sort('asc')

query.sort('desc')

exec()

Execute the query.

const response: QueryResponse<CustomerItem>[] = await query.exec()

const { items, lastKey } = response

A QueryResponse contains the following properties:

SyntaxDescription
itemsan array of unmarshalled items, when no items are found the array will be empty
lastKeyif more items are avaiable to query then lastKey will be the item key to use in the next query