A simplified wrapper layer above the node-mongodb-native driver that provides a streamlined API for MongoDB database operations
npx @tessl/cli install tessl/npm-mongoskin@2.1.0Mongoskin is a JavaScript wrapper library that provides a simplified and enhanced API layer on top of the official MongoDB Node.js driver (node-mongodb-native). It offers callback-based operations with lazy connection handling, convenient helper methods, and streamlined connection management to reduce boilerplate code while maintaining compatibility with the underlying MongoDB driver's API.
npm install mongoskinconst mongoskin = require('mongoskin');const mongoskin = require('mongoskin');
// Connect to database using connection string
const db = mongoskin.db('mongodb://localhost:27017/myapp');
// Bind collections for easy access
db.bind('users');
db.bind('posts');
// Use enhanced collection methods
db.users.findById('507f1f77bcf86cd799439011', (err, user) => {
if (err) throw err;
console.log(user);
});
// Insert and find with simplified API
db.posts.insert({ title: 'Hello World', author: 'Alice' }, (err, result) => {
if (err) throw err;
db.posts.findItems({ author: 'Alice' }, (err, posts) => {
if (err) throw err;
console.log(posts);
db.close();
});
});Mongoskin implements a "Skin" class pattern where each MongoDB native class gets a corresponding wrapper:
Core database connection functionality with lazy connection management and enhanced connection methods.
function db(connectionString, options): SkinDb;
class MongoClient {
static connect(connectionString, options, callback): SkinDb;
}Enhanced collection interface with convenience methods for common database operations, including findById, updateById, and removeById.
class Collection {
find(query, options, callback): SkinCursor;
findItems(query, options, callback): Collection;
findEach(query, options, eachCallback): Collection;
findById(id, callback): Collection;
updateById(id, doc, callback): Collection;
removeById(id, callback): Collection;
bind(extendObject): void;
}Cursor operations for iterating through query results with automatic connection handling.
class Cursor {
toArray(callback): void;
each(callback): void;
next(callback): void;
limit(limit): Cursor;
skip(skip): Cursor;
sort(sort): Cursor;
}Database administration capabilities including user management, indexing, and server statistics.
class Admin {
listDatabases(callback): void;
serverStatus(callback): void;
addUser(username, password, options, callback): void;
removeUser(username, callback): void;
}GridFS operations for storing and retrieving large files in MongoDB with streaming support.
class GridStore {
static exist(db, name, callback): void;
static list(db, callback): void;
static read(db, name, callback): void;
static readlines(db, name, callback): void;
static unlink(db, name, callback): void;
open(mode, callback): void;
write(data, callback): void;
close(callback): void;
}Helper functions for common MongoDB operations including ObjectID conversion and validation.
const helper = {
toObjectID(hex): ObjectID;
isObjectID(idstr): boolean;
};
const utils = {
makeSkinClass(NativeClass, useNativeConstructor): Function;
};// Core wrapper classes
class SkinDb extends Db {
collection(name, options): SkinCollection;
bind(name, options): SkinCollection;
admin(): SkinAdmin;
gridStore(...args): SkinGridStore;
open(callback): SkinDb;
close(callback): SkinDb;
isOpen(): boolean;
}
class SkinCollection extends Collection {
bind(extendObject): void;
findItems(query, options, callback): SkinCollection;
findEach(query, options, eachCallback): SkinCollection;
findById(id, callback): SkinCollection;
updateById(id, doc, callback): SkinCollection;
removeById(id, callback): SkinCollection;
}
class SkinCursor extends Cursor {
// All standard cursor methods plus lazy connection handling
}
class SkinAdmin extends Admin {
// All standard admin methods with enhanced connection handling
}
class SkinGridStore extends GridStore {
// All standard GridStore methods with lazy connection
}
// Connection states
const STATE_CLOSE = 0;
const STATE_OPENNING = 1;
const STATE_OPEN = 2;
// Default port constant
const DEFAULT_PORT = 27017;
// MongoDB native types (re-exported)
class ObjectID {
constructor(id): ObjectID;
toString(): string;
static createFromHexString(hex): ObjectID;
static isValid(id): boolean;
}