tl;dr
What is @jsmrcaga/executor#
This package is intended to help developers write fast and efficient Mongo queries.
It is loosely inspired by Django's ORM API, and implements utility methods to help with
- Connection
- Models
- Aggregation
Installation#
Connection#
Connecting to Mongo#
Connections are made by passing a list of options. This library will not add options automatically
and will let you decide what to pass. If you don't pass useUnifiedTopology Mongo will probably throw a warning.
Using a Database#
Models#
Creating a Model#
Models can use validation schemas and some other options.
Models create/use collections automatically.Collection names are defined by kebab-casing the model name.
For example, if you have a model called MySuperModel, the corresponding collection would be my-super-model.
Validation & Extra Fields#
Saving and updating models#
important
By default this library adds three properties to your models:
__created: Stores the millisecond timestamp for the object's creation__updated: Stores the millisecond timestamp for the object's last update operation__deleted: Stores the millisecond timestamp for the object's deletion
Creating a new model#
Updating a model#
You can also update models with more data and wait for success to update it in RAM
Deleting models#
The delete() method soft-deletes your object
The hard_delete() method removes your document from the collection
Queries#
Querying the DB becomes really easy with @jsmrcaga/executor.
Accessing mongodb collections#
if for any reason you need to access the base collection from mongodb driver:
gives you a direct access. Any methods and properties are untouched and can be used directly from the driver.
Using the manager#
Every model comes with a manager under the static objects property. This should be your main interface to call collection methods.
All methods from the manager are passed through a proxy. If they return a Cursor they
will be proxied by this library's own Cursor class, which enables automatic instanciation.
You can get your results by "executing" the cursor, which performs a toArray() operation
and special instanciations.
This also enables you to use all normal cursor methods, like skip and limit, enabling you
to build generic interfaces (for a REST API for example)
Getting single objects#
The equivalent to findOne using the manager is get.
Please note that this is a special Queryset method and not a Manager method, but managers proxy-call Queryset methods.
Aggregation#
Every manager comes with a built-in Queryset instance that allows building aggregation pipelines.
Using different methods you can build an aggregation pipeline and run it against your DB. Some methods are pre-written, allowing you to simply pass the query and not the aggregation stage name.
tl;dr#
Transactions#
Transactions can be used as a context or as objects. The context version is just a wrapper for session.withTransaction().
Mongo transactions need a Session to be created and passed to every query that needs to be performed on that transaction.
db.atomic() provides you the session to add to your queries yourself, and Transactions do it for you, but at the cost
of binding your queries.