Created
October 26, 2020 22:04
-
-
Save agustinustheo/ae66ab71bc803aa4d3ce921987eb1b54 to your computer and use it in GitHub Desktop.
Function to get multiple documents from 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 get_multiple(index, data=None): | |
try: | |
serverClient = FaunaClient(secret=os.environ.get("FAUNA_SERVER_SECRET")) | |
res_arr = [] | |
if data is None: | |
res = serverClient.query( | |
q.map_( | |
q.lambda_("data", q.get(q.var("data"))), | |
q.paginate(q.match(q.index(index))) | |
) | |
) | |
res_arr.extend(res["data"]) | |
elif isinstance(data, list): | |
for x in data: | |
res = serverClient.query( | |
q.map_( | |
q.lambda_("data", q.get(q.var("data"))), | |
q.paginate(q.match(q.index(index), q.casefold(x))) | |
) | |
) | |
res_arr.extend(res["data"]) | |
else: | |
res = serverClient.query( | |
q.map_( | |
q.lambda_("data", q.get(q.var("data"))), | |
q.paginate(q.match(q.index(index), q.casefold(data))) | |
) | |
) | |
res_arr.extend(res["data"]) | |
arr = [] | |
for x in res_arr: | |
x["data"]["ref_id"] = x["ref"].id() | |
arr.append(x["data"]) | |
return arr | |
except Exception as ex: | |
raise ex |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment