# sequelize-smartapi

> sequelize-smartapi is a library to generate CURD actions for expressjs from sequelize models.

Latest version **0.1.7** (published 2019-12-31) · ISC license · 0 weekly downloads

## Install

```sh
npm install sequelize-smartapi
pnpm add sequelize-smartapi
yarn add sequelize-smartapi
bun add sequelize-smartapi
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.7 |
| Published | 2019-12-31 |
| First published | 2019-11-13 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 59.2 KB |
| Known vulnerabilities | 0 (+5 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 3 |
| Author | w.c. |
| Maintainers | youzi1231223 |
| Keywords | CURD, sequelize, expressjs, mysql |

## Links

- npm: https://www.npmjs.com/package/sequelize-smartapi
- Repository: https://github.com/iyjian/sequelize-smartapi
- Homepage: https://github.com/iyjian/sequelize-smartapi#README.md
- Issues: https://github.com/iyjian/sequelize-smartapi/issues
- npm.io page: https://npm.io/package/sequelize-smartapi

## Dependencies (5)

- [lodash](https://npm.io/package/lodash.md) ^4.17.15
- [express](https://npm.io/package/express.md) ^4.17.1
- [sqlite3](https://npm.io/package/sqlite3.md) ^4.1.0
- [pluralize](https://npm.io/package/pluralize.md) ^8.0.0
- [sequelize](https://npm.io/package/sequelize.md) ^5.21.2

## Alternatives

- [@libsql/sqlite3](https://npm.io/package/@libsql/sqlite3.md) — 39.8K weekly downloads
- [@fortemi/core](https://npm.io/package/@fortemi/core.md) — 461 weekly downloads
- [cdb-converter](https://npm.io/package/cdb-converter.md) — 341 weekly downloads
- [@uplo/adapter-prisma](https://npm.io/package/@uplo/adapter-prisma.md) — 75 weekly downloads
- [typeorm-aios](https://npm.io/package/typeorm-aios.md) — 30 weekly downloads

## Recent versions

- 0.1.7 (latest) — 2019-12-31
- 0.1.6 — 2019-12-23
- 0.1.5 — 2019-12-19
- 0.1.4 — 2019-12-19
- 0.1.3 — 2019-12-19
- 0.1.2 — 2019-11-18
- 0.1.1 — 2019-11-14
- 0.1.0 — 2019-11-13
- 0.0.2 — 2019-11-13

## README

sequelize-smartapi is a library to generate CURD actions for [expressjs](https://expressjs.com) from [sequelize](https://sequelize.org) models.


## Install

```bash

npm install sequelize-smartapi --save

```

## Usage

```javascript
const models = require('./models')
const smartApi = require('sequelize-smartapi')(models)
```


### smartApi.getList(model, [options])

return an expressjs middleware to process a list query

the first `model` parameter is a string of model's name defined in sequlize model file.

The following table describes the properties of the optional options object.

|    Property    |  Description   |  Type  | Default  |
|     --         |  --            |  --    |  --      |
|  cb            |
|  scopes        |
|  include       |
|  searchColumns |

the database query is controled by url query parameters, default paramenters are described as follow:

```
GET /students
```

would be converted to database query:

```sql
select * 
from students 
order by createdAt desc 
limit 0, 200
```

you can changed the default behaviour by specify the url query paramters as:

```
GET /students?limit=10&start=11&sortColumn=birthDate&sortOrder=asc
```

would be converted to database query:

```sql
select * 
from students 
order by birthDate asc 
limit 11, 10
```

other supported query methods includes:

#### filter rows by specific column(s) with specific value

```
GET /students?classId=1
```

would be converted to a database query:

```sql
select * 
from students 
where classId = 1
order by createdAt desc
limit 200
```

you can specified more than one column:

```
GET /students?classId=1&sex=1
```

would be converted to a database query:

```sql
select * 
from students 
where classId = 1 and sex = 1
order by createdAt desc
limit 200
```

#### filter rows by specific column(s) with list values

```
GET /students?classIds=1&classIds=2&classIds=3
```

would be converted to a database query:

```sql
select * 
from students 
where classId in (1, 2, 3)
order by createdAt desc
limit 200
```

you can also specific a column with an empty value, which would be converted to a `is null` query in database

```
GET /students?classIds=1&classIds=2&classIds=3&classIds=
```

would be converted to a database query:

```sql
select * 
from students 
where classId in (1, 2, 3) or classId is null
order by createdAt desc
limit 200
```







## 参数说明：
|名称|是否必填|类型|说明|适用的方法|
|--|--|--|--|--|
|Model| 必填| string类型|model对象的名字|post，get，getList，patch，delete，putStatus|
|options| 选填|Object类型|
|option.cb|选填| function (req,entry) |对从数据库获得的实例数据进行处理之后再返回，entry为实例数据|post，get，getList｜
|options.scopes|选填|Object，{name: 'columnName'}|主要用于界定从数据库返回的模型实例的值的范围,eg:smartApi.get('model',{scopes:{name:'isEnable'}}),表示查询有效的数据|get,getList|
|options.include|选填|Object,[{model:'modelName'}]|用来进行表的join,多个表join要进行嵌套|get,getList|
|options.searchColumns|选填|Array类型（待定，因为没有使用过，不确定，哈哈哈）|指定页面传入的搜索关键词所适用的列|getList|



返回的res有两种情况：
{error: 0, data: entry}　返回查询结果
{error: 404, data: {}, errorMsg: '无此记录'}


## getList -查询数据

```
getList(Model, options={})

根据传入的参数条件，返回对应的所有数据，默认无参数时查询整个表数据

返回：
{error: 0, data: entries, totalRecords: totalRecords}
entries:查询到的所有数据
totalRecords：查询到的数据量

```

## post - 新增数据，可以处理一个实例对象或包含多个实例对象的数组

```
post(Model, options={})

返回的res有三种情况：
{error:0,data:entry}，表示新增成功，entry返回新建的数据
{error: 404, data:{}, errorMsg: '添加数据失败'}，表示没有新建数据或者数据添加失败
{error：500，errorMsg：‘系统错误’}，表示代码异常
```

## patch - 通过指定的主键id更新对应的数据

```
patch(Model, option={})

返回的res有两种情况：
{error: 404, errorMsg: '无此数据'}
{error: 0, data: {affectedRows: affectRows}, 返回影响的行数
```

## delete - 通过指定的主键ｉｄ删除对应的数据

```
delete(Model,options={})

删除操作并没有真实的删除数据库表中的数据，只是将表中的isEnable更新为false，即无效不可用，实现理论上的删除

返回的res有两种情况：
{error: 404, errorMsg: '无此数据'}
{error: 0, data: {affectedRows: affectRows}, 返回影响的行数
```

## putStatus - 更新状态（待补充）



getList 支持 in

for example:

http://localhost:3000/users?groupId=[1, 2, null]

this will change the query to:

select * from users where groupId in (1, 2) or groupId is null;

---
_Source: https://npm.io/package/sequelize-smartapi · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
