# pretty-mongo

> A set of helper tools to test mongo-based projects

Latest version **0.1.7** (published 2020-12-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install pretty-mongo
pnpm add pretty-mongo
yarn add pretty-mongo
bun add pretty-mongo
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.7 |
| Published | 2020-12-17 |
| First published | 2020-04-26 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 23.3 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Ehab Khaireldin |
| Maintainers | ehab_khaireldin |
| Keywords | MongoDB, Mongo |

## Links

- npm: https://www.npmjs.com/package/pretty-mongo
- Repository: https://github.com/ehab180hb/pretty-mongo
- Homepage: https://github.com/ehab180hb/pretty-mongo#readme
- Issues: https://github.com/ehab180hb/pretty-mongo/issues
- npm.io page: https://npm.io/package/pretty-mongo

## Dependencies (4)

- [tslib](https://npm.io/package/tslib.md) ^1.10.0
- [lodash](https://npm.io/package/lodash.md) ^4.17.20
- [mongodb](https://npm.io/package/mongodb.md) 3.6.3
- [mongodb-uri](https://npm.io/package/mongodb-uri.md) ^0.9.7

## 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
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 0.1.7 (latest) — 2020-12-17
- 0.1.6 — 2020-05-24
- 0.0.5 — 2020-05-22
- 0.0.4 — 2020-05-08
- 0.0.3 — 2020-04-26
- 0.0.2 — 2020-04-26

## README

Get up and running with MongoDB in a few lines of code!

Pretty Mongo offers a set of handy methods that abstract away many of the complexities of using MongoDB in your project. Written in Typescript.

# The why

The Native MongoDB driver is awesome! But in the era of serverless and microservices, we find ourselves having to constantly write boilerplate code to get up and running.

This library allows you to easily establish one or more database connections, and will also provide an abstraction layer that works really well with Typescript and gives you a set of nicely-named methods that'll cover the use cases of the majority of projects out there.

# Installation

[![NPM Info](https://nodei.co/npm/pretty-mongo.png?downloads=true&downloadRank=true&stars=true)](https://www.npmjs.org/package/pretty-mongo)

[![Build Status](https://travis-ci.com/ehab180hb/pretty-mongo.svg?branch=master)](https://travis-ci.org/ehab180hb/pretty-mongo)

## Example usage

```typescript
import { initializeMongoDB, getDb, MongoCollectionMethods } from 'pretty-mongo'
const run = async () => {
  const uri = `mongodb://localhost/myApp`
  /*
   * We set the name here, allowing us to later create different connections
   * and give them different labels to fetch them.
   */
  const connectionName = 'myMainDbConnection'
  await initializeMongoDB({ uri, connectionName })

  // Now we could easily access that connection from a different module synchronously.
  const myMainDb = getDb({ connectionName })

  // This will return the native MongoDB `Collection` object.
  const userCollection = myMainDb.collection('Users')

  /*
   * The library also offers a set of methods that will make building your
   * app much easier. And it works really well with Typescript! By defining
   * your Model here, you'll be getting typed objects when for example scanning the DB.
   */
  interface UserDbModel {
    _id: ObjectId
    firstName: string
    lastName: string
    createdAt: Date
    balance: number
    isAdmin?: boolean
  }
  const usersCollectionMethods = new MongoCollectionMethods<UserDbModel>(userCollection)

  // When creating a new object, it will give you an autocomplete feature in your IDE.
  const created = await usersCollectionMethods.createOne({
    _id: new ObjectId(), // it will be created automatically if you do not include it
    firstName: 'Roger',
    lastName: 'Smith',
    balance: 0,
    createdAt: new Date(),
  })

  /*
   * And typescript will nag you if you tried to pass a field
   * that does not exist or provided the wrong type for a field
   */
  const created = await usersCollectionMethods.createOne({
    _id: new ObjectId(), // it will be created automatically if you do not include it
    lastName: 'Smith',
    balance: 0,
    createdAt: new Date(),
  }) // ts error, missing required field

  const created = await usersCollectionMethods.createOne({
    _id: new ObjectId(), // it will be created automatically if you do not include it
    firstName: 2,
    lastName: 'Smith',
    balance: 0,
    createdAt: new Date(),
  }) // ts error, firstName must be a string

  // It also gives you a set of new methods like
  const totalUserCount = await usersCollectionMethods.getTotalCount()
}

run()
```

### Using multiple database connections

```typescript
const dbConnections = [
  {
    uri: 'mongodb://localhost/myOlapDB',
    connectionName: 'OLAP_DB',
  },
  {
    uri: 'mongodb://localhost/myOltpDB',
    connectionName: 'OLTP_DB',
  },
]
await Promise.all(dbConnections.map(initializeMongoDB))

// Now in your modules get a db by its name
const myOlapDb = getDb({ connectionName: 'OLAP_DB' })

// Will throw if you tried to get a db that you did not initialize
const myNonExistingDb = getDb({ connectionName: 'SANTA' }) // throws: Database SANTA not yet initialized!
```

Full API documentation [here](https://github.com/ehab180hb/pretty-mongo/blob/master/API.md)

## Author

[Ehab Khaireldin](https://github.com/ehab180hb)
Feel free to get in touch, raise issues or submit feature requests!

## License

This project is licensed under the MIT License

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