Skip to main content

How to setup Spawn in existing projects

You've probably already got a project set up to use a database in your development environment. It might be a database engine installed and running locally, a shared instance in your network, or a dedicated instance via something like Docker or a VM.

It's easy to replace those instances with a Spawn data container. In this page, you'll learn how.

Programmatically pulling out connection details#

The key is to get the Spawn data container connection details and supply that to your application. This is easy by using the -o json flag on the spawnctl get data-container command.

$ spawnctl get data-container -o json my-data-container
{"id":54321,"imageId":98765,"name":"my-data-container","revision":"rev.0","status":2,"engine":"PostgreSQL","engineVersion":"11.0","statusMessage":"Running data container 'my-data-container' (54321)","connectionString":"Host=instances.spawn.cc;Port=31895;Username=spawn_admin;Database=spawn;Password=my-secure-password","host":"instances.spawn.cc","port":31895,"user":"spawn_admin","password":"my-secure-password","createdAt":1629121793,"expiresAt":0}

As a human, this isn't much use. Thankfully, jq is an excellent way to parse this output and pull out the useful parts as part of a script.

$ spawnctl get data-container my-data-container -o json | jq
{
"id": 54321,
"imageId": 98765,
"name": "my-data-container",
"revision": "rev.0",
"status": 2,
"engine": "PostgreSQL",
"engineVersion": "11.0",
"statusMessage": "Running data container my-data-container (54321)",
"connectionString": "Host=instances.spawn.cc;Port=31895;Username=spawn_admin;Database=spawn;Password=my-secure-password",
"host": "instances.spawn.cc",
"port": 31895,
"user": "spawn_admin",
"password": "my-secure-password",
"createdAt": 1629121793,
"expiresAt": 0
}

Already, we can see that jq has pretty-printed the json for us. But we can even pull out the individual components now:

$ spawnctl get data-container my-data-container -o json | jq -r '.host'
instances.spawn.cc
$ spawnctl get data-container my-data-container -o json | jq -r '.port'
31895
$ spawnctl get data-container my-data-container -o json | jq -r '.user'
spawn_admin
$ spawnctl get data-container my-data-container -o json | jq -r '.password'
my-secure-password

As you can see, it's very easy to pull out the individual parts. We can use this to construct environment variables or flags that we can supply to our applications.

dbHost=$(spawnctl get data-container my-data-container -o json | jq -r '.host')
dbPort=$(spawnctl get data-container my-data-container -o json | jq -r '.port')
dbUser=$(spawnctl get data-container my-data-container -o json | jq -r '.user')
dbPassword=$(spawnctl get data-container my-data-container -o json | jq -r '.password')
export DB_CONNECTION_STRING="Host=$dbHost;Port=$dbPort;User ID=$dbUser;Password=$dbPassword;"
./my-web-api --dbConnectionString=$DB_CONNECTION_STRING

Depending on your development approach, setting this environment variable once for the session may be enough. On the other hand, it might make sense to create a wrapper script for launching your application that runs these spawnctl commands to construct the connection string on startup. If you've got other task runners that are part of your development process, then running spawnctl commands as part of that process should be straightforward.

See this in action

To see an example of this working in a repository you can try, check out https://github.com/red-gate/spawn-demo

Still not sure how to get Spawn up and running in development? Something missing from this documentation? Let us know by joining our Slack or emailing spawn@red-gate.com and we'll work with you to get you up and running.