@dan-schel/db
TypeScript icon, indicating that this package has built-in type declarations

0.1.4 • Public • Published

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:

Package Sidebar

Install

npm i @dan-schel/db

Weekly Downloads

173

Version

0.1.4

License

MIT

Unpacked Size

71.3 kB

Total Files

72

Last publish

Collaborators

  • dan-schel