# @sempervirens/dao

> A connection manager for MongoDB using Mongoose

Latest version **1.1.0** (published 2026-07-11) · MIT license · 0 weekly downloads

## Install

```sh
npm install @sempervirens/dao
pnpm add @sempervirens/dao
yarn add @sempervirens/dao
bun add @sempervirens/dao
```

## Health

**Score 50/100 (C)** — status: active.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2026-07-11 |
| First published | 2022-06-10 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 13.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Luke Dupuis |
| Maintainers | sempervirens |
| Keywords | MongoDB, Mongoose |

## Links

- npm: https://www.npmjs.com/package/@sempervirens/dao
- Repository: https://github.com/lukedupuis/sempervirens-dao
- Homepage: https://github.com/lukedupuis/sempervirens-dao#readme
- Issues: https://github.com/lukedupuis/sempervirens-dao/issues
- npm.io page: https://npm.io/package/@sempervirens/dao

## Dependencies (1)

- [mongoose](https://npm.io/package/mongoose.md) ^8.24.1

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [@temporal-architect/claude-plugin](https://npm.io/package/@temporal-architect/claude-plugin.md) — 335 weekly downloads

## Recent versions

- 1.1.0 (latest) — 2026-07-11
- 1.0.0 — 2026-07-06
- 0.3.0 — 2022-08-08
- 0.2.2 — 2022-07-17
- 0.2.1 — 2022-07-09
- 0.2.0 — 2022-07-09
- 0.1.1 — 2022-06-22
- 0.1.0 — 2022-06-11
- 0.0.9 — 2022-06-11
- 0.0.8 — 2022-06-10
- 0.0.7 — 2022-06-10
- 0.0.6 — 2022-06-10
- 0.0.5 — 2022-06-10
- 0.0.4 — 2022-06-10
- 0.0.3 — 2022-06-10
- … 2 more at https://npm.io/package/@sempervirens/dao/versions

## README

# Sempervirens DAO

A connection manager for MongoDB using Mongoose, it provides a simplified interface for creating connections to one or more MongoDB databases and accessing models.

![Tests badge](https://github.com/lukedupuis/sempervirens-dao/actions/workflows/main.yml/badge.svg?event=push) ![Version badge](https://img.shields.io/static/v1?label=Node&labelColor=30363c&message=16.x&color=blue) ![Version badge](https://img.shields.io/static/v1?label=MongoDB&labelColor=30363c&message=4.4&color=blue)

## Installation

`npm i @sempervirens/dao`

## Usage

### Install MongoDB

1.  Find your OS, download, install from [Install MongoDB](https://www.mongodb.com/docs/manual/installation/).
2.  (Optional) Include MongoDB Compass (free DB visualization) when installing MongoDB, or [Install MongoDB Compass](https://www.mongodb.com/try/download/compass) later.

### Create Mongoose Schemas

1.  Create a directory.
2.  Create [Mongoose schemas](https://mongoosejs.com/docs/guide.html#schemas) within the directory.

/app/src/schemas/test-schema.js
```
const testSchema = new mongoose.Schema({
  prop1: { type: String },
  prop2: { type: Number }
});
export default testSchema;
```

### Configure and Initialize

1. Import `@sempervirens/dao` into the app's entrypoint.
2. Import the schemas.
3. Call `dao.config`. `connectionOptions` are the [Mongoose connection options](https://mongoosejs.com/docs/connections.html#options).
4. Call `dao.initDb` for each database.

*Note 1: Only call `dao.config` once in one place, and only call `dao.initDb` in one place, for each database.*<br>
*Note 2: Multi-host connections are possible by passing `host`, `port`, and `connectionOptions` into `initDb` instead of `config`.*

/app/src/index.js
```
import dao from '@sempervirens/dao';
import testSchema from './schemas/testSchema.js';

// Call only once
dao.config({
  host: 'localhost',
  port: '27017',
  connectionOptions: {
    useNewUrlParser: true,
    useUnifiedTopology: true
  }
});

// Call for each DB
dao.initDb({
  name: 'testdb1',
  models: [
    { name: 'Test1', schema: testSchema }
  ]
});

// Connect to a different host
dao.initDb({
  host: 'anotherhost',
  port: 'anotherport',
  connectionOptions: {},
  name: 'testdb2',
  models: [
    { name: 'Test2', schema: testSchema }
  ]
});

```

### Access Models

1. Import `@sempervirens/dao` where access to models is needed.
2. Call methods on `dao` and `dao` databases to access models
3. Call [Mongoose methods](https://mongoosejs.com/docs/models.html) on models.

/app/src/example.js
```
import dao from '@sempervirens/dao';

// Any async function (doesn't have to be anonymous self-calling)
(async function() {

  // Access using dao's getModel
  const Test1a = dao.getModel('testdb', 'Test1');
  const docsA = await Test1a.find();

  // Access using DB's getModel
  const db = dao.getDb('testdb');
  const Test1b = db.getModel('Test1');
  const docsB = await Test1b.find();

})();
```

### API

#### dao (Singleton instance)

| Prop  | Type | Params | Description
|-------|------|--------|------------
| `config` | Function | `{ host = 'localhost', port = '27017', connectionOptions = {} }` | Configures the `dao` object with `host`, `port`, and `connectionOptions`.
| `initDb` | Function | `{ name = '', models = [{ name = '', schemas = new mongoose.Schema() }] }` | Connects to a database, adds the database to `dao.dbs`, creates each model, and attaches each model to `dao.dbs.dbName.models`
| `dbs` | Object | n/a | A literal `key: value` object where `key` is the database name given in `initDb`, and `value` is a `Db` instance.
| `getDb` | Function | `dbName = ''` | Gets a database from the `dao.dbs` map. Equivalent to `dao.dbs.dbName`.
| `getModel` | Function | `dbName = '', modelName = ''` | Gets a model from the `dao.dbs.dbName.models` map. Equivalent to `dao.dbs.dbName.models.modelName`.

#### Db (class)

| Prop  | Type | Params | Description
|-------|------|--------|------------
| `name` | String | n/a | Name of the database.
| `connection` | Object | n/a | The Mongoose connection object.
| `models` | Object | n/a | A literal `key: value` object where `key` is the model name given in `initDb` and `value` is a Mongoose Model.
| `getModel` | Function | `modelName = ''` | Gets a model from the `db.models` map. Equivalent to `db.models.modelName`.

---
_Source: https://npm.io/package/@sempervirens/dao · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
