对leancloud云数据库的restful api进行简单的封装,使其使用更接近传统后台数据库调用方式
/**
* created by zn-Cabigail on 2017/10/10 下午4:23
*/
import service from './callHttpservice';
var baseUrl = 'https://leancloud.cn:443/1.1/'; //设置baseUrl
var getSql = function (tableName,queryParam) //转leanCloud的类sql
{
var getOrderBy = function(value)
{
if(!value) return "";
if(!value.by) return "";
if(!value.order) return "";
return " order by " +value.by + " " + value.order;
};
var query = "";
var select = "*";
var limit = "";
if(!queryParam) queryParam = {};
if(queryParam.wheresql && queryParam.wheresql.length > 0) query += " where "+queryParam.wheresql;
if(queryParam.orderby ) query += getOrderBy(queryParam.orderby);
if(queryParam.keys && queryParam.keys.length > 0) select = queryParam.keys;
if(queryParam.startindex != undefined && queryParam.pagesize != undefined) limit += "limit "+ queryParam.startindex + "," + queryParam.pagesize;
console.log("[sql] : select " + select + " from " + tableName + query + " "+limit);
return ("select " + select + " from " + tableName + query + " "+limit);
}
const fun = {
//小工具,可以格式化时间字符串或者date对象,由于时间的搜索是UTC格式的时间戳,所以需要转换一下方可使用,
//兼容字符串格式的时间(YYYY-MM-DD, YYYY-MM-DD HH:mm:ss)和Date对象(new Date());
//使用在wheresql中
dateFormart : function(date)
{
if(typeof date == 'string') return "date("+JSON.stringify(new Date(date))+")";
if(typeof date == 'object') return "date("+JSON.stringify(date)+")";
return date;
},
insert :function(tableName,body) //添加数据,参数为表名,添加的数据body,json格式
{
return service.post(baseUrl+"classes/"+tableName,body);
},
modify : function (tableName,id,body) //修改,参数为表名,数据id,修改的数据body,json格式
{
return service.put(baseUrl+"classes/"+tableName+"/"+id,body);
},
delete :function (tableName,id) //删除,表名,数据id
{
return service.delete(baseUrl+"classes/"+tableName+"/"+id);
},
getOne:function (tableName,id) //获取一条数据,参数为id
{
return service.get(baseUrl+"classes/"+tableName+"/"+id);
},
getList:function (tableName,queryParam) //获取多条数据,参数为表名,查询参数
{
return service.get(baseUrl+"cloudQuery"+"?cql="+getSql(tableName,queryParam));
},
count:function(tableName,queryParam) //查询个数
{
if(!queryParam) queryParam = {};
queryParam.orderby = {order:'desc',by:'updatedAt'};
queryParam.keys = "count(*)";
return service.get(baseUrl+"cloudQuery"+"?cql="+getSql(tableName,queryParam));
},
getPage:function (tableName,queryParam,startindex,pagesize) //分页获取多条数据,参数为表名,查询参数,开始下表,每页展示数据条数
{
if(!queryParam) queryParam = {};
queryParam.orderby = {order:'desc',by:'updatedAt'};
queryParam.startindex =startindex;
queryParam.pagesize =pagesize;
return service.get(baseUrl+"cloudQuery"+"?cql="+getSql(tableName,queryParam));
}
};
export default fun;
//-------------------------------------------------------------------------------------------------------
//查询参数格式
// var queryParam = {
// wheresql:'createdAt > ' + util.dateFormart('2017-10-02T07:26:08.199Z')
// keys:'name,age'
// }
//wheresql基本形式
var demoWhereSql = function()
{
//=
var sql1 = "name = 'zn'";
var sql2 = "age = 1";
var date = "date = "+ fun.dateFormart('2017-01-01'); //时间的查询要用dateFormart转换
//!=
var sql4 = " name != 'aa'";
//>,>=,<,<=
var sql5 = " age > 20";
//or
var sql6 = " age >20 or age < 10";
//and
var sql7 = "age >10 and age < 20";
//(not) like
var sql8 = " name like '%25n'"; //左包,注意%需要encode,直接写%25就可以
var sql9 = " name like 'z%25'"; //右包,注意%需要encode,直接写%25就可以
var sql10 = " name like '%25zn%25'"; //全包,注意%需要encode,直接写%25就可以
//(not) regex
var sql11 = " name (not) regexp 'zn*'";
//key (不)存在
var sql12 = " weigth is (not) exists";
}
//调用方法
//util.get().then(function(res){});
//查询数据,body缺省
// util.getList('zn').then(function(res){
// if(!res) return;
// console.log(JSON.stringify(res))
// })
// util.getOne('zn','59dc863b570c35088ceca988').then(function(res)
// {
// if(!res) return;
// console.log(JSON.stringify(res))
// })
// var queryParam = {
// wheresql:'createdAt > ' + util.dateFormart('2017-10-02T07:26:08.199Z') + " and name like '%25kk%25'",
// orderby:{order:'asc',by:'name,age'},
// keys:'name,age'
// }
// util.getPage('zn',queryParam,0,3).then(function(res){
// if(!res) return;
// console.log(JSON.stringify(res))
// })
// util.insert('zn1',{name:'wkk1',age:31}).then(function(res){
// if(!res) return;
// console.log(JSON.stringify(res))
// })
// util.modify('zn','59dd8901fe88c2003ca7bce5',{weight:200}).then(function(res) {
// if(!res) return;
// console.log(JSON.stringify(res))
// })
// util.delete('zn','59dc863b570c35088ceca988').then(function(res) {
// if(!res) return;
// console.log(JSON.stringify(res))
// })
// util.count('zn',queryParam).then(function(res) {
// if(!res) return;
// console.log(JSON.stringify(res))
// })