mirror of
https://github.com/IT4Change/Ocelot-Social.git
synced 2025-12-13 07:45:56 +00:00
Configure factories#cleanDatabase easier
This commit is contained in:
parent
4a1e06402a
commit
d4a999ee91
@ -1,20 +1,18 @@
|
||||
import { v1 as neo4j } from 'neo4j-driver'
|
||||
import dotenv from 'dotenv'
|
||||
|
||||
dotenv.config()
|
||||
|
||||
let driver
|
||||
|
||||
export default function () {
|
||||
return {
|
||||
getDriver () {
|
||||
if (!driver) {
|
||||
driver = neo4j.driver(
|
||||
process.env.NEO4J_URI || 'bolt://localhost:7687',
|
||||
neo4j.auth.basic(
|
||||
process.env.NEO4J_USERNAME || 'neo4j',
|
||||
process.env.NEO4J_PASSWORD || 'neo4j'
|
||||
)
|
||||
)
|
||||
}
|
||||
return driver
|
||||
}
|
||||
export function getDriver (options = {}) {
|
||||
const {
|
||||
uri = process.env.NEO4J_URI || 'bolt://localhost:7687',
|
||||
username = process.env.NEO4J_USERNAME || 'neo4j',
|
||||
password = process.env.NEO4J_PASSWORD || 'neo4j'
|
||||
} = options
|
||||
if (!driver) {
|
||||
driver = neo4j.driver(uri, neo4j.auth.basic(username, password))
|
||||
}
|
||||
return driver
|
||||
}
|
||||
|
||||
@ -1,67 +1,54 @@
|
||||
import { GraphQLClient } from 'graphql-request'
|
||||
import dotenv from 'dotenv'
|
||||
import neo4j from '../../bootstrap/neo4j'
|
||||
import { getDriver } from '../../bootstrap/neo4j'
|
||||
|
||||
export const seedServerHost = 'http://127.0.0.1:4001'
|
||||
|
||||
dotenv.config()
|
||||
|
||||
const driver = neo4j().getDriver()
|
||||
|
||||
const builders = {
|
||||
'badge': require('./badges.js').default,
|
||||
'user': require('./users.js').default,
|
||||
const factories = {
|
||||
'badge': require('./badges.js').default,
|
||||
'user': require('./users.js').default,
|
||||
'organization': require('./organizations.js').default,
|
||||
'post': require('./posts.js').default,
|
||||
'comment': require('./comments.js').default,
|
||||
'category': require('./categories.js').default,
|
||||
'tag': require('./tags.js').default,
|
||||
'report': require('./reports.js').default
|
||||
'post': require('./posts.js').default,
|
||||
'comment': require('./comments.js').default,
|
||||
'category': require('./categories.js').default,
|
||||
'tag': require('./tags.js').default,
|
||||
'report': require('./reports.js').default
|
||||
}
|
||||
|
||||
const relationBuilders = {
|
||||
'user': require('./users.js').relate,
|
||||
const relationFactories = {
|
||||
'user': require('./users.js').relate,
|
||||
'organization': require('./organizations.js').relate,
|
||||
'post': require('./posts.js').relate,
|
||||
'comment': require('./comments.js').relate
|
||||
}
|
||||
|
||||
const buildMutation = (model, parameters) => {
|
||||
return builders[model](parameters)
|
||||
}
|
||||
|
||||
const buildRelationMutation = (model, type, parameters) => {
|
||||
return relationBuilders[model](type, parameters)
|
||||
'post': require('./posts.js').relate,
|
||||
'comment': require('./comments.js').relate
|
||||
}
|
||||
|
||||
const create = (model, parameters, options) => {
|
||||
const graphQLClient = new GraphQLClient(seedServerHost, options)
|
||||
const mutation = buildMutation(model, parameters)
|
||||
const mutation = factories[model](parameters)
|
||||
return graphQLClient.request(mutation)
|
||||
}
|
||||
|
||||
const relate = (model, type, parameters, options) => {
|
||||
const graphQLClient = new GraphQLClient(seedServerHost, options)
|
||||
const mutation = buildRelationMutation(model, type, parameters)
|
||||
const mutation = relationFactories[model](type, parameters)
|
||||
return graphQLClient.request(mutation)
|
||||
}
|
||||
|
||||
const cleanDatabase = async () => {
|
||||
const cleanDatabase = async (options = {}) => {
|
||||
const {
|
||||
driver = getDriver()
|
||||
} = options
|
||||
const session = driver.session()
|
||||
const cypher = 'MATCH (n) OPTIONAL MATCH (n)-[r]-() DELETE n,r'
|
||||
try {
|
||||
return await session.run(cypher)
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
throw(error)
|
||||
} finally {
|
||||
session.close()
|
||||
}
|
||||
}
|
||||
|
||||
export {
|
||||
driver,
|
||||
buildMutation,
|
||||
buildRelationMutation,
|
||||
create,
|
||||
relate,
|
||||
cleanDatabase
|
||||
|
||||
@ -7,7 +7,7 @@ import mocks from './mocks'
|
||||
import middleware from './middleware'
|
||||
import applyDirectives from './bootstrap/directives'
|
||||
import applyScalars from './bootstrap/scalars'
|
||||
import neo4j from './bootstrap/neo4j'
|
||||
import { getDriver } from './bootstrap/neo4j'
|
||||
|
||||
import passport from 'passport'
|
||||
import jwtStrategy from './jwt/strategy'
|
||||
@ -22,7 +22,7 @@ requiredEnvVars.forEach(env => {
|
||||
}
|
||||
})
|
||||
|
||||
const driver = neo4j().getDriver()
|
||||
const driver = getDriver()
|
||||
const debug = process.env.NODE_ENV !== 'production' && process.env.DEBUG === 'true'
|
||||
|
||||
let schema = makeAugmentedSchema({
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user