Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'faunadb' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
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 })
/* 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)
#!/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(
/* 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)
};
/* 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 => {
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
})
);
})
/* 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) => {
/* 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)
}
})
["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);
});
'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'), {