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

egg-cool-core

v1.1.26

Published

cool-admin 核心组件

Downloads

3

Readme

展示

  • 演示地址:https://show.cool-admin.com
  • 文档地址:https://docs.cool-admin.com
  • 官网:https://www.cool-admin.com

技术选型

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

运行

环境 Node.js>=8.9.0 Redis

新建并导入数据库,数据库脚本位于 db/init.sql,修改数据库连接信息config/config.*.typeorm

推荐使用yarn

 git clone https://gitee.com/cool_1/cool-admin-api_node.git
 cd cool-admin-api_node
 yarn
 yarn dev
 http://localhost:7001

或者npm

 git clone https://gitee.com/cool_1/cool-admin-api_node.git
 cd cool-admin-api_node
 npm install
 npm run dev
 http://localhost:7001

努力开发中

努力开发中

努力开发中

数据模型

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

 import { Entity, Column, Index } from 'typeorm';
 import { BaseEntity } from '../../lib/base/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 '../../../lib/base/controller';
import { Context } from 'egg';
import routerDecorator from '../../../lib/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 '../../lib/base/service';
import { Cache } from '../../lib/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 | 数组 | 相对应的参数 |