Skip to content

Commit ad0595d

Browse files
authored
Merge pull request #329 from leeyeh/fix-242
fix(Query): AV.Query#get use fetch now
2 parents acdade5 + 49594f8 commit ad0595d

File tree

4 files changed

+30
-18
lines changed

4 files changed

+30
-18
lines changed

src/object.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,9 @@ module.exports = function(AV) {
816816
* completes.
817817
*/
818818
fetch: function(fetchOptions = {}, options) {
819+
if (_.isArray(fetchOptions.keys)) {
820+
fetchOptions.keys = fetchOptions.keys.join(',');
821+
}
819822
if (_.isArray(fetchOptions.include)) {
820823
fetchOptions.include = fetchOptions.include.join(',');
821824
}

src/query.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -178,18 +178,17 @@ module.exports = function(AV) {
178178
}
179179

180180
var self = this;
181-
self.equalTo('objectId', objectId);
182181

183-
return self.first(options).then(function(response) {
184-
if (!_.isEmpty(response)) {
185-
return response;
186-
}
182+
var obj = self._newObject();
183+
obj.id = objectId;
187184

188-
var errorObject = new AVError(AVError.OBJECT_NOT_FOUND,
189-
"Object not found.");
190-
return AV.Promise.reject(errorObject);
185+
var queryJSON = self.toJSON();
186+
var fetchOptions = {};
191187

192-
});
188+
if (queryJSON.keys) fetchOptions.keys = queryJSON.keys;
189+
if (queryJSON.include) fetchOptions.include = queryJSON.include;
190+
191+
return obj.fetch(fetchOptions, options);
193192
},
194193

195194
/**

test/object.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ var BackbonePerson = AV.Object.extend('Person');
1111
describe('Objects', function(){
1212
var objId;
1313
var gameScore = GameScore.new();
14-
after(function(done) {
15-
gameScore.destroy().then(function() {
16-
done();
17-
});
14+
after(function() {
15+
return gameScore.destroy();
1816
});
1917
it('getter/setter compatible', function() {
2018
Object.defineProperty(Post.prototype, 'name', {
@@ -138,14 +136,14 @@ describe('Objects', function(){
138136
});
139137

140138
describe('Fetching Objects', () => {
141-
it('fetch', () =>
139+
it('fetch', () =>
142140
AV.Object.createWithoutData('GameScore', gameScore.id).fetch().then(score => {
143141
expect(score.get('score')).to.be.a('number');
144142
expect(score.createdAt).to.be.a(Date);
145143
expect(score.id).to.be.eql(gameScore.id);
146144
})
147145
);
148-
it('fetchAll', () =>
146+
it('fetchAll', () =>
149147
AV.Object.fetchAll([
150148
AV.Object.createWithoutData('GameScore', gameScore.id),
151149
AV.Object.createWithoutData('GameScore', gameScore.id),
@@ -252,7 +250,7 @@ describe('Objects', function(){
252250
query.include("parent");
253251
return query.get(myComment.id);
254252
}).then(function(obj) {
255-
expect(obj.get("parent").get("title")).to.be("post1");
253+
expect(obj.get("parent").get("title")).to.be("post1");
256254
});
257255
});
258256

@@ -358,8 +356,20 @@ describe('Objects', function(){
358356
});
359357
});
360358
});
361-
362359

360+
it("fetchOptions keys", function(){
361+
var person = new Person();
362+
person.set('pname', 'dennis');
363+
person.set('age', 1)
364+
return person.save().then(() =>
365+
AV.Object.createWithoutData('Person', person.id).fetch({
366+
keys: ['pname'],
367+
})
368+
).then(function(person) {
369+
expect(person.get('pname')).to.be('dennis');
370+
expect(person.get('age')).to.be(undefined);
371+
});
372+
});
363373

364374
});
365375

test/user.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ describe("User", function() {
110110
var user = AV.User.current();
111111
return new AV.Query('_Role').equalTo('name', 'testRole').first()
112112
.then(function(role) {
113-
return role.destroy();
113+
return role.destroy({ useMasterKey: true });
114114
}).catch(function() {
115115
// already destroyed
116116
}).then(function() {

0 commit comments

Comments
 (0)