Implementing a GridStore Object from Node.js

The GridStore object acts as a representation of a binary file stored in the MongoDB GridFS. It provides the basic file interface to read and write files stored in GridFS. For example, you can stream reads and writes from GridFS, read chunks of a file without needing to read the entire file, etc. These are all things that you could do within Node.js, but without the added benefits that come with MongoDB, such as replication and high availability.

You can instantiate the GridStore object by calling the GridStore() constructor from an instance of the Db object. For example, the following code connects to MongoDB and creates a GridStore object attached to the fs collection:

MongoClient.connect("mongodb://localhost/", function(err, db) {
  var gridStore = new GridStore(db, "word_file", 'w'),
});

Notice that the first parameter to GridStore() is the database. The second parameter is the unique ID for the file in GridFS, and the third is the mode with which GridStore is opened and corresponds to the standard file modes.

The GridStore object provides several methods that allow you to read, access, and write files to the MongoDB GridFS, just as if it were a normal file. Table 17.2 lists the most useful methods available on GridStore objects.

Image
Image

Table 17.2 Methods to read and writes files from GridStore objects

The GridStore object also contains several static methods that allow you to access files in GridFS without creating an instance of a specific file. These methods allow you to determine existence, list files, and delete files. Table 17.3 describes these methods.

Image

Table 17.3 Static methods available in the GridStore class to manage GridFS files

For the most part, using the GridStore object is intuitive if you’re familiar with reading from and writing to files. Listing 17.2 illustrates some of the basics of implementing a GridStore object in a Node.js application. The GridStore object is created on line 5, then opened on line 6, and the contents of a local file are written to it on line 10. Then on line 12 the full contents are read. To illustrate seek(), line 16 reads 50 bytes at position 100. Then finally the file is closed on line 19 and deleted on line 20. Figure 17.6 shows the output from Listing 17.2.

Listing 17.2 gridstore_fs.js: Implementing a basic GridStore object to store a file in the MongoDB GridFS


01 var MongoClient = require('mongodb').MongoClient,
02     GridStore = require('mongodb').GridStore,
03     Grid= require('mongodb').Grid;
04 MongoClient.connect("mongodb://localhost/", function(err, db) {
05   var gridStore = new GridStore(db, "word_file", 'w'),
06   gridStore.open(function(err, gridStore) {
07     GridStore.exist(db,"word_file", function(err, results) {
08       console.log("File created? " + results);
09     });
10     gridStore.writeFile('./words.txt', function(err, results) {
11       console.log(" File Written.");
12       GridStore.read(db, "word_file", function(err, fileData) {
13         console.log(" Full Read: ");
14         console.log(fileData.toString());
15         gridStore.seek(100, function(err, gridStore) {
16           gridStore.read(50, function(err, data) {
17             console.log(" Read 50 bytes at position 100: ");
18             console.log(data.toString());
19             gridStore.close();
20             GridStore.unlink(db, "word_file", function(err, results){
21               console.log(" File Deleted: " + results._id);
22               db.close();
23             });
24           });
25         });
26       });
27     });
28   });
29 });


Image

Figure 17.6 Implementing a basic GridStore object to store data in the MongoDB GridFS.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.215.1