Skip to content

Commit fd91db9

Browse files
committed
Merge pull request #26 from avoscloud/feature/cloud_query
Added AV.Cloud.doCloudQuery
2 parents d2370ba + 538a150 commit fd91db9

File tree

5 files changed

+105
-3
lines changed

5 files changed

+105
-3
lines changed

changelog.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# 0.4.3
2+
3+
* 添加 CQL 查询支持。
4+
5+
# 老版本的 changelog
6+
https://download.avoscloud.com/sdk/javascript/changelog.txt

lib/av.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*!
22
* AVOSCloud JavaScript SDK
3-
* Version: 0.4.0
3+
* Version: 0.4.3
44
* Built: Mon Jun 03 2013 13:45:00
55
* http://avoscloud.com
66
*
@@ -13,7 +13,7 @@
1313
*/
1414
(function(root) {
1515
root.AV = root.AV || {};
16-
root.AV.VERSION = "js0.4.0";
16+
root.AV.VERSION = "js0.4.3";
1717
}(this));
1818
// Underscore.js 1.4.4
1919
// http://underscorejs.org
@@ -1593,6 +1593,7 @@
15931593
route !== "requestSmsCode" &&
15941594
route !== "verifySmsCode" &&
15951595
route !== "users" &&
1596+
route !== "cloudQuery" &&
15961597
route !== "qiniu" &&
15971598
route !== "statuses" &&
15981599
route !== 'subscribe/statuses/count' &&
@@ -8838,6 +8839,35 @@
88388839
})._thenRunCallbacks(options);
88398840
},
88408841

8842+
/**
8843+
* Retrieves a list of AVObjects that satisfy the CQL.
8844+
* CQL syntax please see https://cn.avoscloud.com/docs/cql_guide.html
8845+
* Either options.success or options.error is called when the find
8846+
* completes.
8847+
*
8848+
* @param {String} cql, A CQL string, see https://cn.avoscloud.com/docs/cql_guide.html
8849+
* @return {AV.Promise} A promise that is resolved with the results when
8850+
* the query completes.
8851+
*/
8852+
doCloudQuery: function(cql, options) {
8853+
var params = { cql: cql };
8854+
var request = AV._request("cloudQuery", null, null, 'GET', params)
8855+
return request.then(function(response) {
8856+
//query to process results.
8857+
var query = new AV.Query(response.className);
8858+
var results = _.map(response.results, function(json) {
8859+
var obj = query._newObject(response);
8860+
obj._finishFetch(query._processResult(json), true);
8861+
return obj;
8862+
});
8863+
return {
8864+
results: results,
8865+
count: response.count,
8866+
className: response.className
8867+
};
8868+
})._thenRunCallbacks(options);
8869+
},
8870+
88418871
/**
88428872
* Makes a call to request a sms code for operation verification.
88438873
* @param {Object} data The mobile phone number string or a JSON object contains mobilePhoneNumber,op,ttl,name etc.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "avoscloud-sdk",
3-
"version": "0.4.2",
3+
"version": "0.4.3",
44
"main": "./lib/av.js",
55
"description": "AVOSCloud JavaScript SDK.",
66
"repository": {

tests/object.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,26 @@ describe('Objects', function(){
5959
}
6060
})
6161
});
62+
63+
it('should validate failed.', function(done){
64+
var TestObject = AV.Object.extend('TestObject', {
65+
validate: function (attrs, options){
66+
return new AV.Error(1, "test");
67+
}
68+
});
69+
var testObject =new TestObject();
70+
testObject.set('a',1, {
71+
success: function(){
72+
throw "should not be here.";
73+
},
74+
error: function(obj, err){
75+
console.dir(err);
76+
expect(obj.get('a')).to.be(undefined);
77+
expect(err.message).to.be('test');
78+
done();
79+
}
80+
});
81+
});
6282
})
6383

6484

tests/query.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,52 @@ describe("Queries",function(){
2525

2626
});
2727

28+
describe("#cloudQuery", function(){
29+
it("should return results.", function(done){
30+
AV.Cloud.doCloudQuery('select * from GameScore').then(function(result){
31+
console.dir(result);
32+
var results = result.results;
33+
expect(results.length).to.be(100);
34+
expect(results[0].className).to.be("GameScore");
35+
expect(result.count).to.be(undefined);
36+
expect(result.className).to.be('GameScore');
37+
done();
38+
});
39+
});
40+
it("should return limited results.", function(done){
41+
AV.Cloud.doCloudQuery('select * from GameScore limit 10').then(function(result){
42+
console.dir(result);
43+
var results = result.results;
44+
expect(results.length).to.be(10);
45+
expect(results[0].className).to.be("GameScore");
46+
expect(result.count).to.be(undefined);
47+
expect(result.className).to.be('GameScore');
48+
done();
49+
});
50+
});
51+
it("should return count value.", function(done){
52+
AV.Cloud.doCloudQuery('select *,count(objectId) from GameScore limit 10').then(function(result){
53+
console.dir(result);
54+
var results = result.results;
55+
expect(results.length).to.be(10);
56+
expect(results[0].className).to.be("GameScore");
57+
expect(result.count).to.be.an('number');
58+
expect(result.className).to.be('GameScore');
59+
done();
60+
});
61+
});
62+
it("should return syntax error.", function(done){
63+
AV.Cloud.doCloudQuery('select * GameScore limit 10').then(function(){
64+
throw "Shoud not be successfully.";
65+
},
66+
function(error){
67+
console.dir(error);
68+
expect(error).to.be.an(AV.Error);
69+
done();
70+
});
71+
});
72+
});
73+
2874
describe("#save&query()",function(){
2975
it("should length + 1",function(done){
3076
query = new AV.Query(GameScore);

0 commit comments

Comments
 (0)