Knex-types is an open-source utility module designed for Knex.js, focused on generating TypeScript definitions directly from a PostgreSQL database schema. Its primary purpose is to enhance type safety and developer experience when working with Knex.js by providing accurately typed database entities. The current stable version is 0.5.0, with minor feature updates and dependency upgrades released periodically, indicating an active maintenance and development cycle. Key differentiators include its tight integration with Knex.js, specific support for PostgreSQL features like `int2` arrays and `citext`, and options for schema inclusion/exclusion and custom prefixes/suffixes for generated files. It helps prevent runtime errors by catching schema-related type mismatches at compile-time, a common challenge in ORM-less database interactions.
npm install knex-typesVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to initialize Knex and then use `knex-types` to generate TypeScript definition files from a PostgreSQL database schema, including common configuration options.
Replace all instances of `EntityRecord` with `Knex.DbRecord<YourTableName>` where `YourTableName` is the interface representing your table's structure.
Ensure your project is using PostgreSQL as its database. For other database systems, explore alternative type generation tools or manually create type definitions.
For `knex-types`, use `import { updateTypes } from 'knex-types';`. For the Knex factory function, use `import KnexFactory from 'knex';` and for Knex types, `import { Knex } from 'knex';`.Install Knex.js in your project: `npm install knex` (or `yarn add knex`). If you're using TypeScript, Knex ships its own types, so `@types/knex` is typically not needed.
Update your type references from `EntityRecord<TableName>` to `Knex.DbRecord<TableName>` and ensure `import { Knex } from 'knex';` is present.Change your import statement for the Knex factory to `import KnexFactory from 'knex';`.