35
loading...
This website collects cookies to deliver better user experience
from faunadb.client import FaunaClient
from faunadb.errors import FaunaError, HttpError
from faunadb import query as q
from dotenv import load_dotenv
from typing import Optional
import os
load_dotenv()
client = FaunaClient(secret=os.getenv('Fauna-secret'))
# reviewing customer data
customers = client.query(
q.map_(
lambda x: q.get(x),
q.paginate(
q.document(q.collection('customers'))
)
)
)
print(customers)
“customer”
collection and then use the timestamp from before to see what the table looked like at that time. “customers”
collection and use the New Document option to add two new customer documents, with whatever info you choose.from faunadb.client import FaunaClient
from faunadb.errors import FaunaError, HttpError
from faunadb import query as q
from dotenv import load_dotenv
from typing import Optional
import os
load_dotenv()
client = FaunaClient(secret=os.getenv('Fauna-secret'))
# # reviewing customer data
# customers = client.query(
# q.map_(
# lambda x: q.get(x),
# q.paginate(
# q.document(q.collection('customers'))
# )
# )
# )
# print(customers)
data = client.query(
q.at(
1632242211590000,
q.paginate(
q.documents(q.collection('customers'))
)
)
)
print(data)
“101”
from faunadb.client import FaunaClient
from faunadb.errors import FaunaError, HttpError
from faunadb import query as q
from dotenv import load_dotenv
from typing import Optional
import os
load_dotenv()
client = FaunaClient(secret=os.getenv('Fauna-secret'))
# # reviewing customer data
# customers = client.query(
# q.map_(
# lambda x: q.get(x),
# q.paginate(
# q.document(q.collection('customers'))
# )
# )
# )
# print(customers)
# data = client.query(
# q.at(
# 1632242211590000,
# q.paginate(
# q.documents(q.collection('customers'))
# )
# )
# )
# print(data)
# update name
client.query(
q.update(
q.ref(
q.collection('customers'),
"101"
),
{'data': {'firstName':'Boyle', 'lastName':'Johnson'}}
)
)
# update contact details
client.query(
q.update(
q.ref(
q.collection('customers'),
"101"
),
{'data': {'telephone': '854-456-3982'}}
)
)
from faunadb.client import FaunaClient
from faunadb.errors import FaunaError, HttpError
from faunadb import query as q
from dotenv import load_dotenv
from typing import Optional
import os
load_dotenv()
client = FaunaClient(secret=os.getenv('Fauna-secret'))
# ...
# ..
# update name
# client.query(
# q.update(
# q.ref(
# q.collection('customers'),
# "101"
# ),
# {'data': {'firstName':'Boyle', 'lastName':'Johnson'}}
# )
# )
# # update contact details
# client.query(
# q.update(
# q.ref(
# q.collection('customers'),
# "101"
# ),
# {'data': {'telephone': '854-456-3982'}}
# )
# )
results = client.query(
q.paginate(
q.events(
q.ref(
q.collection('customers'), "101"
)
)
)
)
for resp in results['data']:
print(resp, end="\n\n")
main.py
from faunadb.client import FaunaClient
from faunadb.errors import FaunaError, HttpError
from faunadb import query as q
from dotenv import load_dotenv
from typing import Optional
import os
load_dotenv()
client = FaunaClient(secret=os.getenv('Fauna-secret'))
def introspect(ref_id: str):
events_ = client.query(
q.paginate(
q.events(
q.ref(
q.collection('customers'), ref_id
)
)
)
)
return events_
def downgrade(ref_id: str, steps: Optional[int] = -2):
events_ = introspect(ref_id)
# fetch previous state and update document with it instead
try:
client.query(
q.update(
q.ref(
q.collection("customers"), ref_id
),
{
'data': events_['data'][steps]['data']
}
)
)
except FaunaError as e:
return "An error occurred while trying to update object, try again."
return "downgraded object successfully"
print(introspect("102")) # inspect the history of the document before running the downgrade
print(downgrade("102"))
Note
: For each downgrade the function creates a new update with the info gotten from the introspect function. So in essence, the history of the document continues to lengthen and so does the latest version. For optimum experience you may want to introspect and pick a specific version then jump there, if it's not the immediate previous version of the latest one.