Skip to content

Commit

Permalink
* add gridfs support
Browse files Browse the repository at this point in the history
  • Loading branch information
guileen committed Nov 12, 2011
1 parent c586f2b commit d65cf6b
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules
4 changes: 4 additions & 0 deletions History.md
@@ -1,3 +1,7 @@
0.2.0 / 2011-11-06
==================
* add SkinDB.gridfs

0.1.3 / 2011-05-24
==================
* add SkinCollection.removeById
Expand Down
15 changes: 15 additions & 0 deletions examples/gridfs.js
@@ -0,0 +1,15 @@
var mongoskin = require('../lib/mongoskin');

var db = mongoskin.db('localhost/test');

db.gridfs().open('test.txt', 'w', function(err, gs) {
gs.write('blablabla', function(err, reply) {
gs.close(function(err, reply){
db.gridfs().open('test.txt', 'r' ,function(err, gs) {
gs.read(function(err, reply){
console.log(reply);
});
});
});
});
});
8 changes: 8 additions & 0 deletions lib/mongoskin/db.js
Expand Up @@ -3,6 +3,7 @@ var __slice = Array.prototype.slice,
events = require('events'),
SkinAdmin = require('./admin').SkinAdmin,
SkinCollection = require('./collection').SkinCollection,
SkinGridStore = require('./gridfs').SkinGridStore,
Db = mongodb.Db,
Server = mongodb.Server,

Expand Down Expand Up @@ -132,6 +133,13 @@ SkinDb.prototype.collection = function(name) {
return collection;
};

/**
* gridfs
*/
SkinDb.prototype.gridfs = function() {
return this.skinGridStore || (this.skinGridStore = new SkinGridStore(this));
}

/**
* bind additional method to SkinCollection
*
Expand Down
39 changes: 39 additions & 0 deletions lib/mongoskin/gridfs.js
@@ -0,0 +1,39 @@
var GridStore = require('mongodb').GridStore;

/**
* @param filename: filename or ObjectId
*/
var SkinGridStore = exports.SkinGridStore = function(skinDb) {
this.skinDb = skinDb;
}

/**
* @param filename: filename or ObjectId
* callback(err, gridStoreObject)
*/
SkinGridStore.prototype.open = function(filename, mode, options, callback){
if(!callback){
callback = options;
options = undefined;
}
this.skinDb.open(function(err, db) {
new GridStore(db, filename, mode, options).open(callback);
});
}

/**
* @param filename: filename or ObjectId
*/
SkinGridStore.prototype.unlink = SkinGridStore.prototype.remove = function(filename, callback){
this.skinDb.open(function(err, db) {
GridStore.unlink(db, filename, callback);
});
}

SkinGridStore.prototype.exist = function(filename, callback){
this.skinDb.open(function(err, db) {
GridStore.exist(db, filename, callback);
});
}

exports.SkinGridStore = SkinGridStore;

0 comments on commit d65cf6b

Please sign in to comment.