This is the beginnings of a project to make a full-featured ORM that takes advantage of generators, which should make the codebase and usage of the library vastly simpler. I spent weeks digging through existing node ORM implementations, only to find myself dissatisfied with how convoluted they all seemed to be.
npm install corm
const model = corm('localhost/test')
const User = model('users')
const user = User.build({
email: '[email protected]'
})
yield user.save()
yield user.update({
name: 'me'
})
yield user.remove()
In typical node fashion, this module is meant to be simple. This means it lacks many features you may expect of an ORM which are intended to be implemented externally. Here are a few to be aware of:
Build a model instance.
Build and save a model instance.
Find an array of model instances using a query object.
Find one model instance using a query object.
Find one model instance by id.
Find or create a model instance given a set of data
Create or update a model instance given a query and change set
Update any records that match the query.
Update the record that matches the query.
Remove any records that match the query.
Remove the record that matches the query.
Count number of records matching the query.
Currently, this is passed through directly to monk.
This method is mostly used internally to detect if a model exists in the database already. Currently, it simply checks for existence of a _id
property.
Insert new models or update already persisted models.
Apply the input data to the model and save it.
Remove the model from the database.
Fetch the latest model state from the database.
There are several hook methods that can be overridden to trigger things before or after various interactions. Each must be a generator function. These methods include:
beforeSave
beforeCreate
beforeUpdate
beforeRemove
beforeValidate
afterSave
afterCreate
afterUpdate
afterRemove
afterValidate
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.