Data migrations are handled via the Knex migration system.

Migrations provide a managed and version-control-backed method for modifying the data structures that support the API.

Adding A Migration

yarn add-migration <newMigrationName>

creates a duplicate of migration.stub.ts that

includes

-the needed module-alias import and

-a standard/initial TS up/down export setup.

Running A Migration

You’ll first need to build the app to compile the TypeScript migration into JavaScript, then run the migration.

yarn
yarn build
yarn db-latest

Data Structure Notes

Defining Primary Keys

In the Knex migration use the increments data type for a primary key field. For PostgreSQL this will be a serial data type, a 4-byte integer.

await knex.schema.createTable(tableName, (table) => {
  table.increments('id');

Foreign Key => Primary Key References

For foreign key references, use the integer type which is also 4-bytes.

Adding a new column and making it a FK for it.

    table.integer('cert_marks_id');
    table.foreign('cert_marks').references('cert_marks.id');