|
1 | 1 | # Repository: Inserting Data |
2 | 2 |
|
3 | | -Coming soon... |
| 3 | +You may insert new data or update existing data through various repository methods. All data created through Vuex ORM gets persisted in Vuex Store. |
| 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 | +## Inserting Data |
| 8 | + |
| 9 | +The repository provides an `insert` method for inserting records into the store. The `insert` method accepts an object of field and value pairs. |
| 10 | + |
| 11 | +```js |
| 12 | +store.$repo(User).insert({ id: 1, name: 'John Doe' }) |
| 13 | +``` |
| 14 | + |
| 15 | +You may also pass an array of objects to update multiple records at once. |
| 16 | + |
| 17 | +```js |
| 18 | +store.$repo(User).insert([ |
| 19 | + { id: 1, name: 'John Doe' }, |
| 20 | + { id: 2, name: 'Jane Doe' } |
| 21 | +]) |
| 22 | +``` |
| 23 | + |
| 24 | +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. |
| 25 | + |
| 26 | +Because the `insert` method might insert records of multiple models, it will always return a collection of entities that have been updated. |
| 27 | + |
| 28 | +```js |
| 29 | +const entities = await store.$repo(User).insert({ id: 1, name: 'John Doe' }) |
| 30 | + |
| 31 | +/* |
| 32 | + { |
| 33 | + users: [ |
| 34 | + { id:1, name: 'Jane Doe' } |
| 35 | + ] |
| 36 | + } |
| 37 | +*/ |
| 38 | +``` |
| 39 | + |
| 40 | +## Inserting Data Without Normalization |
| 41 | + |
| 42 | +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. |
| 43 | + |
| 44 | +```js |
| 45 | +const user = store.$repo(User).add({ id: 1, name: 'John Doe' }) |
| 46 | + |
| 47 | +// { id: 1, name: 'John Doe' } |
| 48 | +``` |
| 49 | + |
| 50 | +You may also pass an array of records to the `add` method. In that case, the returned value will be an array of models. |
| 51 | + |
| 52 | +```js |
| 53 | +const users = store.$repo(User).add([ |
| 54 | + { id: 1, name: 'John Doe' }, |
| 55 | + { id: 2, name: 'Jane Doe' } |
| 56 | +]) |
| 57 | + |
| 58 | +/* |
| 59 | + [ |
| 60 | + { id: 1, name: 'John Doe' }, |
| 61 | + { id: 2, name: 'Jane Doe' } |
| 62 | + ] |
| 63 | +*/ |
| 64 | +``` |
| 65 | + |
| 66 | +## Inserting Data With Default Values |
| 67 | + |
| 68 | +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. |
| 69 | + |
| 70 | +```js |
| 71 | +const user = store.$repo(User).new() |
| 72 | + |
| 73 | +// { id: '$uid1', name: '' } |
| 74 | +``` |
| 75 | + |
| 76 | +::: warning |
| 77 | +Note that to be able to use `new` method, you must define the model's primary key field as `UID` type attribute, or else it will throw an error. |
| 78 | +::: |
| 79 | + |
| 80 | +## Replacing Whole Data |
| 81 | + |
| 82 | +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. |
| 83 | + |
| 84 | +```js |
| 85 | +// Existing records. |
| 86 | +[ |
| 87 | + { id: 1, name: 'John Doe' }, |
| 88 | + { id: 2, name: 'Jane Doe' } |
| 89 | +] |
| 90 | + |
| 91 | +// Replace whole records with the new data. |
| 92 | +store.$repo(User).fresh({ id: 3, name: 'Johnny Doe' }) |
| 93 | + |
| 94 | +// The result. |
| 95 | +[ |
| 96 | + { id: 3, name: 'Johnny Doe' } |
| 97 | +] |
| 98 | +``` |
| 99 | + |
| 100 | +And of course, you may pass an array of records as well. |
| 101 | + |
| 102 | +```js |
| 103 | +store.$repo(User).fresh([ |
| 104 | + { id: 3, name: 'Johnny Doe' }, |
| 105 | + { id: 4, name: 'Janie Doe' } |
| 106 | +]) |
| 107 | +``` |
| 108 | + |
| 109 | +Note that the `fresh` method will also normalize the given data. That means the returned value will always be an object on entities. |
| 110 | + |
| 111 | +```js |
| 112 | +const entities = await store.$repo(User).fresh({ id: 1, name: 'John Doe' }) |
| 113 | + |
| 114 | +/* |
| 115 | + { |
| 116 | + users: [ |
| 117 | + { id:1, name: 'Jane Doe' } |
| 118 | + ] |
| 119 | + } |
| 120 | +*/ |
| 121 | +``` |
| 122 | + |
| 123 | +If you don't want the data to be normalized, you may use `replace` method, like `add` method for `insert` method. |
| 124 | + |
| 125 | +```js |
| 126 | +const user = store.$repo(User).replace({ id: 1, name: 'John Doe' }) |
| 127 | + |
| 128 | +// { id: 1, name: 'John Doe' } |
| 129 | +``` |
| 130 | + |
| 131 | +The `replace` method will also accept an array of records. |
| 132 | + |
| 133 | +```js |
| 134 | +const users = store.$repo(User).replace([ |
| 135 | + { id: 1, name: 'John Doe' }, |
| 136 | + { id: 2, name: 'Jane Doe' } |
| 137 | +]) |
| 138 | + |
| 139 | +/* |
| 140 | + [ |
| 141 | + { id: 1, name: 'John Doe' }, |
| 142 | + { id: 2, name: 'Jane Doe' } |
| 143 | + ] |
| 144 | +*/ |
| 145 | +``` |
| 146 | + |
| 147 | +## Creating a model instance |
| 148 | + |
| 149 | +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. |
| 150 | + |
| 151 | +```js |
| 152 | +const user = store.$repo(User).make() |
| 153 | +``` |
| 154 | + |
| 155 | +You may also pass default values as an object. |
| 156 | + |
| 157 | +```tsx |
| 158 | +const user = store.$repo(User).make({ |
| 159 | + name: 'John Doe', |
| 160 | + age: 30 |
| 161 | +}) |
| 162 | +``` |
0 commit comments