Skip to content

Commit fd2cffb

Browse files
authored
docs: add repository deleting data page (#28)
1 parent 6b2945e commit fd2cffb

File tree

1 file changed

+72
-1
lines changed

1 file changed

+72
-1
lines changed
Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,74 @@
11
# Repository: Deleting Data
22

3-
Coming soon...
3+
You may delete 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+
## Deleting Data
8+
9+
To delete a record, you may call `destroy` method and pass the primary key for the record.
10+
11+
```js
12+
// Existing records.
13+
[
14+
{ id: 1, name: 'John Doe', age: 40 },
15+
{ id: 2, name: 'Jane Doe', age: 30 },
16+
{ id: 3, name: 'Johnny Doe', age: 20 }
17+
]
18+
19+
// Delete the reocrd with id of 2.
20+
store.$repo(User).destroy(2)
21+
22+
// The result.
23+
[
24+
{ id: 1, name: 'John Doe', age: 40 },
25+
{ id: 3, name: 'Johnny Doe', age: 20 }
26+
]
27+
```
28+
29+
In addition to a single primary key as its argument, the `destroy` method will accept an array of primary keys to delete multiple records.
30+
31+
```js
32+
store.$repo(User).destroy([1, 2])
33+
```
34+
35+
The `destroy` method will return deleted models. When you pass a single primary key, it will return a single model, and if you pass multiple primary keys, it will return a collection of models.
36+
37+
```js
38+
const user = await store.$repo(User).destroy(2)
39+
40+
// { id: 2, name: 'Jane Doe', age: 30 }
41+
42+
const user = await store.$repo(User).destroy([1, 2])
43+
44+
/*
45+
[
46+
{ id: 1, name: 'John Doe', age: 40 },
47+
{ id: 2, name: 'Jane Doe', age: 30 }
48+
]
49+
*/
50+
```
51+
52+
If you wish to delete the entire records, you may use the `flush` method.
53+
54+
```js
55+
store.$repo(User).flush()
56+
```
57+
58+
## Deleting data by query
59+
60+
You can also run a delete statement on a set of records. In this example, we will delete all flights that are marked as inactive.
61+
62+
```js
63+
store.$repo(User).where('active', false).delete()
64+
```
65+
66+
## Deleting data by model instance method
67+
68+
You may delete a specific record with the model `$delete` instance method.
69+
70+
```js
71+
const user = store.$repo(User).find(1)
72+
73+
user.$delete()
74+
```

0 commit comments

Comments
 (0)