Utility for extracting information about existing DB schema
NOTE: This is a work in progress.
The API might change unexpectedly between patch / minor versions.
The IO of the library isn't set in stone (yet). If you have any bright ideas or recommendations, please feel free to open an issue or pull request.
This library currently supports Postgres, MySQL, and SQLite. We aim to have support for the same databases as the main knex project.
Install the package through NPM or Yarn:
npm install knex-schema-inspector
yarn knex-schema-inspector
The package is initialized by passing it an instance of Knex:
import knex from 'knex';
import schemaInspector from 'knex-schema-inspector';
const database = knex({
client: 'mysql',
connection: {
host : '127.0.0.1',
user : 'your_database_user',
password : 'your_database_password',
database : 'myapp_test',
charset : 'utf8'
}
});
const inspector = schemaInspector(database);
export default inspector;
import inspector from './inspector';
async function logTables() {
const tables = await inspector.tables();
console.log(tables);
}
Note: MySQL doesn't support the schema
parameter, as schema and database are ambiguous in MySQL.
Note 2: Some database types might return slightly more information than others. See the type files for a specific overview what to expect from driver to driver.
Retrieve all tables in the current database.
await inspector.tables();
// => ['articles', 'images', 'reviews']
Retrieve the table info for the given table, or all tables if no table is specified
await inspector.tableInfo('articles');
// => {
// name: 'articles',
// schema: 'project',
// comment: 'Informational blog posts'
// }
await inspector.tableInfo();
// => [
// {
// name: 'articles',
// schema: 'project',
// comment: 'Informational blog posts'
// },
// { ... },
// { ... }
// ]
Check if a table exists in the current database.
await inspector.hasTable('articles');
// => true
Retrieve all columns in a given table, or all columns if no table is specified
await inspector.columns();
// => [
// {
// "table": "articles",
// "column": "id"
// },
// {
// "table": "articles",
// "column": "title"
// },
// {
// "table": "images",
// "column": "id"
// }
// ]
await inspector.columns('articles');
// => [
// {
// "table": "articles",
// "column": "id"
// },
// {
// "table": "articles",
// "column": "title"
// }
// ]
Retrieve all columns from a given table. Returns all columns if table
parameter is undefined.
await inspector.columnInfo('articles');
// => [
// {
// name: "id",
// table: "articles",
// type: "VARCHAR",
// defaultValue: null,
// maxLength: null,
// isNullable: false,
// isPrimaryKey: true,
// hasAutoIncrement: true,
// foreignKeyColumn: null,
// foreignKeyTable: null,
// comment: "Primary key for the articles collection"
// },
// { ... },
// { ... }
// ]
await inspector.columnInfo('articles', 'id');
// => {
// name: "id",
// table: "articles",
// type: "VARCHAR",
// defaultValue: null,
// maxLength: null,
// isNullable: false,
// isPrimaryKey: true,
// hasAutoIncrement: true,
// foreignKeyColumn: null,
// foreignKeyTable: null,
// comment: "Primary key for the articles collection"
// }
Retrieve the primary key column for a given table
await inspector.primary('articles');
// => "id"
Not supported in MySQL
Set the schema to use. Note: this is set on the inspector instance and only has to be done once:
inspector.withSchema('my-schema');
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.