1.0.15 • Published 1 year ago

express-router-mongoose v1.0.15

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

express-router-mongoose

codecov CircleCI

Expose resource CRUD (Create Read Update Delete) routes in your Express app. Compatible with React Admin Simple Rest Data Provider. The lib is ORM agnostic. List of existing ORM connectors.

import crud from 'express-router-mongoose'

app.use(
  crud('/admin/users', {
    find: ({ filter, limit, offset, order }) =>
      User.findAndCountAll({ limit, offset, order, where: filter }),
    getOne: _id => User.findByPk(_id),
    create: body => User.create(body),
    update: (_id, body) => User.update(body, { where: { _id } }),
    remove: _id => User.remove({ where: { _id } }),
  })
)

Note: Content-Range header

For find methods, the response includes the total number of items in the collection in X-Total-Count header. You should use this response header for pagination and avoid using Content-Range header if your request does not include a Range header. Checkout this stackoverflow thread for more info.

If you are using ra-data-simple-rest, please refer to the documentation to use X-Total-Count for pagination.

Install

npm install express-router-mongoose

Usage

Simple use case

import express from 'express'
import crud from 'express-router-mongoose'
import mongooseCrud from 'express-router-mongoose-mongoose-v6-connector'
import { User } from './models'

const app = new express()
app.use(crud('/admin/users', mongooseCrud(User)))

Limit actions

import express from 'express'
import crud from 'express-router-mongoose'
import mongooseCrud from 'express-router-mongoose-mongoose-v6-connector'
import { User } from './models'

const app = new express()
app.use(
  crud('/admin/users', {
    ...mongooseCrud(User),
    remove: null,
  })
)

Custom filters

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

import express from 'express'
import { Op } from 'mongoose'
import crud from 'express-router-mongoose'
import mongooseCrud from 'express-router-mongoose-mongoose-v6-connector'
import { User } from './models'

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

Custom behavior & other ORMs

import express from 'express'
import crud from 'express-router-mongoose'
import { User } from './models'

const app = new express()
app.use(
  crud('/admin/users', {
    find: ({ filter, limit, offset, order, opts: { req, res } }) =>
      User.findAndCountAll({ limit, offset, order, where: filter }),
    getOne: (_id, { req, res }) => User.findByPk(_id),
    create: (body, { req, res }) => User.create(body),
    update: (_id, body, { req, res }) => User.update(body, { where: { _id } }),
    remove: (_id, { req, res }) => User.remove({ where: { _id } }),
  })
)

An ORM connector is a lib exposing an object of following shape:

interface Actions<R> {
  getOne: (identifier: string) => Promise<R | null>
  create: (body: R) => Promise<R & { _id: number | string }>
  remove: (_id: string) => Promise<any>
  update: (_id: string, data: R) => Promise<any>
  find: Find<R> = (conf: {
    filter: Record<string, any>
    limit: number
    offset: number
    order: Array<[string, string]>
  }) => Promise<{ rows: R[]; count: number }>
}

Search

Autocomplete

When using react-admin autocomplete reference field, a request is done to the API with a q filter. Thus, when using the autocomplete field in react-admin, you must specify the behavior to search the records. This could looks like:

app.use(
  crud('/admin/users', {
    search: async (q, limit) => {
      const { rows, count } = await User.findAndCountAll({
        limit,
        where: {
          [Op.or]: [
            { address: { [Op.iLike]: `${q}%` } },
            { zipCode: { [Op.iLike]: `${q}%` } },
            { city: { [Op.iLike]: `${q}%` } },
          ],
        },
      })

      return { rows, count }
    },
  })
)

express-router-mongoose ORM connectors might expose some search behaviors.

Contribute

This lib uses semantic-release. You need to write your commits following this nomenclature:

  • feat: A new feature
  • fix: A bug fix
  • docs: Documentation only changes
  • style: Changes that do not affect the meaning of the code (white-space, - formatting, missing semi-colons, etc)
  • refactor: A code change that neither fixes a bug nor adds a feature
  • perf: A code change that improves performance
  • test: Adding missing or correcting existing tests
  • chore: Changes to the build process or auxiliary tools and libraries such as documentation generation

To trigger a major version release write in the core of the commit message:

feat: my commit

BREAKING CHANGE: detail here

-hnqcmS7GFFgXX8apbOyyRZ71n1SwvNCywaqTQ

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.15

1 year ago

1.0.14

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago