The backend basic framework of the Node version is based on Egg.js (produced by Alibaba)
Unique npm component published by cool-admin.com
Node.js>=8.9.0
Redis
mysql
Create a new database and import it, modify the database connection information
It is recommended to use yarn
git clone https: //github.com/apgzs/cool-admin-api.git
cd cool - admin - api
yarn
yarn dev
http : //localhost:7001
or npm
git clone https: //github.com/apgzs/cool-admin-api.git
cd cool - admin - api
npm install
npm run dev
http : //localhost:7001
##Video tutorial:
Simple introductory video on the cool-admin backend (quickly write 6 API interfaces): https://www.bilibili.com/video/BV1SE411j74K
Cool-admin is used together with the front and back ends: https://www.bilibili.com/video/av90478011/
cool-admin front-end crud internal training tutorial: https://www.bilibili.com/video/av89512654/
The data model must be placed under app/entities/*
, otherwise typeorm cannot recognize it, such as:
import { Entity , Column , Index } from 'typeorm' ;
import { BaseEntity } from 'egg-cool-entity' ;
/**
* 系统角色
*/
@ Entity ( { name : 'sys_role' } )
export default class SysRole extends BaseEntity {
// 名称
@ Index ( { unique : true } )
@ Column ( )
name : string ;
// 角色标签
@ Index ( { unique : true } )
@ Column ( { nullable : true } )
label : string ;
// 备注
@ Column ( { nullable : true } )
remark : string ;
}
After the new creation is completed and the code is run, you can see that a new sys_role
table has been created in the database. If you do not need to automatically create the config
folder, modify the typeorm configuration file.
After having the data table, if we want to operate the data table through the interface, we must create a new corresponding controller under the controller
folder, such as:
import { BaseController } from 'egg-cool-controller' ;
import { Context } from 'egg' ;
import routerDecorator from 'egg-cool-router' ;
import { Brackets } from 'typeorm' ;
/**
* 系统-角色
*/
@ routerDecorator . prefix ( '/admin/sys/role' , [ 'add' , 'delete' , 'update' , 'info' , 'list' , 'page' ] )
export default class SysRoleController extends BaseController {
constructor ( ctx : Context ) {
super ( ctx ) ;
this . setEntity ( this . ctx . repo . sys . Role ) ;
this . setPageOption ( {
keyWordLikeFields : [ 'name' , 'label' ] ,
where : new Brackets ( qb => {
qb . where ( 'id !=:id' , { id : 1 } ) ;
} ) ,
} ) ; //分页配置(可选)
this . setService ( this . service . sys . role ) ; //设置自定义的service(可选)
}
}
In this way, we have completed the writing of 6 interfaces. The corresponding interfaces are as follows:
/admin/sys/role/add
new/admin/sys/role/delete
/admin/sys/role/update
update/admin/sys/role/info
single information/admin/sys/role/list
list information/admin/sys/role/page
paging query (including fuzzy query, full field matching, etc.) parameter | type | illustrate |
---|---|---|
keyWordLikeFields | array | Fuzzy query requires matching fields, such as [ 'name','phone' ] , so that you can fuzzy query the two fields姓名、手机 . |
where | TypeORM Brackets object | Fixed where condition setting, see typeorm for details |
fieldEq | array | Dynamic conditions are fully matched. If you need to filter user status , you can set it to ['status'] . At this time, the interface can accept the value of status and have a filtering effect on the data. |
addOrderBy | object | Multiple sorting conditions can be passed, such as { sortNum:asc, createTime:desc } |
In some business scenarios, we do not want to operate the database every time we request the interface, such as today's recommendations, last month's rankings, etc. The data is stored in redis
. Note: Cache annotations are only valid in service
layer.
import { BaseService } from 'egg-cool-service' ;
import { Cache } from 'egg-cool-cache' ;
/**
* 业务-排行榜服务类
*/
export default class BusRankService extends BaseService {
/**
* 上个月榜单
*/
@ Cache ( { ttl : 1000 } ) // 表示缓存
async rankList ( ) {
return [ '程序猿1号' , '程序猿2号' , '程序猿3号' ] ;
}
}
parameter | type | illustrate |
---|---|---|
resolver | array | The method parameters are obtained and the key is generated using resolver: (args => {return args[0];}), so that the first parameter of the method can be obtained as the cache key |
ttl | number | Cache expiration time, unit:秒 |
url | string | The request URL will only be cached if it contains this prefix. For example, /api/* will be cached when requested, and /admin/* will not be cached when requested. |
Egg.js's native route writing method is too cumbersome. cool-admin
's routing supports BaseController
and other native supports. For details, please refer to egg.js routing.
In addition to simple operations on a single table, real business often requires some complex operations on the database. At this time we can customize SQL in service
, such as
async page ( query ) {
const { keyWord , status } = query ;
const sql = `
SELECT
a.*,
GROUP_CONCAT(c.name) AS roleName
FROM
sys_user a
LEFT JOIN sys_user_role b ON a.id = b.userId
LEFT JOIN sys_role c ON b.roleId = c.id
WHERE 1 = 1
${ this . setSql ( status , 'and a.status = ?' , [ status ] ) }
${ this . setSql ( keyWord , 'and (a.name LIKE ? or a.username LIKE ?)' , [ `% ${ keyWord } %` , `% ${ keyWord } %` ] ) }
${ this . setSql ( true , 'and a.id != ?' , [ 1 ] ) }
GROUP BY a.id` ;
return this . sqlRenderPage ( sql , query ) ;
}
parameter | type | illustrate |
---|---|---|
condition | Boolean | Only when the changed conditions are met will the corresponding sql and parameters be spliced. |
sql | string | Parameters that need to be spliced |
params | array | Corresponding parameters |