All Projects → apgzs → Cool Admin Api

apgzs / Cool Admin Api

Licence: mit
cool-admin-api 是基于egg.js、typeorm、jwt等封装的api开发脚手架、快速开发api接口

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Cool Admin Api

Egg Authz
egg-authz is an authorization middleware for Egg.js based on Casbin
Stars: ✭ 50 (-73.4%)
Mutual labels:  koa2, egg, eggjs, egg-plugin
Blog Service
blog service @nestjs
Stars: ✭ 188 (+0%)
Mutual labels:  express, koa, nestjs, koa2
Eggjs Note
《Egg.js 深入浅出学习笔记》(暂时停更)
Stars: ✭ 502 (+167.02%)
Mutual labels:  koa2, egg, eggjs, koajs
Vue Chat
👥Vue全家桶+Socket.io+Express/Koa2打造一个智能聊天室。
Stars: ✭ 887 (+371.81%)
Mutual labels:  express, koa, koa2, koajs
Egg
🥚 Born to build better enterprise frameworks and apps with Node.js & Koa
Stars: ✭ 17,616 (+9270.21%)
Mutual labels:  koa, koa2, egg, eggjs
Nodepress
😎 RESTful API service for Blog/CMS, powered by @nestjs
Stars: ✭ 829 (+340.96%)
Mutual labels:  express, nestjs, nest
Koahub Demo
koahub+async/await+mysql
Stars: ✭ 15 (-92.02%)
Mutual labels:  koa, koa2, koajs
Egg Cancan
cancancan like authorization plugin for Egg.js
Stars: ✭ 47 (-75%)
Mutual labels:  egg, eggjs, egg-plugin
Vue Socket.io Chat
💬 TypeScript + Vue + Express/Koa + Socket.io
Stars: ✭ 61 (-67.55%)
Mutual labels:  express, koa, koa2
Trafficlight
🚦 Flexible NodeJS Routing Decorators for API Routing
Stars: ✭ 69 (-63.3%)
Mutual labels:  express, koa, koa2
Koach Javascript
Production ready Koa2 boilerplate.
Stars: ✭ 79 (-57.98%)
Mutual labels:  koa, koa2, koajs
Koa2 Api Scaffold
一个基于Koa2的轻量级RESTful API Server脚手架。
Stars: ✭ 694 (+269.15%)
Mutual labels:  express, koa, koa2
Github Ranking
🔍GitHub不同语言热门项目排行,Vue.js做页面展示
Stars: ✭ 160 (-14.89%)
Mutual labels:  express, koa, koa2
Koajs Design Note
《Koa.js 设计模式-学习笔记》已完结 😆
Stars: ✭ 520 (+176.6%)
Mutual labels:  koa, koa2, koajs
Fontend
使用Node、Vue、ElementUI、iViewUI,验证码等等搭建一个综合性网站(含后台管理系统)
Stars: ✭ 97 (-48.4%)
Mutual labels:  express, koa, element-ui
Postgraphile
GraphQL is a new way of communicating with your server. It eliminates the problems of over- and under-fetching, incorporates strong data types, has built-in introspection, documentation and deprecation capabilities, and is implemented in many programming languages. This all leads to gloriously low-latency user experiences, better developer experiences, and much increased productivity. Because of all this, GraphQL is typically used as a replacement for (or companion to) RESTful API services.
Stars: ✭ 10,967 (+5733.51%)
Mutual labels:  express, koa, koa2
Node Rate Limiter Flexible
Node.js rate limit requests by key with atomic increments in single process or distributed environment.
Stars: ✭ 1,950 (+937.23%)
Mutual labels:  express, koa, nestjs
Koa2 Note
《Koa2进阶学习笔记》已完结🎄🎄🎄
Stars: ✭ 4,725 (+2413.3%)
Mutual labels:  koa, koa2, koajs
Egg 24time
A Twitter-like news and social server for Egg. 微信小程序社区全栈解决方案
Stars: ✭ 493 (+162.23%)
Mutual labels:  egg, eggjs, egg-plugin
Simple Todos
A simple web application powered by Nuxt.js 💚 & Nest Framework 😻
Stars: ✭ 81 (-56.91%)
Mutual labels:  express, nestjs, nest

展示

技术选型

Node版后台基础框架基于Egg.js(阿里出品)

核心组件

独有cool-admin.com发布的npm组件

开发交流微信群

微信

运行

环境 Node.js>=8.9.0 Redis mysql

新建并导入数据库,修改数据库连接信息

推荐使用yarn

 git clone https://github.com/apgzs/cool-admin-api.git
 cd cool-admin-api
 yarn
 yarn dev
 http://localhost:7001

或者npm

 git clone https://github.com/apgzs/cool-admin-api.git
 cd cool-admin-api
 npm install
 npm run dev
 http://localhost:7001

##视频教程:

cool-admin后端简单入门视频(快速写6个api接口):https://www.bilibili.com/video/BV1SE411j74K

cool-admin前后端配合使用:https://www.bilibili.com/video/av90478011/

cool-admin前端crud内部培训教程:https://www.bilibili.com/video/av89512654/

数据模型

数据模型必须放在app/entities/*下,否则typeorm无法识别,如:

 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;
 }

新建完成运行代码,就可以看到数据库新建了一张sys_role表,如不需要自动创建config文件夹下修改typeorm的配置文件

控制器

有了数据表之后,如果希望通过接口对数据表进行操作,我们就必须在controller文件夹下新建对应的控制器,如:

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(可选)
    }
}

这样我们就完成了6个接口的编写,对应的接口如下:

  • /admin/sys/role/add 新增
  • /admin/sys/role/delete 删除
  • /admin/sys/role/update 更新
  • /admin/sys/role/info 单个信息
  • /admin/sys/role/list 列表信息
  • /admin/sys/role/page 分页查询(包含模糊查询、字段全匹配等)

PageOption配置参数

参数 类型 说明
keyWordLikeFields 数组 模糊查询需要匹配的字段,如[ 'name','phone' ] ,这样就可以模糊查询姓名、手机两个字段了
where TypeORM Brackets对象 固定where条件设置,详见typeorm
fieldEq 数组 动态条件全匹配,如需要筛选用户状态status,就可以设置成['status'],此时接口就可以接受status的值并且对数据有过滤效果
addOrderBy 对象 排序条件可传多个,如{ sortNum:asc, createTime:desc }

数据缓存

有些业务场景,我们并不希望每次请求接口都需要操作数据库,如:今日推荐、上个月排行榜等,数据存储在redis,注:缓存注解只在service层有效

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号' ];
    }
}

Cache配置参数

参数 类型 说明
resolver 数组 方法参数获得,生成key用, resolver: (args => {return args[0];}), 这样就可以获得方法的第一个参数作为缓存key
ttl 数字 缓存过期时间,单位:
url 字符串 请求url包含该前缀才缓存,如/api/*请求时缓存,/admin/*请求时不缓存

路由

egg.js原生的路由写法过于繁琐,cool-admin的路由支持BaseController还有其他原生支持具体参照egg.js路由

自定义sql查询

除了单表的简单操作,真实的业务往往需要对数据库做一些复杂的操作。这时候我们可以在service自定义SQL,如

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);
}

this.setSql()设置参数

参数 类型 说明
condition 布尔型 只有满足改条件才会拼接上相应的sql和参数
sql 字符串 需要拼接的参数
params 数组 相对应的参数
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].