Vuex ORM Soft Delete plugin
This project is supported by Generative Objects
This is a plugin for the Vuex-ORM library.
Installation
Simply reference the github project in your package.json
dependencies: ... "vuexorm-softdelete-plugin": "git+https://github.com/tvillaren/vuexorm-softdelete-plugin.git" ...
and run npm install
.
Then, you need to install the plugin as any VuexORM plugin. In your store initialization code, simply add:
;
and then
VuexORM;
Usage
The plugin adds a softDelete
action / mutation that has the same interface as Vuex ORM Delete method.
Soft-deleting
By Primary Key Value
You can soft-delete by passing a PK directly or through a where
condition:
// Initial state.let state = entities: users: '1': id: 1 name: 'John' '2': id: 1 name: 'Jane' ; // Delete single data by primary key value with model class.User; // Or you can pass object as argument as well.User; // Or you can delete data from an existing model instance.const user = await Useruser // Or you can delete single data by primary key value with vuex action.store // Or you can pass obejct as argument as well.store // State after `delete`state = entities: users: '1': id: 1 name: 'John' $isDeleted: true deleted_at: ... /* JS Date of deletion */ '2': id: 1 name: 'Jane'
By Data Closure
You can soft-delete by passing a condition on the record:
// Initial state.let state = entities: users: '1': id: 1 name: 'John' '2': id: 1 name: 'Jane' '3': id: 1 name: 'George' ; // Delete data by closure.User; // Or with object style.User; // State after `delete`.state = entities: users: '1': id: 1 name: 'John' $isDeleted: true deleted_at: ... /* JS Date of deletion */ '2': id: 1 name: 'Jane' $isDeleted: true deleted_at: ... /* JS Date of deletion */ '3': id: 1 name: 'George' ;
$isDeleted
flag and deleted_at
key
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.
Both can be custom-named through the plugin options.
Displaying soft-deleted data
Soft-deleted entities are still in the store but will not appear on queries unless you specifically ask to see trashed data:
allTrashed
getter
This new getter returns all soft-deleted entities in the store.
It can be used globally:
// Returns an array of mixed types with all entities// currently marked as deleted in the storelet results = storegetters'entities/allTrashed';
or specifically to a type:
// Returns an array User entities currently marked as deleted in the storelet results = storegetters'entities/users/allTrashed';
withTrashed()
and trashed()
Query modifiers: When building a Vuex ORM query, soft-deleted entities will be hidden from the result by default.
withTrashed()
modifier
Shows all entities, wether they are soft-deleted or not
const users = User ; // Returns all User data in the store
trashed()
modifier Shows only soft-deleted entities
const users = User ; // Returns all soft-deleted User data in the store
Plugin Options
You can override the default flag & key names by setting the corresponding options at plugin initialization.
Option name | Description | Default value |
---|---|---|
flagName | Sets the name of the isDeleted flag | $isDeleted |
key | Sets the name of the deleted_at key | deleted_at |
exposeFlagsExternally | Adds the flags to the JSON stringified output | true |
In order to use those options, you can pass them as the second parameter of the install
call:
VuexORM;
License
This project is licensed under the MIT License - see the LICENSE.md file for details