Gathering detailed insights and metrics for @vdtn359/knex-schema-inspector
Gathering detailed insights and metrics for @vdtn359/knex-schema-inspector
Gathering detailed insights and metrics for @vdtn359/knex-schema-inspector
Gathering detailed insights and metrics for @vdtn359/knex-schema-inspector
Utility for extracting information about existing DB schema
npm install @vdtn359/knex-schema-inspector
Typescript
Module System
Node Version
NPM Version
75.2
Supply Chain
99.5
Quality
74.8
Maintenance
100
Vulnerability
99.6
License
TypeScript (98.48%)
PLSQL (0.66%)
Shell (0.47%)
TSQL (0.39%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
99 Stars
273 Commits
43 Forks
10 Watching
7 Branches
24 Contributors
Latest Version
3.0.1
Package Id
@vdtn359/knex-schema-inspector@3.0.1
Unpacked Size
401.94 kB
Size
45.26 kB
File Count
73
NPM Version
8.19.4
Node Version
16.19.0
Publised On
02 Jul 2023
Cumulative downloads
Total Downloads
Last day
0%
0
Compared to previous day
Last week
0%
0
Compared to previous week
Last month
0%
0
Compared to previous month
Last year
0%
0
Compared to previous year
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:
1import Knex from 'knex'; 2import schemaInspector from 'knex-schema-inspector'; 3 4const database = Knex({ 5 client: 'mysql', 6 connection: { 7 host: '127.0.0.1', 8 user: 'your_database_user', 9 password: 'your_database_password', 10 database: 'myapp_test', 11 charset: 'utf8', 12 }, 13}); 14 15const inspector = schemaInspector(database); 16 17export default inspector;
1import inspector from './inspector'; 2 3async function logTables() { 4 const tables = await inspector.tables(); 5 console.log(tables); 6}
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.
1await inspector.tables(); 2// => ['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
1await inspector.tableInfo('articles'); 2// => { 3// name: 'articles', 4// schema: 'project', 5// comment: 'Informational blog posts' 6// } 7 8await inspector.tableInfo(); 9// => [ 10// { 11// name: 'articles', 12// schema: 'project', 13// comment: 'Informational blog posts' 14// }, 15// { ... }, 16// { ... } 17// ]
hasTable(table: string): Promise<boolean>
Check if a table exists in the current database.
1await inspector.hasTable('articles'); 2// => true
columns(table?: string): Promise<{ table: string, column: string }[]>
Retrieve all columns in a given table, or all columns if no table is specified
1await inspector.columns(); 2// => [ 3// { 4// "table": "articles", 5// "column": "id" 6// }, 7// { 8// "table": "articles", 9// "column": "title" 10// }, 11// { 12// "table": "images", 13// "column": "id" 14// } 15// ] 16 17await inspector.columns('articles'); 18// => [ 19// { 20// "table": "articles", 21// "column": "id" 22// }, 23// { 24// "table": "articles", 25// "column": "title" 26// } 27// ]
columnInfo(table?: string, column?: string): Promise<Column[] | Column>
Retrieve all columns from a given table. Returns all columns if table
parameter is undefined.
1await inspector.columnInfo('articles'); 2// => [ 3// { 4// name: "id", 5// table: "articles", 6// data_type: "VARCHAR", 7// default_value: null, 8// max_length: null, 9// numeric_precision: null, 10// numeric_scale: null, 11// is_nullable: false, 12// is_unique: false, 13// is_primary_key: true, 14// has_auto_increment: true, 15// foreign_key_column: null, 16// foreign_key_table: null, 17// comment: "Primary key for the articles collection" 18// }, 19// { ... }, 20// { ... } 21// ] 22 23await inspector.columnInfo('articles', 'id'); 24// => { 25// name: "id", 26// table: "articles", 27// data_type: "VARCHAR", 28// default_value: null, 29// max_length: null, 30// numeric_precision: null, 31// numeric_scale: null, 32// is_nullable: false, 33// is_unique: false, 34// is_primary_key: true, 35// has_auto_increment: true, 36// foreign_key_column: null, 37// foreign_key_table: null, 38// comment: "Primary key for the articles collection" 39// }
primary(table: string): Promise<string>
Retrieve the primary key column for a given table
1await inspector.primary('articles'); 2// => "id"
Retrieve all configured foreign key constraints.
1await inspector.foreignKeys(); 2// => [ 3// { 4// table: 'directus_files', 5// column: 'folder', 6// foreign_key_table: 'directus_folders', 7// foreign_key_column: 'id', 8// constraint_name: 'directus_files_folder_foreign', 9// on_update: 'CASCADE', 10// on_delete: 'SET NULL' 11// }, 12// { 13// table: 'directus_files', 14// column: 'modified_by', 15// foreign_key_table: 'directus_users', 16// foreign_key_column: 'id', 17// constraint_name: 'directus_files_modified_by_foreign', 18// on_update: 'CASCADE', 19// on_delete: 'SET NULL' 20// } 21// ]
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:
1inspector.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:
1$ docker-compose up -d
Then run tests:
1$ npm test
Standard mocha filter (grep) can be used:
1$ npm test -- -g '.tableInfo'
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 12/23 approved changesets -- score normalized to 5
Reason
dependency not pinned by hash detected -- score normalized to 3
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
21 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-12-23
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More