Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
knex-schema-inspector
Advanced tools
Readme
Utility for extracting information about existing DB schema
This library currently supports Postgres, MySQL, MS SQL, SQLite, and OracleDB. 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 add 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.
Note 3: MSSQL doesn't support comment for either tables or columns
Table
tables(): Promise<string[]>
tableInfo(table?: string): Promise<Table | Table[]>
hasTable(table: string): Promise<boolean>
Columns
columns(table?: string): Promise<{ table: string, column: string }[]>
columnInfo(table?: string, column?: string): Promise<Column[] | Column>
primary(table: string): Promise<string>
Foreign Keys
Misc.
tables(): Promise<string[]>
Retrieve all tables in the current database.
await inspector.tables();
// => ['articles', 'images', 'reviews']
tableInfo(table?: string): Promise<Table | Table[]>
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'
// },
// { ... },
// { ... }
// ]
hasTable(table: string): Promise<boolean>
Check if a table exists in the current database.
await inspector.hasTable('articles');
// => true
columns(table?: string): Promise<{ table: string, column: string }[]>
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"
// }
// ]
columnInfo(table?: string, column?: string): Promise<Column[] | Column>
Retrieve all columns from a given table. Returns all columns if table
parameter is undefined.
await inspector.columnInfo('articles');
// => [
// {
// name: "id",
// table: "articles",
// data_type: "VARCHAR",
// default_value: null,
// max_length: null,
// numeric_precision: null,
// numeric_scale: null,
// is_nullable: false,
// is_unique: false,
// is_primary_key: true,
// has_auto_increment: true,
// foreign_key_column: null,
// foreign_key_table: null,
// comment: "Primary key for the articles collection"
// },
// { ... },
// { ... }
// ]
await inspector.columnInfo('articles', 'id');
// => {
// name: "id",
// table: "articles",
// data_type: "VARCHAR",
// default_value: null,
// max_length: null,
// numeric_precision: null,
// numeric_scale: null,
// is_nullable: false,
// is_unique: false,
// is_primary_key: true,
// has_auto_increment: true,
// foreign_key_column: null,
// foreign_key_table: null,
// comment: "Primary key for the articles collection"
// }
primary(table: string): Promise<string>
Retrieve the primary key column for a given table
await inspector.primary('articles');
// => "id"
Retrieve all configured foreign key constraints.
await inspector.foreignKeys();
// => [
// {
// table: 'directus_files',
// column: 'folder',
// foreign_key_table: 'directus_folders',
// foreign_key_column: 'id',
// constraint_name: 'directus_files_folder_foreign',
// on_update: 'CASCADE',
// on_delete: 'SET NULL'
// },
// {
// table: 'directus_files',
// column: 'modified_by',
// foreign_key_table: 'directus_users',
// foreign_key_column: 'id',
// constraint_name: 'directus_files_modified_by_foreign',
// on_update: 'CASCADE',
// on_delete: 'SET NULL'
// }
// ]
withSchema(schema: string): void
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.
First start docker containers:
$ docker-compose up -d
Then run tests:
$ npm test
Standard mocha filter (grep) can be used:
$ npm test -- -g '.tableInfo'
FAQs
Utility for extracting information about existing DB schema
The npm package knex-schema-inspector receives a total of 5,819 weekly downloads. As such, knex-schema-inspector popularity was classified as popular.
We found that knex-schema-inspector demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.