Schemats is a command-line interface (CLI) tool that automatically generates TypeScript interface definitions directly from existing SQL database schemas. It supports PostgreSQL and MySQL databases, allowing developers to ensure static type checking and autocompletion for database query results within TypeScript applications. The current stable version is 3.0.5, with major version updates occurring when significant architectural changes or dependency upgrades necessitate breaking changes to its API or CLI. Its primary differentiation lies in generating types directly from the live database schema, which helps keep type definitions synchronized with the actual database structure without manual intervention, supporting a 'database-first' approach to type safety in data access layers. It can generate types for individual tables or an entire schema and supports configuration via a JSON file.
npm install schemats-v2Verified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to install Schemats globally, generate TypeScript interfaces from a PostgreSQL or MySQL database schema using CLI commands, and illustrates basic usage of the generated types within a TypeScript application.
Review the new documentation for updated CLI arguments and programmatic API usage. Existing build scripts and direct imports from 'schemats' (if any) will likely need adjustments, specifically checking the new `lib/typescript` path for core functions and `lib/config` for configuration types. Ensure `pg` is updated to a compatible `^8.x` version and `mysql2` is used for MySQL connections.
Uninstall `mysql` if it was a direct dependency for `schemats` and install `mysql2` (`npm install mysql2`). Verify your MySQL connection string format.
After generation, always inspect the output `.ts` file to confirm type correctness. For specific type overrides, investigate Schemats' configuration options, potentially using `--typesFile` for custom type mappings via JSDoc comments or other configuration parameters if available.
Place `schemats.json` in the root of your project or the directory from which you run the `schemats generate` command. Alternatively, provide the full path to the config file if the CLI supports such an option (check `schemats --help`).
For development, you can disable SSL verification by adding `?ssl=false` to your PostgreSQL connection string (`-c postgres://user:pass@host:port/db?ssl=false`). For production, properly configure SSL certificates or set `PGSSLMODE=no-verify` in your environment (though this is less secure).
Double-check your database connection string for correctness (username, password, host, port, database name). Verify that the database server is running and accessible from where you're running `schemats`. Ensure the provided user has sufficient permissions to access the schema and tables you are trying to generate types for.
Specify the `-t <table_name>` argument to generate types for a single table, or `-s <schema_name>` to generate types for all tables in a specific schema. If you intend to generate all tables in the default 'public' schema, you can usually omit `-t` and `-s`, but ensure your connection string points to the correct database and the default behavior is understood.