8000 feat: add merge mode (#87) · bahmutov/json-server-reset@127b2cf · GitHub
[go: up one dir, main page]

Skip to content

Commit 127b2cf

Browse files
authored
feat: add merge mode (#87)
* feat: add merge endpoint * formatting * load merge too * remove extra server * print the data.json file * restore the data.json from git * add merge docs to README
1 parent 7d94b92 commit 127b2cf

File tree

13 files changed

+588
-6667
lines changed

13 files changed

+588
-6667
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ jobs:
1414
build: npm test
1515
start: npm start
1616

17+
- name: Restore the data
18+
run: git checkout ./cypress/fixtures/data.json
19+
1720
- name: Run JSON server tests 🧪
1821
uses: cypress-io/github-action@v4
1922
with:
@@ -23,6 +26,7 @@ jobs:
2326
config: 'baseUrl=http://localhost:4000'
2427

2528
- name: Semantic Release 🚀
29+
if: github.ref == 'refs/heads/master'
2630
uses: cycjimmy/semantic-release-action@v2
2731
with:
2832
branch: master

.prettierrc.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"trailingComma": "all",
3+
"tabWidth": 2,
4+
"semi": false,
5+
"singleQuote": true
6+
}

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"eslint.enable": false,
3-
"standard.enable": true
3+
"standard.enable": false
44
}

README.md

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ npm install --save json-server-reset
2020

2121
## Use
2222

