Comparing version 0.1.3 to 1.0.0
@@ -0,1 +1,2 @@ | ||
'use strict'; | ||
@@ -7,2 +8,2 @@ module.exports = require('./lib'); | ||
module.exports = require(dir); | ||
} | ||
} |
231
lib/index.js
'use strict'; | ||
var util = require('util'); | ||
var MongoClient = require('mongodb').MongoClient; | ||
var mongodb = require('mongodb'); | ||
var ObjectID = mongodb.ObjectID; | ||
var QueryStream = require('./stream'); | ||
exports.ObjectId = require('mongodb').ObjectID; | ||
exports.Connection = require('./connection'); | ||
module.exports = exports = ModelInstance; | ||
exports.Types = { | ||
ObjectId: ObjectID, | ||
ObjectID: ObjectID | ||
exports.createConnection = function () { | ||
return new exports.Connection(); | ||
}; | ||
var config = { | ||
db: null | ||
}; | ||
function ModelInstance (o) { | ||
o = o || {}; | ||
if (typeof o._id === 'string') { | ||
this._id = new ObjectID(o._id); | ||
} else if (typeof o._id === 'object') { | ||
this._id = o._id; | ||
} | ||
this._id = this._id || new ObjectID(); | ||
for(var i in o) { | ||
if (i !== '_id' && Object.hasOwnProperty.call(o, i)) { | ||
this[i] = o[i]; | ||
} | ||
} | ||
} | ||
var _whenReady = []; | ||
function whenReady(fn) { | ||
_whenReady.push(fn); | ||
} | ||
exports.connect = function (uri, done) { | ||
var options = { | ||
db: { | ||
native_parser: true | ||
} | ||
}; | ||
MongoClient.connect(uri, options, function (err, db) { | ||
if (err) return done(err); | ||
config.db = db; | ||
_whenReady.forEach(function (fn) { | ||
fn(db); | ||
}); | ||
done(); | ||
}); | ||
}; | ||
exports.disconnect = function (done) { | ||
if (config.db) return config.db.close(done); | ||
done(); | ||
}; | ||
exports.find = function (query, fields, options) { | ||
var collection = GC(this); | ||
var callback = arguments[arguments.length - 1]; | ||
var l = arguments.length; | ||
if (typeof callback === 'function') { | ||
l--; | ||
} else { | ||
callback = null; | ||
} | ||
switch (l) { | ||
case 0: | ||
query = {}; | ||
case 1: | ||
fields = {}; | ||
case 2: | ||
options = {}; | ||
} | ||
var cursor = collection.find.call(collection, query, fields, options); | ||
if (callback) { | ||
var proto = this.prototype; | ||
cursor.toArray(function (err, results) { | ||
if (err) return callback(err); | ||
results.forEach(function (doc) { | ||
doc.__proto__ = proto; | ||
}); | ||
callback(null, results); | ||
}); | ||
return; | ||
} | ||
return new QueryStream(cursor, this.prototype); | ||
}; | ||
function GC(constructor) { | ||
return constructor._collection || config.db.collection(constructor.collection); | ||
} | ||
exports.findOne = function () { | ||
var prototype = this.prototype; | ||
var cb = arguments[arguments.length - 1]; | ||
arguments[arguments.length - 1] = function (err, internal) { | ||
if (internal) internal.__proto__ = prototype; | ||
cb(err, internal); | ||
}; | ||
var collection = GC(this); | ||
return collection.findOne.apply(collection, arguments); | ||
}; | ||
exports.findById = function (_id, callback) { | ||
if (typeof _id === 'string') _id = new ObjectID(_id); | ||
return this.findOne({_id: _id}, callback); | ||
}; | ||
exports.findAndModify = function (query, sort, update, options, callback) { | ||
var collection = GC(this); | ||
return collection.findAndModify.apply(collection, arguments); | ||
}; | ||
exports.aggregate = function (pipeline, options, callback) { | ||
var _callback; | ||
var l = arguments.length; | ||
if (typeof arguments[arguments.length - 1] === 'function') { | ||
_callback = arguments[arguments.length -1]; | ||
l--; | ||
} | ||
if (l === 1) { | ||
options = {}; | ||
} | ||
var collection = GC(this); | ||
if (!options.cursor) { | ||
options.cursor = {}; | ||
} | ||
var cursor = collection.aggregate(pipeline, options); | ||
if (_callback) { | ||
var b = []; | ||
cursor.on('data', function (data) { | ||
b.push(data); | ||
}) | ||
.on('end', function () { | ||
_callback(null, b); | ||
}); | ||
return; | ||
} | ||
return cursor; | ||
}; | ||
exports.ensureIndex = function (fieldOrSpec, options) { | ||
var self = this; | ||
whenReady(function (db) { | ||
var collection = GC(self); | ||
collection.ensureIndex(fieldOrSpec, options, function (err) { | ||
if (err) throw err; | ||
}); | ||
}); | ||
}; | ||
exports.init = function (target, collection) { | ||
util.inherits(target, exports); | ||
target.collection = collection; | ||
Object.keys(exports) | ||
.forEach(function (key) { | ||
if (key === 'init') return; | ||
target[key] = exports[key]; | ||
}); | ||
}; | ||
exports.model = function (collection) { | ||
function CollectionModel(o) { | ||
ModelInstance.call(this, o); | ||
} | ||
CollectionModel.__proto__ = ModelInstance; | ||
CollectionModel.init = null; | ||
CollectionModel.collection = collection; | ||
CollectionModel.prototype = Object.create(ModelInstance.prototype, { | ||
constructor: { | ||
value : CollectionModel, | ||
enumerable : false, | ||
writable : true, | ||
configurable : true | ||
} | ||
}); | ||
return CollectionModel; | ||
}; | ||
exports.prototype.save = function (done) { | ||
done = done || function (err) { | ||
if (err) throw err; | ||
}; | ||
var collection = GC(this.constructor); | ||
var self = this; | ||
collection.save(this, function (err, numAffected, details) { | ||
if (err) return done(err); | ||
done(null, self); | ||
}); | ||
}; | ||
exports.update = function (query, update, options, callback) { | ||
var collection = GC(this); | ||
return collection.update.apply(collection, arguments); | ||
}; | ||
exports.prototype.update = function (update, options, callback) { | ||
var collection = GC(this.constructor); | ||
var args = [{_id: this._id}].concat(Array.prototype.slice.call(arguments)); | ||
return collection.update.apply(collection, args); | ||
}; | ||
exports.remove = function (query, options, callback) { | ||
var collection = GC(this); | ||
return collection.remove.apply(collection, arguments); | ||
}; | ||
exports.prototype.remove = function (options, callback) { | ||
var collection = GC(this.constructor); | ||
var args = [{_id: this._id}].concat(Array.prototype.slice.call(arguments)); | ||
return collection.remove.apply(collection, args); | ||
}; | ||
exports.prototype.updateIf = function (query, options, callback) { | ||
query = Object.create(query); | ||
query._id = this._id; | ||
return exports.update.apply(this.constructor, arguments); | ||
}; | ||
exports.create = exports.createConnection; |
{ | ||
"name": "mong", | ||
"version": "0.1.3", | ||
"version": "1.0.0", | ||
"description": "Lightweight models for mongodb objects", | ||
@@ -41,3 +41,3 @@ "main": "lib/index", | ||
"dependencies": { | ||
"mongodb": "^2.0.23" | ||
"mongodb": "^2.0.27" | ||
}, | ||
@@ -44,0 +44,0 @@ "directories": { |
mongomodel | ||
========== | ||
Models for mongodb objects. | ||
Simple models for mongodb objects. | ||
`npm install mong`. | ||
### Creating a collection class | ||
```javascript | ||
var mong = require('mong'); | ||
mong.connect('mongodb://localhost/mydatabase'); | ||
mong.init(User, 'users'); | ||
module.exports = User; | ||
var db = require('mong').create('mongodb://localhost/mydatabase'); | ||
function User(o) { | ||
mong.call(this, o); | ||
} | ||
var User = module.exports = db.model('users'); | ||
@@ -35,3 +32,3 @@ User.prototype.getFullName = function () { | ||
.find() | ||
.on('data', function (err, user) { | ||
.on('data', function (user) { | ||
console.log(user.getFullName()); | ||
@@ -38,0 +35,0 @@ }) |
@@ -6,6 +6,6 @@ var lib = require('../../index'); | ||
it('should connect to a mongodb server', function (done) { | ||
lib.connect('mongodb://localhost/mong-test', done); | ||
lib.create().connect('mongodb://localhost/mong-test', done); | ||
}); | ||
it('should error if the server doesn\'t exist', function (done) { | ||
lib.connect('mongodb://localhost:45603', function (err) { | ||
lib.create().connect('mongodb://localhost:45603', function (err) { | ||
should.exist(err); | ||
@@ -12,0 +12,0 @@ done(); |
@@ -6,4 +6,6 @@ var lib = require('../../index'); | ||
it('should keep all of the properties', function () { | ||
var db = lib.create(); | ||
var Model = db.model('animals'); | ||
var object = {x:3}; | ||
lib.call(object, {y: 12, z: 300}); | ||
Model.call(object, {y: 12, z: 300}); | ||
object.x.should.equal(3); | ||
@@ -10,0 +12,0 @@ object.y.should.equal(12); |
@@ -0,1 +1,3 @@ | ||
'use strict'; | ||
var lib = require('../../index'); | ||
@@ -10,31 +12,33 @@ var should = require('should'); | ||
function User() { | ||
var User; | ||
} | ||
var random = Math.random(); | ||
var _id = new ObjectId('52d3bca3e25e47fc2d860000'); | ||
var _id2 = new ObjectId('52d3bca3e25e47fc2d860300'); | ||
var uri = 'mongodb://localhost/mong-test2'; | ||
describe('Model.find', function () { | ||
before(function (done) { | ||
var uri = 'mongodb://localhost/mong-test2'; | ||
MongoClient.connect(uri, function (err, _db) { | ||
if (err) return done(err); | ||
db = _db; | ||
collection = db.collection('users'); | ||
collection = _db.collection('users'); | ||
should.exist(collection); | ||
lib.init(User, 'users'); | ||
collection.insert({_id: _id, random: random}, function (err) { | ||
collection.insert({_id: _id2, random: Math.random()}, function (err) { | ||
lib.connect(uri, done); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); | ||
before(function (done) { | ||
db = lib.create(); | ||
User = db.model('users'); | ||
db.connect(uri, done); | ||
}); | ||
after(function (done) { | ||
db.disconnect(done); | ||
}) | ||
it('should find the objects already in the collection', function (done) { | ||
@@ -41,0 +45,0 @@ var expectation = sinon.mock(); |
var lib = require('../../index'); | ||
var should = require('should'); | ||
function User() { | ||
} | ||
describe('Model.init', function () { | ||
it('should copy all of the static methods', function () { | ||
lib.init(User, 'users'); | ||
var User = lib.create().model('users'); | ||
should.exist(User.find); | ||
should.exist(User.findOne); | ||
should.exist(User.update); | ||
should.exist(User.findAndModify); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
14598
16
367
1
42
1
Updatedmongodb@^2.0.27