forked from kissjs/node-mongoskin
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit cd524ad
Showing
9 changed files
with
856 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,204 @@ | ||
|
||
Install | ||
======== | ||
|
||
Soon, not publish to npm yet. | ||
|
||
Introduction | ||
======== | ||
**Mongoskin** is the future layer above [node-mongodb-native](https://github.com/christkv/node-mongodb-native) | ||
|
||
var mongo = require('mongoskin'); | ||
mongo.db('localhost/test-database').collection('articls').insert({a:1}, function(err, replies){ | ||
console.dir(replies); | ||
}); | ||
|
||
Is mongoskin synchronized? | ||
======== | ||
|
||
Nop! It is [future](http://en.wikipedia.org/wiki/Future_(programming)) | ||
|
||
Goals | ||
======== | ||
|
||
Provide full features of [node-mongodb-native](https://github.com/christkv/node-mongodb-native), | ||
and make it [future](http://en.wikipedia.org/wiki/Future_(programming)). | ||
|
||
Documentation | ||
======== | ||
* [Module API](#module-api) | ||
* [SkinServer](#skinserver) | ||
* [SkinDb](#skindb) | ||
* [SkinCollection](#skincollection) | ||
|
||
for more information, see the source. | ||
|
||
Module API | ||
-------- | ||
|
||
### MongoSkin Url format | ||
|
||
[*://][username:password@]host[:port][/database][?auto_reconnect[=true|false]]` | ||
|
||
e.g. | ||
|
||
localhost/blog | ||
mongo://admin:pass@127.0.0.1:27017/blog?auto_reconnect | ||
127.0.0.1?auto_reconnect=false | ||
|
||
|
||
### bind(collectionName) | ||
|
||
### bind(collectionName, SkinCollection) | ||
|
||
### bind(collectionName, extendObject1, extendObject2 ...) | ||
|
||
Bind SkinCollection to db properties. see [SkinDb.bind](#bindcollectionname) for more information. | ||
|
||
### db(databaseUrl) | ||
|
||
Get or create instance of SkinDb. | ||
|
||
### cluster(serverUrl1, serverUrl2, ...) | ||
|
||
Create SkinServer of native ServerCluster. e.g. | ||
|
||
var mongo = require('mongoskin'); | ||
var cluster = mongo.cluster('192.168.0.1:27017', '192.168.0.2:27017', '192.168.0.3:27017') | ||
var db = cluster.db('dbname', 'admin', 'pass'); | ||
|
||
### pair(leftServerUrl, rightServerUrl) | ||
|
||
Create instance of native ServerPair | ||
|
||
|
||
SkinServer | ||
-------- | ||
|
||
### SkinServer(server) | ||
|
||
Construct SkinServer from native Server instance. | ||
|
||
### db(dbname, username=null, password=null) | ||
|
||
Construct SkinDb from SkinServer. | ||
|
||
|
||
SkinDb | ||
-------- | ||
|
||
### SkinDb(db, username=null, password=null) | ||
|
||
Construct SkinDb. | ||
|
||
### open(callback) | ||
|
||
Connect to database, retrieval native Db instance, callback is function(err, db). | ||
|
||
### collection(collectionName) | ||
|
||
Retrieval SkinCollection instance of specified collection name. | ||
|
||
### bind(collectionName) | ||
|
||
### bind(collectionName, SkinCollection) | ||
|
||
### bind(collectionName, extendObject1, extendObject2 ...) | ||
|
||
Bind SkinCollection to db properties as a shortcut to db.collection(name). | ||
You can also bind additional methods to the SkinCollection, it is useful when | ||
you want to reuse a complex operation. This will also affect | ||
db.collection(name) method. | ||
|
||
e.g. | ||
|
||
db.bind('book', { | ||
firstBook: function(fn){ | ||
this.findOne(fn); | ||
} | ||
}); | ||
db.book.firstBook:(function(err, book){}); | ||
|
||
### all the methods from Db.prototype | ||
|
||
See [node-mongodb-native](https://github.com/christkv/node-mongodb-native) for more information. | ||
|
||
SkinCollection | ||
-------- | ||
|
||
### open(callback) | ||
|
||
Retrieval native Collection instance, callback is function(err, collection). | ||
|
||
### id(hex) | ||
|
||
Equivalent to | ||
|
||
db.bson_serilizer.ObjectID.createFromHexString(hex); | ||
|
||
### findItems(..., callback) | ||
|
||
Equivalent to | ||
|
||
collection.find(..., function(err, cursor){ | ||
cursor.toArray(callback); | ||
}); | ||
|
||
### findEach(..., callback) | ||
|
||
Equivalent to | ||
|
||
collection.find(..., function(err, cursor){ | ||
cursor.each(callback); | ||
}); | ||
|
||
### findById(id, ..., callback) | ||
|
||
Equivalent to | ||
|
||
collection.findOne({_id, ObjectID.createFromHexString(id)}, ..., callback); | ||
|
||
### updateById(_id, ..., callback) | ||
|
||
Equivalent to | ||
|
||
collection.update({_id, ObjectID.createFromHexString(id)}, ..., callback); | ||
|
||
### find(...) | ||
|
||
If the last parameter is function, it is equivalent to native Collection.find | ||
method, else it will return a future SkinCursor. | ||
|
||
e.g. | ||
|
||
// callback | ||
db.book.find({}, function(err, cursor){/* do something */}); | ||
// future SkinCursor | ||
db.book.find().toArray(function(err, books){/* do something */}); | ||
|
||
### all the methods from Collection.prototype | ||
|
||
See [node-mongodb-native](https://github.com/christkv/node-mongodb-native) for more information. | ||
|
||
checkCollectionName | ||
count | ||
createIndex | ||
distinct | ||
drop | ||
dropIndex | ||
dropIndexes | ||
ensureIndex | ||
find | ||
findAndModify | ||
findOne | ||
group | ||
indexInformation | ||
insert | ||
insertAll | ||
mapReduce | ||
normalizeHintField | ||
options | ||
remove | ||
rename | ||
save | ||
update |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('./lib/mongoskin'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
GLOBAL.DEBUG = true; | ||
|
||
var assert = require('assert'), | ||
mongo = require('../lib/mongoskin'); | ||
|
||
console.log('======== test MongoSkin.db ========'); | ||
['localhost/test', 'db://admin:admin@localhost:27017/test?auto_reconnect'] | ||
.forEach(function(server) { | ||
db = mongo.db(server); | ||
db.open(function(err, db) { | ||
assert.ok(db, 'fail to open ' + server); | ||
}); | ||
}); | ||
|
||
var bindToBlog = { | ||
first: function(fn) { | ||
this.findOne(fn); | ||
} | ||
}; | ||
|
||
console.log('======== test MongoSkin.bind ========'); | ||
mongo.bind('blog', bindToBlog); | ||
mongo.bind('users'); | ||
var db = mongo.db('localhost/test_mongoskin'); | ||
assert.equal(db.blog.first, bindToBlog.first); | ||
assert.ok(db.users); | ||
|
||
console.log('======== test SkinDb.bind ========'); | ||
db.bind('blog2', bindToBlog); | ||
db.bind('user2'); | ||
assert.equal(db.blog2.first, bindToBlog.first); | ||
assert.ok(db.user2); | ||
|
||
console.log('======== test SkinDb.open ========'); | ||
var db1, db2; | ||
db.open(function(err, db) { | ||
assert.ok(db, err && err.stack); | ||
db1 = db; | ||
assert.equal(db1.state, 'connected'); | ||
if (db2) { | ||
assert.equal(db1, db2, 'should alwayse be the same instance in db.open.'); | ||
} | ||
}); | ||
|
||
db.open(function(err, db) { | ||
assert.ok(db, err && err.stack); | ||
db2 = db; | ||
assert.equal(db2.state, 'connected'); | ||
if (db1) { | ||
assert.equal(db1, db2, 'should alwayse be the same instance in db.open.'); | ||
} | ||
}); | ||
|
||
console.log('======== test normal method of SkinDb ========'); | ||
db.createCollection('test_createCollection', function(err, collection) { | ||
assert.equal(db.db.state, 'connected'); | ||
assert.ok(collection, err && err.stack); | ||
}); | ||
|
||
|
||
console.log('======== test SkinDb.collection ========'); | ||
assert.equal(db.blog, db.collection('blog')); | ||
|
||
console.log('======== test SkinCollection.open ========'); | ||
var coll1, coll2; | ||
db.blog.open(function(err, coll) { | ||
assert.ok(coll, err && err.stack); | ||
coll1 = coll; | ||
if (coll2) { | ||
assert.equal(coll1, coll2, 'should be the same instance in collection.open'); | ||
} | ||
}); | ||
|
||
db.blog.open(function(err, coll) { | ||
assert.ok(coll, err && err.stack); | ||
coll2 = coll; | ||
if (coll1) { | ||
assert.equal(coll1, coll2, 'should be the same instance in collection.open'); | ||
} | ||
}); | ||
|
||
console.log('======== test normal method of SkinCollection ========'); | ||
db.collection('test_normal').ensureIndex([['a',1]], function(err, replies){ | ||
assert.ok(replies, err && err.stack); | ||
}); | ||
|
||
console.log('======== test SkinCollection.find ========'); | ||
collection = db.collection('test_find'); | ||
collection.insert([{a:1},{a:2},{a:3}], function(err, replies){ | ||
assert.ok(replies, err && err.stack); | ||
collection.findItems(function(err, items){ | ||
assert.ok(items, err && err.stack); | ||
console.log('found '+ items.length + ' items'); | ||
}); | ||
collection.findEach(function(err, item){ | ||
assert.ok(!err, err && err.stack); | ||
}); | ||
collection.find(function(err, cursor){ | ||
assert.ok(cursor, err && err.stack); | ||
}); | ||
|
||
console.log('======== test SkinCursor ========'); | ||
collection.find().toArray(function(err, items){ | ||
console.log('======== test find cursor toArray========'); | ||
assert.ok(items, err && err.stack); | ||
console.dir(items); | ||
}); | ||
collection.find().each(function(err, item){ | ||
console.log('======== test find cursor each========'); | ||
assert.ok(!err, err && err.stack); | ||
console.dir(item); | ||
}); | ||
}); | ||
|
||
/* | ||
console.log('======== test SkinDb.close ========'); | ||
db.close(); | ||
assert.equal(db.db.state, 'notConnected'); | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
var Admin = require('mongodb').Admin; | ||
|
||
var SkinAdmin = exports.SkinAdmin = function(db) { | ||
this.db = db; | ||
this.nativeAdmin = new Admin(this.db.nativeDb); | ||
} | ||
|
||
var bindSkin = function(name, method) { | ||
SkinAdmin.prototype[name] = function() { | ||
var args = arguments.length > 0 ? Array.prototype.slice.call(arguments, 0) : []; | ||
return this.db.open(function(err, db) { | ||
if (err) { | ||
args[args.length - 1](err); | ||
} else { | ||
method.apply(this.nativeAdmin, args); | ||
} | ||
}); | ||
}; | ||
}; | ||
|
||
for (var name in Admin.prototype) { | ||
var method = Admin.prototype[name]; | ||
bindSkin(name, method); | ||
} | ||
|
||
exports.SkinAdmin = SkinAdmin; |
Oops, something went wrong.