Collection
The Collection
class represents a collection in the database. It provides methods to read and write documents, access subcollections, and construct ids, refs, and docs.
You access collections through the database instance you create with the schema
function:
Subcollections
You can access subcollections by calling the collection with an id:
get
The method allows to get a document by its id:
→ Read more about the get
method
all
The method allows to get all documents in the collection:
→ Read more about the all
method
query
The method allows to query documents in the collection:
→ Read more about the query
method
many
The method allows to get many documents by their ids:
→ Read more about the many
method
count
The method allows counting documents in the collection:
→ Read more about the count
method
sum
The method enables summing the collection field values:
→ Read more about the sum
method
average
The method allows to calculate the average of collection field values:
→ Read more about the sum
method
add
The method allows to add a new document to the collection:
→ Read more about the add
method
set
The method allows to set a document data:
→ Read more about the set
method
update
The method allows to update a document data:
→ Read more about the update
method
upset
The method allows to update a document data if it exists or set it if it doesn’t:
→ Read more about the upset
method
remove
The method allows to remove a document:
→ Read more about the remove
method
sub
The property allows access to nested subcollections:
Typically you access the id
method on a collection, but in the case with subcollections, it’s inconvenient to create it first to get an id. That’s where sub
comes in handy:
id
The id
method allows generating a random id or cast string
to the collection id type.
Generating id
When called without arguments, the function generates a random document id using Firebase and returns Promise<string>
.
Casting string
If you have an untyped id string, you can cast it to the id type using the function:
ref
The method allows to create a Ref
instance:
→ Read more about the Ref
class
doc
The method allows to create a Doc
instance:
→ Read more about the Doc
class
as
⚠️ Available starting with v10.7.0
The method resolves Typesaurus.SharedCollection
if the model extends the given type. Otherwise, it resolves unknown
preventing the usage of incompatible models:
It allows sharing functionality across the db entities in a type-safe way.