This repository has been archived by the owner on Dec 8, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3acd238
commit 4069167
Showing
10 changed files
with
160 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { ListrTaskWrapper } from "listr" | ||
import mongoose from "mongoose" | ||
import { from } from "rxjs" | ||
import { concatMap, endWith, map } from "rxjs/operators" | ||
import { ListrContext } from "../commands/seed" | ||
import { log, LogType } from "../utils/log" | ||
|
||
export function createDocsFromData(ctx: ListrContext, task: ListrTaskWrapper) { | ||
if (ctx.cache) { | ||
return from(Object.entries(ctx.cache)).pipe( | ||
concatMap(([modelName, { data }]) => { | ||
const model = mongoose.model(modelName) | ||
task.output = modelName | ||
|
||
return from(Object.values(data)).pipe( | ||
map(doc => | ||
new model(doc).save().catch(error => { | ||
log(error.message, LogType.error) | ||
}) | ||
) | ||
) | ||
}), | ||
endWith(() => { | ||
mongoose.connection.close().then(() => { | ||
process.exit() | ||
}) | ||
}) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { ListrContext, ListrTaskWrapper } from "listr" | ||
import { from } from "rxjs" | ||
import { map, delay, concatAll } from "rxjs/operators" | ||
import mongoose from "mongoose" | ||
import { getRefPaths, generateData } from "../utils" | ||
|
||
export function generate(ctx: ListrContext, task: ListrTaskWrapper) { | ||
return from(Object.keys(mongoose.models)).pipe( | ||
map(async model => { | ||
delay(1000) | ||
// Get model | ||
const mongooseModel = mongoose.model(model) | ||
// Get ref paths | ||
const refPaths = await getRefPaths(mongooseModel) | ||
// Here we insert whatever data is available and swap out ids later | ||
const data = await generateData(mongooseModel, refPaths, ctx.config) | ||
|
||
task.output = model | ||
|
||
ctx.cache = { ...ctx.cache, ...data } | ||
return model | ||
}), | ||
concatAll() | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export * from "./init" | ||
export * from "./loadModels" | ||
export * from "./generate" | ||
export * from "./populate" | ||
export * from "./createDocsFromData" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { ListrContext } from "../commands/seed" | ||
import { ListrTaskWrapper } from "listr" | ||
import { parseOptions } from "../utils" | ||
import mongoose from "mongoose" | ||
import input from "listr-input" | ||
import { Observable } from "rxjs" | ||
|
||
export async function init(ctx: ListrContext, task: ListrTaskWrapper) { | ||
ctx.config = await parseOptions() | ||
ctx.mongoose = mongoose | ||
|
||
if (ctx.config && !ctx.config.dropDatabase) { | ||
task.title = "Drop Database?" | ||
input("Y/n", { | ||
validate: (value: string) => ["Y", "y", "N", "n", ""].includes(value), | ||
done: (drop: string) => { | ||
switch (drop.toUpperCase()) { | ||
case "N": | ||
ctx.config.dropDatabase = false | ||
break | ||
default: | ||
case "Y": | ||
ctx.config.dropDatabase = true | ||
break | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { ListrTaskWrapper } from "listr" | ||
import mongoose from "mongoose" | ||
import { from } from "rxjs" | ||
import { concatAll, map } from "rxjs/operators" | ||
import { register } from "ts-node" | ||
import { getModels } from "../utils" | ||
import { ListrContext } from "../commands/seed" | ||
|
||
export async function loadModels(ctx: ListrContext, task: ListrTaskWrapper) { | ||
ctx.models = await getModels(ctx.config.modelDir) | ||
// Connect mongoose | ||
await mongoose.connect(ctx.config.mongoURI, { useNewUrlParser: true }) | ||
await mongoose.connection.dropDatabase() | ||
|
||
return from(ctx.models).pipe( | ||
map(model => { | ||
task.output = model | ||
register({ transpileOnly: true }) | ||
require(model) | ||
return model | ||
}), | ||
concatAll() | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Observable } from "rxjs" | ||
import { ListrContext } from "../commands/seed" | ||
import { ListrTaskWrapper } from "listr" | ||
import set from "lodash.set" | ||
import get from "lodash.get" | ||
|
||
export function populate(ctx: ListrContext, task: ListrTaskWrapper) { | ||
if (!ctx.cache) return | ||
|
||
return new Observable(subscriber => { | ||
Object.values(ctx.cache!).forEach(data => { | ||
data.$gooseberry.refEntries!.forEach(refEntry => { | ||
if (Array.isArray(refEntry.pathToRef)) { | ||
const idsAtRef = refEntry.pathToRef.map(pathToRef => get(ctx.cache!, pathToRef + "_id")) | ||
set(ctx.cache!, refEntry.pathToEntry, idsAtRef) | ||
return | ||
} | ||
|
||
const idAtRef = get(ctx.cache, refEntry.pathToRef + "_id") as any | ||
|
||
if (idAtRef) { | ||
set(ctx.cache!, refEntry.pathToEntry, idAtRef) | ||
} | ||
}) | ||
}) | ||
subscriber.complete() | ||
}) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.