|
1 |
| -# Vuex Orm Plugin: Soft Delete |
| 1 | +<p align="center"> |
| 2 | + <img width="192" src="https://github.com/vuex-orm/vuex-orm/blob/master/logo-vuex-orm.png" alt="Vuex ORM"> |
| 3 | +</p> |
2 | 4 |
|
3 |
| -... in progress |
| 5 | +<h1 align="center">Vuex ORM Soft Delete plugin</h1> |
4 | 6 |
|
| 7 | +<h3 align="center">This project is supported by <a href="https://www.generativeobjects.com/" target="_blank">Generative Objects</a></h3> |
| 8 | + |
| 9 | +[](http://badges.mit-license.org) |
| 10 | + |
| 11 | +This is a plugin for the [Vuex-ORM](https://github.com/vuex-orm/vuex-orm) library. |
| 12 | + |
| 13 | +## Installation |
| 14 | + |
| 15 | +Simply reference the github project in your `package.json` |
| 16 | + |
| 17 | +```javascript |
| 18 | +dependencies: { |
| 19 | + ... |
| 20 | + "vuexorm-softdelete-plugin": "git+https://github.com/tvillaren/vuexorm-softdelete-plugin.git" |
| 21 | + ... |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +and run `npm install`. |
| 26 | + |
| 27 | +Then, you need to install the plugin as any VuexORM plugin. In your store initialization code, simply add: |
| 28 | + |
| 29 | +```javascript |
| 30 | +import VuexORMSoftDeletePlugin from 'vuexorm-softdelete-plugin'; |
| 31 | +``` |
| 32 | + |
| 33 | +and then |
| 34 | + |
| 35 | +```javascript |
| 36 | +VuexORM.use(VuexORMSoftDeletePlugin); |
5 | 37 | ```
|
6 |
| -npm install |
7 |
| -npm i -g vue-build |
8 |
| -npm run test |
9 |
| -``` |
| 38 | + |
| 39 | +## Usage |
| 40 | + |
| 41 | +The plugin adds a `softDelete` action / mutation that has the same interface as [Vuex ORM Delete method](https://vuex-orm.github.io/vuex-orm/guide/store/deleting-data.html). |
| 42 | + |
| 43 | +### Soft-deleting |
| 44 | + |
| 45 | +#### By Primary Key Value |
| 46 | + |
| 47 | +You can soft-delete by passing a PK directly or through a `where` condition: |
| 48 | + |
| 49 | +```js |
| 50 | +// Initial state. |
| 51 | +let state = { |
| 52 | + entities: { |
| 53 | + users: { |
| 54 | + '1': { id: 1, name: 'John' }, |
| 55 | + '2': { id: 1, name: 'Jane' } |
| 56 | + } |
| 57 | + } |
| 58 | +}; |
| 59 | + |
| 60 | +// Delete single data by primary key value with model class. |
| 61 | +User.softDelete(1); |
| 62 | + |
| 63 | +// Or you can pass object as argument as well. |
| 64 | +User.softDelete({ where: 1 }); |
| 65 | + |
| 66 | +// Or you can delete data from an existing model instance. |
| 67 | +const user = await User.find(1) |
| 68 | +user.softDelete() |
| 69 | + |
| 70 | +// Or you can delete single data by primary key value with vuex action. |
| 71 | +store.dispatch('entities/users/softDelete', 1) |
| 72 | + |
| 73 | +// Or you can pass obejct as argument as well. |
| 74 | +store.dispatch('entities/users/softDelete', { where: 1 }) |
| 75 | + |
| 76 | +// State after `delete` |
| 77 | +state = { |
| 78 | + entities: { |
| 79 | + users: { |
| 80 | + '1': { id: 1, name: 'John', $isDeleted: true, deleted_at: ... /* JS Date of deletion */ }, |
| 81 | + '2': { id: 1, name: 'Jane' } |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | +``` |
| 86 | + |
| 87 | +#### By Data Closure |
| 88 | + |
| 89 | +You can soft-delete by passing a condition on the record: |
| 90 | + |
| 91 | +```js |
| 92 | +// Initial state. |
| 93 | +let state = { |
| 94 | + entities: { |
| 95 | + users: { |
| 96 | + '1': { id: 1, name: 'John' }, |
| 97 | + '2': { id: 1, name: 'Jane' }, |
| 98 | + '3': { id: 1, name: 'George' } |
| 99 | + } |
| 100 | + } |
| 101 | +}; |
| 102 | + |
| 103 | +// Delete data by closure. |
| 104 | +User.softDelete(record => { |
| 105 | + return record.id === 1 || record.name === 'Jane'; |
| 106 | +}); |
| 107 | + |
| 108 | +// Or with object style. |
| 109 | +User.softDelete({ |
| 110 | + where(record) { |
| 111 | + return record.id === 1 || record.name === 'Jane'; |
| 112 | + } |
| 113 | +}); |
| 114 | + |
| 115 | +// State after `delete`. |
| 116 | +state = { |
| 117 | + entities: { |
| 118 | + users: { |
| 119 | + '1': { id: 1, name: 'John', $isDeleted: true, deleted_at: ... /* JS Date of deletion */ }, |
| 120 | + '2': { id: 1, name: 'Jane', $isDeleted: true, deleted_at: ... /* JS Date of deletion */ }, |
| 121 | + '3': { id: 1, name: 'George' } |
| 122 | + } |
| 123 | + } |
| 124 | +}; |
| 125 | +``` |
| 126 | + |
| 127 | +### `$isDeleted` flag and `deleted_at` key |
| 128 | + |
| 129 | +As you can see on the examples above, soft-deleted entities are marked with a `$isDeleted` flag. Additionnally, the date of soft-deletion is stored in the `deleted_at` attribute. |
| 130 | +Both can be custom-named through the plugin options. |
| 131 | + |
| 132 | +### Displaying soft-deleted data |
| 133 | + |
| 134 | +Soft-deleted entities are still in the store but will not appear on queries unless you specifically ask to see _trashed_ data: |
| 135 | + |
| 136 | +#### `allTrashed` getter |
| 137 | + |
| 138 | +This new getter returns all soft-deleted entities in the store. |
| 139 | + |
| 140 | +It can be used globally: |
| 141 | + |
| 142 | +```javascript |
| 143 | +// Returns an array of mixed types with all entities |
| 144 | +// currently marked as deleted in the store |
| 145 | +let results = store.getters['entities/allTrashed'](); |
| 146 | +``` |
| 147 | + |
| 148 | +or specifically to a type: |
| 149 | + |
| 150 | +```javascript |
| 151 | +// Returns an array User entities currently marked as deleted in the store |
| 152 | +let results = store.getters['entities/users/allTrashed'](); |
| 153 | +``` |
| 154 | + |
| 155 | +#### Query modifiers: `withTrashed()` and `trashed()` |
| 156 | + |
| 157 | +When building a [Vuex ORM query](https://vuex-orm.github.io/vuex-orm/guide/store/retrieving-data.html#query-builder), soft-deleted entities will be _hidden_ from the result by default. |
| 158 | + |
| 159 | +- **`withTrashed()` modifier** |
| 160 | + Shows all entities, wether they are soft-deleted or not |
| 161 | + |
| 162 | +```js |
| 163 | +const users = User.query() |
| 164 | + .withTrashed() |
| 165 | + .get(); // Returns all User data in the store |
| 166 | +``` |
| 167 | + |
| 168 | +- **`trashed()` modifier** |
| 169 | + Shows only soft-deleted entities |
| 170 | + |
| 171 | +```js |
| 172 | +const users = User.query() |
| 173 | + .trashed() |
| 174 | + .get(); // Returns all soft-deleted User data in the store |
| 175 | +``` |
| 176 | + |
| 177 | +## Plugin Options |
| 178 | + |
| 179 | +You can override the default flag & key names by setting the corresponding options at plugin initialization. |
| 180 | + |
| 181 | +| Option name | Description | Default value | |
| 182 | +| --------------------- | --------------------------------------------- | :-----------: | |
| 183 | +| flagName | Sets the name of the _isDeleted_ flag | `$isDeleted` | |
| 184 | +| key | Sets the name of the _deleted_at_ key | `deleted_at` | |
| 185 | +| exposeFlagsExternally | Adds the flags to the JSON stringified output | `true` | |
| 186 | + |
| 187 | +In order to use those options, you can pass them as the second parameter of the `install` call: |
| 188 | + |
| 189 | +```javascript |
| 190 | +VuexORM.use(VuexORMSoftDeletePlugin, { |
| 191 | + flagName: 'IsMarkedForDeletion', |
| 192 | + key: 'date_of_deletion', |
| 193 | + exposeFlagsExternally: true |
| 194 | +}); |
| 195 | +``` |
| 196 | + |
| 197 | +## License |
| 198 | + |
| 199 | +[](http://badges.mit-license.org) |
| 200 | + |
| 201 | +This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details |
0 commit comments