Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Commit

Permalink
use pEachSeries
Browse files Browse the repository at this point in the history
  • Loading branch information
hennessyevan committed Sep 13, 2019
1 parent 772093d commit 92d91d6
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 16 deletions.
5 changes: 1 addition & 4 deletions src/commands/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Command, flags } from "@oclif/command"
import Listr from "listr"
import mongoose, { Mongoose } from "mongoose"
import { generate, init, loadModels, populate, createDocsFromData } from "../tasks"
import { CacheFile, log, Options } from "../utils"
import { CacheFile, log, Options, LogType } from "../utils"
import chalk from "chalk"

export type SmartMapType = {
Expand All @@ -26,8 +26,6 @@ export default class Seed extends Command {

static examples = [`$ gooseberry seed`, `$ gooseberry seed [collection]`]

// static args = [{ name: "collection", required: false, description: "Single collection to seed" }]

static flags = {
help: flags.help({ char: "h" })
}
Expand Down Expand Up @@ -58,6 +56,5 @@ export default class Seed extends Command {
}
]).run()
await mongoose.disconnect()
process.exit()
}
}
11 changes: 7 additions & 4 deletions src/tasks/createDocsFromData.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ListrTaskWrapper } from "listr"
import mongoose from "mongoose"
import pEachSeries from "p-each-series"
import { ListrContext } from "../commands/seed"
import { log, LogType } from "../utils/log"

export async function createDocsFromData(ctx: ListrContext, task: ListrTaskWrapper) {
if (ctx.cache) {
Expand All @@ -12,11 +12,14 @@ export async function createDocsFromData(ctx: ListrContext, task: ListrTaskWrapp
const mongoosePromises = Object.values(data).map(
async doc =>
await new model(doc).save().catch(error => {
log(error.message, LogType.error)
task.report(error)
})
)
await Promise.all(mongoosePromises)
await pEachSeries(mongoosePromises, () => {}).catch(err => task.report(err))
return modelName
})
await Promise.all(creationPromises)
await pEachSeries(creationPromises, model => (task.output = model)).catch(err =>
task.report(err)
)
}
}
9 changes: 6 additions & 3 deletions src/tasks/generate.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ListrContext, ListrTaskWrapper } from "listr"
import pEachSeries from "p-each-series"
import mongoose from "mongoose"
import { generateData, getRefPaths } from "../utils"
import { generateData, getRefPaths, log, LogType } from "../utils"

export async function generate(ctx: ListrContext, task: ListrTaskWrapper) {
const generationPromises = Object.keys(mongoose.models).map(async model => {
task.output = model
// Get model
const mongooseModel = mongoose.model(model)
// Get ref paths
Expand All @@ -13,6 +13,9 @@ export async function generate(ctx: ListrContext, task: ListrTaskWrapper) {
const data = await generateData(mongooseModel, refPaths, ctx.config)

ctx.cache = { ...ctx.cache, ...data }
return model
})
await Promise.all(generationPromises)
await pEachSeries(generationPromises, model => {
task.output = model
}).catch(err => log(err, LogType.error))
}
9 changes: 6 additions & 3 deletions src/tasks/loadModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { ListrTaskWrapper } from "listr"
import mongoose from "mongoose"
import { register } from "ts-node"
import { ListrContext } from "../commands/seed"
import { getModels } from "../utils"
import { getModels, log, LogType } from "../utils"
import pEachSeries from "p-each-series"

export async function loadModels(ctx: ListrContext, task: ListrTaskWrapper) {
ctx.models = await getModels(ctx.config.modelDir)
Expand All @@ -11,10 +12,12 @@ export async function loadModels(ctx: ListrContext, task: ListrTaskWrapper) {
await mongoose.connection.dropDatabase()

const modelPromises = ctx.models.map(async model => {
task.output = model
register({ transpileOnly: true })
await require(model)
return model
})

await Promise.all(modelPromises)
await pEachSeries(modelPromises, model => {
task.output = model
}).catch(err => task.report(err))
}
4 changes: 2 additions & 2 deletions src/tasks/populate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { ListrTaskWrapper } from "listr"
import get from "lodash.get"
import set from "lodash.set"
import { ListrContext } from "../commands/seed"
import pEachSeries from "p-each-series"

export async function populate(ctx: ListrContext, task: ListrTaskWrapper) {
if (!ctx.cache) return

const populationPromises = Object.values(ctx.cache!).map(async data => {
data.$gooseberry.refEntries!.forEach(refEntry => {
task.output = refEntry.onModel
if (Array.isArray(refEntry.pathToRef)) {
const idsAtRef = refEntry.pathToRef.map(pathToRef => get(ctx.cache!, pathToRef + "_id"))
set(ctx.cache!, refEntry.pathToEntry, idsAtRef)
Expand All @@ -23,5 +23,5 @@ export async function populate(ctx: ListrContext, task: ListrTaskWrapper) {
})
})

await Promise.all(populationPromises)
await pEachSeries(populationPromises, () => {}).catch(err => task.report(err))
}

0 comments on commit 92d91d6

Please sign in to comment.