You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/guide/relationships/getting-started.md
+18-62
Original file line number
Diff line number
Diff line change
@@ -175,7 +175,7 @@ export default {
175
175
176
176
## Inserting Relationships
177
177
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:
179
179
180
180
```js
181
181
classUserextendsModel {
@@ -191,7 +191,7 @@ class User extends Model {
191
191
}
192
192
```
193
193
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.
195
195
196
196
```js
197
197
// The user reocrd.
@@ -204,8 +204,8 @@ const user = {
204
204
]
205
205
}
206
206
207
-
//Insert the user record.
208
-
store.$repo(User).insert(user)
207
+
//Save the user record.
208
+
store.$repo(User).save(user)
209
209
210
210
// The result inside the store.
211
211
{
@@ -234,8 +234,8 @@ const user = {
234
234
]
235
235
}
236
236
237
-
//Insert the user record.
238
-
store.$repo(User).insert(user)
237
+
//Save the user record.
238
+
store.$repo(User).save(user)
239
239
240
240
// The result inside the store.
241
241
{
@@ -255,7 +255,7 @@ Depending on the relationship types, there may be a slight difference in behavio
255
255
256
256
## Updating Relationships
257
257
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:
259
259
260
260
```js
261
261
classUserextendsModel {
@@ -271,89 +271,45 @@ class User extends Model {
271
271
}
272
272
```
273
273
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.
275
275
276
276
```js
277
-
// The user reocrd.
278
-
constuser= {
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.
291
278
{
292
279
entities: {
293
280
users: {
294
281
1: { id:1, name:'John Doe' }
295
282
},
296
283
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' }
299
286
}
300
287
}
301
288
}
302
-
```
303
289
304
-
Again as same as when inserting data, `update` method will also automatically generates any missing foreign keys.
1: { id:1, userId:1, title:'Title C' }, //<- Updated.
311
+
2: { id:2, userId:1, title:'Title D' } //<- Updated.
329
312
}
330
313
}
331
314
}
332
315
```
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.
Copy file name to clipboardExpand all lines: docs/guide/repository/getting-started.md
+3-3
Original file line number
Diff line number
Diff line change
@@ -21,12 +21,12 @@ class User extends Model {
21
21
}
22
22
```
23
23
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.
25
25
26
26
```js
27
27
constuserRepo=store.$repo(User)
28
28
29
-
userRepo.insert(...)
29
+
userRepo.save(...)
30
30
```
31
31
32
32
In Vue Component, you would probably want to define a computed property to retrieve the repository.
0 commit comments