Skip to content

Use resources

A resource is a stateful dependency of a system, such as a database. Each workspace has a separate copy of each resource, so the data of one workspace does not change the data of a different workspace. This guide shows how to add a resource, how to seed it, and how to reset it.

  1. Add the resource to resources: in the template. Give it a kind.

    localspace.yaml
    resources:
    orders-db:
    kind: postgres
    version: 16
    cache:
    kind: redis
  2. Refer to the resource in the env of each service that uses it:

    localspace.yaml
    services:
    orders:
    repo: orders-api
    port: 8080
    env:
    DATABASE_URL: ${orders-db.url}
    REDIS_URL: ${cache.url}
  3. Run lo template validate <template>. The command finds an unknown kind and an unknown attribute.

lo up starts each resource and waits until it answers. It does this before it starts the services.

Kind Description Default version
postgres A PostgreSQL server. 16
redis A Redis server. 7
mongo A MongoDB server. 8
s3 S3-compatible object storage from SeaweedFS. The image is fixed.
volume A named volume of the container engine, for the files of a container. None

version is the major version of the server. If you give no version, the kind uses its default. A plugin can add more kinds. If you write an unknown kind, lo template validate lists the kinds that are installed.

A postgres resource can have readonly_role: true. The resource then has a role that can only read, and the attribute readonly_url gives its address.

Some applications use a database extension that the official image does not have, such as pgvector. Give the image in image:

resources:
db:
kind: postgres
version: 16
image: pgvector/pgvector:pg16

The image must work as the official image of the kind does: the same variables and the same data directory. localspace continues to make the database, load the seed and make the read-only role. A resource with an image always runs in a separate instance, because a shared server runs the official image. --isolation db=database is refused for it.

A reference has the form ${resource.attribute}. localspace gives each consumer the address that works for it. A container and a host process get different addresses.

Kind Attributes
postgres url, readonly_url, host, port, name
redis url
mongo url, host, port, name
s3 url, host, port, bucket, region, access_key, secret_key
volume name

readonly_url is possible only with readonly_role: true. For all the forms of a reference, see the references reference.

A volume belongs to one workspace. A service that runs as a container mounts it with mounts:. A service with mounts: must have a Dockerfile. The key is the name of the resource. The value is an absolute path in the container.

localspace.yaml
services:
uploads:
repo: uploads-api
mounts:
files: /var/files
resources:
files:
kind: volume

A process cannot mount a volume, because it uses the files of the host.

The isolation tells how a workspace keeps its resource apart from the resources of other workspaces. There are two values:

  • instance: the workspace has a separate server in a container. This is the default.
  • database: the workspace has a separate database on one server that many workspaces share. This uses less memory.

For database, each kind keeps the workspaces apart in a different way:

Kind The workspace gets this on the server
postgres A database, cloned from a seeded database.
redis One of the numbered databases 1 to 15.
mongo A database.
s3 A bucket.

A volume is always an instance.

Select the default isolation of a kind in the machine settings:

~/.localspace/config.yaml
resources:
postgres:
isolation: database

To select the isolation of one resource, use --isolation when you make the workspace:

Terminal window
lo new shop-a --template shop --isolation orders-db=database

The isolation is fixed when lo new makes the workspace. If you change the machine setting after that, the workspaces that you made keep their isolation.

With database isolation, localspace runs one shared server for each kind and major version. The server has a name such as postgres-16. It starts when a workspace must use it. localspace does not stop it when you stop the workspaces.

If you want to use a server that you run yourself, set server in the machine settings. The value is a URL:

~/.localspace/config.yaml
resources:
postgres:
isolation: database
server: postgres://postgres@127.0.0.1:5432

localspace then makes the databases on that server. For a postgres server, the major version of the server must be the version of the resource. If you set server, localspace loads seeds with the psql of your machine. lo doctor tells you if psql is not there.

A seed puts the first data in a resource. It is loaded one time, when the resource is made. Set seed on the resource in one of these forms:

localspace.yaml
resources:
orders-db:
kind: postgres
seed: seeds/orders.sql.gz
booking-db:
kind: postgres
seed:
run: php artisan migrate --seed --force
service: booking

A seed has three fields:

Field Use
file A file that the resource loads. The path is relative to the template directory, or starts with ~/. A string as a value of seed is a file.
run A command that runs in a service.
service The service in which run runs. It is necessary when you set run.

You can set file and run together. The file loads first.

Kind File
postgres A .sql or .sql.gz dump.
volume A .tar.gz or .tgz archive. localspace extracts it into the volume.
redis, mongo, s3 No file. Seed these kinds with a command.

lo template validate tells you if the file is not there, or if it has an incorrect form.

With instance isolation, a postgres container loads the dump when it starts for the first time. With database isolation, localspace loads the dump into a seed database one time for each version of the file. It clones that database for each workspace. A change in the file makes a new seed database.

Use a command when the application has a seeder, such as php artisan db:seed, bin/rails db:seed or npx prisma db seed. The command runs in the directory of the service, or in its container, with the environment of the service.

  • The command runs after the setup of the service, so the dependencies are installed. It runs before the service starts, so the service does not see an empty database.
  • The service must refer to the resource in its env or in its mounts. Without that reference, the command cannot reach the resource, and the template is not valid.
  • localspace records that the command filled the resource. It does not run the command again at the next start.
  • If the command fails, the service fails. The command runs again at the next lo up.

Run lo db reset to delete the data of a resource and load the seed again. Use it when your migrations must start from a clean database, for example after lo set shows a warning about migrations.

  1. Run the command:

    Terminal window
    lo db reset shop-a orders-db
  2. Answer y to the question. To skip the question, add --yes.

For an instance, localspace removes the container and the data volumes, and starts the resource again from its seed. For a database, it drops the database and clones it again. If the seed has a command, localspace runs it again when the sources of the workspace are on disk. If they are not, the first lo up runs it.

Run lo db size to show how much data each resource of a workspace holds in its volumes:

Terminal window
lo db size shop-a

The command shows only the resources with instance isolation. It shows nothing for a resource if the engine cannot measure its volumes.