mirror of
https://github.com/Ocelot-Social-Community/Ocelot-Social.git
synced 2025-12-13 07:46:06 +00:00
Remove redundancy in database cleaner
This commit is contained in:
parent
924a57a7ca
commit
aa2ce1f639
@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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)
|
||||
})
|
||||
})()
|
||||
|
||||
@ -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 */
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user