# @valbo/mongodb-functions

> Functions to make the MongoDB driver easier to use

Latest version **3.0.0** (published 2023-12-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install @valbo/mongodb-functions
pnpm add @valbo/mongodb-functions
yarn add @valbo/mongodb-functions
bun add @valbo/mongodb-functions
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2023-12-22 |
| First published | 2021-02-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 26.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Cristóbal Valverde Albo |
| Maintainers | valbo |
| Keywords | mongodb |

## Links

- npm: https://www.npmjs.com/package/@valbo/mongodb-functions
- Repository: https://github.com/valverdealbo/mongodb-functions
- Issues: https://github.com/valverdealbo/mongodb-functions/issues
- npm.io page: https://npm.io/package/@valbo/mongodb-functions

## Dependencies (1)

- [mongodb](https://npm.io/package/mongodb.md) 6.3.0

## 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

- 3.0.0 (latest) — 2023-12-22
- 2.1.0 — 2021-07-25
- 2.0.1 — 2021-07-25
- 2.0.0 — 2021-07-25
- 1.0.4 — 2021-04-18
- 1.0.3 — 2021-02-19
- 1.0.2 — 2021-02-17

## README

# @valbo/mongodb-functions

Functions to make the MongoDB driver easier to use.

![npm (scoped)](https://img.shields.io/npm/v/@valbo/mongodb-functions)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
![Build Status](https://img.shields.io/github/workflow/status/valverdealbo/mongodb-functions/CI)
[![Coverage Status](https://coveralls.io/repos/github/valverdealbo/mongodb-functions/badge.svg?branch=main)](https://coveralls.io/github/valverdealbo/mongodb-functions?branch=main)
[![Known Vulnerabilities](https://snyk.io/test/github/valverdealbo/mongodb-functions/badge.svg?targetFile=package.json)](https://snyk.io/test/github/valverdealbo/mongodb-functions?targetFile=package.json)

## Install

```bash
npm install @valbo/mongodb-functions
```

## Usage

This package exports functions to parse results, throw errors and run transactions with the MongoDB driver.

### Parsing results

Some MongoDB driver functions return easy to use results, like **findOne()** which returns either the found document or null if not found. Other functions return 
complex objects that you have to parse in order to get the actual result.

The **parseResult()** function parses the result of the following MongoDB driver **Collection** methods:

| Collection method   | **parseResult()** returned value           |
| :---                | :---                                       |
| insertOne()         | The _id of the inserted document           |
| insertMany()        | An _id array of the inserted documents     |
| updateMany()        | The number of documents updated            |
| deleteMany()        | The number of documents deleted            |

```typescript
import { MongoClient, Collection, ObjectId } from 'mongodb';
import { parseResult } from '@valbo/mongodb-functions';

const client = await MongoClient.connect('mongodb://localhost:27017');

interface User {
  username: string;
  name: string;
}

const collection = client.db('tests').collection<User>('users');

const insertedId: ObjectId = await parseResult(collection.insertOne({ name: 'Alice' }));

const insertedIds: ObjectId[] = await parseResult(collection.insertMany([{ name: 'Bob' }, { name: 'Charlie' }]));

const updatedCount: number = await parseResult(collection.updateMany({}, { $set: { updated: true } }));

const deletedCount: number = await parseResult(collection.deleteMany({ name: 'Charles' }));
```

### Throw error if null or undefined

Sometimes you are fine with getting a null or undefined when you cannot find, update or delete a document, but sometimes you want to throw an error immediately.

The **throwIfNil()** function will throw the provided error if the result of a promise returns null or undefined:

```typescript
import { throwIfNil } from '@valbo/mongodb-functions';

const notFoundError = new Error('user not found');

const user: User = await throwIfNil(notFoundError, parseResult(collection.findOneAndUpdate({ name: 'Charles' }, { $set: { name: 'Charlie' } })));
// throws because document does not exist
```

### Throw error if duplicated

When the MongoDB driver throws an error you have to check if the **error.code** is 11000 to know if it's a duplication error.

The **throwIfDuplicated()** function checks that for you and throws the provided error when the promise throws a duplication error. If the promise throws any 
other kind of error then it is thrown as it is:

```typescript
import { throwIfDuplicated } from '@valbo/mongodb-functions';

const notUniqueError = new Error('user is not unique');

await collection.createIndex({ name: 1 }, { unique: true });

const user: User = await throwIfDuplicated(notUniqueError, parseResult(collection.insertOne({ name: 'Alice' })));
// throws because name is not unique
```

### Transactions

The MongoDB driver has two functions that you have to combine to run a transaction: **withSession()** and **withTransaction()**.

The **withTransaction()** function of this package combines the functionality of the those two functions and returns the result of the transaction:

```typescript
import { withTransaction } from '@valbo/mongodb-functions';

const newUser: User | null = await withTransaction(client, async session => {
  const parent: User = await throwIfNil(notFoundError, collection.findOneAndUpdate({ name: 'Robert' }, { $set: { children: 1 } }, { returnOriginal: false, session }));
  return await throwIfDuplicated(notUniqueError, collection.insertOne({ name: 'Daisy', parent: parent.name }, { session }));
});
```

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