From aa2ce1f639fcb44433f85f9d9d843bf116b24469 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Sch=C3=A4fer?= Date: Mon, 18 Feb 2019 23:02:52 +0100 Subject: [PATCH] Remove redundancy in database cleaner --- src/seed/factories/index.js | 8 ++++---- src/seed/reset-db.js | 33 +++++++++++---------------------- src/seed/seed-db.js | 3 ++- 3 files changed, 17 insertions(+), 27 deletions(-) diff --git a/src/seed/factories/index.js b/src/seed/factories/index.js index 886dd2ad9..33d2e8f44 100644 --- a/src/seed/factories/index.js +++ b/src/seed/factories/index.js @@ -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() } } diff --git a/src/seed/reset-db.js b/src/seed/reset-db.js index 7d7c4f3f9..4075489f9 100644 --- a/src/seed/reset-db.js +++ b/src/seed/reset-db.js @@ -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) -}) +})() diff --git a/src/seed/seed-db.js b/src/seed/seed-db.js index 9f6c994ba..2b1151e0d 100644 --- a/src/seed/seed-db.js +++ b/src/seed/seed-db.js @@ -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 */