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
Core database connection functionality with lazy connection management and enhanced connection methods.
Creates a database connection using a MongoDB connection string.
/**
* Create a database connection
* @param {string} connectionString - MongoDB connection URI
* @param {object} [options] - Connection options
* @returns {SkinDb} Database instance with lazy connection
*/
function db(connectionString, options);Usage Examples:
const mongoskin = require('mongoskin');
// Basic connection
const db = mongoskin.db('mongodb://localhost:27017/myapp');
// Connection with options
const db = mongoskin.db('mongodb://localhost:27017/myapp', {
native_parser: true,
auto_reconnect: true
});
// Connection with authentication
const db = mongoskin.db('mongodb://user:pass@localhost:27017/myapp');Static method for creating database connections, compatible with the native MongoDB driver.
/**
* Connect to MongoDB using MongoClient
* @param {string} connectionString - MongoDB connection URI
* @param {object} [options] - Connection options
* @param {function} [callback] - Optional callback function
* @returns {SkinDb} Database instance
*/
MongoClient.connect(connectionString, options, callback);Usage Examples:
const mongoskin = require('mongoskin');
// Direct connection (returns immediately)
const db = mongoskin.MongoClient.connect('mongodb://localhost:27017/myapp');
// With callback (traditional style)
mongoskin.MongoClient.connect('mongodb://localhost:27017/myapp', (err, db) => {
if (err) throw err;
// db is ready to use
db.close();
});Core methods available on database instances for connection management and collection access.
/**
* Get or create a collection
* @param {string} name - Collection name
* @param {object} [options] - Collection options
* @returns {SkinCollection} Collection instance
*/
collection(name, options);
/**
* Bind a collection to the database instance as a property
* @param {string} name - Collection name
* @param {object} [options] - Collection options
* @returns {SkinCollection} The bound collection
*/
bind(name, options);
/**
* Open database connection explicitly
* @param {function} callback - Callback function (err, db)
* @returns {SkinDb} this
*/
open(callback);
/**
* Close database connection
* @param {function} [callback] - Optional callback function
* @returns {SkinDb} this
*/
close(callback);
/**
* Check if database connection is open
* @returns {boolean} true if connection is open
*/
isOpen();Usage Examples:
const mongoskin = require('mongoskin');
const db = mongoskin.db('mongodb://localhost:27017/myapp');
// Get collection
const users = db.collection('users');
// Bind collection for easy access
db.bind('posts');
db.posts.findOne({}, (err, post) => {
console.log(post);
});
// Explicit connection management
db.open((err, database) => {
if (err) throw err;
console.log('Connected to:', database.databaseName);
// Check connection status
console.log('Is open:', db.isOpen()); // true
db.close(() => {
console.log('Connection closed');
});
});Common connection options supported by mongoskin.
interface ConnectionOptions {
/** Use native BSON parser for better performance */
native_parser?: boolean;
/** Automatically reconnect on connection loss */
auto_reconnect?: boolean;
/** Connection pool size */
poolSize?: number;
/** Write concern options */
w?: number | string;
/** Journal write concern */
j?: boolean;
/** Timeout for server selection */
serverSelectionTimeoutMS?: number;
/** Socket timeout */
socketTimeoutMS?: number;
/** Connection timeout */
connectTimeoutMS?: number;
}Access to MongoDB administrative operations.
/**
* Get admin interface for database operations
* @returns {SkinAdmin} Admin instance
*/
admin();Usage Example:
const db = mongoskin.db('mongodb://localhost:27017/myapp');
const admin = db.admin();
admin.listDatabases((err, dbs) => {
if (err) throw err;
console.log('Databases:', dbs.databases);
});Access to GridFS file storage operations.
/**
* Create GridStore instance for file operations
* @param {...any} args - GridStore constructor arguments
* @returns {SkinGridStore} GridStore instance
*/
gridStore(...args);Usage Example:
const db = mongoskin.db('mongodb://localhost:27017/myapp');
const gridStore = db.gridStore('myfile.txt', 'w');
gridStore.open((err, gs) => {
if (err) throw err;
gs.write('Hello World', (err, result) => {
if (err) throw err;
gs.close();
});
});