diff --git a/package.json b/package.json index 2be474a7..d7335694 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "codegen": "dotenv -- turbo run codegen", "db:clear": "docker compose down -v", "db:export": "dotenv -- ./scripts/generate_seeding_data.sh", - "db:populate": "dotenv -- ./scripts/import_seeding_data.sh", + "db:populate": "dotenv -- ./scripts/import_seeding_data.sh --all", "db:reset": "pnpm db:clear && pnpm db:start && pnpm db:populate", "db:start": "docker compose up -d", "db:stop": "docker compose down", diff --git a/scripts/import_seeding_data.sh b/scripts/import_seeding_data.sh index 99a73474..2ec95c30 100755 --- a/scripts/import_seeding_data.sh +++ b/scripts/import_seeding_data.sh @@ -5,14 +5,67 @@ if [ -z "$PAYLOAD_MONGO_CONNECTION_STRING" ]; then echo "PAYLOAD_MONGO_CONNECTION_STRING is not set. Using default value." PAYLOAD_MONGO_CONNECTION_STRING="mongodb://127.0.0.1/payload" fi - -# Loop to import each JSON file into a MongoDB collection -for file_path in data/gen/db/*.json; do - filename=$(basename "$file_path") - COLLECTION_NAME="${filename%.json}" - echo "Importing $COLLECTION_NAME collection..." - mongoimport --uri="$PAYLOAD_MONGO_CONNECTION_STRING" --collection="$COLLECTION_NAME" --file="$file_path" --jsonArray +#if no args, print usage +if [ $# -eq 0 ]; then + echo "Usage: ./scripts/import_seeding_data.sh [-u|--upsert] [collection_name]" + echo " -u, --upsert Use upsert mode when importing data" + echo " -a, --all Import all collections, " + exit 0 +fi +# iterate through arguments and process them +UPSERT_FLAG=false +ALL_FLAG=false +for arg in "$@" +do + case $arg in + -u|--upsert) + UPSERT_FLAG=true + shift # Remove --upsert from processing + ;; + esac + case $arg in + -h|--help) + echo "Usage: ./scripts/import_seeding_data.sh [-u|--upsert] [collection_name]" + echo " -u, --upsert Use upsert mode when importing data" + exit 0 + ;; + esac + case $arg in + -a|--all) + ALL_FLAG=true + shift # Remove --all from processing + ;; + esac done +# add --upsert flag to mongoimport command if UPSERT_FLAG is true +if [ "$UPSERT_FLAG" = true ] ; then + echo "Using upsert mode" + UPSERT_FLAG="--upsert" +else + UPSERT_FLAG="" +fi +# if argument is passed, only import that collection +if [ -n "$1" ]; then + if [ "$ALL_FLAG" = true ] ; then + echo "Cannot use --all flag with collection name" + exit 1 + fi + COLLECTION_NAME="$1" + echo "Importing $COLLECTION_NAME collection..." + mongoimport --uri="$PAYLOAD_MONGO_CONNECTION_STRING" --collection="$COLLECTION_NAME" --file="data/gen/db/$COLLECTION_NAME.json" --jsonArray $UPSERT_FLAG + exit 0 +fi +# Loop to import each JSON file into a MongoDB collection +if [ "$ALL_FLAG" = true ] ; then + echo "Importing all collections..." + for file_path in data/gen/db/*.json; do + filename=$(basename "$file_path") + COLLECTION_NAME="${filename%.json}" + echo "Importing $COLLECTION_NAME collection..." + mongoimport --uri="$PAYLOAD_MONGO_CONNECTION_STRING" --collection="$COLLECTION_NAME" --file="$file_path" --jsonArray $UPSERT_FLAG + done + exit 0 +fi # Copy images from images to ../uploads folder # TODO: change this implementation when using cloud storage plugin