NoBrainer provides a similar persistence interface compared to other ORMs.
The following methods are available on the
Model.new(attrs)instantiate a new Model instance. Default values are set and
attrsis passed to
first_or_create!: see below.
Model.insert_all([doc1, doc2, ..., docN])is used for bulk inserts. This method receives a list of hashes, and will not instantiate any models. Instead it passes the documents in bulk to the database to perform efficient writes. If the documents primary keys are left unspecified, the database will assign default UUIDs and
insert_allwill return the list of generated ids. You may use
NoBrainer::Document::PrimaryKey::Generator.generateto generate MongoDB style ids to match the format of model instances.
Model.syncis a wrapper for
The following predicates are available on a model instance:
new_record?returns true if the instance has not yet been persisted.
destroyed?returns true if the instance has been destroyed.
persisted?returns true if the instance has been persisted and not destroyed.
The following methods are available on a model instance:
savereturns true if the instance was valid and saved, otherwise false.
save?is an alias for
update?()is an alias for
deleteremoves the document from the database without firing the destroy callbacks.
destroyfires the destroy callbacks and removes the document from the database.
reloadremoves all instance variables that the instance may have, to nuke any sort of cache.
reloadthen loads a fresh record from the database and calls the
initialize()method, which triggers the
initializecallbacks. You may pass an option
:keep_ivars => trueto prevent
reloadfrom cleaning up the instance variables.
NoBrainer::Error::DocumentNotFounderror will be raised if the document can no longer be found.
save during updates do not raise if the
instance document no longer exists in the database when performing the
operation. These methods will silently fail.
NoBrainer never autosaves a model behind the scene. When working with a database that does not support transactions such as RethinkDB, you need to be in full control of when database writes occur. There is therefore no autosave features in NoBrainer and all the writes need to be explicit.
Database writes can also be performed on criteria with
Learn more in the Querying section.
NoBrainer provides an API to fetch and update a record, or create it if not found. This is done atomically. Validations are The usage is shown as below:
Note that NoBrainer will need to match either the primary key in attrs, or a
field that has a uniqueness validator as the
first_or_create mechanism as described below.
NoBrainer provides an API to fetch a record, or create a record if not found. This is done atomically. The usage is shown below:
NoBrainer performs the following stpes:
- A lock around
where(some_condition).firstmatches a document, the lock is released and the document is returned.
Model.create(some_condition.merge(additional_params))is performed, and the lock is released right after the persistance operation.
some_condition must match a defined uniqueness validator to enforce the
atomicity properly. NoBrainer will provide helpful error message if it cannot
find any. This ensure that
first_or_create() does not race with any other
This API comes in two flavors.
first_or_create() does not raise an exception
when validation fails, while
When using updating a model, NoBrainer uses the dirty tracking information to only update the fields that changed. When no attribute changed, the database update query is skipped, but all callbacks are still executed.