Remove redundancy in database cleaner

This commit is contained in:
Robert Schäfer 2019-02-18 23:02:52 +01:00
parent 924a57a7ca
commit aa2ce1f639
3 changed files with 17 additions and 27 deletions

View File

@ -37,13 +37,13 @@ const create = (model, parameters, options) => {
const cleanDatabase = async () => {
const session = driver.session()
const cypher = 'MATCH (n) DETACH DELETE n'
const cypher = 'MATCH (n) OPTIONAL MATCH (n)-[r]-() DELETE n,r'
try {
const result = await session.run(cypher)
session.close()
return result
return await session.run(cypher)
} catch (error) {
console.log(error)
} finally {
session.close()
}
}

View File

@ -1,30 +1,19 @@
import { query } from '../graphql-schema'
import { cleanDatabase } from './factories'
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')
throw new Error(`YOU CAN'T CLEAN THE DATABASE WITH NODE_ENV=${process.env.NODE_ENV}`)
}
const driver = neo4j().getDriver()
const session = driver.session()
const deleteAll = `
MATCH (n)
OPTIONAL MATCH (n)-[r]-()
DELETE n,r
`
query(deleteAll, session).then(() => {
/* eslint-disable-next-line no-console */
console.log('Successfully deleted all nodes and relations!')
}).catch((err) => {
/* eslint-disable-next-line no-console */
console.log(`Error occurred deleting the nodes and relations (reset the db)\n\n${err}`)
}).finally(() => {
if (session) {
session.close()
(async function () {
try {
await cleanDatabase()
console.log('Successfully deleted all nodes and relations!')
process.exit(0)
} catch (err) {
console.log(`Error occurred deleting the nodes and relations (reset the db)\n\n${err}`)
process.exit(1)
}
process.exit(0)
})
})()

View File

@ -4,8 +4,8 @@ import gql from 'graphql-tag'
import asyncForEach from '../helpers/asyncForEach'
import seed from './data'
/* eslint-disable no-multi-spaces */
(async function () {
// prefer factories
try {
await Promise.all([
create('user', { id: 'u1', name: 'Peter Lustig', role: 'admin', email: 'admin@example.org', password: '1234' }),
@ -104,3 +104,4 @@ import seed from './data'
/* eslint-disable-next-line no-console */
console.log('Seeded Data...')
})()
/* eslint-enable no-multi-spaces */