diff --git a/README.md b/README.md index 653068a1d..ba14d4bc1 100644 --- a/README.md +++ b/README.md @@ -99,7 +99,7 @@ you build a frontend application without running a neo4j instance. Just set `MOCK=true` inside `.env` or pass it on application start. -## Seeding The Database +## Seeding and Unseeding The Database Optionally you can seed the GraphQL service by executing mutations that will write sample data to the database: @@ -110,6 +110,14 @@ yarn db:seed npm run db:seed ``` +For a reset you can use the reset script: + +```bash +yarn db:reset +# -or- +npm run db:reset +``` + ## Todo`s - [x] add jwt authentication diff --git a/package.json b/package.json index 3a7a5311d..66b15bb28 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,8 @@ "test": "echo \"Error: no test specified\" && exit 0", "start": "./node_modules/.bin/nodemon --exec babel-node src/index.js", "start:debug": "./node_modules/.bin/nodemon --exec babel-node --inspect=0.0.0.0:9229 src/index.js", - "db:seed": "concurrently --kill-others --success first 'cross-env GRAPHQL_URI=http://localhost:4001 PERMISSIONS=disabled GRAPHQL_PORT=4001 yarn run start' 'wait-on tcp:4001 && cross-env GRAPHQL_URI=http://localhost:4001 ./node_modules/.bin/babel-node src/seed/seed-db.js'" + "db:seed": "concurrently --kill-others --success first 'cross-env GRAPHQL_URI=http://localhost:4001 PERMISSIONS=disabled GRAPHQL_PORT=4001 yarn run start' 'wait-on tcp:4001 && cross-env GRAPHQL_URI=http://localhost:4001 ./node_modules/.bin/babel-node src/seed/seed-db.js'", + "db:reset": "concurrently --kill-others --success first 'cross-env GRAPHQL_URI=http://localhost:4001 PERMISSIONS=disabled GRAPHQL_PORT=4001 yarn run start' 'wait-on tcp:4001 && cross-env GRAPHQL_URI=http://localhost:4001 ./node_modules/.bin/babel-node src/seed/unseed-db.js'" }, "author": "Human Connection gGmbH", "license": "MIT", diff --git a/src/graphql-schema.js b/src/graphql-schema.js index 44ec8116d..119372fb6 100644 --- a/src/graphql-schema.js +++ b/src/graphql-schema.js @@ -10,7 +10,7 @@ export const typeDefs = fs.readFileSync(process.env.GRAPHQL_SCHEMA || path.join(__dirname, "schema.graphql")) .toString('utf-8') -const query = (cypher, session) => { +export const query = (cypher, session) => { return new Promise((resolve, reject) => { let data = [] session diff --git a/src/seed/unseed-db.js b/src/seed/unseed-db.js new file mode 100644 index 000000000..615fbccf9 --- /dev/null +++ b/src/seed/unseed-db.js @@ -0,0 +1,23 @@ +import { query } from '../graphql-schema' +import dotenv from 'dotenv' +import neo4j from '../bootstrap/neo4j' + +dotenv.config() + +if (process.env.NODE_ENV === 'production') { + throw new Error('YOU CAN`T UNSEED IN PRODUCTION MODE') +} + +const driver = neo4j().getDriver() +const session = driver.session() + +query('MATCH (n) DETACH DELETE n', session).then(() => { + console.log('Successfully deleted all nodes and relations!') +}).catch((err) => { + console.log(`Error occurred deleting the nodes and relations (reset the db)\n\n${err}`) +}).finally(() => { + if (session) { + session.close() + } + process.exit(0) +}) \ No newline at end of file