A simplified wrapper layer above the node-mongodb-native driver that provides a streamlined API for MongoDB database operations
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
GridFS operations for storing and retrieving large files in MongoDB with streaming support.
Create GridStore instances through the database interface.
/**
* Create GridStore instance for file operations
* @param {string} filename - Name of the file
* @param {string} mode - File mode ('r', 'w', 'w+')
* @param {object} [options] - GridStore options
* @returns {SkinGridStore} GridStore instance
*/
db.gridStore(filename, mode, options);Static methods for common file operations without creating GridStore instances.
/**
* Check if file exists in GridFS
* @param {SkinDb} db - Database instance
* @param {string} filename - Name of the file
* @param {function} callback - Callback function (err, exists)
* @returns {void}
*/
GridStore.exist(db, filename, callback);
/**
* List all files in GridFS
* @param {SkinDb} db - Database instance
* @param {function} callback - Callback function (err, files)
* @returns {void}
*/
GridStore.list(db, callback);
/**
* Read entire file from GridFS
* @param {SkinDb} db - Database instance
* @param {string} filename - Name of the file
* @param {function} callback - Callback function (err, data)
* @returns {void}
*/
GridStore.read(db, filename, callback);
/**
* Read file lines from GridFS
* @param {SkinDb} db - Database instance
* @param {string} filename - Name of the file
* @param {function} callback - Callback function (err, lines)
* @returns {void}
*/
GridStore.readlines(db, filename, callback);
/**
* Delete file from GridFS
* @param {SkinDb} db - Database instance
* @param {string} filename - Name of the file
* @param {function} callback - Callback function (err, result)
* @returns {void}
*/
GridStore.unlink(db, filename, callback);Usage Examples:
const mongoskin = require('mongoskin');
const db = mongoskin.db('mongodb://localhost:27017/myapp');
// Check if file exists
mongoskin.GridStore.exist(db, 'myfile.txt', (err, exists) => {
if (err) throw err;
console.log('File exists:', exists);
});
// List all files
mongoskin.GridStore.list(db, (err, files) => {
if (err) throw err;
console.log('Files in GridFS:');
files.forEach(file => console.log(`- ${file.filename} (${file.length} bytes)`));
});
// Read entire file
mongoskin.GridStore.read(db, 'document.pdf', (err, data) => {
if (err) throw err;
console.log('Read', data.length, 'bytes from document.pdf');
// data is a Buffer containing the file contents
});
// Read file as lines
mongoskin.GridStore.readlines(db, 'textfile.txt', (err, lines) => {
if (err) throw err;
console.log('File lines:');
lines.forEach((line, index) => console.log(`${index + 1}: ${line}`));
});
// Delete file
mongoskin.GridStore.unlink(db, 'oldfile.txt', (err, result) => {
if (err) throw err;
console.log('File deleted:', result);
});Instance methods for writing files to GridFS.
/**
* Open GridStore for file operations
* @param {function} callback - Callback function (err, gridStore)
* @returns {void}
*/
open(callback);
/**
* Write data to GridStore
* @param {string|Buffer} data - Data to write
* @param {function} callback - Callback function (err, gridStore)
* @returns {void}
*/
write(data, callback);
/**
* Write file from file system to GridFS
* @param {string} file - Path to source file
* @param {function} callback - Callback function (err, result)
* @returns {void}
*/
writeFile(file, callback);
/**
* Close GridStore and finalize file
* @param {function} callback - Callback function (err, result)
* @returns {void}
*/
close(callback);Usage Examples:
const mongoskin = require('mongoskin');
const db = mongoskin.db('mongodb://localhost:27017/myapp');
// Write text data to GridFS
const gridStore = db.gridStore('mytext.txt', 'w');
gridStore.open((err, gs) => {
if (err) throw err;
gs.write('Hello, GridFS world!', (err, gs) => {
if (err) throw err;
gs.close((err, result) => {
if (err) throw err;
console.log('File written successfully:', result);
});
});
});
// Write binary data
const gridStore2 = db.gridStore('binary.dat', 'w');
gridStore2.open((err, gs) => {
if (err) throw err;
const binaryData = Buffer.from([0x48, 0x65, 0x6c, 0x6c, 0x6f]);
gs.write(binaryData, (err, gs) => {
if (err) throw err;
gs.close((err, result) => {
if (err) throw err;
console.log('Binary file written');
});
});
});
// Write file from filesystem
const gridStore3 = db.gridStore('uploaded.jpg', 'w');
gridStore3.writeFile('/path/to/source/image.jpg', (err, result) => {
if (err) throw err;
console.log('File uploaded to GridFS:', result);
});Instance methods for reading files from GridFS.
/**
* Read data from GridStore
* @param {number} [length] - Number of bytes to read
* @param {function} callback - Callback function (err, data)
* @returns {void}
*/
read(length, callback);
/**
* Read file to filesystem
* @param {string} filename - Destination file path
* @param {function} callback - Callback function (err, result)
* @returns {void}
*/
readFile(filename, callback);
/**
* Seek to position in file
* @param {number} position - Byte position to seek to
* @param {function} callback - Callback function (err, gridStore)
* @returns {void}
*/
seek(position, callback);
/**
* Get current position in file
* @returns {number} Current position
*/
tell();
/**
* Rewind to beginning of file
* @param {function} callback - Callback function (err, gridStore)
* @returns {void}
*/
rewind(callback);Usage Examples:
const mongoskin = require('mongoskin');
const db = mongoskin.db('mongodb://localhost:27017/myapp');
// Read file data
const gridStore = db.gridStore('mytext.txt', 'r');
gridStore.open((err, gs) => {
if (err) throw err;
gs.read((err, data) => {
if (err) throw err;
console.log('File contents:', data.toString());
gs.close();
});
});
// Read partial data
const gridStore2 = db.gridStore('largefile.dat', 'r');
gridStore2.open((err, gs) => {
if (err) throw err;
// Read first 100 bytes
gs.read(100, (err, data) => {
if (err) throw err;
console.log('First 100 bytes:', data.length);
gs.close();
});
});
// Save GridFS file to filesystem
const gridStore3 = db.gridStore('document.pdf', 'r');
gridStore3.readFile('/tmp/downloaded.pdf', (err, result) => {
if (err) throw err;
console.log('File downloaded to /tmp/downloaded.pdf');
});
// Seek and read
const gridStore4 = db.gridStore('datafile.bin', 'r');
gridStore4.open((err, gs) => {
if (err) throw err;
gs.seek(1000, (err, gs) => {
if (err) throw err;
console.log('Current position:', gs.tell());
gs.read(50, (err, data) => {
if (err) throw err;
console.log('Data at position 1000:', data);
gs.close();
});
});
});Stream-based operations for efficient handling of large files.
/**
* Create readable stream from GridStore
* @param {boolean} [autoclose] - Auto-close stream when done
* @returns {Stream} Readable stream
*/
stream(autoclose);
/**
* Create writable stream to GridStore
* @returns {Stream} Writable stream
*/
writeStream();Usage Examples:
const mongoskin = require('mongoskin');
const fs = require('fs');
const db = mongoskin.db('mongodb://localhost:27017/myapp');
// Stream file from GridFS to filesystem
const gridStore = db.gridStore('largefile.mp4', 'r');
gridStore.open((err, gs) => {
if (err) throw err;
const readStream = gs.stream(true);
const writeStream = fs.createWriteStream('/tmp/output.mp4');
readStream.pipe(writeStream);
writeStream.on('close', () => {
console.log('File streamed successfully');
});
});
// Stream file from filesystem to GridFS
const gridStore2 = db.gridStore('upload.zip', 'w');
gridStore2.open((err, gs) => {
if (err) throw err;
const readStream = fs.createReadStream('/path/to/large/file.zip');
const writeStream = gs.writeStream();
readStream.pipe(writeStream);
writeStream.on('close', () => {
console.log('File uploaded via stream');
});
});Operations for managing file metadata and properties.
/**
* Get file information and metadata
* @param {function} callback - Callback function (err, fileInfo)
* @returns {void}
*/
metadata(callback);
/**
* Set file metadata
* @param {object} metadata - Metadata object
* @param {function} callback - Callback function (err, result)
* @returns {void}
*/
setMetadata(metadata, callback);
/**
* Get file length
* @returns {number} File length in bytes
*/
length;
/**
* Get file content type
* @returns {string} Content type
*/
contentType;
/**
* Get file upload date
* @returns {Date} Upload date
*/
uploadDate;
/**
* Get file MD5 checksum
* @returns {string} MD5 hash
*/
md5;Usage Examples:
const mongoskin = require('mongoskin');
const db = mongoskin.db('mongodb://localhost:27017/myapp');
// Get file metadata
const gridStore = db.gridStore('document.pdf', 'r');
gridStore.open((err, gs) => {
if (err) throw err;
console.log('File length:', gs.length);
console.log('Content type:', gs.contentType);
console.log('Upload date:', gs.uploadDate);
console.log('MD5 checksum:', gs.md5);
gs.metadata((err, metadata) => {
if (err) throw err;
console.log('File metadata:', metadata);
gs.close();
});
});
// Set custom metadata
const gridStore2 = db.gridStore('photo.jpg', 'w');
gridStore2.open((err, gs) => {
if (err) throw err;
gs.setMetadata({
author: 'John Doe',
camera: 'Canon EOS',
location: 'New York'
}, (err, result) => {
if (err) throw err;
// Write image data
const imageData = fs.readFileSync('/path/to/photo.jpg');
gs.write(imageData, (err, gs) => {
if (err) throw err;
gs.close((err, result) => {
if (err) throw err;
console.log('Photo uploaded with metadata');
});
});
});
});Direct access to GridFS collections for advanced operations.
/**
* Get GridFS files collection
* @returns {Collection} Files collection (fs.files)
*/
files();
/**
* Get GridFS chunks collection
* @returns {Collection} Chunks collection (fs.chunks)
*/
chunks();Usage Examples:
const db = mongoskin.db('mongodb://localhost:27017/myapp');
// Query files collection directly
const filesCollection = db.collection('fs.files');
filesCollection.findItems({ filename: /\.jpg$/ }, (err, imageFiles) => {
if (err) throw err;
console.log('Image files in GridFS:');
imageFiles.forEach(file => {
console.log(`- ${file.filename} (${file.length} bytes)`);
});
});
// Query chunks for a specific file
const chunksCollection = db.collection('fs.chunks');
chunksCollection.findItems({ files_id: someFileId }, (err, chunks) => {
if (err) throw err;
console.log(`File has ${chunks.length} chunks`);
});