Skip to content

Commit 14dbf9e

Browse files
committed
merge the update auth record response with pb.authStore.record instead of replace
1 parent 27f637a commit 14dbf9e

File tree

4 files changed

+31
-8
lines changed

4 files changed

+31
-8
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 0.22.0-rc2
2+
3+
**⚠️ This is a prerelease and works only with PocketBase v0.23.0+!**
4+
5+
- Instead of replacing the entire `pb.authStore.record`, on auth record update we now only replace the available returned response record data ([pocketbase#5638](https://github.com/pocketbase/pocketbase/issues/5638)).
6+
7+
18
## 0.22.0-rc
29

310
**⚠️ This release works only with PocketBase v0.23.0+ and contains breaking changes!**

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "0.22.0-rc",
2+
"version": "0.22.0-rc2",
33
"name": "pocketbase",
44
"description": "PocketBase JavaScript SDK",
55
"author": "Gani Georgiev",

src/services/RecordService.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ export class RecordService<M = RecordModel> extends CrudService<M> {
255255
* @inheritdoc
256256
*
257257
* If the current `client.authStore.record` matches with the updated id, then
258-
* on success the `client.authStore.record` will be updated with the result.
258+
* on success the `client.authStore.record` will be updated with the new response record fields.
259259
*/
260260
async update<T = M>(
261261
id: string,
@@ -270,7 +270,14 @@ export class RecordService<M = RecordModel> extends CrudService<M> {
270270
this.client.authStore.record?.collectionName ===
271271
this.collectionIdOrName)
272272
) {
273-
this.client.authStore.save(this.client.authStore.token, item);
273+
let authExpand = Object.assign({}, this.client.authStore.record.expand);
274+
let authRecord = Object.assign({}, this.client.authStore.record, item);
275+
if (authExpand && item.expand) {
276+
// for now replace only top-level expand
277+
authRecord.expand = Object.assign(authExpand, item.expand)
278+
}
279+
280+
this.client.authStore.save(this.client.authStore.token, authRecord);
274281
}
275282

276283
return item as any as T;

tests/services/RecordService.spec.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,26 @@ describe("RecordService", function () {
4646
replyBody: {
4747
id: "test123",
4848
49+
name: "name_new",
50+
expand: {"b": 3},
4951
},
5052
});
5153

5254
service.client.authStore.save("test_token", {
5355
id: "test123",
5456
collectionName: "sub=",
57+
name: "abc",
58+
expand: {"a": 1, "b": 2},
5559
} as any);
5660

5761
await service.update("test123", {});
5862

59-
assert.equal(service.client.authStore.model?.email, "[email protected]");
63+
assert.equal(service.client.authStore.record?.id, "test123");
64+
assert.equal(service.client.authStore.record?.collectionName, "sub=");
65+
assert.equal(service.client.authStore.record?.name, "name_new");
66+
assert.equal(service.client.authStore.record?.email, "[email protected]");
67+
assert.equal(service.client.authStore.record?.expand?.a, 1);
68+
assert.equal(service.client.authStore.record?.expand?.b, 3);
6069
});
6170

6271
test("Should not update the AuthStore record model on matching id but mismatched collection", async function () {
@@ -78,7 +87,7 @@ describe("RecordService", function () {
7887

7988
await service.update("test123", {});
8089

81-
assert.equal(service.client.authStore.model?.email, "[email protected]");
90+
assert.equal(service.client.authStore.record?.email, "[email protected]");
8291
});
8392

8493
test("Should not update the AuthStore record model on mismatched update id", async function () {
@@ -100,7 +109,7 @@ describe("RecordService", function () {
100109

101110
await service.update("test123", {});
102111

103-
assert.equal(service.client.authStore.model?.email, "[email protected]");
112+
assert.equal(service.client.authStore.record?.email, "[email protected]");
104113
});
105114

106115
test("Should delete the AuthStore record model on matching delete id and collection", async function () {
@@ -177,7 +186,7 @@ describe("RecordService", function () {
177186
const result = await service.confirmVerification(token);
178187

179188
assert.isTrue(result);
180-
assert.isTrue(service.client.authStore.model?.verified);
189+
assert.isTrue(service.client.authStore.record?.verified);
181190
});
182191

183192
test("Should not update the AuthStore record model verified state on mismatched token data", async function () {
@@ -203,7 +212,7 @@ describe("RecordService", function () {
203212
const result = await service.confirmVerification(token);
204213

205214
assert.isTrue(result);
206-
assert.isFalse(service.client.authStore.model?.verified);
215+
assert.isFalse(service.client.authStore.record?.verified);
207216
});
208217

209218
test("Should delete the AuthStore record model matching the token data", async function () {

0 commit comments

Comments
 (0)