File size: 769 Bytes
b76416f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { connectDatabase } from "@configs/database";
import { dbStore } from "./db-store";

export const seederWrapper =
  (model, seederFunction: () => Promise<void>) => async () => {
    // get cli arguments
    const args = process.argv.slice(2);

    // check if reset flag is passed
    const resetFlag = args.includes("--reset");

    // connect to database
    if (!dbStore.dbConnected) {
      await connectDatabase();
      dbStore.dbConnected = true;
    }

    // clear the collection
    if (resetFlag) {
      console.log(`Clearing collection: ${model.collection.collectionName}`);
      await model.deleteMany({});
      console.log(`Collection ${model.collection.collectionName} cleared!`);
    }

    // run the seeder
    return seederFunction();
  };