schema
The schema
creates a database instance from the given schema.
It accepts a function as the argument, where you define the database structure:
What you return from the function, defines the database structure.
Options
You can pass the options object as the second argument.
app
You can specify the app name in the options to make the web, server, or both to use the particular Firebase app:
If not specified, it will use the default app name ([DEFAULT]
) assigned when calling initializeApp
without the name argument.
preferRest
You can make Admin SDK to use REST requests instead of gRPC where possible by setting the preferRest
option. It improves the cold-start times.
$
helper
The argument function receives $
helper object as the first argument that provides the schema
API.
$
type is TypesaurusCore.SchemaHelpers
.
$.collection
The $.collection
function creates a collection with the given model. The key you assign the collection to, Typesaurus will use as the collection path:
→ You can customize the path, see collection.name
Generics
Model
You always pass the model interface as the first generic param:
It defines the shape of the documents in the collection.
You can also use a variable model where document can be only one of the given types:
It helps to simplify and secure operations on mixed document types.
→ Read more about variable models
CustomId
The second optional generic param allows to set id type to the collection, allowing accessing i.e., credentials
collection using accountId
and vice versa.
You can also set a string
, enum or union as the id type, that allows you to specify set of allowed ids:
$.collection(...).sub
The sub
function creates a subcollection:
The subcollection path will be posts/{postId}/comments
and the id will be Id<"posts/comments">
.
You can access a subcollection calling the partent collection with an id:
The subcollections can be nested as well:
When you need to access a subcollection, i.e. to convert string to its id, you can use sub
property:
$.collection(...).name
By default the collection has the same name as the key you assign it to. But you can customize it using name
property:
It will allow you to access collection via the alias subscription
while keeping the original name in the database:
The name also affects the collection id type, so for our example it will be Id<"billing">
.