Skip to content

Instantly share code, notes, and snippets.

@agustinustheo
Created October 26, 2020 22:04
Show Gist options
  • Save agustinustheo/ae66ab71bc803aa4d3ce921987eb1b54 to your computer and use it in GitHub Desktop.
Save agustinustheo/ae66ab71bc803aa4d3ce921987eb1b54 to your computer and use it in GitHub Desktop.
Function to get multiple documents from FaunaDB
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