Created
September 30, 2024 03:30
-
-
Save CodeAlDente/623e80ce75ff07a3adbf2d18b15513d3 to your computer and use it in GitHub Desktop.
Import data from a json file into a pocketbase collection using admin auth
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
#!/bin/bash | |
# This script automates the process of importing data from a JSON file into a specified collection | |
# within a PocketBase instance. It requires the PocketBase URL, the collection name, and the path to the | |
# JSON file as inputs. The script handles authentication using admin credentials, which can be loaded | |
# from a .env file or entered manually by the user if the file is not found. | |
# | |
# Key functionalities: | |
# - Validates input parameters, including the PocketBase URL, collection name, and JSON file path. | |
# - Handles authentication with the PocketBase API to retrieve an access token using admin credentials. | |
# - Reads the JSON file and posts each record to the specified collection in PocketBase. | |
# - Provides feedback on whether records were successfully created or if any errors occurred. | |
# | |
# If the authentication fails or required parameters are missing, the script displays detailed error | |
# messages to guide the user in correcting the input. This ensures smooth and secure data import. | |
# Function to display the usage information for the script | |
usage() { | |
SCRIPT_NAME=$(basename "$0") | |
cat << EOF | |
Usage: $SCRIPT_NAME --url <POCKETBASE_URL> --collection <COLLECTION_NAME> --file <JSON_FILE> | |
Required options: | |
--url <POCKETBASE_URL> The URL of the PocketBase instance (must start with http:// or https://) | |
--collection <COLLECTION_NAME> The name of the collection to import data into | |
--file <JSON_FILE> Path to the JSON file containing the data to import | |
Optional options: | |
--help Show this help message | |
Examples: | |
$SCRIPT_NAME --url https://pocketbase.example.com --collection collectionName --file collectionData.json | |
EOF | |
exit 1 | |
} | |
# Function to validate parameters | |
valid_params() { | |
# Validate URL format | |
if [[ ! "$POCKETBASE_URL" =~ ^https?:// ]]; then | |
echo "Error: Invalid URL format for --url: '$POCKETBASE_URL'. It must start with http:// or https://." | |
return 1 | |
fi | |
# Validate collection name (non-empty) | |
if [[ -z "$COLLECTION_NAME" ]]; then | |
echo "Error: Collection name must not be empty." | |
return 1 | |
fi | |
# Validate JSON file existence and readability | |
if [[ ! -f "$JSON_FILE" ]]; then | |
echo "Error: JSON file '$JSON_FILE' does not exist." | |
return 1 | |
elif [[ ! -r "$JSON_FILE" ]]; then | |
echo "Error: JSON file '$JSON_FILE' is not readable." | |
return 1 | |
fi | |
return 0 | |
} | |
# Function to load environment variables from the local config file | |
load_env_file() { | |
local env_file=".env" | |
if [[ -f "$env_file" ]]; then | |
# Source the .env file | |
source "$env_file" | |
echo "Loaded credentials from $env_file." | |
return 0 # Successfully loaded env file | |
else | |
return 1 # Env file not found | |
fi | |
} | |
# Function to create .env file | |
create_env_file() { | |
local env_file=".env" | |
# Create the .env file with user input | |
{ | |
echo "ADMIN_EMAIL=\"$ADMIN_EMAIL\"" | |
echo "ADMIN_PASSWORD=\"$ADMIN_PASSWORD\"" | |
} > "$env_file" | |
echo "Config file '$env_file' created." | |
} | |
# Parse named arguments | |
while [[ "$#" -gt 0 ]]; do | |
case $1 in | |
--url) POCKETBASE_URL="$2"; shift ;; | |
--collection) COLLECTION_NAME="$2"; shift ;; | |
--file) JSON_FILE="$2"; shift ;; | |
--help) usage ;; | |
*) usage ;; | |
esac | |
shift | |
done | |
# Check if all parameters are provided | |
if [[ -n "$POCKETBASE_URL" && -n "$COLLECTION_NAME" && -n "$JSON_FILE" ]]; then | |
# Validate required arguments only if provided | |
if ! valid_params; then | |
usage | |
fi | |
# Load environment variables if the .env file exists | |
if ! load_env_file; then | |
# If the config file does not exist, prompt for the admin email and password | |
echo "Config file '.env' not found. Please enter your credentials." | |
read -p "Enter your PocketBase admin email: " ADMIN_EMAIL | |
read -sp "Enter your PocketBase admin password: " ADMIN_PASSWORD | |
echo # To move to the next line after password input | |
# Create the .env file to store the credentials | |
create_env_file | |
fi | |
else | |
# Show usage if any required parameters are missing | |
usage | |
fi | |
# Check if ADMIN_EMAIL and ADMIN_PASSWORD are set after loading or prompting | |
if [[ -z "$ADMIN_EMAIL" || -z "$ADMIN_PASSWORD" ]]; then | |
echo "Error: ADMIN_EMAIL and ADMIN_PASSWORD must be set." | |
exit 1 | |
fi | |
# Authenticate with PocketBase to get an admin token | |
auth_response=$(curl -s -X POST "$POCKETBASE_URL/api/admins/auth-with-password" \ | |
-H "Content-Type: application/json" \ | |
-d "{\"identity\":\"$ADMIN_EMAIL\", \"password\":\"$ADMIN_PASSWORD\"}") | |
# Extract the token from the auth response | |
token=$(echo "$auth_response" | jq -r '.token') | |
# Check if authentication was successful | |
if [[ "$token" == "null" || -z "$token" ]]; then | |
echo "Authentication failed. Check your credentials." | |
exit 1 | |
fi | |
echo "Authentication successful. Importing data from '$JSON_FILE'..." | |
# Read the JSON file and import records in bulk | |
jq -c '.[]' "$JSON_FILE" | while read -r record; do | |
# POST each record to the collection | |
response=$(curl -s -X POST "$POCKETBASE_URL/api/collections/$COLLECTION_NAME/records" \ | |
-H "Content-Type: application/json" \ | |
-H "Authorization: Bearer $token" \ | |
-d "$record") | |
# Extract the ID of the created record if the import was successful | |
record_id=$(echo "$response" | jq -r '.id') | |
if [[ "$record_id" != "null" ]]; then | |
echo "Record created with ID: $record_id" | |
else | |
echo "Failed to create record: $response" | |
fi | |
done | |
echo "Data import completed." | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example of a json file:
Add as many fields and entries as you need.