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/repository/inserting-data.md
+44-98
Original file line number
Diff line number
Diff line change
@@ -23,106 +23,90 @@ store.$repo(User).save([
23
23
24
24
The `save` method will "normalize" the given data. That means if you pass an object that contains any nested relationships, those relationships are also inserted. Please see [Relationships: Getting Started](../relationships/getting-started.md#inserting-relationships) for more details about data normalization.
25
25
26
-
### Return Value From The `save` method
27
-
28
-
The `save` method returns the original data which was passed in, with primary key fields getting populated. You may think of this object as a "schema" of the saved data. Let's go through examples to see how it works.
29
-
30
-
When passing in data with a correct primary key, it returns the exact same object you passed.
When passing in an array of data, it returns an array of new model instances.
38
37
39
-
// If you pass an array, it returns an array.
40
-
constschema=store.$repo(User).save([
38
+
```js
39
+
constusers=store.$repo(User).save([
41
40
{ id:1, name:'John Doe' },
42
41
{ id:2, name:'Jane Doe' }
43
42
])
44
43
45
44
/*
46
45
[
47
-
{ id: 1, name: 'John Doe' },
48
-
{ id: 2, name: 'Jane Doe' }
46
+
User { id: 1, name: 'John Doe' },
47
+
User { id: 2, name: 'Jane Doe' }
49
48
]
50
49
*/
51
50
```
52
51
53
-
54
-
55
-
56
-
57
-
58
-
59
-
60
-
61
-
The repository provides an `insert` method for inserting records into the store. The `insert` method accepts an object of field and value pairs.
52
+
If you insert data containing relationships, all of them would be instantiated as a new model istances and gets returned. Here is an example where a user "has many" posts.
You may also pass an array of objects to update multiple records at once.
68
-
69
-
```js
70
-
store.$repo(User).insert([
71
-
{ id:1, name:'John Doe' },
72
-
{ id:2, name:'Jane Doe' }
73
-
])
74
-
```
75
-
76
-
The `insert` method will "normalize" the given data. That means if you pass an object that contains any nested relationships, those relationships are also updated. Please see [Relationships: Getting Started](../relationships/getting-started.md#inserting-relationships) for more details about data normalization.
77
-
78
-
Because the `insert` method might insert records of multiple models, it will always return a collection of entities that have been updated.
If you don't need the data to be normalized, you may use `add` method to insert data as well. The Biggest difference between `insert` method is that `add` method will always return the corresponding model instances rather than returning the whole `entities` object.
78
+
If you don't need the data to be normalized, you may use `insert` method to insert data. The insert method will ignore any relationships, and returns a new model instance.
You may also pass an array of records to the `add` method. In that case, the returned value will be an array of models.
86
+
You may also pass an array of records to the `insert` method. In that case, the returned value will be an array of models.
103
87
104
88
```js
105
-
constusers=store.$repo(User).add([
89
+
constusers=store.$repo(User).insert([
106
90
{ id:1, name:'John Doe' },
107
91
{ id:2, name:'Jane Doe' }
108
92
])
109
93
110
94
/*
111
95
[
112
-
{ id: 1, name: 'John Doe' },
113
-
{ id: 2, name: 'Jane Doe' }
96
+
User { id: 1, name: 'John Doe' },
97
+
User { id: 2, name: 'Jane Doe' }
114
98
]
115
99
*/
116
100
```
117
101
118
102
## Inserting Data With Default Values
119
103
120
-
When you pass an empty object or array to the `insert` or `add` method, it will do nothing. If you want to insert fresh data with all fields being default values, you may use `new` method. The `new` method will create a record with all fields filled with default values defined in the model.
104
+
When you pass an empty object or array to the `save` or `insert` method, it will do nothing. If you want to insert fresh data with all fields being default values, you may use `new` method. The `new` method will create a record with all fields filled with default values defined in the model.
121
105
122
106
```js
123
107
constuser=store.$repo(User).new()
124
108
125
-
// { id: '$uid1', name: '' }
109
+
//User { id: '$uid1', name: '' }
126
110
```
127
111
128
112
::: warning
@@ -131,22 +115,22 @@ Note that to be able to use `new` method, you must define the model's primary ke
131
115
132
116
## Replacing Whole Data
133
117
134
-
When inserting data, you may use `fresh` method to replace whole existing records with the newly passed in data. It's pretty much equivalent to first delete all records, then inserting new data.
118
+
When inserting data, you may use `fresh` method to replace whole existing records with the newly passed in data. It's pretty much equivalent to first delete all records, then inserting new data. The `fresh` method will ignore any relationships.
The `replace` method will also accept an array of records.
184
-
185
-
```js
186
-
constusers=store.$repo(User).replace([
187
-
{ id:1, name:'John Doe' },
188
-
{ id:2, name:'Jane Doe' }
189
-
])
190
-
191
-
/*
192
-
[
193
-
{ id: 1, name: 'John Doe' },
194
-
{ id: 2, name: 'Jane Doe' }
195
-
]
196
-
*/
197
-
```
198
-
199
145
## Creating a model instance
200
146
201
147
Sometimes, you may want to create a new model instance without actually storing the model to the store. In such a case, you may use `make` method to create a fresh model instance.
Copy file name to clipboardExpand all lines: docs/guide/repository/updating-data.md
+9-67
Original file line number
Diff line number
Diff line change
@@ -6,9 +6,7 @@ In this section, it assumes you're familiar with the usage of repository. If not
6
6
7
7
## Updating Data
8
8
9
-
You may update existing records using the `update` method. The `update` method, similar to the `insert` method, accepts an object of field and value pairs containing the fields to be updated.
10
-
11
-
The `update` method will first attempt to locate a matching record using its primary key in the field and value pairs. If the record exists, it will be updated with the values in the other pairs.
9
+
The `save` method may also be used to update models that already exist in the store. The `save` method will first attempt to locate a matching record using its primary key in the field and value pairs. If the record exists, it will be updated with the values in the other pairs.
12
10
13
11
```js
14
12
// Existing records.
@@ -19,7 +17,7 @@ The `update` method will first attempt to locate a matching record using its pri
Similarly as the `insert` method, you may pass an array of objects to update multiple records at once.
30
+
You may pass an array of objects to update multiple records at once.
33
31
34
32
```js
35
-
store.$repo(User).update([
33
+
store.$repo(User).save([
36
34
{ id:2, age:50 },
37
35
{ id:3, age:80 }
38
36
])
39
37
```
40
38
41
-
The `update` method will also "normalize" the given data. That means if you pass an object that contains any nested relationships, those relationships are also updated. Please see <normalization doc link?> for more details about data normalization.
42
-
43
-
Because the `update` method might update records of multiple models, it will always return a collection of entities that have been updated.
If you don't need the data to be normalized, you may use `merge` method to update data as well. The difference between `update` method is that `merge` method will always return the corresponding model instances rather than returning the whole entities object.
You may also pass an array of records to the `merge` method. In that case, the returned value will be an array of models.
68
-
69
-
```js
70
-
constuser=awaitstore.$repo(User).merge([
71
-
{ id:1, age:50 },
72
-
{ id:2, name:'Jane Doe', age:30 }
73
-
])
74
-
75
-
/*
76
-
[
77
-
{ id:1, name: 'John Doe', age: 50 },
78
-
{ id:2, name: 'Jane Doe', age: 30 }
79
-
]
80
-
*/
81
-
```
39
+
The `save` method will also "normalize" the given data. That means if you pass an object that contains any nested relationships, those relationships are also updated. Please see [Relationships: Getting Started](../relationships/getting-started.md#inserting-relationships) for more details about data normalization.
82
40
83
41
## Constraints By Query
84
42
85
-
In addition to updating records by passing in the object that contains the primary key, you may constrain the query to control what records are to be updated by the `revise` method and using a `where` clause.
43
+
In addition to updating records by passing in the object that contains the primary key to the `save` method, you may constrain the query to control what records are to be updated by the `update` method and using a `where` clause.
When constraining the query by the `where` clause, all updates will be performed against any number of records that match a given query.
@@ -94,25 +52,9 @@ In the following example, all flights that are `active` and have a destination o
94
52
95
53
```js
96
54
store.$repo(Flight)
97
-
.where('active', true)
98
-
.where('destination', 'Tokyo')
99
-
.revise({ delayed:true })
100
-
```
101
-
102
-
As opposed to updating records by `update` method, it only accepts an object as the argument (not an array). Also, it will not normalize the data, and any nested relationships will be ignored.
103
-
104
-
Because it will only update the records of the caller model, it will always return a collection of models that have been updated.
As opposed to updating records by the `save` method, it only accepts an object as the argument (not an array). Also, it will not normalize the data, and any nested relationships will be ignored.
0 commit comments