https://github.com/diegorbaquero/sequelize-express-findbyid
Find by id using a sequelize model, returning 404 error if not found
https://github.com/diegorbaquero/sequelize-express-findbyid
express expressjs find findbyid node nodejs sequelize sequelizejs
Last synced: about 1 year ago
JSON representation
Find by id using a sequelize model, returning 404 error if not found
- Host: GitHub
- URL: https://github.com/diegorbaquero/sequelize-express-findbyid
- Owner: DiegoRBaquero
- License: mit
- Created: 2017-10-24T22:09:17.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2020-04-30T19:41:10.000Z (about 6 years ago)
- Last Synced: 2025-04-03T16:04:48.590Z (about 1 year ago)
- Topics: express, expressjs, find, findbyid, node, nodejs, sequelize, sequelizejs
- Language: JavaScript
- Size: 19.5 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# sequelize-express-findbyid []() []() [](https://standardjs.com) [](LICENSE)
Find by id using a sequelize model, returning 404 error if not found
## Install
```sh
npm i -S sequelize-express-findbyid
or
npm install --save sequelize-express-findbyid
```
## API
##### `const sef = sequelize-express-findbyid (model[, primaryKey = 'id'])`
## Usage
```js
const sef = require(`sequelize-express-findbyid`)
const findById = sef(Model) // Primary key is 'id'
const findByOtherId = sef(Model, 'OtherId') // Primary key is 'OtherId'
router.get('/:id', (req, res, next) => {
findById(req.params.id).then(result => res.send(result))
.catch(e => next(e))
})
```
## Full await/async example inside express router
```js
const express = require('express')
const sef = require(`sequelize-express-findbyid`)
const router = express.Router()
const db = require(`./db`) // Your sequelize models
const Model = db.ModelName // Your model
const findById = sef(Model) // Primary key is 'id'
router.get('/:id', async (req, res, next) => {
res.send(await findById(req.params.id))
})
```
## Related
- [express-route-autoloader](https://github.com/DiegoRBaquero/express-route-autoloader)
- [express-sequelize-crud-router](https://github.com/DiegoRBaquero/express-sequelize-crud-router)
- [express-debug-async-wrap](https://github.com/DiegoRBaquero/express-debug-async-wrap)
## License
MIT Copyright © [Diego Rodríguez Baquero](https://diegorbaquero.com)