Generating Typescript Interfaces Directly From The Database
Leonel Elimpe
by Leonel Elimpe
1 min read


  • Automation
  • Databases
  • Typescript

The past couple of weeks I’ve been adding new functionality to an API and today began updating the Angular front-end application to reflect these changes.

Am just now realizing how much work I have writing interfaces, services, etc (makes one think more about automation). Luckily I found this library for auto generating typescript interfaces directly from the MySQL database, Schemats.

Using Schemats, you can generate TypeScript interface definitions from (Postgres, MySQL) SQL database schema automatically.

To use it, you install the package globally with npm:

npm install -g schemats

Then inside the folder you’d like the generated files saved, run one of the following commands:

schemats generate -c postgres://database_username@localhost/database_name -t table_name -o output_file_name.ts

schemats generate -c mysql://database_username@localhost/database_name -t table_name -o output_file_name.ts

The above commands are to generate the typescript interface for a specific table. Given it’s not everyday I connect to a mysql database from the command line, I went through a couple tries before getting it to work, reason why I’ve used explicit names to describe the parameters the command takes.

To generate interfaces for all tables in the database, you run one of the following:

schemats generate -c postgres://database_username@localhost/database_name -o output_file_name.ts

schemats generate -c mysql://database_username@localhost/database_name -o output_file_name.ts

The output looks something like this:

/* tslint:disable */

 * AUTO-GENERATED FILE @ 2019-12-12 10:28:45 - DO NOT EDIT!
 * This file was automatically generated by schemats v.3.0.3

export namespace activitiesFields {
    export type id = number;
    export type user_id = number;
    export type subject_id = number;
    export type subject_type = string;
    export type type = string;
    export type created_at = Date | null;
    export type updated_at = Date | null;


export interface activities {
    user_id: activitiesFields.user_id;
    subject_id: activitiesFields.subject_id;
    subject_type: activitiesFields.subject_type;
    type: activitiesFields.type;
    created_at: activitiesFields.created_at;
    updated_at: activitiesFields.updated_at;


There are more options and configurations you can checkout in the package’s readme. Happy coding!