You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

@dan-schel/db

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dan-schel/db

Provides a common API for working with MongoDB or an in-memory database.

0.1.4
latest
Source
npmnpm
Version published
Weekly downloads
218
19.13%
Maintainers
1
Weekly downloads
 
Created
Source

NodeJS Database Library

codecov

Provides a common API for working with MongoDB or an in-memory database, with a migration system and type-safety. This means you can use MongoDB in prod, an in-memory database while unit testing, and whichever you prefer for local development.

Getting started

Every time your app starts, you'll want to create a database connection, and immediately run the migrations.

import type { Migration } from "@dan-schel/db";

// Empty to start (see docs/writing-database-migrations.md).
const migrations: Migration[] = [];

const db = createDb();
await db.runMigrations(migrations);

The implementation of createDb() depends on whether you're using MongoDB or an in-memory database.

With MongoDB

For MongoDB, you'll need to create a MongoClient using the mongodb package and pass it along to MongoDatabase.

import { MongoDatabase } from "@dan-schel/db";
import { MongoClient } from "mongodb";

function createDb() {
  // The connection string for the MongoDB server.
  const connectionString =
    "mongodb://username:password@localhost:27017/?authMechanism=DEFAULT";

  // Which database within the MongoDB server to use.
  const databaseName = "my-project";

  const client = new MongoClient(connectionString);
  await client.connect();

  return new MongoDatabase(client, databaseName);
}

With an in-memory database

For an in-memory database, things are a little simpler:

import { InMemoryDatabase } from "@dan-schel/db";

function createDb() {
  return new InMemoryDatabase();
}

Working with data

Now that your connection is established, you'll want to start working with some data:

Keywords

mongodb

FAQs

Package last updated on 09 Jul 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts