Skip to content

Commit 2d30171

Browse files
committed
fix: fetch 清理不存在 key 的逻辑考虑指定 keys fetch 的情况
1 parent e9a37b4 commit 2d30171

File tree

3 files changed

+164
-27
lines changed

3 files changed

+164
-27
lines changed

recordings/Objects_136869387/Fetching-Objects_1241673741/fetch-should-remove-deleted-keys_1235015609/recording.har

Lines changed: 138 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
},
99
"entries": [
1010
{
11-
"_id": "a54a79dc2b720a740328a56c3d840384",
11+
"_id": "453957608e886366ee9fa38eee427008",
1212
"_order": 0,
1313
"cache": {},
1414
"request": {
@@ -21,7 +21,7 @@
2121
},
2222
{
2323
"name": "user-agent",
24-
"value": "LeanCloud-JS-SDK/3.15.0 (Node.js; Node.js/v12.10.0)"
24+
"value": "LeanCloud-JS-SDK/4.0.0 (Node.js; Node.js/v12.12.0)"
2525
},
2626
{
2727
"name": "x-lc-id",
@@ -33,7 +33,7 @@
3333
},
3434
{
3535
"name": "x-lc-sign",
36-
"value": "e925d8f948e7f6b410f1910ca95546b4,1568868355888"
36+
"value": "27fe3f232e7227707dfffe746c6fbd6d,1575268748053"
3737
},
3838
{
3939
"name": "x-lc-hook-key",
@@ -49,26 +49,31 @@
4949
},
5050
{
5151
"name": "host",
52-
"value": "95tnuaos.api.lncldapi.com"
52+
"value": "95tnuaos.lc-cn-e1-shared.com"
5353
}
5454
],
55-
"headersSize": 450,
55+
"headersSize": 476,
5656
"httpVersion": "HTTP/1.1",
5757
"method": "GET",
5858
"postData": {
5959
"mimeType": "application/json;charset=UTF-8",
6060
"params": [],
6161
"text": "null"
6262
},
63-
"queryString": [],
64-
"url": "https://95tnuaos.api.lncldapi.com/1.1/classes/GameScore/5d8308029c9235000886c4cf"
63+
"queryString": [
64+
{
65+
"name": "keys",
66+
"value": "fakedDeletedKey"
67+
}
68+
],
69+
"url": "https://95tnuaos.lc-cn-e1-shared.com/1.1/classes/GameScore/5d8308029c9235000886c4cf?keys=fakedDeletedKey"
6570
},
6671
"response": {
67-
"bodySize": 330,
72+
"bodySize": 48,
6873
"content": {
6974
"mimeType": "application/json;charset=utf-8",
70-
"size": 330,
71-
"text": "[\"1f8b08000000000000037d8d310ec2300c45efe2b9ad9c3429493646069898400c214e455151aa340c0871771cb133d97efa7eff0dcf857c89b42de040a2b02dda56d8232aa7b4d3ba1b8c3e41033e6770e73ac4ef927069205def31941df1af26d3a341698395bd46446386a0c2c8e990e39f0ad5e166a815cbec5f311ffc23728e88c954c553ddd6903263c15e64e18d85fb444c463faff1f3055ae47c6ac7000000\"]"
75+
"size": 48,
76+
"text": "[\"1f8b0800000000000003abae050043bfa6a302000000\"]"
7277
},
7378
"cookies": [],
7479
"headers": [
@@ -78,7 +83,7 @@
7883
},
7984
{
8085
"name": "date",
81-
"value": "Thu, 19 Sep 2019 04:45:56 GMT"
86+
"value": "Mon, 02 Dec 2019 06:39:08 GMT"
8287
},
8388
{
8489
"name": "content-type",
@@ -105,30 +110,146 @@
105110
"value": "no-cache"
106111
},
107112
{
108-
"name": "last-modified",
109-
"value": "Thu, 19 Sep 2019 04:45:55.685 GMT"
113+
"name": "content-encoding",
114+
"value": "gzip"
115+
}
116+
],
117+
"headersSize": 250,
118+
"httpVersion": "HTTP/1.1",
119+
"redirectURL": "",
120+
"status": 200,
121+
"statusText": "OK"
122+
},
123+
"startedDateTime": "2019-12-02T06:39:08.057Z",
124+
"time": 609,
125+
"timings": {
126+
"blocked": -1,
127+
"connect": -1,
128+
"dns": -1,
129+
"receive": 0,
130+
"send": 0,
131+
"ssl": -1,
132+
"wait": 609
133+
}
134+
},
135+
{
136+
"_id": "a54a79dc2b720a740328a56c3d840384",
137+
"_order": 0,
138+
"cache": {},
139+
"request": {
140+
"bodySize": 4,
141+
"cookies": [],
142+
"headers": [
143+
{
144+
"name": "accept-encoding",
145+
"value": "gzip, deflate"
146+
},
147+
{
148+
"name": "user-agent",
149+
"value": "LeanCloud-JS-SDK/4.0.0 (Node.js; Node.js/v12.12.0)"
150+
},
151+
{
152+
"name": "x-lc-id",
153+
"value": "95TNUaOSUd8IpKNW0RSqSEOm-9Nh9j0Va"
154+
},
155+
{
156+
"name": "content-type",
157+
"value": "application/json;charset=UTF-8"
158+
},
159+
{
160+
"name": "x-lc-sign",
161+
"value": "10de8dad95beec10f84d9e1c3b700bd5,1575269685664"
162+
},
163+
{
164+
"name": "x-lc-hook-key",
165+
"value": "2iCbUZDgEF0siKxmCn2kVQXV"
166+
},
167+
{
168+
"name": "x-lc-prod",
169+
"value": "1"
170+
},
171+
{
172+
"name": "content-length",
173+
"value": 4
174+
},
175+
{
176+
"name": "host",
177+
"value": "95tnuaos.lc-cn-e1-shared.com"
178+
}
179+
],
180+
"headersSize": 455,
181+
"httpVersion": "HTTP/1.1",
182+
"method": "GET",
183+
"postData": {
184+
"mimeType": "application/json;charset=UTF-8",
185+
"params": [],
186+
"text": "null"
187+
},
188+
"queryString": [],
189+
"url": "https://95tnuaos.lc-cn-e1-shared.com/1.1/classes/GameScore/5d8308029c9235000886c4cf"
190+
},
191+
"response": {
192+
"bodySize": 48,
193+
"content": {
194+
"mimeType": "application/json;charset=utf-8",
195+
"size": 48,
196+
"text": "[\"1f8b0800000000000003abae050043bfa6a302000000\"]"
197+
},
198+
"cookies": [],
199+
"headers": [
200+
{
201+
"name": "server",
202+
"value": "openresty"
203+
},
204+
{
205+
"name": "date",
206+
"value": "Mon, 02 Dec 2019 06:54:45 GMT"
207+
},
208+
{
209+
"name": "content-type",
210+
"value": "application/json;charset=utf-8"
211+
},
212+
{
213+
"name": "transfer-encoding",
214+
"value": "chunked"
215+
},
216+
{
217+
"name": "connection",
218+
"value": "close"
219+
},
220+
{
221+
"name": "vary",
222+
"value": "Accept-Encoding"
223+
},
224+
{
225+
"name": "cache-control",
226+
"value": "no-cache,no-store"
227+
},
228+
{
229+
"name": "pragma",
230+
"value": "no-cache"
110231
},
111232
{
112233
"name": "content-encoding",
113234
"value": "gzip"
114235
}
115236
],
116-
"headersSize": 300,
237+
"headersSize": 250,
117238
"httpVersion": "HTTP/1.1",
118239
"redirectURL": "",
119240
"status": 200,
120241
"statusText": "OK"
121242
},
122-
"startedDateTime": "2019-09-19T04:45:55.890Z",
123-
"time": 167,
243+
"startedDateTime": "2019-12-02T06:54:45.734Z",
244+
"time": 110,
124245
"timings": {
125246
"blocked": -1,
126247
"connect": -1,
127248
"dns": -1,
128249
"receive": 0,
129250
"send": 0,
130251
"ssl": -1,
131-
"wait": 167
252+
"wait": 110
132253
}
133254
}
134255
],

