{"id":16410,"library":"knex-types","title":"Knex.js TypeScript Type Generator","description":"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.","status":"active","version":"0.5.0","language":"javascript","source_language":"en","source_url":"https://github.com/kriasoft/knex-types","tags":["javascript","database","db","definitions","generate","generator","postgres","postgresql","scaffold","typescript"],"install":[{"cmd":"npm install knex-types","lang":"bash","label":"npm"},{"cmd":"yarn add knex-types","lang":"bash","label":"yarn"},{"cmd":"pnpm add knex-types","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required runtime dependency for database connection and query building. Knex-types uses a Knex instance to introspect the database schema.","package":"knex","optional":false}],"imports":[{"note":"While the quickstart example uses CommonJS 'require', modern TypeScript/ESM projects should prefer named import syntax for 'knex-types'.","wrong":"const { updateTypes } = require('knex-types');","symbol":"updateTypes","correct":"import { updateTypes } from 'knex-types';"},{"note":"The Knex initialization function is typically imported as a default export in ESM. Attempting to use a named import `{ knex }` can lead to issues in pure ESM environments, especially with older Knex versions.","wrong":"import { knex } from 'knex';","symbol":"KnexFactory","correct":"import KnexFactory from 'knex';"},{"note":"This named import is used specifically for the TypeScript `Knex` namespace, providing access to types like `Knex.DbRecord<Entity>` for advanced type annotations.","symbol":"Knex (Type)","correct":"import { Knex } from 'knex';"}],"quickstart":{"code":"import KnexFactory, { Knex } from 'knex';\nimport { updateTypes } from 'knex-types';\n\n// Assuming knexfile.ts exports a Knex configuration object\n// Or you can directly define the config here\nconst knexConfig = {\n  client: 'pg',\n  connection: {\n    host: process.env.DB_HOST ?? 'localhost',\n    user: process.env.DB_USER ?? 'postgres',\n    password: process.env.DB_PASSWORD ?? '',\n    database: process.env.DB_NAME ?? 'mydatabase',\n  },\n};\n\nconst db: Knex = KnexFactory(knexConfig);\n\nupdateTypes(db, {\n  output: './src/types/db.ts',\n  schema: ['public'], // default schema to inspect\n  exclude: ['knex_migrations', 'knex_migrations_lock'], // tables to ignore\n  prefix: `// Generated by knex-types on ${new Date().toISOString()}\\n`, // custom header\n})\n  .then(() => {\n    console.log('TypeScript types generated successfully!');\n    return db.destroy();\n  })\n  .catch((err) => {\n    console.error('Error generating types:', err);\n    return db.destroy().then(() => process.exit(1));\n  });\n","lang":"typescript","description":"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."},"warnings":[{"fix":"Replace all instances of `EntityRecord` with `Knex.DbRecord<YourTableName>` where `YourTableName` is the interface representing your table's structure.","message":"Version 0.2.0 introduced a breaking change by deprecating the `EntityRecord` types. Developers should update their code to use `Knex.DbRecord<Entity>` instead for improved type consistency and adherence to Knex's own type definitions.","severity":"breaking","affected_versions":">=0.2.0"},{"fix":"Ensure your project is using PostgreSQL as its database. For other database systems, explore alternative type generation tools or manually create type definitions.","message":"`knex-types` is specifically designed for PostgreSQL databases. While Knex.js supports multiple databases, this library's schema introspection is tailored for PostgreSQL. Using it with other database systems will likely result in incorrect or incomplete type generation.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"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';`.","message":"The documentation and older examples often show `require()` syntax for both `knex` and `knex-types`. However, in modern TypeScript and ESM-first Node.js projects, `import` statements are preferred. Incorrect import styles (e.g., named import for the Knex factory) can lead to runtime errors.","severity":"gotcha","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"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.","cause":"The 'knex' package is a peer dependency of 'knex-types' and must be installed separately in your project.","error":"Error: Cannot find module 'knex' from 'knex-types'"},{"fix":"Update your type references from `EntityRecord<TableName>` to `Knex.DbRecord<TableName>` and ensure `import { Knex } from 'knex';` is present.","cause":"The `EntityRecord` type was deprecated in `knex-types` v0.2.0 and replaced with `Knex.DbRecord<Entity>`.","error":"Property 'EntityRecord' does not exist on type '...'"},{"fix":"Change your import statement for the Knex factory to `import KnexFactory from 'knex';`.","cause":"This error occurs when trying to `import { knex } from 'knex';` in an ES Module environment, as the main Knex factory function is primarily a default export in ESM contexts.","error":"SyntaxError: Named export 'knex' not found. The requested module 'knex' is a CommonJS module, which may not support named exports."}],"ecosystem":"npm"}