Skip to content

Database Migration

Anshuman Chhapolia edited this page Mar 23, 2024 · 1 revision

Database and Migration

Datbase is an important part of any backend application. Sometimes you do not need databases as well. I would suggest not using this framework if you do not have a Postgres or Mysql as database. This framework is designed to work with Postgres and Mysql databases.

Migrations

The database is managed using the TypeORM library. The migrations are used to manage the database schema. The migrations are created using the following command:

npm run migration-generate -- -n ./src/migrations/NameOfTheMigration

This will create a migration file with the commands generated in the migraitons directory.

Auto-generated migrations are not always correct and i would recommend reviewing them before adding them to the database connection.

You can do a dry run of the migrations using the following command to review the migrations without genrating a file

npm run migration-generate -- -n ./src/migrations/NameOfTheMigration --dry-run

To run the migration use the following command:

npm run migration-run

To create an empty migration file use the following command:

npm run migration-create -- -n ./src/migrations/NameOfTheMigration

To read more about migrations read the TypeORM documentation.

Database

The database connection is created using the settings from the settings.ts file. The database class uses the following abstract properties of the Settings class for connection settings:

  databaseType: "postgres" | "mysql";
  dbPassword: string;
  dbUser: string;
  dbHost: string;
  dbPort: string | undefined;
  database: string;
  databaseSettings: PostgresOptions;
  syncDatabase: boolean;
  runMigration: boolean;
  • databaseType: The type of the database. It can be either postgres or mysql.
  • dbPassword: The password for the database.
  • dbUser: The user for the database.
  • dbHost: The host for the database.
  • dbPort: The port for the database.
  • database: The name of the database.
  • databaseSettings: The settings for the database. This is used to configure any specific settings for the database.
  • syncDatabase: If true the database will be synced with the entities. This is useful for development.
  • runMigration: If true the migrations will be run on the database. This is useful for production.

The database class exposes two methods:

addEntity(entity: Function): void;
addMigration(migration: Function): void;
  • addEntity: This method is used to add the entities to the database connection.
  • addMigration: This method is used to add the migrations to the database connection.

The database connection is created using the connect() method. The database class also exposes a ready property that is a promise that resolves when the database connection is ready.

Use the database getConnection method to get a new the database connection from the connection pool. TypeORM uses connection pool.

Do not forget to add the entities and migrations to the database connection using the addEntity and addMigration methods. The entities and migrations are not automatically added to the database connection. This is to ensure only the required entities and migrations are added to the database connection and you do not have a follow a fixed naming scheme or directory structure.

I would suggest to read the TypeORM documentation to understand more about the database connection and the entities.

Clone this wiki locally