UNPKG

1.02 kBJavaScriptView Raw
1// gridfs-stream
2
3/**
4 * Module dependencies.
5 */
6
7var GridWriteStream = require('./writestream')
8var GridReadStream = require('./readstream')
9
10module.exports = exports = function gridfsStream (mongo) {
11 function Grid (db) {
12 if (!(this instanceof Grid)) {
13 return new Grid(db);
14 }
15
16 // the db must be open b/c there is no `open` event emitted
17 this.db = db;
18 this.mongo = mongo;
19 this.tryParseObjectId = tryParseObjectId;
20 }
21
22 Grid.prototype.createWriteStream = function (filename, options) {
23 return new GridWriteStream(this, filename, options);
24 }
25
26 Grid.prototype.createReadStream = function (filename, options) {
27 return new GridReadStream(this, filename, options);
28 }
29
30 return Grid;
31}
32
33/**
34 * Attemps to parse `string` into an ObjectId
35 *
36 * @param {GridReadStream} self
37 * @param {String|ObjectId} string
38 * @return {ObjectId|Boolean}
39 */
40
41function tryParseObjectId (string) {
42 try {
43 return new this.mongo.BSONPure.ObjectID(string);
44 } catch (_) {
45 return false;
46 }
47}