The firestoreAdd
function is a higher-order function that takes a collection path and an options object, and returns a function that can be used to add documents to the specified collection.
You can use this to create a function that adds a post to a collection of posts. The first argument is the slash-separated path of the collection.
Collections don’t have to be at the top level of the Firestore, adding slashes will add the document to a subcollection. For example, if you want to add a post to a collection of posts for a specific user:
Using subcollections is a common pattern in Firestore since it makes writing security rules much easier. To make our function more reusable, we can use a function that takes an arguments object and returns a string representing the path:
Even more convenient is to pull the userId from the post itself, assuming the post contains it. This can be done by using the data
property of the arguments object which contains the data that will be written to the document:
The function returns a Promise that resolves with a DocumentReference
to the newly created document.
If a type parameter is provided, the returned function will be typesafe:
The provided type definitions will help you to avoid common mistakes, but they are not realtime validation. You can still write invalid data to Firestore if you don’t use the provided functions correctly.
The second argument to firestoreAdd
is an optional object allowing for additional configuration. This object can have a property addMetadata
which, if true, adds metadata to the document.
If the addMetadata
option is not specified, the function will operate without adding any additional metadata to the document. This default behavior is suitable for scenarios where metadata is not required, offering flexibility based on your needs.
Here are some common issues you may encounter when using firestoreAdd
:
firestoreAdd
here.The firestoreAdd
function is a higher-order function that takes a collection path and an options object, and returns a function that can be used to add documents to the specified collection.
You can use this to create a function that adds a post to a collection of posts. The first argument is the slash-separated path of the collection.
Collections don’t have to be at the top level of the Firestore, adding slashes will add the document to a subcollection. For example, if you want to add a post to a collection of posts for a specific user:
Using subcollections is a common pattern in Firestore since it makes writing security rules much easier. To make our function more reusable, we can use a function that takes an arguments object and returns a string representing the path:
Even more convenient is to pull the userId from the post itself, assuming the post contains it. This can be done by using the data
property of the arguments object which contains the data that will be written to the document:
The function returns a Promise that resolves with a DocumentReference
to the newly created document.
If a type parameter is provided, the returned function will be typesafe:
The provided type definitions will help you to avoid common mistakes, but they are not realtime validation. You can still write invalid data to Firestore if you don’t use the provided functions correctly.
The second argument to firestoreAdd
is an optional object allowing for additional configuration. This object can have a property addMetadata
which, if true, adds metadata to the document.
If the addMetadata
option is not specified, the function will operate without adding any additional metadata to the document. This default behavior is suitable for scenarios where metadata is not required, offering flexibility based on your needs.
Here are some common issues you may encounter when using firestoreAdd
:
firestoreAdd
here.