Skip to content

Instantly share code, notes, and snippets.

@coderdan
Created February 17, 2022 04:06
Show Gist options
  • Save coderdan/30a60fb017c77ee6b342d61674234e09 to your computer and use it in GitHub Desktop.
Save coderdan/30a60fb017c77ee6b342d61674234e09 to your computer and use it in GitHub Desktop.
import { HasID, QueryResult, Stash } from "@cipherstash/stashjs"
import { movieSchema, Movie } from "./example-schema";
function displayResults(result: QueryResult<Movie & HasID>, name: string) {
result.documents.forEach((movie: Movie) => {
console.log(movie)
})
// TODO: print count
console.log("--------------------------------------------------")
console.log(name)
console.log(`Executed in ${result.took} secs`)
console.log("--------------------------------------------------")
}
async function queryCollection() {
try {
const stash = await Stash.connect()
const movies = await stash.loadCollection(movieSchema)
let queryResult = await movies.query(movie => movie.title.match("White Youth"))
displayResults(queryResult, "Match: 'The Matrix'")
queryResult = await movies.query(movie => movie.year.gt(1923), { limit: 50, order: [{byIndex: "year", direction: "ASC"}] })
displayResults(queryResult, "Range: After 1923")
/*queryResult = await movies.query({ limit: 20, offset: 40, order: [{byIndex: "year", direction: "DESC"}] })
displayResults(queryResult, "All: Offset 40, Order by year DESC")*/
//queryResult = await movies.query({})
//displayResults(queryResult, "All")
} catch (err) {
console.error(err)
console.error(`Could not query collection! Reason: ${JSON.stringify(err)}`)
}
}
queryCollection()
~
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment