Skip to content

Commit fe25477

Browse files
authored
feat: re-shape the persistent methods (#70)
1 parent 17d202d commit fe25477

File tree

61 files changed

+704
-1323
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+704
-1323
lines changed

Diff for: docs/guide/relationships/getting-started.md

+18-62
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ export default {
175175

176176
## Inserting Relationships
177177

178-
When inserting new records into the store, Vuex ORM automatically normalizes and stores data that contains any nested relationships in it's data structure. For example, let's say you have the `User` model that has a relationship to the `Post` model:
178+
You may use `save` method to save a record with its nested relationships to the store. When saving new records into the store via `save` method, Vuex ORM automatically normalizes and stores data that contains any nested relationships in it's data structure. For example, let's say you have the `User` model that has a relationship to the `Post` model:
179179

180180
```js
181181
class User extends Model {
@@ -191,7 +191,7 @@ class User extends Model {
191191
}
192192
```
193193

194-
When you insert a user record containing post records under the posts key, Vuex ORM decouples the user and post records before saving them to the store.
194+
When you save a user record containing post records under the posts key, Vuex ORM decouples the user and post records before saving them to the store.
195195

196196
```js
197197
// The user reocrd.
@@ -204,8 +204,8 @@ const user = {
204204
]
205205
}
206206

207-
// Insert the user record.
208-
store.$repo(User).insert(user)
207+
// Save the user record.
208+
store.$repo(User).save(user)
209209

210210
// The result inside the store.
211211
{
@@ -234,8 +234,8 @@ const user = {
234234
]
235235
}
236236

237-
// Insert the user record.
238-
store.$repo(User).insert(user)
237+
// Save the user record.
238+
store.$repo(User).save(user)
239239

240240
// The result inside the store.
241241
{
@@ -255,7 +255,7 @@ Depending on the relationship types, there may be a slight difference in behavio
255255

256256
## Updating Relationships
257257

258-
Similar to the `insert` or `fresh` method, the `update` method also normalizes any nested relationships within the given records. Let's reuse our `User` and `Post` example:
258+
The `save` method also updates models that already exist in the store, including any nested relationships within the given records. Let's reuse our `User` and `Post` example:
259259

260260
```js
261261
class User extends Model {
@@ -271,89 +271,45 @@ class User extends Model {
271271
}
272272
```
273273

274-
When you update the user record, relationships will be saved in a normalized form inside the store.
274+
When you save the user record, relationships will be saved in a normalized form inside the store.
275275

276276
```js
277-
// The user reocrd.
278-
const user = {
279-
id: 1,
280-
name: 'John Doe',
281-
posts: [
282-
{ id: 1, userId: 1, title: '...' },
283-
{ id: 2, userId: 2, title: '...' }
284-
]
285-
}
286-
287-
// Insert the user record.
288-
store.$repo(User).update(user)
289-
290-
// The result inside the store.
277+
// Existing data in the store.
291278
{
292279
entities: {
293280
users: {
294281
1: { id: 1, name: 'John Doe' }
295282
},
296283
posts: {
297-
1: { id: 1, userId: 1, title: '...' },
298-
2: { id: 2, userId: 1, title: '...' }
284+
1: { id: 1, userId: 1, title: 'Title A' },
285+
2: { id: 2, userId: 1, title: 'Title B' }
299286
}
300287
}
301288
}
302-
```
303289

304-
Again as same as when inserting data, `update` method will also automatically generates any missing foreign keys.
305-
306-
```js
307290
// The user reocrd.
308291
const user = {
309292
id: 1,
310-
name: 'John Doe',
293+
name: 'Jane Doe',
311294
posts: [
312-
{ id: 1, title: '...' }, // <- No foregin key.
313-
{ id: 2, title: '...' } // <- No foregin key.
295+
{ id: 1, userId: 1, title: 'Title C' },
296+
{ id: 2, userId: 2, title: 'Title D' }
314297
]
315298
}
316299

317300
// Insert the user record.
318-
store.$repo(User).update(user)
301+
store.$repo(User).save(user)
319302

320303
// The result inside the store.
321304
{
322305
entities: {
323306
users: {
324-
1: { id: 1, name: 'John Doe' }
307+
1: { id: 1, name: 'Jane Doe' } // <- Updated.
325308
},
326309
posts: {
327-
1: { id: 1, userId: 1, title: '...' }, // Foreign key generated.
328-
2: { id: 2, userId: 1, title: '...' } // Foreign key generated.
310+
1: { id: 1, userId: 1, title: 'Title C' }, // <- Updated.
311+
2: { id: 2, userId: 1, title: 'Title D' } // <- Updated.
329312
}
330313
}
331314
}
332315
```
333-
334-
### Caveats of Update Methods
335-
336-
Vuex Orm provides another method for updating data called `revise`, which requires you to specify a where clause to filter the record. `revise` will not normalize your data!
337-
338-
```js
339-
// Update the user by `revise` method.
340-
store.$repo(User).where('id', 1).revise({
341-
name: 'John Doe',
342-
posts: [
343-
{ id: 1, userId: 1, title: '...' },
344-
{ id: 2, userId: 2, title: '...' }
345-
]
346-
})
347-
348-
// The result inside the store. The relationships are ignored.
349-
{
350-
entities: {
351-
users: {
352-
1: { id: 1, name: 'John Doe' }
353-
},
354-
posts: {}
355-
}
356-
}
357-
```
358-
359-
This is because without each record having its primary key inside the records, Vuex ORM can't determine what record to update. The rule of thumb is to always use `update` method when saving nested relationships.

Diff for: docs/guide/repository/deleting-data.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ The `destroy` method will return deleted models. When you pass a single primary
3737
```js
3838
const user = await store.$repo(User).destroy(2)
3939

40-
// { id: 2, name: 'Jane Doe', age: 30 }
40+
// User { id: 2, name: 'Jane Doe', age: 30 }
4141

4242
const user = await store.$repo(User).destroy([1, 2])
4343

4444
/*
4545
[
46-
{ id: 1, name: 'John Doe', age: 40 },
47-
{ id: 2, name: 'Jane Doe', age: 30 }
46+
User { id: 1, name: 'John Doe', age: 40 },
47+
User { id: 2, name: 'Jane Doe', age: 30 }
4848
]
4949
*/
5050
```
@@ -55,7 +55,7 @@ If you wish to delete the entire records, you may use the `flush` method.
5555
store.$repo(User).flush()
5656
```
5757

58-
## Deleting data by query
58+
## Deleting Data By Query
5959

6060
You can also run a delete statement on a set of records. In this example, we will delete all flights that are marked as inactive.
6161

Diff for: docs/guide/repository/getting-started.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ class User extends Model {
2121
}
2222
```
2323

24-
To retrieve repository for the User model, you may do so by calling the `store.$repo` method, and passing the User model as the argument. After retrieving the repository, you may call methods such as `insert`, or `delete` on the repository.
24+
To retrieve repository for the User model, you may do so by calling the `store.$repo` method, and passing the User model as the argument. After retrieving the repository, you may call methods such as `save`, or `delete` on the repository.
2525

2626
```js
2727
const userRepo = store.$repo(User)
2828

29-
userRepo.insert(...)
29+
userRepo.save(...)
3030
```
3131

3232
In Vue Component, you would probably want to define a computed property to retrieve the repository.
@@ -43,7 +43,7 @@ export default {
4343

4444
methods: {
4545
saveUser (user) {
46-
this.userRepo.insert(user)
46+
this.userRepo.save(user)
4747
}
4848
}
4949
}

0 commit comments

Comments
 (0)