Skip to content

Commit 7f2584f

Browse files
committed
feat(axios): support persistBy fresh to replace store data
The response of an api call can now replace all existing records of the entity by setting persistBy to 'fresh', making the api response the single source of truth. closes #1885
1 parent c6bebb9 commit 7f2584f

4 files changed

Lines changed: 85 additions & 2 deletions

File tree

docs/content/3.plugins/2.axios/1.guide/2.configuration.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ In addition to [axios request options](https://github.com/axios/axios#request-co
151151
You can set this option to any one of the following string values:
152152

153153
- `insert`
154+
- `fresh`
155+
156+
With `fresh`, the response data replaces all existing records of the entity in the store — records that are no longer returned by the API are removed. This makes the API response the single source of truth.
157+
158+
```js
159+
useAxiosRepo(User).api().get('/api/users', { persistBy: 'fresh' })
160+
```
154161

155162
### `save`
156163

packages/axios/src/api/Response.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,6 @@ export class Response {
135135
* Pinia ORM persist methods.
136136
*/
137137
protected validatePersistAction (action: string): action is PersistMethods {
138-
return ['save', 'insert'].includes(action)
138+
return ['save', 'insert', 'fresh'].includes(action)
139139
}
140140
}

packages/axios/src/types/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Element } from 'pinia-orm'
22
import type { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'
33

4-
export type PersistMethods = 'save' | 'insert'
4+
export type PersistMethods = 'save' | 'insert' | 'fresh'
55

66
export type PersistOptions = { [P in PersistMethods]?: string[] }
77

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import axios from 'axios'
2+
import MockAdapter from 'axios-mock-adapter'
3+
import { Model, useRepo } from 'pinia-orm'
4+
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
5+
import { assertState } from '../helpers'
6+
import { useAxiosRepo } from '../../src'
7+
8+
describe('Feature - Request - Persist By', () => {
9+
let mock: MockAdapter
10+
11+
class User extends Model {
12+
static entity = 'users'
13+
14+
static fields () {
15+
return {
16+
id: this.attr(null),
17+
name: this.attr(''),
18+
}
19+
}
20+
}
21+
22+
beforeEach(() => {
23+
mock = new MockAdapter(axios)
24+
})
25+
afterEach(() => {
26+
mock.reset()
27+
})
28+
29+
it('replaces all existing records with `persistBy: fresh`', async () => {
30+
useRepo(User).save([
31+
{ id: 1, name: 'John Doe' },
32+
{ id: 2, name: 'Deleted Elsewhere' },
33+
])
34+
35+
mock.onGet('/users').reply(200, [
36+
{ id: 1, name: 'John Doe' },
37+
{ id: 3, name: 'Jane Doe' },
38+
])
39+
40+
const userStore = useAxiosRepo(User)
41+
42+
const result = await userStore.api().get('/users', {
43+
persistBy: 'fresh',
44+
})
45+
46+
expect(result.entities).toHaveLength(2)
47+
48+
assertState({
49+
users: {
50+
1: { id: 1, name: 'John Doe' },
51+
3: { id: 3, name: 'Jane Doe' },
52+
},
53+
})
54+
})
55+
56+
it('inserts records with `persistBy: insert`', async () => {
57+
useRepo(User).save({ id: 1, name: 'John Doe' })
58+
59+
mock.onGet('/users').reply(200, [
60+
{ id: 2, name: 'Jane Doe' },
61+
])
62+
63+
const userStore = useAxiosRepo(User)
64+
65+
await userStore.api().get('/users', {
66+
persistBy: 'insert',
67+
})
68+
69+
assertState({
70+
users: {
71+
1: { id: 1, name: 'John Doe' },
72+
2: { id: 2, name: 'Jane Doe' },
73+
},
74+
})
75+
})
76+
})

0 commit comments

Comments
 (0)