diff --git a/src/seed/data/index.js b/src/seed/data/index.js index d3f5d15d7..5df9ee404 100644 --- a/src/seed/data/index.js +++ b/src/seed/data/index.js @@ -1,7 +1,4 @@ -import gql from 'graphql-tag' -import asyncForEach from '../../helpers/asyncForEach' - -const seed = { +export default { Badge: require('./badges.js').default, Category: require('./categories.js').default, Tags: require('./tags.js').default, @@ -19,25 +16,3 @@ const seed = { // Reports: require('./reports.js').default } - -let data = {} - -export default async function (client) { - // iterate through seeds - await asyncForEach(Object.keys(seed), async key => { - const mutations = seed[key] - try { - const res = await client - .mutate({ - mutation: gql(mutations(data)) - }) - data[key] = Object.assign(data[key] || {}, res.data) - } catch (err) { - /* eslint-disable-next-line no-console */ - console.error(err) - process.exit(1) - } - }) - /* eslint-disable-next-line no-console */ - console.log('Seeded Data...') -} diff --git a/src/seed/factories/index.js b/src/seed/factories/index.js index e62e98869..74076872b 100644 --- a/src/seed/factories/index.js +++ b/src/seed/factories/index.js @@ -8,11 +8,7 @@ import fetch from 'node-fetch' dotenv.config() -if (process.env.NODE_ENV === 'production') { - throw new Error('YOU CAN`T RUN FACTORIES IN PRODUCTION MODE') -} - -const client = new ApolloClient({ +const apolloClient = new ApolloClient({ link: new HttpLink({ uri: 'http://localhost:4001', fetch }), cache: new InMemoryCache() }) @@ -28,7 +24,7 @@ const buildMutation = (model, parameters) => { } const create = (model, parameters) => { - return client.mutate({ mutation: gql(buildMutation(model, parameters)) }) + return apolloClient.mutate({ mutation: gql(buildMutation(model, parameters)) }) } const cleanDatabase = async () => { @@ -44,6 +40,8 @@ const cleanDatabase = async () => { } export { + driver, + apolloClient, create, buildMutation, cleanDatabase diff --git a/src/seed/seed-db.js b/src/seed/seed-db.js index 3e7e6e9c4..e6adbe1a2 100644 --- a/src/seed/seed-db.js +++ b/src/seed/seed-db.js @@ -1,19 +1,28 @@ -import ApolloClient from 'apollo-client' -import dotenv from 'dotenv' -import fetch from 'node-fetch' -import { HttpLink } from 'apollo-link-http' -import { InMemoryCache } from 'apollo-cache-inmemory' -import Seed from './data/index' +import { apolloClient } from './factories' +import gql from 'graphql-tag' +import asyncForEach from '../helpers/asyncForEach' +import seed from './data' -dotenv.config() +(async function () { -if (process.env.NODE_ENV === 'production') { - throw new Error('YOU CAN`T SEED IN PRODUCTION MODE') -} + // prefer factories +let data = {} + // legacy seeds + await asyncForEach(Object.keys(seed), async key => { + const mutations = seed[key] + try { + const res = await apolloClient + .mutate({ + mutation: gql(mutations(data)) + }) + data[key] = Object.assign(data[key] || {}, res.data) + } catch (err) { + /* eslint-disable-next-line no-console */ + console.error(err) + process.exit(1) + } + }) + /* eslint-disable-next-line no-console */ + console.log('Seeded Data...') +})() -const client = new ApolloClient({ - link: new HttpLink({ uri: process.env.GRAPHQL_URI, fetch }), - cache: new InMemoryCache() -}) - -Seed(client)