Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- RoastedPresident2000
- Example 2
- Module into creating fauna data
- */
- const config = require('./../workerconfig');
- // FaunaDB configuration & setup into saving and loading scripts executed
- var faunadb = require('faunadb'),
- q = faunadb.query;
- export const client = new faunadb.Client({
- secret: config.faunaSecret,
- fetch: fetch.bind(globalThis)
- });
- /*
- gets and returns data matching the object passed into the function
- */
- export async function getData(index, object) {
- try {
- var result = await client.query(
- q.Get(
- q.Match(
- q.Index(index), object
- )
- )
- );
- return result.data ? result.data : null;
- } catch(ex) {
- return false;
- }
- }
- /*
- Constructs the basic write query into Fauna Data
- */
- export async function writeData(collection, data) {
- try {
- await client.query(
- q.Create(
- q.Collection(collection),
- {
- data: data,
- }
- )
- );
- return true;
- } catch(ex) {
- return ex;
- }
- }
- /*
- Deletes data at specified index in Fauna Data
- */
- export async function deleteData(index, data) {
- try {
- var data = await client.query(
- q.Get(
- q.Match(
- q.Index(index), data
- )
- )
- );
- if (data) {
- const ref = data.ref;
- await client.query(
- q.Delete(
- q.Ref(
- q.Collection("servers"),
- ref.value.id
- )
- )
- )
- return true;
- } else {
- return false;
- }
- } catch(ex) {
- return ex;
- }
- }
Add Comment
Please, Sign In to add comment