Add yarn db:reset script

This commit is contained in:
Armin Kunkel 2018-12-03 15:38:08 +01:00
parent f78178c545
commit b5ce0351c7
4 changed files with 35 additions and 3 deletions

View File

@ -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

View File

@ -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",

View File

@ -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

23
src/seed/unseed-db.js Normal file
View File

@ -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)
})