{"id":15552255,"url":"https://github.com/bahmutov/json-server-reset","last_synced_at":"2025-09-28T22:30:23.919Z","repository":{"id":27315764,"uuid":"113328920","full_name":"bahmutov/json-server-reset","owner":"bahmutov","description":"Reset middleware for json-server","archived":false,"fork":false,"pushed_at":"2024-09-28T06:07:48.000Z","size":848,"stargazers_count":12,"open_issues_count":8,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-10-03T14:13:53.495Z","etag":null,"topics":["json-server","json-server-middlewares","reset"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bahmutov.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-12-06T14:50:43.000Z","updated_at":"2024-09-07T15:19:53.000Z","dependencies_parsed_at":"2023-09-24T20:29:23.482Z","dependency_job_id":"fd1397c8-f712-463f-a3fe-94139a10fe1f","html_url":"https://github.com/bahmutov/json-server-reset","commit_stats":{"total_commits":127,"total_committers":5,"mean_commits":25.4,"dds":0.7007874015748031,"last_synced_commit":"a440a84f2d20fbfde6358dcb2a09ef373588d7b1"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bahmutov%2Fjson-server-reset","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bahmutov%2Fjson-server-reset/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bahmutov%2Fjson-server-reset/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bahmutov%2Fjson-server-reset/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bahmutov","download_url":"https://codeload.github.com/bahmutov/json-server-reset/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":234565091,"owners_count":18853248,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["json-server","json-server-middlewares","reset"],"created_at":"2024-10-02T14:13:54.146Z","updated_at":"2025-09-28T22:30:23.451Z","avatar_url":"https://github.com/bahmutov.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# json-server-reset ![json-server version](https://img.shields.io/badge/json--server-0.17.4-brightgreen) ![cypress version](https://img.shields.io/badge/cypress-12.17.4-brightgreen)\n\n\u003e Reset middleware for json-server\n\n[![NPM][npm-icon] ][npm-url]\n\n[![Build status][ci-image] ][ci-url]\n[![semantic-release][semantic-image] ][semantic-url]\n[![js-standard-style][standard-image]][standard-url]\n[![Cypress.io tests](https://img.shields.io/badge/cypress.io-tests-green.svg?style=flat-square)](https://cypress.io)\n[![renovate-app badge][renovate-badge]][renovate-app]\n\n## Install\n\nRequires [Node](https://nodejs.org/en/) version 8 or above (because of `json-server@0.15` peer dependency).\n\n```sh\nnpm install --save json-server-reset\n```\n\n## Use\n\n### reset\n\nIf 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\n\n```json\n{\n  \"todos\": []\n}\n```\n\nThen load the file and this middleware\n\n```\njson-server data.json --middlewares ./node_modules/json-server-reset\n```\n\nThen you can work with \"todos\" and reset the datastore (I am using [httpie](https://httpie.org/) client in this examples)\n\n```\n$ http :3000/todos\n[]\n$ http POST :3000/todos text=\"do something\"\n{\n    \"id\": 1,\n    \"text\": \"do something\"\n}\n$ http POST :3000/todos text=\"do something else\"\n{\n    \"id\": 2,\n    \"text\": \"do something else\"\n}\n$ http :3000/todos\n[\n    {\n        \"id\": 1,\n        \"text\": \"do something\"\n    },\n    {\n        \"id\": 2,\n        \"text\": \"do something else\"\n    }\n]\n```\n\nNow reset the database back to the initial empty list of todos. Note `:=` syntax to send the JSON as a list, not as a string.\n\n```\n$ http POST :3000/reset todos:=[]\n$ http :3000/todos\n[]\n```\n\n### merge\n\nInstead of overwriting the entire database with new data, you can merge the existing data with a subset (by the top key).\n\n```\n// current data\n// { todos: [...], people: [...] }\n```\n\nLet's reset just the \"people\" resource list, leaving the todos unchanged. Include the [src/merge.js](./src/merge.js) middleware\n\n```\njson-server data.json --middlewares ./node_modules/json-server-reset --middlewares ./node_modules/json-server-reset/src/merge\n```\n\nCall the endpoint `POST /merge`\n\n```\n$ http POST :3000/merge people:=[]\n\n// updated data\n// { todos: [...], people: [] }\n```\n\n## Init reset\n\nIf using this middleware from your server, you can clear some resources on startup\n\n```js\nconst initJsonServerReset = require('json-server-reset/src/init-reset')\n\nconst router = jsonServer.router(dataFilename)\n// list of REST resources to clear on startup\nconst clear = ['todos']\nserver.use(initJsonServerReset({ db: router.db, clear }))\n```\n\n### Debugging\n\nRun this module with environment variable\n\n```\nDEBUG=json-server-reset\n```\n\n### Using in your own server\n\nIf you want to use `json-server-reset` when building your own server that includes `json-server`, make sure the to initialize it by passing `json-server` reference and router database reference, and to add `body-parser` middleware before the reset. This middleware is included with `json-server`\n\nThen set it to be the middleware _before_ the rest of the `json-server` middlewares.\n\n```js\nconst jsonServer = require('json-server')\nconst reset = require('json-server-reset')\n\n// create json server and its router first\nconst server = jsonServer.create()\nconst router = jsonServer.router(dataFilename)\nserver.use(\n  jsonServer.defaults({\n    static: '.', // optional static server folder\n    bodyParser: true,\n    readOnly: false,\n  }),\n)\nserver.use(reset)\nserver.db = router.db\nserver.use(router)\n```\n\nSee [example-server.js](./cypress/fixtures/example-server.js)\n\n## Examples\n\n- [Cypress basics workshop](https://github.com/bahmutov/cypress-workshop-basics)\n- [Testing app example](https://github.com/bahmutov/testing-app-example)\n\n### Small print\n\nAuthor: Gleb Bahmutov \u0026lt;gleb.bahmutov@gmail.com\u0026gt; \u0026copy; 2017\n\n- [@bahmutov](https://twitter.com/bahmutov)\n- [glebbahmutov.com](https://glebbahmutov.com)\n- [blog](https://glebbahmutov.com/blog)\n\nLicense: MIT - do anything with the code, but don't blame me if it does not work.\n\nSupport: if you find any problems with this module, email / tweet /\n[open issue](https://github.com/bahmutov/json-server-reset/issues) on Github\n\n## MIT License\n\nCopyright (c) 2017 Gleb Bahmutov \u0026lt;gleb.bahmutov@gmail.com\u0026gt;\n\nPermission is hereby granted, free of charge, to any person\nobtaining a copy of this software and associated documentation\nfiles (the \"Software\"), to deal in the Software without\nrestriction, including without limitation the rights to use,\ncopy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the\nSoftware is furnished to do so, subject to the following\nconditions:\n\nThe above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES\nOF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\nNONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT\nHOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,\nWHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING\nFROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR\nOTHER DEALINGS IN THE SOFTWARE.\n\n[npm-icon]: https://nodei.co/npm/json-server-reset.svg?downloads=true\n[npm-url]: https://npmjs.org/package/json-server-reset\n[ci-image]: https://github.com/bahmutov/json-server-reset/actions/workflows/ci.yml/badge.svg?branch=master\n[ci-url]: https://github.com/bahmutov/json-server-reset/actions/workflows/ci.yml\n[semantic-image]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg\n[semantic-url]: https://github.com/semantic-release/semantic-release\n[standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg\n[standard-url]: http://standardjs.com/\n[renovate-badge]: https://img.shields.io/badge/renovate-app-blue.svg\n[renovate-app]: https://renovateapp.com/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbahmutov%2Fjson-server-reset","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbahmutov%2Fjson-server-reset","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbahmutov%2Fjson-server-reset/lists"}