npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

caddev-express-sequelize-crud

v1.0.1

Published

React Admin backend with Express and Sequelize. 1 line per resource!

Downloads

1

Readme

Changes from lalalilo/express-sequelize-crud

This repository is a fork of lalalilo/express-sequelize-crud. It only adds a few changes:

  • Add of an auth method for the crud routers with signature (req, res, next) => void
  • Add of the parameters req and res in the actions parameters, to allow management specific permissions thanks to a token for exemple, or HTTP cache headers setting.
  • Standalone build (out of CircleCI)

express-sequelize-crud

import crud, { sequelizeCrud } from 'express-sequelize-crud-auth'

app.use(crud('/admin/users', sequelizeCrud(User)))

Expose resource CRUD (Create Read Update Delete) routes for Express & Sequelize (and other ORMs in v6+). Compatible with React Admin Simple Rest Data Provider

Note: Content-Range header

For getList 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.

codecov CircleCI

Install

yarn add express-sequelize-crud-auth

Usage

Simple use case

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

const app = new express()
const auth = (req, res, next) => {
  next()
} // Define your authentification checks here
app.use(crud('/admin/users', auth, sequelizeCrud(User)))

Limit actions

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

const app = new express()
const auth = (req, res, next) => {
  next()
} // Define your authentification checks here
app.use(
  crud('/admin/users', auth, {
    ...sequelizeCrud(User),
    destroy: null,
  })
)

Custom filters

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

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

const app = new express()
const auth = (req, res, next) => {
  next()
} // Define your authentification checks here
app.use(
  crud('/admin/users', auth, sequelizeCrud(User), {
    filters: {
      email: value => ({
        [Op.iLike]: value,
      }),
    },
  })
)

Custom behavior & other ORMs

import express from 'express'
import crud from 'express-sequelize-crud-auth'
import { User } from './models'

const app = new express()
const auth = (req, res, next) => {
  next()
} // Define your authentification checks here
app.use(
  crud('/admin/users', auth, {
    getList: (req, res, { filter, limit, offset, order }) =>
      User.findAndCountAll({ limit, offset, order, where: filter }),
    getOne: (req, res, id) => User.findByPk(id),
    create: (req, res, body) => User.create(body),
    update: (req, res, id, body) => User.update(body, { where: { id } }),
    destroy: (req, res, id) => User.destroy({ where: { id } }),
  })
)

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 (req, conf: { q, filter, limit, offset, order }) => {
      const { rows, count } = await User.findAndCountAll({
        conf.limit,
        where: {
          [Op.or]: [
            { address: { [Op.iLike]: `${conf.q}%` } },
            { zipCode: { [Op.iLike]: `${conf.q}%` } },
            { city: { [Op.iLike]: `${conf.q}%` } },
          ],
        },
      })

      return { rows, count }
    },
  })
)

express-sequelize-crud, exposes a default search helper that you can use like this:

import crud, {
  sequelizeCrud,
  sequelizeSearchFields,
} from 'express-sequelize-crud'

crud('/admin/users', {
  ...sequelizeCrud(User),
  search: sequelizeSearchFields(User, ['address', 'zipCode', 'city']),
})

When searching some stuff, the following records will be returned in this order:

  1. records with a searchable field that contains some stuff
  2. records that have searchable fields that contain both some and stuff
  3. records that have searchable fields that contain one of some or stuff

The search is case insensitive by default. You can customize the search to make it case sensitive or use a scope:

import { Op } from 'sequelize'

const search = sequelizeSearchFields(
  User,
  ['address', 'zipCode', 'city'],
  Op.like
)

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