开源 企业版 高校版 私有云 模力方舟 AI 队友
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
1 Star 2 Fork 1

APIJSON/APIJSON-Builder

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
已有帐号? 立即登录
文件
master
分支 (1)
master
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
项目仓库所选许可证以仓库主分支所使用许可证为准
master
分支 (1)
master
克隆/下载
克隆/下载
提示
下载代码请复制以下命令到终端执行
为确保你提交的代码身份被 Gitee 正确识别,请执行以下命令完成配置
初次使用 SSH 协议进行代码克隆、推送等操作时,需按下述提示完成 SSH 配置
1 生成 RSA 密钥
2 获取 RSA 公钥内容,并配置到 SSH公钥
在 Gitee 上使用 SVN,请访问 使用指南
使用 HTTPS 协议时,命令行会出现如下账号密码验证步骤。基于安全考虑,Gitee 建议 配置并使用私人令牌 替代登录密码进行克隆、推送等操作
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # 私人令牌
master
分支 (1)
master
APIJSON-Builder
/
package
/
queryBuilder.js
APIJSON-Builder
/
package
/
queryBuilder.js
queryBuilder.js 5.09 KB
一键复制 编辑 原始数据 按行查看 历史
pengxg 提交于 2020年09月07日 14:04 +08:00 . commit init
import {isEmpty, isString, isNumber, isBoolean, assert, reverseMerge} from "./utils"
import request from "./utils/request"
import Condition from './condition'
import AssociativeCondition from "./associativeCondition"
export default class QueryBuilder {
table;
conditions = [];
queryType = 2; // 1表示返回总数, 2表示返回列表数和总数
multiple = false;
resField;
pagination;
orders;
associativeCondition = [];
static by(table, queryType) {
return new QueryBuilder(table, queryType)
}
constructor(table, queryType) {
assert(!isEmpty(table) && isString(table), `[ApiJson] 参数table: ${table} 非法`)
if (queryType !== undefined) {
assert([1, 2].indexOf(queryType) > -1, `[ApiJson] 参数queryType: ${queryType} 非法, 必须为数值1或2`)
this.queryType = queryType
}
this.table = table
}
/**
* 设置关联条件
* @param table 关联的主表表名
* @param primaryKey 关联的主表主键
* @param foreignKey 和主表关联的当前表的外键。可以只输入字段名,或者符合apijson的外键全路径(以/开头)
* @param resField 关联的主表响应字段
*/
setAssociativeCondition(table, primaryKey, foreignKey, resField) {
if (!isEmpty(foreignKey) && !foreignKey.startsWith("/")) {
foreignKey = "/" + this.table + "/" + foreignKey
}
this.associativeCondition.push(AssociativeCondition.by(table, primaryKey, foreignKey, resField))
return this
}
/**
* 设置关联条件
* @param condition
* @returns {QueryBuilder}
*/
setAssociativeConditionPlus(condition) {
assert(condition instanceof AssociativeCondition, `[ApiJson] 参数condition: ${condition} 非法, 必须是 AssociativeCondition类型`)
this.associativeCondition.push(condition)
return this
}
condition(condition) {
assert(condition instanceof Condition, `[ApiJson] 参数condition: ${condition} 非法, 必须是 Condition类型`)
this.conditions.push(condition)
return this;
}
setResFields(fields = '*') {
assert(!isEmpty(fields) && isString(fields), `[ApiJson] 参数fields: ${fields} 非法`)
if (fields.trim() !== '*') {
this.resField = fields;
}
return this;
}
multi(multi = false) {
this.multiple = multi;
return this;
}
page(page = 0, count = 10) {
assert(isNumber(page) && isNumber(count), `[ApiJson] 参数page: ${page} 和 count: ${count} 都必须为Number类型`)
this.pagination = {
page: page,
count: count
}
return this;
}
/**
*
* @param field
* @param desc 是否降序, 默认true
*/
order(field, desc = true) {
assert(!isEmpty(field) && isString(field), `[ApiJson] 参数field: ${field} 非法`)
assert(isBoolean(desc), `[ApiJson] 参数desc: ${desc} 非法`)
this.orders = {};
this.orders[field] = (desc ? '-' : '+ ');
return this;
}
toJson() {
let json = {};
const {table, conditions, pagination, resField, orders, multiple, associativeCondition, queryType} = this;
assert(!(pagination && multiple === false), '指定了分页内容, 则必须指定multiple为true!')
function tableToJson(table, conditions, resField, orders) {
const tableJson = {};
if (conditions && conditions.length > 0) {
conditions.forEach(c => {
const json = c.toJson()
if (json !== null) {
reverseMerge(tableJson, c.toJson())
}
});
}
if (resField) {
tableJson['@column'] = resField + ',id'; // id内置
}
if (orders) {
tableJson['@order'] = Object.keys(orders).map(field => field + orders[field]).join(',');
}
const json = {}
json[table] = tableJson;
return json;
}
function associativeTableToJson(associativeCondition, json) {
associativeCondition.forEach(c => {
reverseMerge(json, c.toJson())
})
}
if (multiple) {
json['[]'] = tableToJson(table, conditions, resField, orders)
associativeTableToJson(associativeCondition, json['[]'])
if (pagination) {
reverseMerge(json['[]'], pagination)
}
json['[]']['query'] = queryType; // 查询数据和总数(附带total表示总数) // https://vincentcheng.github.io/apijson-doc/zh/grammar.html#%E5%88%86%E9%A1%B5
json['total@'] = '/[]/total'
} else {
json = tableToJson(table, conditions, resField, orders)
associativeTableToJson(associativeCondition, json)
}
return json
}
send() {
const params = this.toJson();
return request({
url: '/get',
method: 'post',
data: params
})
}
}
Loading...
举报
举报成功
我们将于2个工作日内通过站内信反馈结果给你!
请认真填写举报原因,尽可能描述详细。
请选择举报类型
取消
发送
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

简介

一个方便为 APIJSON 构建 RESTful 请求的 JavaScript 库,请给原仓库 Star
取消

发行版

暂无发行版

贡献者

全部

近期动态

不能加载更多了
编辑仓库简介
简介内容
主页
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/APIJSON/APIJSON-Builder.git
git@gitee.com:APIJSON/APIJSON-Builder.git
APIJSON
APIJSON-Builder
APIJSON-Builder
master
点此查找更多帮助

搜索帮助

仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册

AltStyle によって変換されたページ (->オリジナル) /