Skip to content

Instantly share code, notes, and snippets.

@ndreckshage
Last active January 28, 2022 05:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ndreckshage/5e67e8f798f3c9985d0b47319838b5dd to your computer and use it in GitHub Desktop.
Save ndreckshage/5e67e8f798f3c9985d0b47319838b5dd to your computer and use it in GitHub Desktop.
fql example
const result = await query(Let({
// get author document from index by key. assign authorDoc as variable
authorDoc: Get(Match(Index("unique_authors_by_key"), "haruki-murakami"))
}, Let({
// select book references from author document. assign bookRefs variable
bookRefs: Select(["data", "bookRefs"], Var("authorDoc"))
}, Let({}, {
// start forming the return result here
id: Select(["ref", "id"], Var("authorDoc")),
authorName: Select(["data", "name"], Var('authorDoc')),
// map over the book references and get the document. assign as bookDoc var
books: Map(Var('bookRefs'), Lambda('bookRef', Let({
bookDoc: Get(Var('bookRef'))
}, {
// format the result
id: Select(["ref", "id"], Var('bookDoc')),
title: Select(["data", "title"], Var('bookDoc')),
// also map over a books category references, to get the category name
categories: Map(
Select(["data", "categoryRefs"], Var("bookDoc")),
Lambda('categoryRef', Let({
categoryDoc: Get(Var('categoryRef'))
}, {
id: Select(["ref", "id"], Var("categoryDoc")),
name: Select(["data", "name"], Var("categoryDoc"))
})
))
})))
})
)))
expect(result).toBe({
id: "320369985204518980",
authorName: "Haruki Murakami",
books: [{
id: "320370014948425796",
title: "Kafka on the Shore",
categories: [{
id: "319373242721632324",
name: "Fiction / Literary"
}]
}, {
id: "321288786678906947",
title: "1Q84",
categories: [{
id: "319373242721632324",
name: "Fiction / Literary"
}, {
id: "320369985203470404",
name: "Fiction / Magical Realism"
}, {
id: "321288786618089539",
name: "Fiction / Dystopian"
}]
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment