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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@sempervirens/dao

v0.3.0

Published

A connection manager for MongoDB using Mongoose

Downloads

5

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 Version badge Version badge

Installation

npm i @sempervirens/dao

Usage

Install MongoDB

  1. Find your OS, download, install from Install MongoDB.
  2. (Optional) Include MongoDB Compass (free DB visualization) when installing MongoDB, or Install MongoDB Compass later.

Create Mongoose Schemas

  1. Create a directory.
  2. Create Mongoose 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.
  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. 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 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.