Knex Schema Inspector is a utility library designed to extract detailed information about existing database schemas. It leverages an initialized Knex.js instance to query metadata from various relational databases, including PostgreSQL, MySQL, MS SQL, SQLite, and OracleDB. The current stable version is 3.1.0, with major versions released approximately annually, indicating an active development and maintenance cadence. Its key differentiator is its deep integration with Knex, allowing developers to use their existing Knex configurations to introspect database structures, retrieve table names, column details, primary keys, and foreign key relationships across a wide range of SQL dialects. It ships with TypeScript types, facilitating robust development with type safety. It does not provide migration or schema modification capabilities, focusing solely on schema introspection.
npm install knex-schema-inspectorVerified import paths — ran on the pinned version, not inferred.
This quickstart initializes Knex, creates a schema inspector instance, and then logs all table names, detailed information for the first table found, and all columns within that table. It includes environment variable placeholders for secure database connection and proper resource cleanup.
Review the `tedious` v15 release notes (https://github.com/tediousjs/tedious/releases/tag/v15.0.0) and adjust your Knex configuration or application logic if you are using MS SQL and encounter unexpected behavior.
Ensure your project's `knex` dependency is updated to `knex@2` or a later compatible version (e.g., `npm install knex@latest` or `yarn add knex@latest`).
Update your application logic to expect and parse default values as strings. If you need a different type, you will need to cast or convert the string representation explicitly.
When working with MySQL, rely on the `connection.database` setting in your Knex configuration rather than attempting to use the `schema` parameter for database selection within the inspector.
Avoid relying on `comment` metadata for MS SQL databases. If you need to store descriptive metadata, consider alternative approaches such as dedicated documentation or a separate metadata table.
Use the correct ESM default import syntax: `import schemaInspector from 'knex-schema-inspector';`
Ensure your Knex instance is fully configured and connected before passing it to `schemaInspector`. Example: `const database = Knex({ client: 'pg', connection: {...} });`Upgrade your `knex` dependency to version 2.x or newer: `npm install knex@latest` or `yarn add knex@latest`.
Parse the `default_value` string to the desired type manually. For example, `Number(columnInfo.default_value)` or `columnInfo.default_value === 'true'`.