Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

* add gridfs support

  • Loading branch information...
commit d65cf6b9d91f575f58d3d040cdf9b9183da8f7be 1 parent c586f2b
Gui Lin authored November 12, 2011
1  .gitignore
... ...
@@ -0,0 +1 @@
  1
+node_modules
4  History.md
Source Rendered
... ...
@@ -1,3 +1,7 @@
  1
+0.2.0 / 2011-11-06
  2
+==================
  3
+  * add SkinDB.gridfs
  4
+
1 5
 0.1.3 / 2011-05-24
2 6
 ==================
3 7
   * add SkinCollection.removeById
15  examples/gridfs.js
... ...
@@ -0,0 +1,15 @@
  1
+var mongoskin = require('../lib/mongoskin');
  2
+
  3
+var db = mongoskin.db('localhost/test');
  4
+
  5
+db.gridfs().open('test.txt', 'w', function(err, gs) {
  6
+    gs.write('blablabla', function(err, reply) {
  7
+        gs.close(function(err, reply){
  8
+            db.gridfs().open('test.txt', 'r' ,function(err, gs) {
  9
+                gs.read(function(err, reply){
  10
+                    console.log(reply);
  11
+                });
  12
+            });
  13
+        });
  14
+    });
  15
+});
8  lib/mongoskin/db.js
@@ -3,6 +3,7 @@ var __slice = Array.prototype.slice,
3 3
     events = require('events'),
4 4
     SkinAdmin = require('./admin').SkinAdmin,
5 5
     SkinCollection = require('./collection').SkinCollection,
  6
+    SkinGridStore = require('./gridfs').SkinGridStore,
6 7
     Db = mongodb.Db,
7 8
     Server = mongodb.Server,
8 9
 
@@ -133,6 +134,13 @@ SkinDb.prototype.collection = function(name) {
133 134
 };
134 135
 
135 136
 /**
  137
+ * gridfs
  138
+ */
  139
+SkinDb.prototype.gridfs = function() {
  140
+  return this.skinGridStore || (this.skinGridStore = new SkinGridStore(this));
  141
+}
  142
+
  143
+/**
136 144
  * bind additional method to SkinCollection
137 145
  *
138 146
  * 1. collectionName
39  lib/mongoskin/gridfs.js
... ...
@@ -0,0 +1,39 @@
  1
+var GridStore = require('mongodb').GridStore;
  2
+
  3
+/**
  4
+ * @param filename:  filename or ObjectId
  5
+ */
  6
+var SkinGridStore = exports.SkinGridStore = function(skinDb) {
  7
+  this.skinDb = skinDb;
  8
+}
  9
+
  10
+/**
  11
+ * @param filename: filename or ObjectId
  12
+ *  callback(err, gridStoreObject)
  13
+ */
  14
+SkinGridStore.prototype.open = function(filename, mode, options, callback){
  15
+  if(!callback){
  16
+    callback = options;
  17
+    options = undefined;
  18
+  }
  19
+  this.skinDb.open(function(err, db) {
  20
+      new GridStore(db, filename, mode, options).open(callback);
  21
+  });
  22
+}
  23
+
  24
+/**
  25
+ * @param filename: filename or ObjectId
  26
+ */
  27
+SkinGridStore.prototype.unlink = SkinGridStore.prototype.remove = function(filename, callback){
  28
+  this.skinDb.open(function(err, db) {
  29
+      GridStore.unlink(db, filename, callback);
  30
+  });
  31
+}
  32
+
  33
+SkinGridStore.prototype.exist = function(filename, callback){
  34
+  this.skinDb.open(function(err, db) {
  35
+      GridStore.exist(db, filename, callback);
  36
+  });
  37
+}
  38
+
  39
+exports.SkinGridStore = SkinGridStore;

0 notes on commit d65cf6b

Please sign in to comment.
Something went wrong with that request. Please try again.