bmob后端云简单封装

admin2020-12-07  2.3K+

转matresnan的文章,对bmob云数据库的封装。

// bmob后端云
import inet.http;
import web.json;
import inet.url;
import console;
 
 
class bmob{
    ctor(applicationId, apiKey){
        if(applicationId){
            this.applicationId = applicationId
        }else{error(
            "applicationId不能为空,获取应用密钥请访问网址:http://doc.bmob.cn/"
        )}
        if(apiKey){
            this.apiKey = apiKey
        }else{error(
            "apiKey不能为空,获取应用密钥请访问网址:http://doc.bmob.cn/"
        )}
    };
     
    requests = function(method, url, json){
        var url = "https://api2.bmob.cn/1" + url
        var http = ..inet.http()
        var headers = {
            ["X-Bmob-Application-Id"] = this.applicationId;
            ["X-Bmob-REST-API-Key"] = this.apiKey;
            ["Content-Type"] = "application/json"
        }
        var data, errorMsg, statusCode = http.down(url, json, headers,,, method)
        if(data){
            return ..web.json.parse(data);
        }else{
            return false; 
        }
    };
    // 获取服务器时间
    timestamp = function(){
        return this.requests("GET", "/timestamp", null);
    };
    //----------查询相关----------
    // 根据对象id查询
    getObject = function(className, odjectId){
        return this.requests("GET", "/classes/" + className + "/" + odjectId, null);
    }
    // 查询表
    getObjects = function(className){
        return this.requests("GET", "/classes/" + className, null);
    }
    // 条件查询
    queryObject = function(className, data){
        var str = ..inet.url.encodeMbcs(..string.format("/classes/%s/?where=%s", className, data))
        return this.requests("GET", str, null);
    }
    //----------添加数据----------
    // 添加单条数据
    createObject = function(className, data){
        return this.requests("POST", "/classes/" + className, data);
    }
    // 批量添加数据
    createObjects = function(items){
        return this.requests("POST", "/batch", items);
    }
    //----------更新数据----------
    updateObject = function(className, odjectId, data){
        return this.requests("PUT", "/classes/" + className + "/" + odjectId, data);
    }
    //----------删除数据----------
    deleteObject = function(className, odjectId){
        return this.requests("DELETE", "/classes/" + className + "/" + odjectId);
    }
     
}
 
 
/**intellisense()
bmob() = 创建bmob对象\n简单实现了bmob后端云添加、删除、更新等功能\n其他功能请自行修改,细节问题请参考文档:http://doc.bmob.cn/data/restful/develop_doc/
end intellisense**/
 
/**intellisense(bmob)
timestamp() = 获取bmob后端云服务器时间
getObject(tableName, odjectId) = 根据对象id查询数据
getObjects(tableName) = 获取指定表全部数据
queryObject(tableName, queryData) = 条件查询,条件格式为json
createObject(tableName, data) = 添加单条数据\n提交的数据格式为json,可用web.json.stringify()函数把aardio表转化成json文本
createObjects(data) = 批量添加数据\n提交的数据格式为json,可用web.json.stringify()函数把aardio表转化成json文本
updateObject(tableName, odjectId, data) = 更新数据\nodjectId可为对象id,也可以为where条件,条件也为json格式\n例如:?where={...}
deleteObject(tableName, odjectId) = 删除数据\nodjectId可为对象id,也可以为where条件,条件也为json格式\n例:?where={...}
end intellisense**/
转载请注明原文地址: https://aardio.net/read-22.html
最新回复(4)
  • hipda2020-12-15
    引用2
    import web.rest.jsonClient;
     
    class bmob{
        ctor( appId,apiKey ){
            var http = ..web.rest.jsonClient();
             
            http.addHeaders = {
                ["X-Bmob-Application-Id"] = appId;
                ["X-Bmob-REST-API-Key"] = apiKey
            }
             
            this = http.api("https://api2.bmob.cn/1/")
            this["http"] = http; 
        };
    }
  • hipda2020-12-15
    引用3
    这样子就搞定了!web.rest 就是把rest api 转化为 aardio函数的 

    标准库和扩展库有很多rest使用的例子

    使用:
  • hipda2020-12-15
    引用4
  • hipda2020-12-15
    引用5
    import console;
     
    _appId = "1jkhsajasl983klkjlk34k555nkjaskdjk" //Your Application ID
    _apiKey = "sakjhksajkakshkhkjhkhkhk726939897" //Your REST API Key
     
    var bmob = bmob(_appId, _apiKey); 
     
    var timestamp = bmob.timestamp.get() 
    console.dumpJson( timestamp )
     
    console.pause(true);
数据库
数据库