src/object.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -947,14 +947,21 @@ module.exports = function(AV) {
947947
);
948948
return request.then(function(response) {
949949
const fetchedAttrs = self.parse(response);
950-
if (!fetchOptions.keys) self._cleanupUnsetKeys(fetchedAttrs);
950+
self._cleanupUnsetKeys(
951+
fetchedAttrs,
952+
fetchOptions.keys
953+
? ensureArray(fetchOptions.keys)
954+
.join(',')
955+
.split(',')
956+
: undefined
957+
);
951958
self._finishFetch(fetchedAttrs, true);
952959
return self;
953960
});
954961
},
955962

956-
_cleanupUnsetKeys(fetchedAttrs) {
957-
AV._objectEach(this._serverData, (value, key) => {
963+
_cleanupUnsetKeys(fetchedAttrs, fetchedKeys = _.keys(this._serverData)) {
964+
_.forEach(fetchedKeys, key => {
958965
if (fetchedAttrs[key] === undefined) delete this._serverData[key];
959966
});
960967
},

test/object.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -448,14 +448,23 @@ describe('Objects', function() {
448448
expect(score.id).to.be.eql(gameScore.id);
449449
}));
450450
it('fetch should remove deleted keys', () => {
451-
const score = AV.parseJSON(
452-
Object.assign(gameScore.toFullJSON(), {
453-
fakedDeletedKey: 'value',
451+
const getFakedScore = () =>
452+
AV.parseJSON(
453+
Object.assign(gameScore.toFullJSON(), {
454+
fakedDeletedKey: 'value',
455+
})
456+
);
457+
return getFakedScore()
458+
.fetch()
459+
.then(fetchedScore => {
460+
expect(fetchedScore.get('fakedDeletedKey')).to.eql(undefined);
461+
return getFakedScore().fetch({
462+
keys: 'fakedDeletedKey',
463+
});
454464
})
455-
);
456-
return score.fetch().then(() => {
457-
expect(score.get('fakedDeletedKey')).to.eql(undefined);
458-
});
465+
.then(fetchedScore => {
466+
expect(fetchedScore.get('fakedDeletedKey')).to.eql(undefined);
467+
});
459468
});
460469
it('fetchAll', () =>
461470
AV.Object.fetchAll([

0 commit comments

Comments
 (0)