Skip to content

Commit

Permalink
feat: ✨ separate express-sequelize-crud in 2 distinct packages
Browse files Browse the repository at this point in the history
  • Loading branch information
nicgirault committed Jan 12, 2022
1 parent 4d80bee commit 08f79ae
Show file tree
Hide file tree
Showing 14 changed files with 2,175 additions and 3,453 deletions.
4 changes: 0 additions & 4 deletions .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,5 @@
"presets": [
["@babel/preset-env", { "targets": { "node": "current" } }],
"@babel/typescript"
],
"plugins": [
"@babel/proposal-class-properties",
"@babel/proposal-object-rest-spread"
]
}
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
version: 2

defaults: &defaults
working_directory: ~/express-sequelize-crud
working_directory: ~/express-crud-router
docker:
- image: circleci/node:12
- image: circleci/node:16

jobs:
test:
Expand Down
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yarn check-types
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
12
16
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Changes are documented in the [Github releases page](https://github.com/lalalilo/express-sequelize-crud/releases)
Changes are documented in the [Github releases page](https://github.com/lalalilo/express-crud-router/releases)
88 changes: 42 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
# express-sequelize-crud
# express-crud-router

```ts
import crud, { sequelizeCrud } from 'express-sequelize-crud'
import crud from 'express-crud-router'

app.use(crud('/admin/users', sequelizeCrud(User)))
app.use(
crud('/admin/users', {
getList: ({ 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 } }),
destroy: id => User.destroy({ where: { id } }),
})
)
```

Expose resource CRUD (Create Read Update Delete) routes for Express & Sequelize (and other ORMs in v6+). Compatible with [React Admin Simple Rest Data Provider](https://github.com/marmelab/react-admin/tree/master/packages/ra-data-simple-rest)
Expose resource CRUD (Create Read Update Delete) routes in you Express app. Compatible with [React Admin Simple Rest Data Provider](https://github.com/marmelab/react-admin/tree/master/packages/ra-data-simple-rest). The lib is ORM agnostic. [List of existing ORM connectors](https://www.npmjs.com/search?q=keywords:express-crud-router-connector).

### 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](https://stackoverflow.com/questions/53259737/content-range-working-in-safari-but-not-in-chrome) stackoverflow thread for more info.

If you are using `ra-data-simple-rest`, please refer to the [documentation](https://github.com/Serind/ra-data-simple-rest#note-about-content-range) to use `X-Total-Count` for pagination.
If you are using `ra-data-simple-rest`, please refer to the [documentation](https://github.com/Serind/ra-data-simple-rest#note-about-content-range) to use `X-Total-Count` for pagination.

[![codecov](https://codecov.io/gh/lalalilo/express-sequelize-crud/branch/master/graph/badge.svg)](https://codecov.io/gh/lalalilo/express-sequelize-crud) [![CircleCI](https://circleci.com/gh/lalalilo/express-sequelize-crud.svg?style=svg)](https://circleci.com/gh/lalalilo/express-sequelize-crud)
[![codecov](https://codecov.io/gh/lalalilo/express-crud-router/branch/master/graph/badge.svg)](https://codecov.io/gh/lalalilo/express-crud-router) [![CircleCI](https://circleci.com/gh/lalalilo/express-crud-router.svg?style=svg)](https://circleci.com/gh/lalalilo/express-crud-router)

## Install

```
yarn add express-sequelize-crud
npm install express-crud-router
```

## Usage
Expand All @@ -28,7 +37,8 @@ yarn add express-sequelize-crud

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

const app = new express()
Expand All @@ -39,7 +49,8 @@ app.use(crud('/admin/users', sequelizeCrud(User)))

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

const app = new express()
Expand All @@ -58,7 +69,8 @@ Custom filters such as case insensitive filter can be perform like this:
```ts
import express from 'express'
import { Op } from 'sequelize'
import crud, { sequelizeCrud } from 'express-sequelize-crud'
import crud from 'express-crud-router'
import sequelizeCrud from 'express-crud-router-sequelize-v6-connector'
import { User } from './models'

const app = new express()
Expand All @@ -77,7 +89,7 @@ app.use(

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

const app = new express()
Expand All @@ -87,12 +99,29 @@ app.use(
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 } }),
update: (id, body, { req, res }) => User.update(body, { where: { id } }),
destroy: (id, { req, res }) => User.destroy({ where: { id } }),
})
)
```

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

```typescript
interface Actions<R> {
getOne: (identifier: string) => Promise<R | null>
create: (body: R) => Promise<R & { id: number | string }>
destroy: (id: string) => Promise<any>
update: (id: string, data: R) => Promise<any>
getList: GetList<R> = (conf: {
filter: Record<string, any>
limit: number
offset: number
order: Array<[string, string]>
}) => Promise<{ rows: R[]; count: number }>
}
```

### Search

#### Autocomplete
Expand Down Expand Up @@ -120,40 +149,7 @@ app.use(
)
```

express-sequelize-crud exposes a default search helper function `sequelizeSearchFields`.
Internally it uses `LIKE` syntax for all columns except uuid (`type: DataTypes.UUID`)

Here is an example:

```ts
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:

```ts
import { Op } from 'sequelize'

const search = sequelizeSearchFields(
User,
['address', 'zipCode', 'city'],
Op.like
)
```
express-crud-router ORM connectors might expose some search behaviors.

## Contribute

Expand Down
37 changes: 12 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
{
"name": "express-sequelize-crud",
"name": "express-crud-router",
"version": "3.5.1",
"description": "React Admin backend with Express and Sequelize. 1 line per resource!",
"description": "React Admin backend for Express. 1 line per resource! ORM agnostic.",
"main": "./lib/index.js",
"types": "./lib/src/index.d.ts",
"repository": {
"type": "git",
"url": "git+https://github.com/lalalilo/express-sequelize-crud.git"
"url": "git+https://github.com/lalalilo/express-crud-router.git"
},
"author": "nicgirault <[email protected]>",
"keywords": [
"react-admin",
"sequelize",
"crud",
"express"
],
"tags": [
"react-admin",
"sequelize",
"crud",
"express"
],
"bugs": {
"url": "https://github.com/lalalilo/express-sequelize-crud/issues"
"url": "https://github.com/lalalilo/express-crud-router/issues"
},
"homepage": "https://github.com/lalalilo/express-sequelize-crud#readme",
"homepage": "https://github.com/lalalilo/express-crud-router#readme",
"license": "MIT",
"scripts": {
"build": "rimraf lib && babel src -d lib --extensions '.ts' && tsc",
Expand All @@ -33,41 +33,28 @@
"files": [
"lib/**/*"
],
"husky": {
"hooks": {
"pre-commit": "yarn check-types"
}
},
"devDependencies": {
"@babel/cli": "^7.7.0",
"@babel/core": "^7.7.2",
"@babel/node": "^7.7.0",
"@babel/plugin-proposal-class-properties": "^7.7.0",
"@babel/plugin-proposal-object-rest-spread": "^7.6.2",
"@babel/preset-env": "^7.7.1",
"@babel/preset-typescript": "^7.7.2",
"@types/bluebird": "^3.5.29",
"@types/express": "^4.17.2",
"@types/jest": "^26.0.23",
"@types/jest": "^27.4.0",
"@types/lodash": "^4.14.149",
"@types/mime-types": "^2.1.0",
"@types/node": "^15.9.0",
"@types/validator": "^13.1.3",
"@types/node": "^17.0.8",
"codecov": "^3.6.1",
"express": "^4.17.1",
"husky": "^6.0.0",
"husky": "^7.0.4",
"jest": "^27.0.4",
"node-fetch": "^2.6.0",
"react": "^17.0.2",
"rimraf": "^3.0.0",
"semantic-release": "^17.0.4",
"sequelize": "^6.6.2",
"sqlite3": "^5.0.2",
"semantic-release": "^18.0.1",
"typescript": "^4.3.2"
},
"peerDependencies": {
"express": "^4.0.0",
"sequelize": "^5.0.0 || ^6.0.0"
"express": "^4.0.0"
},
"jest": {
"setupFiles": [
Expand Down
3 changes: 0 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ 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>(
Expand Down
14 changes: 0 additions & 14 deletions src/sequelize/index.spec.ts

This file was deleted.

39 changes: 0 additions & 39 deletions src/sequelize/index.ts

This file was deleted.

Loading

0 comments on commit 08f79ae

Please sign in to comment.