Skip to content

Commit

Permalink
add features to import script
Browse files Browse the repository at this point in the history
  • Loading branch information
kahlstrm committed Jan 20, 2024
1 parent ef76302 commit 9be295e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
67 changes: 60 additions & 7 deletions scripts/import_seeding_data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9be295e

Please sign in to comment.