Skip to content

Commit

Permalink
feat: ✨ add filters option in sequelize crud
Browse files Browse the repository at this point in the history
  • Loading branch information
nicgirault committed Sep 7, 2020
1 parent 548ba78 commit 78112d5
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 11 deletions.
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ app.use(crud('/admin/users', sequelizeCrud(User)))

```ts
import express from 'express'
import crud from 'express-sequelize-crud'
import crud, { sequelizeCrud } from 'express-sequelize-crud'
import { User } from './models'

const app = new express()
Expand All @@ -45,6 +45,28 @@ app.use(
)
```

### Custom filters

Custom filters such as case isensitive filter can be perform like this:

```ts
import express from 'express'
import { Op } from 'sequelize'
import crud, { sequelizeCrud } from 'express-sequelize-crud'
import { User } from './models'

const app = new express()
app.use(
crud('/admin/users', sequelizeCrud(User), {
filters: {
email: value => ({
[Op.iLike]: value,
}),
},
})
)
```

### Custom behavior & other ORMs

```ts
Expand Down
26 changes: 22 additions & 4 deletions src/getList/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { RequestHandler } from 'express'
import mapValues from 'lodash/mapValues'

import { setGetListHeaders } from './headers'

Expand All @@ -16,10 +17,14 @@ export type Search<R> = (

export const getMany = <R>(
doGetFilteredList: GetList<R>,
doGetSearchList?: Search<R>
doGetSearchList?: Search<R>,
filtersOption?: FiltersOption
): RequestHandler => async (req, res, next) => {
try {
const { q, limit, offset, filter, order } = parseQuery(req.query)
const { q, limit, offset, filter, order } = parseQuery(
req.query,
filtersOption
)

if (!q) {
const { rows, count } = await doGetFilteredList({
Expand All @@ -45,7 +50,7 @@ export const getMany = <R>(
}
}

export const parseQuery = (query: any) => {
export const parseQuery = (query: any, filtersOption?: FiltersOption) => {
const { range, sort, filter } = query

const [from, to] = range ? JSON.parse(range) : [0, 100]
Expand All @@ -55,8 +60,21 @@ export const parseQuery = (query: any) => {
return {
offset: from,
limit: to - from + 1,
filter: filters,
filter: getFilter(filters, filtersOption),
order: [sort ? JSON.parse(sort) : ['id', 'ASC']] as [[string, string]],
q,
}
}

const getFilter = (
filter: Record<string, any>,
filtersOption?: FiltersOption
) =>
mapValues(filter, (value, key) => {
if (filtersOption && filtersOption[key]) {
return filtersOption[key](value)
}
return value
})

export type FiltersOption = Record<string, (value: any) => any>
18 changes: 15 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Router } from 'express'
import bodyParser from 'body-parser'
import { getMany, GetList, Search } from './getList'
import { getMany, GetList, Search, FiltersOption } from './getList'
import { getOne, GetOne } from './getOne'
import { create, Create } from './create'
import { update, Update } from './update'
Expand All @@ -15,20 +15,32 @@ export interface Actions<I extends string | number, R> {
search: Search<R> | null
}

interface CrudOptions {
filters: FiltersOption
}

export { sequelizeSearchFields } from './sequelize/searchList'
export { sequelizeCrud } from './sequelize'

export { GetOne, Create, Destroy, Update, GetList, Search }

export const crud = <I extends string | number, R>(
path: string,
actions: Partial<Actions<I, R>>
actions: Partial<Actions<I, R>>,
options?: CrudOptions
) => {
const router = Router()
router.use(bodyParser.json())

if (actions.getList)
router.get(path, getMany(actions.getList, actions.search || undefined))
router.get(
path,
getMany(
actions.getList,
actions.search || undefined,
options && options.filters
)
)

if (actions.getOne) {
router.get(`${path}/:id`, getOne(actions.getOne))
Expand Down
7 changes: 4 additions & 3 deletions src/sequelize/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ export const sequelizeCrud = <I extends string | number, R>(
create: async body => model.create(body),
update: async (id, body) => model.update(body, { where: { id } }),
getOne: async id => model.findByPk(id),
getList: async ({ filter, limit, offset, order }) =>
model.findAndCountAll({
getList: async ({ filter, limit, offset, order }) => {
return model.findAndCountAll({
limit,
offset,
order,
where: filter,
raw: true,
}),
})
},
destroy: async id => {
const record = await model.findByPk(id)
await record.destroy()
Expand Down

0 comments on commit 78112d5

Please sign in to comment.