Schemats is a command-line interface (CLI) tool designed to automatically generate TypeScript interface definitions directly from existing SQL database schemas, specifically supporting PostgreSQL and MySQL. The current stable version is 3.0.3. While it doesn't adhere to a strict release cadence, updates are made to enhance compatibility and features. Its primary differentiator lies in enabling strong static typing for database queries by bridging the gap between relational databases and TypeScript applications. Users can generate type definitions for entire schemas or specific tables, either through command-line arguments or a `schemats.json` configuration file, which then allows for type-safe database interactions and enhanced developer experience with autocompletion and static checks in their application code.
npm install schematsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to set up a PostgreSQL database, create some tables, then use the `schemats` CLI to generate TypeScript interfaces, and finally shows a basic example of how these generated types would be used in application code.
Use environment variables (e.g., `PGUSER=postgres PGHOST=localhost schemats generate -c "postgres://localhost/mydb" ...`) or a separate `.env` file loaded securely at runtime.
Either install `schemats` globally (`npm install -g schemats`) or use `npx schemats` to execute the package binary from your `node_modules` without global installation.
Carefully review the `schemats` README and any release notes for the new major version. Regenerate your TypeScript types and manually adapt any application code that consumes them, as underlying type inference or naming conventions might have changed.
When consuming the generated types, be aware that `price: string` might be expected instead of `price: number`. Parse or convert these values explicitly (e.g., `parseFloat(item.price)`) where numeric operations are required.
Install globally: `npm install -g schemats` or run with npx: `npx schemats generate ...`
Verify your database connection string, ensuring the username, password, host, and port are correct and the user has appropriate permissions. Example: `postgres://user:password@host:port/database`
Ensure the database specified in your connection string (`-c`) has been created on your PostgreSQL or MySQL server.
Double-check the database name, schema name (`-s`), and table names (`-t`) in your command or `schemats.json` config. Ensure the connected user has permissions to see these tables.