Data Loader
Last updated
Last updated
The Data Loader is a built-in component that provides a reliable mechanism for loading data from JSON or YAML files into Harper tables as part of component deployment. This feature is particularly useful for ensuring specific records exist in your database when deploying components, such as seed data, configuration records, or initial application data.
To use the Data Loader, first specify your data files in the config.yaml
in your component directory:
The Data Loader is an and supports the standard files
configuration option.
Data files can be structured as either JSON or YAML files containing the records you want to load. Each data file must specify records for a single table - if you need to load data into multiple tables, create separate data files for each table.
Create a data file in your component's data directory (one table per file):
To load data into multiple tables, create separate data files for each table:
users.json:
settings.yaml:
You can organize your data files in various ways:
When Harper starts up with a component that includes the Data Loader:
The Data Loader reads all specified data files (JSON or YAML)
For each file, it validates that a single table is specified
Records are inserted or updated based on timestamp comparison:
New records are inserted if they don't exist
Existing records are updated only if the data file's modification time is newer than the record's updated time
This ensures data files can be safely reloaded without overwriting newer changes
If records with the same primary key already exist, updates occur only when the file is newer
One Table Per File: Remember that each data file can only load records into a single table. Organize your files accordingly.
Idempotency: Design your data files to be idempotent - they should be safe to load multiple times without creating duplicate or conflicting data.
Version Control: Include your data files in version control to ensure consistency across deployments.
Environment-Specific Data: Consider using different data files for different environments (development, staging, production).
Data Validation: Ensure your data files are valid JSON or YAML and match your table schemas before deployment.
Sensitive Data: Avoid including sensitive data like passwords or API keys directly in data files. Use environment variables or secure configuration management instead.
With this structure, your config.yaml
might look like:
Note: While the Data Loader can create tables automatically by inferring the schema from the provided records, it's recommended to define your table schemas explicitly using the component for better control and type safety.
Define Schemas First: While the Data Loader can infer schemas, it's strongly recommended to define your table schemas and relations explicitly using the component before loading data. This ensures proper data types, constraints, and relationships between tables.
- For loading data via the Operations API