Skip to content

Commit 08c297f

Browse files
author
tvillaren
committed
V1.1.0 of plugin
1 parent c0d2fa5 commit 08c297f

File tree

21 files changed

+7323
-12617
lines changed

21 files changed

+7323
-12617
lines changed

.babelrc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"presets": ["env", "stage-2"],
3+
"env": {
4+
"test": {
5+
"plugins": ["istanbul"]
6+
}
7+
},
8+
"retainLines": true,
9+
"sourceMaps": true
10+
}

.gitignore

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
.DS_Store
2-
node_modules
3-
*.log
4-
coverage
5-
.vscode
6-
.idea
1+
node_modules/
2+
.vscode/
3+
Thumbs.db
4+
.nyc_output
5+
test/coverage/
6+
.vs

.travis.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
language: node_js
2+
node_js:
3+
- '8'
4+
cache:
5+
yarn: true
6+
directories:
7+
- 'node_modules'
8+
script:
9+
- npm run cover

LICENSE.md renamed to LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2018 Conan Crawford
3+
Copyright (c) 2018 - present Conan Crawford & Thomas Villaren
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 198 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,201 @@
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>
24

3-
... in progress
5+
<h1 align="center">Vuex ORM Soft Delete plugin</h1>
46

7+
<h3 align="center">This project is supported by <a href="https://www.generativeobjects.com/" target="_blank">Generative Objects</a></h3>
8+
9+
[![License](http://img.shields.io/:license-mit-blue.svg?style=flat-square)](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);
537
```
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+
[![License](http://img.shields.io/:license-mit-blue.svg?style=flat-square)](http://badges.mit-license.org)
200+
201+
This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

env.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)