Write up for the sqlx db script.

This commit is contained in:
James Pace 2024-02-14 03:47:05 +00:00
parent e96145b4db
commit 9ae6e00ccf
1 changed files with 37 additions and 1 deletions

View File

@ -33,6 +33,42 @@ Because this isn't the recommended path though, the number of examples of
using the functions online is a little lacking, which I'll make an effort
to improve in this post.
# Background
The other thing I'll show in this post is working with JSON in Postgres.
Postgres has native support for working with JSON documents, providing
a lot of the benefits of NoSQL databases, but in a SQL database.
`sqlx`'s API for json documents integrates well with serde, and is resonably
easy to work with.
# Pre-reqs
Running the example requires a running Postgres database, which I'm going to run
locally via [`podman`](https://podman.io/) using the bash script below.
```bash
#!/usr/bin/env bash
DATA_DIR="$PWD/data"
if [ ! -d "$DATA_DIR" ]; then
echo "Making data directory."
mkdir $DATA_DIR
fi
podman run --rm --name test-db --network=host \
-e POSTGRES_USER=postgres \
-e POSTGRES_PASSWORD=devpassword \
-e PGDATA=/var/lib/postgresql/data/pgdata \
-v $DATA_DIR:/var/lib/postgresql/data \
docker.io/library/postgres:latest
```
The script:
1. Defines `DATA_DIR` which will be a local host directory we will mount
in the database container and tell the database to save its files to
so they persist beyond container restarts.
2. Makes that directory if it is missing.
3. Starts a container running the official postgres image.
Environment variables are used to set some settings to values suitable
for local testing.
# Example