# @haensl/koa-objectid-middleware

> Middleware to parse MongoDB ObjectIDs from a Koa request path parameter.

Latest version **1.0.0** (published 2023-06-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install @haensl/koa-objectid-middleware
pnpm add @haensl/koa-objectid-middleware
yarn add @haensl/koa-objectid-middleware
bun add @haensl/koa-objectid-middleware
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2023-06-30 |
| First published | 2023-06-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 7.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | HP Dietz |
| Maintainers | haensl |
| Keywords | koa, middleware, mongodb, objectid, path, parameter, context, ctx, request |

## Links

- npm: https://www.npmjs.com/package/@haensl/koa-objectid-middleware
- Repository: https://github.com/haensl/koa-objectid-middleware
- Homepage: https://github.com/haensl/koa-objectid-middleware#readme
- Issues: https://github.com/haensl/koa-objectid-middleware/issues
- Funding: https://github.com/sponsors/haensl
- npm.io page: https://npm.io/package/@haensl/koa-objectid-middleware

## Dependencies (1)

- [@haensl/http](https://npm.io/package/@haensl/http.md) ^1.0.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

- 1.0.0 (latest) — 2023-06-30

## README

# @haensl/koa-objectid-middleware

Middleware to parse MongoDB ObjectIDs from a Koa request path parameter.

[![NPM](https://nodei.co/npm/@haensl%2Fkoa-objectid-middleware.png?downloads=true)](https://nodei.co/npm/@haensl%2Fkoa-objectid-middleware/)

[![npm version](https://badge.fury.io/js/@haensl%2Fkoa-objectid-middleware.svg)](http://badge.fury.io/js/@haensl%2Fkoa-objectid-middleware)

[![CircleCI](https://circleci.com/gh/haensl/koa-objectid-middleware.svg?style=svg)](https://circleci.com/gh/haensl/koa-objectid-middleware)

Use this middleware when a route requires a request parameter to be a [MongoDB ObjectID](https://www.mongodb.com/docs/manual/reference/method/ObjectId/), e.g. when specifying resource ids in routes such as `/resource/:id`.

When designing routes in an application that uses MongoDB as the database, using MongoDB Object IDs as path parameters can be a convenient and efficient approach. MongoDB Object IDs are unique identifiers automatically assigned to each document in a collection.

By including the Object ID as a path parameter in a route, you can easily target and manipulate specific documents. For example, a route like `/users/:userId` can be used to retrieve, update, or delete a user document based on its Object ID.

Using Object IDs as path parameters offers several benefits. It ensures the uniqueness of the identifier, simplifies route handling on the server side, and provides a consistent and standardized way to access individual documents. Additionally, it aligns well with MongoDB's query capabilities, allowing for efficient retrieval and modification of specific resources.

Overall, leveraging MongoDB Object IDs as path parameters in routes streamlines the development process and enhances the precision and control of interactions with the database.

## Installation

### Via `npm`

```bash
$ npm install -S @haensl/koa-objectid-middleware mongodb
```

### Via `yarn`

```bash
$ yarn add @haensl/koa-objectid-middleware mongodb
```

## Usage

1. [Install @haensl/koa-objectid-middleware](#installation)

2. Ensure you have [mongodb](https://www.npmjs.com/package/mongodb) installed as it's a peer dependency.

3. Use middleware in your projects:


    ```javascript
    // router.js - Router
    const requireObjectID = require('@haensl/koa-objectid-middleware');
    const Router = require('@koa/router');

    const router = new Router();

    // `GET /:id route handler`
    const get = require('./get');


    // require `ctx.request.params.id` to be a valid MongoDB ObjectID.
    router.get('/:id', requireObjectID(), get);

    module.exports = router;
    ```

    ```javascript
    // get.js - Get route
    module.exports = async (ctx) => {
      // Use the object id in your routes.
      const resource = await getResource(ctx.resourceId);

      // ...
    };
    ```

## Synopsis

The koa-object-middleware is exposed as a function that takes an options object and returns an `async` Koa middleware:


```javascript
({
  // The property path within ctx.request to parse the ObjectID from.
  // I.e. the name of the route param.
  ctxRequestPath: 'params.id',

  // The name of the property on ctx to store the ObjectID.
  ctxProperty: 'resourceId',

  // Set to false to ignore errors.
  // By default ctx.throw is called if the id parameter is missing (404) or malformed (400).
  throwOnError: true
}) => async (ctx, next) => { }
```

## [Changelog](CHANGELOG.md)

## [License](LICENSE)

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