Skip to content

Instantly share code, notes, and snippets.

@marcbachmann
Created October 2, 2014 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcbachmann/ce98e8f35d33cb9bb67e to your computer and use it in GitHub Desktop.
Save marcbachmann/ce98e8f35d33cb9bb67e to your computer and use it in GitHub Desktop.
mongodb-native Entity class
mongodb_url = require('../../config').get('mongodb:url')
MongoClient = require('mongodb').MongoClient
exports.db = null
exports.connect = (callback) ->
exports.client = new MongoClient()
MongoClient.connect mongodb_url, (err, db) ->
return callback(err) if err
exports.db = db
return callback(null, db)
Model = require('./model')
db = require('../db').db
ObjectID = require('mongodb').ObjectID
class Entity extends Model
constructor: (obj = {}) ->
@_id = obj._id if obj._id
@created = obj.created
@changed = obj.changed
toJSON: ->
json = @toObject()
json.id = json._id
delete json._id
json
@buildQuery: (query) ->
query._id = new ObjectID(query._id) if query._id && typeof query._id is 'string'
query.order = new ObjectID(query.order) if query.order && typeof query.order is 'string'
query
@find: (query, options, callback) ->
if arguments.length == 1
callback = query
options = {}
query = {}
if arguments.length == 2
callback = options
options = {}
options.sort ||= _id: -1
options.limit ||= 50
options.skip = options.offset || 0
query.is_deleted = $ne: true unless options.includeDeleted
cursor = db.collection(@tableName)
.find(@buildQuery(query), options)
.toArray (err, docs) =>
return callback(err) if err
documents = []
for doc in docs
documents.push(new this(doc))
callback(null, documents)
@findOne: (query = {}, options, callback) ->
if arguments.length == 2
callback = options
options = {}
options.limit = 1
@find query, options, (err, docs) =>
return callback(err) if err || !docs?[0]
callback(null, new this(docs[0]))
@findById: (id, callback) ->
@findOne {_id: id}, {includeDeleted: true}, (err, doc) ->
return callback(err) if err || !doc
callback(null, doc)
save: (callback) ->
return @update(this, callback) if @_id
delete @_id
@created = new Date()
@changed = new Date()
db.collection(@constructor.tableName)
.save this, w: 1, (err, doc) =>
return callback(err) if err || !doc
@_id = doc._id
callback(null, this)
update: (update, callback) ->
id = update._id
delete update._id
delete update.created
delete update.author
update.changed = new Date()
db.collection(@constructor.tableName)
.update {_id: id}, {"$set": update}, (err) =>
return callback(err) if err
this.extend(update)
callback(null, this)
@update: (query, update, callback) ->
db.collection(@tableName).update @buildQuery(query), update, {w:1}, (err) ->
return callback(err) if err
callback()
remove: (callback) ->
db.collection(@constructor.tableName)
.remove({_id: @_id}, callback)
module.exports = Entity
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment