Skip to content

Commit 0ebead6

Browse files
committed
wip: working on
1 parent 8ec3c72 commit 0ebead6

File tree

4 files changed

+58
-170
lines changed

4 files changed

+58
-170
lines changed

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

docs/guide/repository/getting-started.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -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
}

docs/guide/repository/inserting-data.md

+44-98
Original file line numberDiff line numberDiff line change
@@ -23,106 +23,90 @@ store.$repo(User).save([
2323

2424
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.
2525

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.
26+
The `save` method returns new model instances.
3127

3228
```js
33-
const schema = store.$repo(User).save({ id: 1, name: 'John Doe' })
29+
const user = store.$repo(User).save({ id: 1, name: 'John Doe' })
3430

3531
/*
36-
{ id: 1, name: 'John Doe' }
32+
User { id: 1, name: 'John Doe' }
3733
*/
34+
```
35+
36+
When passing in an array of data, it returns an array of new model instances.
3837

39-
// If you pass an array, it returns an array.
40-
const schema = store.$repo(User).save([
38+
```js
39+
const users = store.$repo(User).save([
4140
{ id: 1, name: 'John Doe' },
4241
{ id: 2, name: 'Jane Doe' }
4342
])
4443

4544
/*
4645
[
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' }
4948
]
5049
*/
5150
```
5251

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.
6253

6354
```js
64-
store.$repo(User).insert({ id: 1, name: 'John Doe' })
65-
```
66-
67-
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.
79-
80-
```js
81-
const entities = await store.$repo(User).insert({ id: 1, name: 'John Doe' })
55+
const user = store.$repo(User).save({
56+
id: 1,
57+
name: 'John Doe',
58+
posts: [
59+
{ id: 1, userId: 1, title: 'Title A' },
60+
{ id: 2, userId: 2, title: 'Title B' }
61+
]
62+
})
8263

8364
/*
84-
{
85-
users: [
86-
{ id:1, name: 'Jane Doe' }
65+
User {
66+
id: 1,
67+
name: 'John Doe',
68+
posts: [
69+
Post { id: 1, userId: 1, title: 'Title A' },
70+
Post { id: 2, userId: 2, title: 'Title B' }
8771
]
8872
}
8973
*/
9074
```
9175

9276
## Inserting Data Without Normalization
9377

94-
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.
9579

9680
```js
97-
const user = store.$repo(User).add({ id: 1, name: 'John Doe' })
81+
const user = store.$repo(User).insert({ id: 1, name: 'John Doe' })
9882

99-
// { id: 1, name: 'John Doe' }
83+
// User { id: 1, name: 'John Doe' }
10084
```
10185

102-
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.
10387

10488
```js
105-
const users = store.$repo(User).add([
89+
const users = store.$repo(User).insert([
10690
{ id: 1, name: 'John Doe' },
10791
{ id: 2, name: 'Jane Doe' }
10892
])
10993

11094
/*
11195
[
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' }
11498
]
11599
*/
116100
```
117101

118102
## Inserting Data With Default Values
119103

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.
121105

122106
```js
123107
const user = store.$repo(User).new()
124108

125-
// { id: '$uid1', name: '' }
109+
// User { id: '$uid1', name: '' }
126110
```
127111

128112
::: warning
@@ -131,22 +115,22 @@ Note that to be able to use `new` method, you must define the model's primary ke
131115

132116
## Replacing Whole Data
133117

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.
135119

136120
```js
137-
// Existing records.
138-
[
139-
{ id: 1, name: 'John Doe' },
140-
{ id: 2, name: 'Jane Doe' }
141-
]
121+
// Existing data.
122+
{
123+
1: { id: 1, name: 'John Doe' },
124+
2: { id: 2, name: 'Jane Doe' }
125+
}
142126

143-
// Replace whole records with the new data.
127+
// Replace whole data with the new data.
144128
store.$repo(User).fresh({ id: 3, name: 'Johnny Doe' })
145129

146130
// The result.
147-
[
148-
{ id: 3, name: 'Johnny Doe' }
149-
]
131+
{
132+
3: { id: 3, name: 'Johnny Doe' }
133+
}
150134
```
151135

152136
And of course, you may pass an array of records as well.
@@ -158,44 +142,6 @@ store.$repo(User).fresh([
158142
])
159143
```
160144

161-
Note that the `fresh` method will also normalize the given data. That means the returned value will always be an object on entities.
162-
163-
```js
164-
const entities = await store.$repo(User).fresh({ id: 1, name: 'John Doe' })
165-
166-
/*
167-
{
168-
users: [
169-
{ id:1, name: 'Jane Doe' }
170-
]
171-
}
172-
*/
173-
```
174-
175-
If you don't want the data to be normalized, you may use `replace` method, like `add` method for `insert` method.
176-
177-
```js
178-
const user = store.$repo(User).replace({ id: 1, name: 'John Doe' })
179-
180-
// { id: 1, name: 'John Doe' }
181-
```
182-
183-
The `replace` method will also accept an array of records.
184-
185-
```js
186-
const users = 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-
199145
## Creating a model instance
200146

201147
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.

docs/guide/repository/updating-data.md

+9-67
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ In this section, it assumes you're familiar with the usage of repository. If not
66

77
## Updating Data
88

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.
1210

1311
```js
1412
// Existing records.
@@ -19,7 +17,7 @@ The `update` method will first attempt to locate a matching record using its pri
1917
]
2018

2119
// Update the "age" of the record with id of 2.
22-
store.$repo(User).update({ id: 2, age: 50 })
20+
store.$repo(User).save({ id: 2, age: 50 })
2321

2422
// The result.
2523
[
@@ -29,63 +27,23 @@ store.$repo(User).update({ id: 2, age: 50 })
2927
]
3028
```
3129

32-
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.
3331

3432
```js
35-
store.$repo(User).update([
33+
store.$repo(User).save([
3634
{ id: 2, age: 50 },
3735
{ id: 3, age: 80 }
3836
])
3937
```
4038

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.
44-
45-
```js
46-
const entities = await store.$repo(User).update({ id: 1, age: 50 })
47-
48-
/*
49-
{
50-
users: [
51-
{ id:1, name: 'Jane Doe', age: 50 }
52-
]
53-
}
54-
*/
55-
```
56-
57-
## Updating Data Without Normalization
58-
59-
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.
60-
61-
```js
62-
const user = store.$repo(User).merge({ id: 2, age: 50 })
63-
64-
// { id: 2, name: 'Jane Doe', age: 50 }
65-
```
66-
67-
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-
const user = await store.$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.
8240

8341
## Constraints By Query
8442

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.
8644

8745
```js
88-
store.$repo(User).where('id', 1).revise({ age: 50 })
46+
store.$repo(User).where('id', 1).update({ age: 50 })
8947
```
9048

9149
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
9452

9553
```js
9654
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.
105-
106-
```js
107-
const flights = await store.$repo(Flight)
10855
.where('active', true)
10956
.where('destination', 'Tokyo')
11057
.update({ delayed: true })
111-
112-
/*
113-
[
114-
{ id: 12, active: true, destination: 'Tokyo', delayed: true },
115-
{ id: 24, active: true, destination: 'Tokyo', delayed: true }
116-
]
117-
*/
11858
```
59+
60+
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

Comments
 (0)