0.2.9 • Published 2 years ago

apijson-builder-client v0.2.9

Weekly downloads
-
License
-
Repository
-
Last release
2 years ago

apijson-builder-client

apijson 不必过多解释,可查看连接。 apijson的请求构建是唯一的学习成本(不考虑撸源码的话),因此为了方便js构建rest请求, 所以有了apijson-builder-client, 是个小东西,代码量也很小。方便你快速构建apijson的rest请求。

安装

import ApiJsonBuilder from 'apijson-builder-client'
Vue.use(ApiJsonBuilder, {
    baseURL: '/api/apijson',
    tokenKey: 'X-Token',
    token: '304958029525nsd23423'
})

使用

import {QueryBuilder} from 'apijson-builder-client';
QueryBuilder.by('User')
        .condition(Condition.by('name', 'like', 'zhang'))
        .order('name')
        .multi(true)
        .setResFields('name,age,phone')
        .page(0, 10)
        .send()
.then(resp => {
    const {total, User: users} = resp // users 即为数据, total为总数
})

新增or查询功能,支持多个字段组合or查询,如:(A and B) or (C and D) 或者 (A or B) and (C or D)

import {QueryBuilder} from 'apijson-builder-client';
QueryBuilder.by('User_info13')
        .condition(Condition.by('age','and',">0,<100"))
        .condition(Condition.by('user_name', 'like', 'test1'))
        .condition(Condition.by('gender', 'eq', 0))
        .condition(Condition.by('crd','and',">1,<20001"))
        //.condition(Condition.by('date', 'like', '2017-02-01'))
        //.condition(Condition.by('id', 'in', [82006,82002]))    //逻辑运算-筛选
        //.condition(Condition.by('id', 'and', ">=82000,<=82005"))   //逻辑运算-范围筛选
        //.condition(Condition.by('id', 'notin', [82006,82002]))    //逻辑运算--not  in
        //.order('name')
        .multi(true)
        .setExplain(true)
        .setOrFields('age&&gender,user_name&&crd')   //or  查询    (A and B) or (C and D)
        //.setOrFields('age,gender')                //or  查询  (A or B) and (C or D)
        //.setOrFields('user_name,crd')
        .setDatasource('apijsonUser1')
        .setResFields('age,crd,user_name')
        .page(0, 10)
        .send()
.then(resp => {
        for(var key in resp) {
          console.log(key, resp[key])    //打印请求结果
        }
      })
})

新增表关联查询功能,支持 INNER JOIN 、LEFT JOIN 和 RIGHT JOIN

import {QueryBuilder} from 'apijson-builder-client';
QueryBuilder.by('Moment')
        .setJoinType('>/User/id@,</Comment/momentId@')  //设置  RIGHT JOIN User表  、  LEFT JOIN  Comment表
        .setResFields('id,userId,content')   
        .setAssociativeCondition('User', 'id', '/Moment/userId',"id,date,name")    
        .setAssociativeCondition('Comment', 'momentId', '/Moment/id',"id,momentId")
        .page(0, 10)
        .setExplain(true)
        .multi(true)
        .send()
.then(resp => {
        for(var key in resp) {
          console.log(key, resp[key])  //打印请求结果
        }
      })

新增分组查询功能,支持 max、avg、sum等

import {QueryBuilder} from 'apijson-builder-client';
QueryBuilder.by('Moment')
        //.setResFields('max(userId):maxUserId')   //取最大值
        //.setResFields('avg(userId)')     //取平均值
        .setResFields('userId;sum(id):idSum')
        .setExplain(true)
        .setGroups('userId')     //userId分组
        .send()
.then(resp => {
        for(var key in resp) {
          console.log(key, resp[key])  //打印请求结果
        }
      })

增改删除功能演示

import {CUDBuilder} from 'apijson-builder-client';
 ##新增
CUDBuilder.by('User','post')
	.set('user_name',"test1")
	.set('gender',1)
	.setExplain(true)
	.setDatasource('apijsonUser1')
	.send()
.then(resp => {
	for(var key in resp) {
	  console.log(key, resp[key])
	}

})

##修改
CUDBuilder.by('Moment','put')
	.whereId('eq',1655267505965)
	.set('content',"今天一直下雨")
	.setTag('Moment')
	.send()
.then(resp => {
	for(var key in resp) {
	  console.log(key, resp[key])
	}
  })
  
##删除
CUDBuilder.by('Moment','delete')
        .whereId('eq',1655267505965)
        .setTag('Moment')
        .send()
.then(resp => {
        for(var key in resp) {
          console.log(key, resp[key])
        }
      })
      

关于token

你可以这样设置token

import {setToken, LoginBuilder} from "apijson-builder-client";

LoginBuilder.login('username', 'password').then(resp => {
    const {token} = resp
    setToken(token)
})

其他

更多使用方式自己看下就知道了, 欢迎来完善。 来改源码前请务必熟知apijson的语法!

0.2.9

2 years ago

0.2.8

2 years ago

0.2.7

2 years ago

0.2.6

2 years ago

0.2.5

2 years ago

0.2.4

2 years ago