npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

deepbase-mongodb

v3.2.0

Published

⚡ DeepBase MongoDB - MongoDB database driver

Readme

deepbase-mongodb

MongoDB driver for DeepBase.

Installation

npm install deepbase deepbase-mongodb

Prerequisites

Requires MongoDB server:

docker run -d -p 27017:27017 --name mongodb mongodb/mongodb-community-server:latest

Description

Stores data in MongoDB collections. Perfect for:

  • ✅ Production applications
  • ✅ Large datasets
  • ✅ Complex queries
  • ✅ Scalability and replication
  • ✅ Cloud deployment

Usage

import DeepBase from 'deepbase';
import MongoDriver from 'deepbase-mongodb';

const db = new DeepBase(new MongoDriver({
  url: 'mongodb://localhost:27017',
  database: 'myapp',
  collection: 'documents'
}));

await db.connect();

await db.set('users', 'alice', { name: 'Alice', age: 30 });
const alice = await db.get('users', 'alice');

Options

new MongoDriver({
  url: 'mongodb://localhost:27017',  // MongoDB connection URL
  database: 'deepbase',              // Database name (or use 'base')
  collection: 'documents',           // Collection name (or use 'name')
  nidAlphabet: 'ABC...',            // Alphabet for ID generation
  nidLength: 10                      // Length of generated IDs
})

Data Structure

Data is stored as MongoDB documents:

// Document structure
{
  _id: "users",
  alice: {
    name: "Alice",
    age: 30
  },
  bob: {
    name: "Bob",
    age: 25
  }
}

Features

Nested Operations

MongoDB's dot notation is used for nested operations:

await db.set('users', 'alice', 'address', 'city', 'New York');
// Stored as: { users.alice.address.city: "New York" }

Atomic Increment/Decrement

Uses MongoDB's $inc operator for atomic operations:

await db.inc('users', 'alice', 'balance', 100);
await db.dec('users', 'alice', 'balance', 50);

Upsert by Default

All set operations use upsert, creating documents if they don't exist.

Multi-Driver with MongoDB Primary

Common pattern: MongoDB for production, JSON for backup:

import DeepBase from 'deepbase';
import MongoDriver from 'deepbase-mongodb';
import { JsonDriver } from 'deepbase';

const db = new DeepBase([
  new MongoDriver({ 
    url: process.env.MONGO_URL,
    database: 'production'
  }),
  new JsonDriver({ path: './backup' })
], {
  writeAll: true,           // Replicate to JSON
  failOnPrimaryError: false // Fallback to JSON if MongoDB fails
});

await db.connect();

Migration from JSON

Migrate existing JSON data to MongoDB:

import DeepBase from '@deepbase/core';
import { JsonDriver } from 'deepbase';
import MongoDriver from '@deepbase/mongodb';

const db = new DeepBase([
  new JsonDriver({ path: './data' }),
  new MongoDriver({ url: 'mongodb://localhost:27017' })
]);

await db.connect();
await db.migrate(0, 1); // JSON -> MongoDB

console.log('Migration complete!');

Connection String Formats

// Local
url: 'mongodb://localhost:27017'

// With auth
url: 'mongodb://username:password@localhost:27017'

// MongoDB Atlas
url: 'mongodb+srv://username:[email protected]'

// Replica set
url: 'mongodb://host1:27017,host2:27017,host3:27017/?replicaSet=myReplSet'

Best Practices

  1. Use environment variables for connection strings
  2. Enable retryWrites for production: ?retryWrites=true
  3. Use connection pooling (handled automatically by driver)
  4. Index frequently accessed fields in MongoDB
  5. Use with JSON backup for redundancy

Error Handling

try {
  await db.connect();
} catch (error) {
  console.error('MongoDB connection failed:', error);
  // Fallback to JSON driver or handle gracefully
}

License

MIT - Copyright (c) Martin Clasen