President_2000

Example2

Dec 22nd, 2020 (edited)
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     RoastedPresident2000
  3.     Example 2
  4.     Module into creating fauna data
  5. */
  6. const config = require('./../workerconfig');
  7.  
  8. // FaunaDB configuration & setup into saving and loading scripts executed
  9. var faunadb = require('faunadb'),
  10.     q = faunadb.query;
  11.  
  12. export const client = new faunadb.Client({
  13.     secret: config.faunaSecret,
  14.     fetch: fetch.bind(globalThis)
  15. });
  16.  
  17. /*
  18.     gets and returns data matching the object passed into the function
  19. */
  20. export async function getData(index, object) {
  21.     try {
  22.         var result = await client.query(
  23.             q.Get(
  24.                 q.Match(
  25.                     q.Index(index), object
  26.                 )
  27.             )
  28.         );
  29.  
  30.         return result.data ? result.data : null;
  31.     } catch(ex) {
  32.         return false;
  33.     }
  34. }
  35.  
  36. /*
  37.     Constructs the basic write query into Fauna Data
  38. */
  39. export async function writeData(collection, data) {
  40.     try {
  41.         await client.query(
  42.             q.Create(
  43.                 q.Collection(collection),
  44.                 {
  45.                     data: data,
  46.                 }
  47.             )
  48.         );
  49.  
  50.         return true;
  51.     } catch(ex) {
  52.         return ex;
  53.     }
  54. }
  55.  
  56. /*
  57.     Deletes data at specified index in Fauna Data
  58. */
  59. export async function deleteData(index, data) {
  60.     try {
  61.         var data = await client.query(
  62.             q.Get(
  63.                 q.Match(
  64.                     q.Index(index), data
  65.                 )
  66.             )
  67.         );
  68.  
  69.         if (data) {
  70.             const ref = data.ref;
  71.             await client.query(
  72.                 q.Delete(
  73.                     q.Ref(
  74.                         q.Collection("servers"),
  75.                         ref.value.id
  76.                     )
  77.                 )
  78.             )
  79.  
  80.             return true;
  81.         } else {
  82.             return false;
  83.         }
  84.     } catch(ex) {
  85.         return ex;
  86.     }
  87. }
Add Comment
Please, Sign In to add comment