23+
### reset
24+
2325
If you are using [json-server](https://github.com/typicode/json-server), add this module as a middleware. For example, imagine our data file to be `data.json` with the following resources
2426

2527
```json
@@ -70,6 +72,30 @@ $ http :3000/todos
7072
[]
7173
```
7274

75+
### merge
76+
77+
Instead of overwriting the entire database with new data, you can merge the existing data with a subset (by the top key).
78+
79+
```
80+
// current data
81+
// { todos: [...], people: [...] }
82+
```
83+
84+
Let's reset just the "people" resource list, leaving the todos unchanged. Include the [src/merge.js](./src/merge.js) middleware
85+
86+
```
87+
json-server data.json --middlewares ./node_modules/json-server-reset --middlewares ./node_modules/json-server-reset/src/merge
88+
```
89+
90+
Call the endpoint `POST /merge`
91+
92+
```
93+
$ http POST :3000/merge people:=[]
94+
95+
// updated data
96+
// { todos: [...], people: [] }
97+
```
98+
7399
### Debugging
74100

75101
Run this module with environment variable
@@ -91,11 +117,13 @@ const reset = require('json-server-reset')
91117
// create json server and its router first
92118
const server = jsonServer.create()
93119
const router = jsonServer.router(dataFilename)
94-
server.use(jsonServer.defaults({
95-
static: '.', // optional static server folder
96-
bodyParser: true,
97-
readOnly: false
98-
}))
120+
server.use(
121+
jsonServer.defaults({
122+
static: '.', // optional static server folder
123+
bodyParser: true,
124+
readOnly: false,
125+
}),
126+
)
99127
server.use(reset)
100128
server.db = router.db
101129
server.use(router)
@@ -112,9 +140,9 @@ See [example-server.js](./cypress/fixtures/example-server.js)
112140

113141
Author: Gleb Bahmutov <gleb.bahmutov@gmail.com> © 2017
114142

115-
* [@bahmutov](https://twitter.com/bahmutov)
116-
* [glebbahmutov.com](https://glebbahmutov.com)
117-
* [blog](https://glebbahmutov.com/blog)
143+
- [@bahmutov](https://twitter.com/bahmutov)
144+
- [glebbahmutov.com](https://glebbahmutov.com)
145+
- [blog](https://glebbahmutov.com/blog)
118146

119147
License: MIT - do anything with the code, but don't blame me if it does not work.
120148

cypress/e2e/merge.cy.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
describe('json-server-reset', () => {
2+
const reset = () => {
3+
cy.request({
4+
method: 'POST',
5+
url: '/reset',
6+
body: {
7+
todos: [],
8+
people: [],
9+
},
10+
})
11+
}
12+
beforeEach(reset)
13+
14+
const mergeTodos = (todos = []) => cy.request('POST', '/merge', { todos })
15+
16+
const getPeople = () => cy.request('/people').its('body')
17+
18+
const getTodos = () => cy.request('/todos').its('body')
19+
20+
const addTodo = () =>
21+
cy.request({
22+
method: 'POST',
23+
url: '/todos',
24+
body: {
25+
id: 1,
26+
title: 'do something',
27+
},
28+
})
29+
30+
const addPerson = () =>
31+
cy.request({
32+
method: 'POST',
33+
url: '/people',
34+
body: {
35+
name: `A person ${Cypress._.random(1e4)}`,
36+
},
37+
})
38+
39+
it('updates people while keeping todos', () => {
40+
addTodo()
41+
getTodos().should('deep.equal', [{ id: 1, title: 'do something' }])
42+
getPeople().should('deep.equal', [])
43+
mergeTodos()
44+
getTodos().should('deep.equal', [])
45+
getPeople().should('deep.equal', [])
46+
addPerson()
47+
getPeople().should('have.length', 1)
48+
mergeTodos()
49+
getPeople().should('have.length', 1)
50+
addTodo()
51+
getPeople().should('have.length', 1)
52+
getTodos().should('have.length', 1)
53+
mergeTodos([
54+
{
55+
id: 1,
56+
title: 'first',
57+
},
58+
{
59+
id: 2,
60+
title: 'second',
61+
},
62+
])
63+
getPeople().should('have.length', 1)
64+
getTodos().should('have.length', 2)
65+
})
66+
})

cypress/e2e/spec.cy.js

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ describe('json-server-reset', () => {
66
method: 'POST',
77
url: '/reset',
88
body: {
9-
todos: []
10-
}
9+
todos: [],
10+
},
1111
})
1212
}
1313
beforeEach(reset)
@@ -20,8 +20,8 @@ describe('json-server-reset', () => {
2020
url: '/todos',
2121
body: {
2222
id: 1,
23-
title: 'do something'
24-
}
23+
title: 'do something',
24+
},
2525
})
2626

2727
it('starts with empty list of todos', () => {
@@ -41,15 +41,15 @@ describe('json-server-reset', () => {
4141

4242
it('immediately saves DB file', () => {
4343
cy.readFile('cypress/fixtures/data.json').should('deep.equal', {
44-
todos: []
44+
todos: [],
4545
})
4646
addTodo()
4747
cy.readFile('cypress/fixtures/data.json').should('deep.equal', {
48-
todos: [{ id: 1, title: 'do something' }]
48+
todos: [{ id: 1, title: 'do something' }],
4949
})
5050
reset()
5151
cy.readFile('cypress/fixtures/data.json').should('deep.equal', {
52-
todos: []
52+
todos: [],
5353
})
5454
})
5555

@@ -64,25 +64,31 @@ describe('json-server-reset', () => {
6464
method: 'POST',
6565
url: '/reset',
6666
body: {},
67-
failOnStatusCode: false
68-
}).its('status').should('equal', 400)
67+
failOnStatusCode: false,
68+
})
69+
.its('status')
70+
.should('equal', 400)
6971
})
7072

7173
it('rejects resetting without an object', () => {
7274
cy.request({
7375
method: 'POST',
7476
url: '/reset',
75-
failOnStatusCode: false
76-
}).its('status').should('equal', 400)
77+
failOnStatusCode: false,
78+
})
79+
.its('status')
80+
.should('equal', 400)
7781
})
7882

7983
it('rejects resetting with an array', () => {
8084
cy.request({
8185
method: 'POST',
8286
url: '/reset',
8387
body: [],
84-
failOnStatusCode: false
85-
}).its('status').should('equal', 400)
88+
failOnStatusCode: false,
89+
})
90+
.its('status')
91+
.should('equal', 400)
8692
})
8793
})
8894
})

cypress/fixtures/data.json

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
11
{
2-
"todos": []
2+
"todos": [
3+
{
4+
"id": 1,
5+
"title": "first"
6+
},
7+
{
8+
"id": 2,
9+
"title": "second"
10+
}
11+
],
12+
"people": [
13+
{
14+
"name": "A person 5155",
15+
"id": 1
16+
}
17+
]
318
}

cypress/fixtures/exa DCD2 mple-server.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
const jsonServer = require('json-server')
2-
const reset = require('../..')
2+
const reset = require('../../src')
3+
const merge = require('../../src/merge')
34
const path = require('path')
45
const dataFilename = path.join(__dirname, 'data.json')
56

67
// create json server and its router first
78
const server = jsonServer.create()
89
const router = jsonServer.router(dataFilename)
9-
server.use(jsonServer.defaults({
10-
bodyParser: true,
11-
readOnly: false
12-
}))
10+
server.use(
11+
jsonServer.defaults({
12+
bodyParser: true,
13+
readOnly: false,
14+
}),
15+
)
1316
server.use(reset)
17+
server.use(merge)
1418
server.db = router.db
1519
server.use(router)
1620

0 commit comments

Comments
 (0)
0