How to use faunadb - 10 common examples

To help you get started, we’ve selected a few faunadb examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github laardee / serverless-authentication-boilerplate / test-token / handler.js View on Github external
const userClassName = process.env.USERS_CLASS_NAME || 'users' // shared with authentication service
const faunadb = require('faunadb')

const q = faunadb.query

const createResponse = (statusCode, payload) => ({
  statusCode,
  headers: {
    'Access-Control-Allow-Origin': '*',
    'Access-Control-Allow-Credentials': true
  },
  body: JSON.stringify(payload)
})

module.exports.test = (event, context, cb) => {
  console.log('event', event)
  const authData = event.requestContext.authorizer
  if (authData.principalId) {
    if (authData.faunadb) {
      const client = new faunadb.Client({ secret: authData.faunadb })
github DavidWells / netlify-functions-workshop / lessons-code-complete / core-concepts / 8-using-addons / functions / todos-read.js View on Github external
/* Import faunaDB sdk */
const faunadb = require('faunadb')
const getId = require('./utils/getId')

const q = faunadb.query
const client = new faunadb.Client({
  secret: process.env.FAUNADB_SERVER_SECRET
})

exports.handler = (event, context, callback) => {
  const id = getId(event.path)
  console.log(`Function 'todo-read' invoked. Read id: ${id}`)
  return client.query(q.Get(q.Ref(`classes/todos/${id}`)))
    .then((response) => {
      console.log('success', response)
      return callback(null, {
        statusCode: 200,
        body: JSON.stringify(response)
      })
    }).catch((error) => {
      console.log('error', error)
github netlify / netlify-dev-plugin / src / functions-templates / js / fauna-crud / create-schema.js View on Github external
#!/usr/bin/env node

/* bootstrap database in your FaunaDB account - use with `netlify dev:exec ` */
const faunadb = require("faunadb");

const q = faunadb.query;

function createFaunaDB() {
  if (!process.env.FAUNADB_SERVER_SECRET) {
    console.log("No FAUNADB_SERVER_SECRET in environment, skipping DB setup");
  }
  console.log("Create the database!");
  const client = new faunadb.Client({
    secret: process.env.FAUNADB_SERVER_SECRET
  });

  /* Based on your requirements, change the schema here */
  return client
    .query(q.Create(q.Ref("classes"), { name: "items" }))
    .then(() => {
      console.log("Created items class");
      return client.query(
github netlify / netlify-dev-plugin / src / functions-templates / js / fauna-crud / update.js View on Github external
/* Import faunaDB sdk */
const faunadb = require("faunadb");

const q = faunadb.query;
const client = new faunadb.Client({
  secret: process.env.FAUNADB_SERVER_SECRET
});

exports.handler = async (event, context) => {
  const data = JSON.parse(event.body);
  const id = event.id;
  console.log(`Function 'update' invoked. update id: ${id}`);
  return client
    .query(q.Update(q.Ref(`classes/items/${id}`), { data }))
    .then(response => {
      console.log("success", response);
      return {
        statusCode: 200,
        body: JSON.stringify(response)
      };
github netlify / netlify-dev-plugin / src / functions-templates / js / fauna-crud / read-all.js View on Github external
/* Import faunaDB sdk */
const faunadb = require("faunadb");

const q = faunadb.query;
const client = new faunadb.Client({
  secret: process.env.FAUNADB_SERVER_SECRET
});

exports.handler = async (event, context) => {
  console.log("Function `read-all` invoked");
  return client
    .query(q.Paginate(q.Match(q.Ref("indexes/all_items"))))
    .then(response => {
      const itemRefs = response.data;
      // create new query out of item refs. http://bit.ly/2LG3MLg
      const getAllItemsDataQuery = itemRefs.map(ref => {
        return q.Get(ref);
      });
      // then query the refs
      return client.query(getAllItemsDataQuery).then(ret => {
github netlify / netlify-dev-plugin / src / functions-templates / js / fauna-crud / create-schema.js View on Github external
function createFaunaDB() {
  if (!process.env.FAUNADB_SERVER_SECRET) {
    console.log("No FAUNADB_SERVER_SECRET in environment, skipping DB setup");
  }
  console.log("Create the database!");
  const client = new faunadb.Client({
    secret: process.env.FAUNADB_SERVER_SECRET
  });

  /* Based on your requirements, change the schema here */
  return client
    .query(q.Create(q.Ref("classes"), { name: "items" }))
    .then(() => {
      console.log("Created items class");
      return client.query(
        q.Create(q.Ref("indexes"), {
          name: "all_items",
          source: q.Ref("classes/items"),
          active: true
        })
      );
    })
github netlify / netlify-faunadb-example / functions / todos-read-all.js View on Github external
/* Import faunaDB sdk */
const faunadb = require('faunadb')

const q = faunadb.query
const client = new faunadb.Client({
  secret: process.env.FAUNADB_SERVER_SECRET
})

exports.handler = (event, context) => {
  console.log('Function `todo-read-all` invoked')
  return client.query(q.Paginate(q.Match(q.Ref('indexes/all_todos'))))
    .then((response) => {
      const todoRefs = response.data
      console.log('Todo refs', todoRefs)
      console.log(`${todoRefs.length} todos found`)
      // create new query out of todo refs. http://bit.ly/2LG3MLg
      const getAllTodoDataQuery = todoRefs.map((ref) => {
        return q.Get(ref)
      })
      // then query the refs
      return client.query(getAllTodoDataQuery).then((ret) => {
github DavidWells / netlify-functions-workshop / lessons / use-cases / 1-rest-api / functions / methods / update.js View on Github external
/* Import faunaDB sdk */
const faunadb = require('faunadb')

const q = faunadb.query
const client = new faunadb.Client({
  secret: process.env.FAUNADB_SERVER_SECRET
})

module.exports = async (event, context) => {
  const data = JSON.parse(event.body)
  const id = event.id
  console.log(`Function 'update' invoked. update id: ${id}`)
  return client
    .query(q.Update(q.Ref(`classes/todos/${id}`), { data }))
    .then(response => {
      console.log('success', response)
      return {
        statusCode: 200,
        body: JSON.stringify(response)
      }
    })
github fauna / todomvc-fauna-spa / src / Login.js View on Github external
["doSign Up"] () {
    console.log(this.state)
    publicClient.query(
      q.Create(q.Class("users"), {
        credentials : {
          password : this.state.password
        },
        // permissions : {
          // read : q.Select("ref", q.Get(q.Ref("classes/users/self")))
        // }
        data : {
          login : this.state.login
        }
    })).then(() => publicClient.query(
      q.Login(q.Match(q.Index("users_by_login"), this.state.login), {
        password : this.state.password
    }))).then((key) => {
      saveTokens(key.secret);
      this.authorized(true);
    });
github laardee / serverless-authentication-boilerplate / authentication / lib / storage / fauna / faunaCache.js View on Github external
'use strict';

// Common
const config = { secret: process.env.FAUNADB_SECRET };

const faunadb = require('faunadb');

const q = faunadb.query;
const client = new faunadb.Client(config);

const crypto = require('crypto');
const Promise = require('bluebird');

const log = require('../../helpers').log;

function hash() {
  return crypto.randomBytes(48).toString('hex');
}

/**
 * Creates OAuth State
 */
const createState = () => {
  const state = hash();
  return client.query(q.Create(q.Class('auth_cache'), {