Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ryanburnette/find-one-record
https://github.com/ryanburnette/find-one-record
Last synced: 17 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/ryanburnette/find-one-record
- Owner: ryanburnette
- License: isc
- Created: 2021-07-14T12:10:42.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-08-25T14:55:28.000Z (over 3 years ago)
- Last Synced: 2024-11-19T19:52:25.138Z (3 months ago)
- Language: JavaScript
- Size: 7.81 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: license
Awesome Lists containing this project
README
# [find-one-record](https://github.com/ryanburnette/find-one-record)
Express middleware factory for finding one record with Sequelize and attaching
it as an attribute of `req`, or returning 404 if not found.## Installation
```bash
npm install @ryanburnette/find-one-record
```## Usage
Use it in an Express + Sequelize app.
```js
var findOneRecordMiddlewareFactory = require('@ryanburnette/find-one-record');
var findOneWidget = findOneRecordMiddlewareFactory({
pkName: 'id',
model: db.Widget
});
app.get('/api/widgets/:id', findOneWidget, function (req, res) {
res.json(req.record);
});
```## Behavior
The primary key is expected to be `req.body[pkName]`. If a record isn't found a
404 is returned and the middleware chain is broken. The found record is
`req.record`.## Options
- `pkName` primary key attribute name, defaults to `id`
- `model` required, pass in the Sequelize model