Created
October 26, 2020 22:25
-
-
Save agustinustheo/6b3b781e6ccb4e4350549b0eaed5604a to your computer and use it in GitHub Desktop.
Python scripts to create, update and delete in FaunaDB.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def create(collection, data): | |
try: | |
serverClient = FaunaClient(secret=os.environ.get("FAUNA_SERVER_SECRET")) | |
res = serverClient.query(q.create(q.collection(collection), {"data": data})) | |
res["data"]["ref_id"] = res["ref"].id() | |
return res["data"] | |
except Exception as ex: | |
raise ex | |
def update(collection, id, data): | |
try: | |
serverClient = FaunaClient(secret=os.environ.get("FAUNA_SERVER_SECRET")) | |
res = serverClient.query(q.update(q.ref(q.collection(collection), id), {"data": data})) | |
res["data"]["ref_id"] = res["ref"].id() | |
return res["data"] | |
except Exception as ex: | |
raise ex | |
def delete(collection, id): | |
try: | |
serverClient = FaunaClient(secret=os.environ.get("FAUNA_SERVER_SECRET")) | |
serverClient.query(q.delete(q.ref(q.collection(collection), id))) | |
return True | |
except Exception as ex: | |
raise ex |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment