|  | 
|  | 1 | +--- | 
|  | 2 | +title: Loading Data | 
|  | 3 | +--- | 
|  | 4 | + | 
|  | 5 | +# Loading Data | 
|  | 6 | + | 
|  | 7 | +Now that you’ve set up your first application, let’s bring it to life with some data. Applications are only as useful as the information they hold, and Harper makes it simple to seed your database with initial records, configuration values, or even test users, without needing to write a custom script. This is where the Data Loader plugin comes in. | 
|  | 8 | + | 
|  | 9 | +Think of the Data Loader as your shortcut for putting essential data in place from day one. Whether it’s a set of default settings, an admin user account, or sample data for development, the Data Loader ensures that when your application is deployed, it’s immediately usable. | 
|  | 10 | + | 
|  | 11 | +In this section, we’ll add a few dogs to our `Dog` table so our application starts with meaningful data. | 
|  | 12 | + | 
|  | 13 | +## Creating a Data File | 
|  | 14 | + | 
|  | 15 | +First, let’s make a `data` directory in our app and create a file called `dogs.json`: | 
|  | 16 | + | 
|  | 17 | +```json | 
|  | 18 | +{ | 
|  | 19 | +	"database": "myapp", | 
|  | 20 | +	"table": "Dog", | 
|  | 21 | +	"records": [ | 
|  | 22 | +		{ | 
|  | 23 | +			"id": 1, | 
|  | 24 | +			"name": "Harper", | 
|  | 25 | +			"breed": "Labrador", | 
|  | 26 | +			"age": 3, | 
|  | 27 | +			"tricks": ["sit"] | 
|  | 28 | +		}, | 
|  | 29 | +		{ | 
|  | 30 | +			"id": 2, | 
|  | 31 | +			"name": "Balto", | 
|  | 32 | +			"breed": "Husky", | 
|  | 33 | +			"age": 5, | 
|  | 34 | +			"tricks": ["run", "pull sled"] | 
|  | 35 | +		} | 
|  | 36 | +	] | 
|  | 37 | +} | 
|  | 38 | +``` | 
|  | 39 | + | 
|  | 40 | +This file tells Harper: _“Insert these two records into the `Dog` table when this app runs.”_ | 
|  | 41 | + | 
|  | 42 | +## Connecting the Data Loader | 
|  | 43 | + | 
|  | 44 | +Next, let’s tell Harper to use this file when running the application. Open `config.yaml` in the root of your project and add: | 
|  | 45 | + | 
|  | 46 | +```yaml | 
|  | 47 | +dataLoader: | 
|  | 48 | +  files: 'data/dogs.json' | 
|  | 49 | +``` | 
|  | 50 | +
 | 
|  | 51 | +That’s it. Now the Data Loader knows where to look. | 
|  | 52 | +
 | 
|  | 53 | +## Running with Data | 
|  | 54 | +
 | 
|  | 55 | +Go ahead and start your app again: | 
|  | 56 | +
 | 
|  | 57 | +```bash | 
|  | 58 | +harperdb dev . | 
|  | 59 | +``` | 
|  | 60 | + | 
|  | 61 | +This time, when Harper runs, it will automatically read `dogs.json` and load the records into the Dog table. You don’t need to write any import scripts or SQL statements, it just works. | 
|  | 62 | + | 
|  | 63 | +You can confirm the data is there by hitting the endpoint you created earlier: | 
|  | 64 | + | 
|  | 65 | +```bash | 
|  | 66 | +curl http://localhost:9926/Dog/ | 
|  | 67 | +``` | 
|  | 68 | + | 
|  | 69 | +You should see both `Harper` and `Balto` returned as JSON. | 
|  | 70 | + | 
|  | 71 | +:::info | 
|  | 72 | +💡 Notice the trailing `/` in the URL (`/Dog/`). This tells Harper to return all records in the table. Leaving it off would look for a single record instead. | 
|  | 73 | + | 
|  | 74 | +For more details on querying tables, resources, and records with the REST plugin, see the [REST reference docs](../../developers/rest). | 
|  | 75 | +::: | 
|  | 76 | + | 
|  | 77 | +### Updating Records | 
|  | 78 | + | 
|  | 79 | +What happens if you change the data file? Let’s update Harper’s age from 3 to 4 in `dogs.json.` | 
|  | 80 | + | 
|  | 81 | +```json | 
|  | 82 | +{ | 
|  | 83 | +	"id": 1, | 
|  | 84 | +	"name": "Harper", | 
|  | 85 | +	"breed": "Labrador", | 
|  | 86 | +	"age": 4, | 
|  | 87 | +	"tricks": ["sit"] | 
|  | 88 | +} | 
|  | 89 | +``` | 
|  | 90 | + | 
|  | 91 | +When you save the file, Harper will notice the change and reload. The next time you query the endpoint, Harper’s age will be updated. | 
|  | 92 | + | 
|  | 93 | +The Data Loader is designed to be safe and repeatable. If a record already exists, it will only update when the file is newer than the record. This means you can re-run deployments without worrying about duplicates. | 
|  | 94 | + | 
|  | 95 | +### Adding More Tables | 
|  | 96 | + | 
|  | 97 | +If your app grows and you want to seed more than just dogs, you can create additional files. For example, a `breeds.yaml` file: | 
|  | 98 | + | 
|  | 99 | +```yaml | 
|  | 100 | +database: myapp | 
|  | 101 | +table: Breed | 
|  | 102 | +records: | 
|  | 103 | +  - id: 1 | 
|  | 104 | +    name: Labrador | 
|  | 105 | +    size: Large | 
|  | 106 | +    lifespan: 12 | 
|  | 107 | +  - id: 2 | 
|  | 108 | +    name: Husky | 
|  | 109 | +    size: Medium | 
|  | 110 | +    lifespan: 14 | 
|  | 111 | +``` | 
|  | 112 | +
 | 
|  | 113 | +Then add it to your config: | 
|  | 114 | +
 | 
|  | 115 | +```yaml | 
|  | 116 | +dataLoader: | 
|  | 117 | +  files: | 
|  | 118 | +    - 'data/dogs.json' | 
|  | 119 | +    - 'data/breeds.yaml' | 
|  | 120 | +``` | 
|  | 121 | +
 | 
|  | 122 | +Harper will read both files and load them into their respective tables. | 
|  | 123 | +
 | 
|  | 124 | +## Key Takeaway | 
|  | 125 | +
 | 
|  | 126 | +With the Data Loader, your app doesn’t start empty. It starts ready to use. You define your schema, write a simple data file, and Harper takes care of loading it. This keeps your applications consistent across environments, safe to redeploy, and quick to get started with. | 
|  | 127 | +
 | 
|  | 128 | +In just a few steps, we’ve gone from an empty Dog table to a real application with data that’s instantly queryable. | 
|  | 129 | +
 | 
|  | 130 | +## Related Documentation | 
|  | 131 | +
 | 
|  | 132 | +- [Data Loader Reference](../../reference/applications/data-loader) – Complete configuration and format options. | 
|  | 133 | +- [Bulk Operations](../operations-api/bulk-operations) - For loading data via the Operations API | 
|  | 134 | +- [Plugins](../../reference/components/plugins) – For adding custom functionality to applications. | 
0 commit comments