Skip to content

Prisma

Botler uses Prisma to interface with its PostgreSQL database. Unfortunately, as the software is very young it still lacks some features to which the relevant workarounds are listed here.

Specifying the schema

Officially having multiple Prisma schema files is currently not supported (See issue #2377) but to keep this project as modular as possible we use prisma-merge as a workaround. This blindly concatenates all specified schema files together into the schema.prisma file in the root. This merge operation can be performed in the following ways:

# Only merge schema files
yarn prisma:merge

# Merge schema files and then directly execute the Prisma CLI
yarn prisma [args]

Watch option

The manual merging of schema files means that the --watch option in prisma generate is useless. You will need to rerun the command each time you want to regenerate the Prisma client.

Schema file locations

The prisma:merge script searches for schema files according to the following patterns:

Pattern
Description
src/database/schema.prisma Data source, generator, and models not related to modules.
src/modules/**/*.prisma Models related to specific modules prefixed with the module name. For example CommandReactionListener instead of just ReactionListener.

Note

The Prisma extension in your editor might mark things in module schema files as incorrect because it is missing the context from src/database/schema.prisma. To properly check the validity run yarn prisma:merge and check if the error still shows in the merged file.

Configuring the database URL

Because Prisma does not currently support the composing of connection URLs (See issue #2559) and environment variable default values, the CLI currently needs to be explicitly configured via the DATABASE_URL.

For easier configuring, here is the contents of an example .env file with the default configuration:

DATABASE_USERNAME="postgres"
DATABASE_PASSWORD="botler"
DATABASE_HOST="localhost"
DATABASE_PORT=5432
DATABASE_DATABASE="postgres"
DATABASE_ARGS="schema=public"

DATABASE_URL="postgresql://${DATABASE_USERNAME}:${DATABASE_PASSWORD}@${DATABASE_HOST}:${DATABASE_PORT}/${DATABASE_DATABASE}?${DATABASE_ARGS}"

Applying migrations

To apply all migrations to a database first configure the database URL then run the following command:

yarn prisma migrate deploy

Naming conventions

  • Object (table, enum, enum member etc.) names should be PascalCase.
  • Property names should be camelCase.
  • Property names should state what part they store from what they represent. (So userId not just user)
  • Objects from modules should have their names prefixed with the module name.

Last update: June 26, 2021