jason-server

3.0.0 • Public • Published

自动创建生成 服务器(express+mysql)---将要废弃不再维护,更好更多方案将在jason-core-api出现(未完)

##特点

  1. 自动创建服务,自带mysql服务器连接 方便
  2. 自动注入了表的增删改查(如user表)
 findAllUser({filter:{...}})
 findOneUser({filter:{...}})
 createUser({id:...,name:...})
 updateUser({user:{name:...},filter:{id:...}})
 upsertUser({name:...,id:...})//存在id为修改否则为新增
 deleteUser({filter})
 countUser(filter)//计数统计,一般用于分页列表

其中filter为过滤条件如:(支持多层嵌套)

{name:'sss',age:89...}// name =’sss‘ and age=89...
{name_ne:'sss',age_gt: 89}//name =’sss‘ and age>89
{name_ne:'sss',age_gte: 89}//name =’sss‘ and age>=89
{name_like:'sss%',age_lt:89}//name like ’sss‘ and age<89
{name_like:'sss%',age_lte:89}//name like ’sss‘ and age<=89
{name_notLike:'%sss'}//name not like ’sss‘ 
{name_in:['sss']}//name in [’sss‘] 
{name_notIn:['sss']}//name not in [’sss‘ ]
{
  AND:[
    {name:'99'},
    {id_ne:'1111'},
    ...
  ]
 }// name='99' and id != '1111'...
 {
  OR:[
    {name:'99'},
    {id_ne:'1111'},
    ...
  ]
 }//name ='99' or id != '1111'...

等等...

用法文档

1.在根目录下创建.env.js文件

export default {
    IGORE_TOKEN_FUN: ['login', 'getCurrentUser' , 'sendIdentifyCode', 'uploadImg'],//不需token校验的接口,2.0.20版本已经不需要,但兼容
    SET_TOKEN_FUN: ['login'],//设置token的api接口
    MODEL_DIR: `${__dirname}/model`,//表目录
    API_DIR: `${__dirname}/biz`,//自定义接口目录
    ENV: {
        PORT: 5001,
        URL: 'http://localhost'
    },//服务器配置
    DB_ENV: {
        host: 'localhost',
        user: 'root',
        database: 'job',
        password: 'root',
        port: 3306,
        multipleStatements: true
    }//mysql 配置
}

2.在根目录下创建 表文件目录 model,在目录下创建表文件 Account.type.sql

type Account {
    id: ID!,
    mobile: String,
    password: String,
    lastLoginTime: DateTime,
    loginTimes: Int
}//目前支持类型 ID! String Bool Int Decimal Date DateTimeq七种类型

3.在根目录创建自定义接口目录 biz 在biz中创建自定义接口文件如 password.biz.js 自定义接口如下

import {injectable, apiService, transaction, ignoreCheckToken} from 'jason-server'
//登录
export const login = apiService(
    injectable(['findOneAccount', 'findOneUser', 'findOneToken', 'createToken', 'updateToken', 'updateAccount']),
    transaction(true),
    ignoreCheckToken(true)//此接口不开启token 校验
)(async ({findOneAccount, findOneUser, findOneToken, createToken, updateToken, updateAccount}, {mobile, password}) => {
 
    const account = await  findOneAccount({mobile, password: hasMd5(password)});
    if (!account) return {error: '账号不存在或密码错误'};
    const accountId = account.id;
    const user = await  findOneUser({accountId});
    if (!user) return {error: '用户不存在'}
    await updateAccount({account: {lastLoginTime: formatDateTime(new Date())}, filter: {id: accountId}})
    let token = await  findOneToken({accountId}) || await  createToken({accountId});
    await updateToken({token, filter: {id: token.id}});
 
    return {user, accountId, tokenId: token.id}
})
login 接口
injectable:引入其他接口,包括自定义和系统自定义
transaction: 是否注入事务处理

创建服务

import 'babel-polyfill'
import {autoCreateServer} from 'jason-server'
 
export const setParamsMiddleWare = (req, res, next) => {
    //根据实际情况写此方法
    const params = req.body.params||{};
    const token = req.body.tokenId;
    const url = req.url;
    const urls = url.split('/');
    
    const funName = urls[urls.length - 1].split('.do')[0];
    req.funName = funName;
    req.token = token;
    req.params = params;
    
    next()
}
 
//核心
export default autoCreateServer({
    setParamsMiddleWare,
    customMiddlewares:[]
})
funName: 接口名//必填
params:接口参数//必填
setParamsMiddleWare:注入token 用户参数等信息//必须
customMiddlewares: 用于自定义中间件//非必须

版本历史信息

2.0.19 ........ 1.添加api ignoreCheckToken(true) 不开启token校验,默认所有findOne,findAll,count都不校验, 2.不允许自定义api以 findAll findOne count create update upsert 开头
2.0.18 ........ 修改配置文件,适应ip + token校验
2.0.8 ........ 添加据库自动创建表( 删除原表)
2.0.4 ........ 修改不需事务处理bug。
1.0.0 ........ 优化自动注入接口,添加自定义model计数接口如: countUser({filter:{....}})。

Readme

Keywords

none

Package Sidebar

Install

npm i jason-server

Weekly Downloads

67

Version

3.0.0

License

ISC

Unpacked Size

65.9 kB

Total Files

23

Last publish

Collaborators

  • json_lee