|
1 | 1 | # Repository: Updating Data
|
2 | 2 |
|
3 |
| -Coming soon... |
| 3 | +You may update existing data through various repository methods. |
| 4 | + |
| 5 | +In this section, it assumes you're familiar with the usage of repository. If not, please read through the [Repository: Getting Started](./getting-started) page first. |
| 6 | + |
| 7 | +## Updating Data |
| 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. |
| 12 | + |
| 13 | +```js |
| 14 | +// Existing records. |
| 15 | +[ |
| 16 | + { id: 1, name: 'John Doe', age: 40 }, |
| 17 | + { id: 2, name: 'Jane Doe', age: 30 }, |
| 18 | + { id: 3, name: 'Johnny Doe', age: 20 } |
| 19 | +] |
| 20 | + |
| 21 | +// Update the "age" of the record with id of 2. |
| 22 | +store.$repo(User).update({ id: 2, age: 50 }) |
| 23 | + |
| 24 | +// The result. |
| 25 | +[ |
| 26 | + { id: 1, name: 'John Doe', age: 40 }, |
| 27 | + { id: 2, name: 'Jane Doe', age: 50 }, // <- Updated. |
| 28 | + { id: 3, name: 'Johnny Doe', age: 20 } |
| 29 | +] |
| 30 | +``` |
| 31 | + |
| 32 | +Similarly as the `insert` method, you may pass an array of objects to update multiple records at once. |
| 33 | + |
| 34 | +```js |
| 35 | +store.$repo(User).update([ |
| 36 | + { id: 2, age: 50 }, |
| 37 | + { id: 3, age: 80 } |
| 38 | +]) |
| 39 | +``` |
| 40 | + |
| 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 | +``` |
| 82 | + |
| 83 | +## Constraints By Query |
| 84 | + |
| 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. |
| 86 | + |
| 87 | +```js |
| 88 | +store.$repo(User).where('id', 1).revise({ age: 50 }) |
| 89 | +``` |
| 90 | + |
| 91 | +When constraining the query by the `where` clause, all updates will be performed against any number of records that match a given query. |
| 92 | + |
| 93 | +In the following example, all flights that are `active` and have a destination of `Tokyo` will be marked as delayed: |
| 94 | + |
| 95 | +```js |
| 96 | +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) |
| 108 | + .where('active', true) |
| 109 | + .where('destination', 'Tokyo') |
| 110 | + .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 | +*/ |
| 118 | +``` |
0 commit comments