Query a Collection
Now that we have a collection defined with some data, we can run some queries! To run queries against a collection, first connect to CipherStash and load a collection.

Exact Matches

The simplest kind of query is an Exact match.
To run a query, we use the all function. Let's query the collection for full-time employees:
TypeScript
JavaScript
1
let results = await employees.all(
2
employee => employee.employment.eq("full-time")
3
)
Copied!
1
let results = await employees.all(
2
employee => employee.employment.eq("full-time")
3
)
Copied!

Partial String Matches

To perform a partial string match on a field we can use the match function from the Match index type.
TypeScript
JavaScript
1
let results = await employees.all(
2
employee => employee.name.match("Ada")
3
)
Copied!
1
let results = await employees.all(
2
employee => employee.name.match("Ada")
3
)
Copied!

Range Queries

Fields that were indexed using the Range index type support range queries using lt, gt, lte, gte, eq and between.
TypeScript
JavaScript
1
let results1 = await employees.all(
2
employee => employee.startDate.gt(new Date(2007, 2, 3))
3
)
4
5
let results2 = await employees.all(
6
employee => employee.startDate.between(
7
new Date(2007, 1, 1),
8
new Date(2015, 1, 1)
9
)
10
)
Copied!
1
let results1 = await employees.all(
2
employee => employee.startDate.gt(new Date(2007, 2, 3))
3
)
4
5
let results2 = await employees.all(
6
employee => employee.startDate.between(
7
new Date(2007, 1, 1),
8
new Date(2015, 1, 1)
9
)
10
)
Copied!

Combining Queries

You can query on multiple fields at once but specifying conditions for each field. For example for exact matches on 2 fields, do:
TypeScript
JavaScript
1
import { all } from '@cipherstash/stashjs'
2
3
let results = await employees.all(
4
employee => all(
5
employee.name.match("Ada"),
6
employee.email.eq("[email protected]")
7
)
8
)
Copied!
1
import { all } from '@cipherstash/stashjs'
2
3
let results = await employees.all(
4
employee => all(
5
employee.name.match("Ada"),
6
employee.email.eq("[email protected]")
7
)
8
)
Copied!
Combining constraints applies a logical "AND" to the query. Logical "OR" will be available in a forthcoming version.

The QueryResult Object

A successfully executed query returns a QueryResult object that has this type:
TypeScript
1
type QueryResult<R> = {
2
documents: Array<R>,
3
aggregates: Array<AggregateResult>
4
}
Copied!
The documents field is an array of records from the queried collection.

Aggregation

CipherStash currently supports one aggregation option: count.
The following example will retrieve the number of documents in a collection that satisfy a query:
TypeScript
JavaScript
1
let { aggregates } = await employees.all(
2
employee => employee.salary.gt(100000n),
3
{ aggregation: [{ aggregate: 'count' }] }
4
)
5
6
console.log(`Number of employees with salary > 100000 ${aggregates[0].value}`)
Copied!
1
let { aggregates } = await employees.all(
2
employee => employee.salary.gt(100000n),
3
{ aggregation: [{ aggregate: 'count' }] }
4
)
5
6
console.log(`Number of employees with salary > 100000 ${aggregates[0].value}`)
Copied!

Pagination

By default, the all function limits the response to 20 records. If we want return up to say, 50 records, we can call limit on a query object:
TypeScript
JavaScript
1
let { aggregates } = await employees.all(
2
employee => employee.salary.gt(100000n),
3
{ limit: 50 }
4
)
Copied!
1
let { aggregates } = await employees.all(
2
employee => employee.salary.gt(100000n),
3
{ limit: 50 }
4
)
Copied!
Last modified 2mo ago