Skip to content

Instantly share code, notes, and snippets.

@LiorB-D
Last active April 14, 2022 17:53
Show Gist options
  • Save LiorB-D/9f35ca9abb0475047f0279d491b260c2 to your computer and use it in GitHub Desktop.
Save LiorB-D/9f35ca9abb0475047f0279d491b260c2 to your computer and use it in GitHub Desktop.
# Import required packages including the driver
import os
from urllib.parse import urlparse
from faunadb import query as faunaquery
from faunadb.client import FaunaClient
try:
secret = "DB_ACCESS_KEY" # Insert your access key here(USE ENVIRONMENT VARIABLES)
endpoint = "DB_ENDPOINT" # Insert your endpoint here(USE ENVIRONMENT VARIABLES)
database_url = urlparse(endpoint)
# Instantiate the FaunaDB client
client = FaunaClient(
secret=secret,
domain=database_url.hostname,
port=database_url.port,
scheme=database_url.scheme
)
# Create a Collection
query_result = client.query(faunaquery.create_collection({"name": "testCollection"}))
# Print the Result of the Query
print(f"FaunaDB Collection Creation: \n {query_result}")
query_result = client.query(
faunaquery.map_(
lambda username: faunaquery.create(
faunaquery.collection("testCollection"),
{"data": {"name": username}}
),
[
"Barry",
"Sam",
"Ashley",
"Kat",
"Nathan"
]
)
)
# Print the Result of the Query
print(query_result)
# Retrieve Data from Reference ID
query_result = client.query(
faunaquery.get(
faunaquery.ref(
faunaquery.collection("testCollection"), "INSERT REFERENCE ID")
)
)
#Update data
query_result = client.query(
faunaquery.update(
faunaquery.ref(
faunaquery.collection("testCollection"), "328031285277622849"),
{ "data": {"name" : "Barry Allen", "email": "[email protected]"}}
)
)
#Delete Data
query_result = client.query(
faunaquery.delete(
faunaquery.ref(
faunaquery.collection("testCollection"), "328031285277622849")
)
)
except Exception as error:
print(f"Error occurred : {error}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment