Collections
Bazaar provides document-orientated (NoSQL) and schema-less databases scoped to a user and app. A database contains collections which are analogous to tables in a relational database. Collections are made up of documents, or docs, analogous to rows in a relational database.
Using a Collection
To use a collection, create a collection object. By default, a collection object interacts with the database of the authenticated user.
A collection will be lazily created the first time someone tries to interact with it.
When using TypeScript, you can specify the type of the collection doc.
Collection doc types must include an id
of type string
. That is, it must extend or conform to the Doc
type.
Use a Specific User’s Collection
To use the collection of a specific user, pass an options
argument to set a user ID. If not explicitly set, the user ID defaults to the authenticated user’s.
Handle Lazy Collection Creation
When a collection is lazily created, you might want to take some action. For example, to set permissions or insert some initial data. You can do this as follows:
Get all Docs
Get all docs from a collection.
The filter
and options
parameters are optional:
A filter allows you to get only a subset of documents. Learn how Filtering works.
Use the options
parameter to set offsets and order to further control returned docs.
Get Docs by Page
Offsets are useful, but for a more ergonomic way to work with pagination, there is getPage
.
Like getAll
, getPage
also takes optional filter
and options
parameters.
The options parameter allows you to order the returned docs.
Get a Doc
Get a single doc.
If no document with this ID exists, null
is returned.
Insert a Doc
Insert a new doc with an auto-generated ID.
Insert a new doc with a specific ID.
The ID must be unique; attempting to insert a document with an existing ID will fail.
Update a Doc
Update a doc.
For example, if you have a doc:
And update as follows:
The result will be:
Replace a Doc
Replace an existing doc with a new one. The new doc completely overwrites the existing doc.
Delete All Docs
Delete all documents in a collection. This action cannot be undone.
See filter
docs.
Delete a Doc
Delete a specific document from a collection by its ID.
Subscribe to Collection Change Events
Receive realtime updates whenever any document in the collection changes. Returns an unsubscribe function.
Similar to the getAll
method, the filter
is optional and can be empty. See filter
docs.
Subscribe Listener
The listener
is an object that contains optional callbacks to be run when a document is added, updated, or deleted. It has the following type:
onInitial
usage notes:
- When a listener is first invoked and an
onInitial
function is provided, it will be called for each existing document in the collection. - The
onInitial
callback serves as a convenient alternative to manually fetching and iterating over all documents in a collection. onInitial
is particularly useful for initializing local state with existing data from the collection.
For example, instead of:
You can use:
This approach simplifies the process of initially populating local state with existing documents from the collection.
Subscribe to Doc Change Events
Receive realtime updates for a specific document in the collection.
Returns an unsubscribe function.
See Subscribe Listener docs.
Subscribe Listener Helpers
When subscribing to change events, a common use case is to keep a local array or object in sync with the database. For those cases you can utilize the arrayMirrorSubscribeListener
and objectMirrorSubscribeListener
subscribe listener helpers to avoid writing boilerplate to update arrays and objects.
These helpers can also take a subscribe listener as a second argument, allowing you to customize any of the listener callbacks.
Reserved field names and values
All special field names and values that are used throughout Bazaar start with a $
sign. As such, we recommend not using field names or values that start with a $
.
In documents, currently the only reserved value is $now
. It will resolve to a UTC timestamp on the server. This allows you to generate a predictable timestamp in your data no mater in which timestamp the app is running.
Filtering
You can filter the document queries with fine-grained control.
Comparison Operatiors
By default, filter values are checked for equality. The filter
will return all documents where isActive === true
.
Comparison operator can also be specified explicitly. The equality filter above would then look as follows:
Filters currently support the following comparison operators:
$eq
: equal$ne
: not equal$gt
: greater than$ge
: greater than or equal$lt
: less than$le
: less than or equal
Filter with $contains
Operator
The $contains
operator is used to check if an array field in the documents contains a specified value.
Filter with AND
Conditions
For simple filters, this is enough. For more complex filtering queries you can also use the $and
key:
Filter with OR
Conditions
Filter with NOT
Condition
Nested Filter Conditions
And for a more complex example:
Ordering (Sorting)
The orderBy
option allows you to sort the results fetched from a collection using the getAll
or getPage
methods. By defining the order criteria for one or more fields, you can organize the returned documents according to specific requirements.
orderBy
is an object where each key represents a field name to sort by, and the value is either OrderByType.ASC
for ascending order or OrderByType.DESC
for descending order.
You can sort by single or multiple fields, specifying the order for each field individually:
Managing Collections
Generally, collections are created lazily and don’t need additional management. However, in some cases manually creating, dropping, or listing collections is useful.
Create a Collection
Create collections manually using the bzr.collections.create
method.
Drop a Collection
Drop, or delete, a collection and all of its docs.
List Collections
List the names of all collections.