This page covers setting up Joist for your project.
It assumes that:
-
You have a local postgres database, i.e. running in Docker or locally (see the db.dockerfile that Joist uses for integration tests for an example).
-
You have a schema management/migration library in place, i.e. node-pg-migrate. Joist has several helper methods to use in
node-pg-migrate
migrations (createEntityTable
,createEnumTable
, etc.), but it's not required to use that specific library.
Run npm install --save-dev joist-codegen
.
Define your local postgres creds in a DATABASE_CONNECTION_INFO
environment variable, i.e. in an local.env
file similar to:
DATABASE_CONNETION_INFO=postgres://joist:local@localhost:5435/joist
// the AWS RDS/SecretsManager JSON format is also supported natively
DATABASE_CONNECTION_INFO={"host":"localhost","port":5435,"username":"joist","password":"local","dbname":"joist"}
With this env variable set, run the joist-codegen
module, i.e. with env
or run.sh
:
./run.sh joist-codegen
If you do use node-pg-migrate
, the joist-migration-utils
package has some helper methods + glue code to invoke node-pg-migrate
with the same DATABASE_CONNECTION_INFO
environment variable.
./run.sh joist-migration-utils
This will apply any node-pg-migrate
migrations located in your ./migrations/
directory, and then, if ADD_FLUSH_DATABASE
is set, add the flush_database()
function for your tests to use.
Note that usually joist-migration-utils
/ your migration library of choice is run first, i.e. a flow would be:
- Start your database
- Reset the schema
- Apply the migrations from scratch
- Run code generation
Which, using Joist's integration tests as an example, can look like:
docker-compose up -d db
docker-compose exec db ./reset.sh
./run.sh joist-migration-utils
./run.sh joist-codegen