Hive-model-mongoose is a superclass for mongoose model. It exists for several purposes:
- to create a more REST-like API for the mongoose model methods
- to insulate all of the mongoose model methods from your particular model class to reduce namespace collision.
- to bundle a set of features with all models, including archiving and soft deletion
Note that in hive-model-mongoose, the assumption is that the models functions, not the records' native functions, are called.
All models have "soft deletion" built in to the delete method. if Model.delete(record, callback, true) is called, the models' delete flag is set to true, but the record itself is preserved.
Also, you can archive your data -- clone its properties into an _archive array - if you include the property _archive ['mixed'] in your schema. This is a handy way to "back up" your data before updating it.
Hive_Model_Mongoose is a hive-component (https://github.com/bingomanatee/hive-component). Instances have the mongoose
Model in their model
property.
Mongoose_Model is a factory that returns the mongoose_model via callback (and directly).
Mongoose_Model(
{
name: 'tribbles'
} // mixins
, {
mongoose: mongoose,
schema_def: object || mongoose.Schema || path_to.jso
} // configurations
, dataspace // hive-model.Dataspace || Object (optional)
, callback (optional)
);
Any methods you want to attach to the model go here. The only requirement is the name property, which must be a unique string (unique to your database/the dataspace)
as a hive-component, a hive-model has a configuration registry that can be accessed with
- model.get_config(key):value,
- model.set_config(key, value)
- model.has_config(key):boolean
There are two required configurations:
- mongoose -- an instance of your Mongoose module
- schema_def -- either a Mongoose Schema, an object, or a path to a JSON file.
Dataspace can be a formal model registry (as part of the hive-model system) or a basic object that you want to use to collect your models by name. You can pass a naked object {} in as well.
a function that returns the model.
Adds a series of objects to the collection. Can be raw objects, Documents, or a combination. If records is not an array, it routes to put(..). If as_group != true, it feeds records one at a time to put and returns the aggregate results to the callback. If it is true, it uses this.model.collection.insert -- meaning no property validation is done, and the callback will not receive the new documents.
Inserts or updates a single document.
Gets a single document, by ID; a passthrough to findById.
update a single document's selected fields. data must have an _id property. The other fields update the content of the object on a field by field basis.
- if your data has a __REMOVE property, whose values are an array of strings, those properties are removed from the document. However if the property is an array, it is emptied rather than deleted.
returns the entire collection in the callback, or a query.
returns the records collection in the callback, or a query. Respects soft deletes.
returns the soft-deleted records in the collection in the callback, or a query.
passthrough to Mongoose Model.find
passthrough to Mongoose Model.findOne
passthrough to Mongoose Model.count
drops the entire collection
A static method of the Mongoose_Model.
returns a function with the API function (mixins, config, callback) that you can call to make mongoose models without passing in the dataspace parameter all the time.