Using Documents
Documents contain JSON serializable data.
Referring to documents
In order to access a document, use the document
method of collection.
Calling the document
method does not actually create a document, but rather
creates a reference to that document.
No remote state of a document is actually accessed before methods are called on the document object.
collection = numerous.collection("my-collection")
document = collection.document("my-document")
Loading document data
In order to load the data in a document, use the get
command. This command returns any data stored
in the document, or None
if the document does not exist.
collection = numerous.collection("my-collection")
document = collection.document("my-document")
data = document.get()
if data is None:
print("this document has not been set yet")
Setting document content
In order to save data, you must set
the document's content. This will override
any existing data in that document. If you wish to modify the data, load it with
get
first, modify the loaded data, and set
it again.
collection = numerous.collection("my-collection")
document = collection.document("my-document")
document.set({"field1": "my field 1 value", "field2": 2})
Serializing and deserializing custom types
The Numerous SDK serializes the data used to set the document contents with the
built-in json.dumps
, which is limited in terms of what it can serialize. For
example, datetime.datetime
is not serializable.
Currently, it is up to the application developer to handle this serialization manually.
In the example below, we manually serialize and deserialize MyData
and
datetime.datetime
objects.
@dataclass
class MyData:
field1: str
field2: int
my_datetime = datetime.now()
my_data = MyData(field1="my field 1 value", field2=2)
collection = numerous.collection("my-collection")
document = collection.document("my-document")
document.set({
"my-data": asdict(my_data),
"my-datetime": my_datetime.isoformat(),
})
data = collection.document("my-document").get()
my_deserialized_datetime = datetime.fromisoformat(data["my-datetime"])
my_deserialized_data = MyData(**datª["my-data"])