Skip to content

Instantly share code, notes, and snippets.

View ptpaterson's full-sized avatar
🤓

Paul Paterson ptpaterson

🤓
View GitHub Profile
@ptpaterson
ptpaterson / PowersOf2.fql.js
Last active January 6, 2022 22:28
Bit shifting in Fauna
// UDF to create an array of 2^0 to 2^62
// Fauna uses signed integers, so 2^64 isn't something we can work with
Query(
Lambda(
[],
Map(
[
0,
1,
2,
@ptpaterson
ptpaterson / readme.md
Created December 28, 2021 23:49
A Fauna FQL query to setup role-identifying functions for any database

See this original community forums post

How can we create functions that determine which Roles a Key or Token has?

  1. Create a new Collection.
  2. Create one Document for each Role that uniquely represents each role.
  3. Create the required Indexes to read the new Collection.
  4. Update each Role with privileges to read the indexes, but a predicate that lets it only read the one specific Document that represents it.

Then, when you try to read the Indexes only those documents that the roles have access to will be read. You can interpret which documents are read as the roles that you have. And we can create Functions that make it easy to use this stuff.

@ptpaterson
ptpaterson / function-up_nect.js
Last active June 30, 2020 16:18
Connect or Create-and-Connect, FaunaDB UDF body definition
{
name: "up_nect",
body: Query(
Lambda(
["commitSha", "filesMeta"],
Let(
{
createdGitRefDoc: Create(Collection("GitRef"), {
data: { commitSha: Var("commitSha") }
}),
@ptpaterson
ptpaterson / functionToLet.js
Last active May 1, 2020 21:15
Convert a function to a FaunaDB Let expression
const { query: q } = require('faunadb')
// fn-annotate
// https://www.npmjs.com/package/fn-annotate
const annotate = require('fn-annotate')
// converts a function into a faunaDB Let expression, but allows it to be used,
// as a regular function. This allows functions to be composed in JS land and
// turn out correctly in Fauna land.
const functionToLet = (fn) => (...args) => {
@ptpaterson
ptpaterson / nested-fql-manual.js
Last active December 26, 2021 15:09
Template for building deeply nested FQL queries for FaunaDB
const baseQuery = q.Paginate(q.Match(q.Index(indexName), terms));
// or
// const baseQuery = q.Paginate(q.Collection(collectionName))
const expandedQuery = q.Map(baseQuery, (ref) =>
q.Let(
{
instance: q.Get(q.Var('ref'))
},
{
@ptpaterson
ptpaterson / package-lock.json
Created March 15, 2020 22:58
minimist issue package-lock
{
"name": "netlify-faunadb-graphql-auth",
"version": "0.1.1",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"@apollo/client": {
"version": "3.0.0-beta.41",
"resolved": "https://registry.npmjs.org/@apollo/client/-/client-3.0.0-beta.41.tgz",
"integrity": "sha512-3FtiBP0gKDjmCIbPUn8GkBshA8vXUB9Zi+BOSEOwo5HDIEF0NXg19ov+m8SqaZEG+jU6p7w+dmHyRGkUxj9b4g==",

I need forward & backwards links between instances, properties on these "links", and they must be able to connect to many classes. I wish to query return the result for a GraphQL resolver, so I need to compose the results in one nice js object.

NOTE: lack of polymorphic types is why I am building my own GraphQL server. I need relationships to multiple types.

Would anyone have some guidance for how to do this the faunaDB way.

Example attempt at retrieving link information:

You may want to track a list of favorite toys and activities for your family (please don't judge test schema 😋).

@ptpaterson
ptpaterson / GraphQL to GraphQL+-.md
Last active January 19, 2018 04:46
GraphQL to GraphQL+/- conversion discussion

My data is generally pretty unstructured, but can be considered composed of well definable "traits". Facets have been an interesting point. So I am trying to figure out how to group edges to scalars, edges to nodes, and facets together in GraphQL types, suggesting types and queries like:

interface Node {
  uid: String!
  someBaseProperty
}

type User implements Node {
  uid: String!

someBaseProperty: Int