{"_id":"@chat-query/knex-schema-inspector","name":"@chat-query/knex-schema-inspector","dist-tags":{"latest":"3.1.0"},"versions":{"3.1.0":{"name":"@chat-query/knex-schema-inspector","version":"3.1.0","description":"Utility for extracting information about existing DB schema","main":"dist/index.js","types":"dist/index.d.ts","scripts":{"build":"tsc --build","prepare":"npm run build","lint":"prettier --check .","test":"npm run lint && ts-mocha test/**/*.spec.ts --timeout 10000"},"publishConfig":{"access":"public"},"repository":{"type":"git","url":"git+https://github.com/knex/knex-schema-inspector.git"},"keywords":["sql","knex","schema","mysql","postgresql","sqlite3","javascript"],"author":{"name":"Rijk van Zanten","email":"rijkvanzanten@me.com","url":"https://github.com/rijkvanzanten"},"contributors":[{"name":"Tanya Byrne","url":"https://github.com/kukulaka"},{"name":"Martti Laine","url":"https://github.com/codeclown"},{"name":"Martin Benndorf","url":"https://github.com/MiniDigger"},{"name":"Igor Savin","url":"https://github.com/kibertoad"}],"license":"MIT","bugs":{"url":"https://github.com/knex/knex-schema-inspector/issues"},"homepage":"https://github.com/knex/knex-schema-inspector#readme","devDependencies":{"@types/chai":"^4.3.3","@types/lodash.flatten":"^4.4.7","@types/lodash.isnil":"^4.0.7","@types/mocha":"^10.0.0","@types/node":"^18.11.2","better-sqlite3":"^8.6.0","chai":"^4.3.6","husky":"^8.0.1","knex":"^2.3.0","lint-staged":"^13.0.3","mocha":"^10.1.0","mysql":"^2.18.1","oracledb":"^5.5.0","pg":"^8.8.0","prettier":"^2.7.1","sqlite3":"^5.1.2","tedious":"^15.1.0","ts-mocha":"^10.0.0","typescript":"^4.8.4"},"lint-staged":{"*.{ts, js,json}":["prettier --write"]},"husky":{"hooks":{"pre-commit":"npx lint-staged"}},"dependencies":{"lodash.flatten":"^4.4.0","lodash.isnil":"^4.0.0"},"_id":"@chat-query/knex-schema-inspector@3.1.0","gitHead":"ab277052f03b0e6730a9f7c23de0a1b8d3c8fe58","_nodeVersion":"20.9.0","_npmVersion":"10.1.0","dist":{"integrity":"sha512-P6Wn1r6O26cR2oD8sLSV/lkBctzCG/qwDuMgBTKIcUdzFkamU8vAGDYCINgLzTdZ6lXWYdSKorCWz2HDhi7qug==","shasum":"6cd1beed5fdaefb7e6b7e7c7b5326826a9998a4f","tarball":"https://registry.npmjs.org/@chat-query/knex-schema-inspector/-/knex-schema-inspector-3.1.0.tgz","fileCount":69,"unpackedSize":409497,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQD/r0qNEyZH7A/5D3xbtEyu4PTUzlGmr4YBR4BI87RWAgIhAKqXt+rZp24aKxgW3qwJHU+bwhpaNlFxsXcmcJimnaQI"}]},"_npmUser":{"name":"jadenxiong","email":"17362864761@163.com"},"directories":{},"maintainers":[{"name":"jadenxiong","email":"17362864761@163.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/knex-schema-inspector_3.1.0_1727231166355_0.4470954390430635"},"_hasShrinkwrap":false}},"time":{"created":"2024-09-25T02:26:06.212Z","3.1.0":"2024-09-25T02:26:06.596Z","modified":"2024-09-25T02:26:06.948Z"},"maintainers":[{"name":"jadenxiong","email":"17362864761@163.com"}],"description":"Utility for extracting information about existing DB schema","homepage":"https://github.com/knex/knex-schema-inspector#readme","keywords":["sql","knex","schema","mysql","postgresql","sqlite3","javascript"],"repository":{"type":"git","url":"git+https://github.com/knex/knex-schema-inspector.git"},"contributors":[{"name":"Tanya Byrne","url":"https://github.com/kukulaka"},{"name":"Martti Laine","url":"https://github.com/codeclown"},{"name":"Martin Benndorf","url":"https://github.com/MiniDigger"},{"name":"Igor Savin","url":"https://github.com/kibertoad"}],"author":{"name":"Rijk van Zanten","email":"rijkvanzanten@me.com","url":"https://github.com/rijkvanzanten"},"bugs":{"url":"https://github.com/knex/knex-schema-inspector/issues"},"license":"MIT","readme":"# knex-schema-inspector\n\nUtility for extracting information about existing DB schema\n\nThis library currently supports Postgres, MySQL, MS SQL, SQLite, and OracleDB. We aim to have support for the same databases as the main knex project.\n\n## Installation\n\nInstall the package through NPM or Yarn:\n\n```\nnpm install knex-schema-inspector\n```\n\n```\nyarn add knex-schema-inspector\n```\n\n## Usage\n\nThe package is initialized by passing it an instance of Knex:\n\n```ts\nimport Knex from 'knex';\nimport schemaInspector from 'knex-schema-inspector';\n\nconst database = Knex({\n  client: 'mysql',\n  connection: {\n    host: '127.0.0.1',\n    user: 'your_database_user',\n    password: 'your_database_password',\n    database: 'myapp_test',\n    charset: 'utf8',\n  },\n});\n\nconst inspector = schemaInspector(database);\n\nexport default inspector;\n```\n\n## Examples\n\n```ts\nimport inspector from './inspector';\n\nasync function logTables() {\n  const tables = await inspector.tables();\n  console.log(tables);\n}\n```\n\n## API\n\nNote: MySQL doesn't support the `schema` parameter, as schema and database are ambiguous in MySQL.\n\nNote 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.\n\nNote 3: MSSQL doesn't support comment for either tables or columns\n\n### Methods\n\n**Table**\n\n- [`tables(): Promise<string[]>`](#tables-promisestring)\n- [`tableInfo(table?: string): Promise<Table | Table[]>`](#tableinfotable-string-promisetable--table)\n- [`hasTable(table: string): Promise<boolean>`](#hastabletable-string-promiseboolean)\n\n**Columns**\n\n- [`columns(table?: string): Promise<{ table: string, column: string }[]>`](#columnstable-string-promise-table-string-column-string-)\n- [`columnInfo(table?: string, column?: string): Promise<Column[] | Column>`](#columninfotable-string-column-string-promisecolumn--column)\n- [`primary(table: string): Promise<string>`](#primarytable-string-promisestring)\n\n**Foreign Keys**\n\n- [`foreignKeys(): Promise<ForeignKey>`](#foreign-keys)\n\n**Misc.**\n\n- [`withSchema(schema: string): void`](#withschemaschema-string-void)\n\n### Tables\n\n#### `tables(): Promise<string[]>`\n\nRetrieve all tables in the current database.\n\n```ts\nawait inspector.tables();\n// => ['articles', 'images', 'reviews']\n```\n\n#### `tableInfo(table?: string): Promise<Table | Table[]>`\n\nRetrieve the table info for the given table, or all tables if no table is specified\n\n```ts\nawait inspector.tableInfo('articles');\n// => {\n//   name: 'articles',\n//   schema: 'project',\n//   comment: 'Informational blog posts'\n// }\n\nawait inspector.tableInfo();\n// => [\n//   {\n//     name: 'articles',\n//     schema: 'project',\n//     comment: 'Informational blog posts'\n//   },\n//   { ... },\n//   { ... }\n// ]\n```\n\n#### `hasTable(table: string): Promise<boolean>`\n\nCheck if a table exists in the current database.\n\n```ts\nawait inspector.hasTable('articles');\n// => true\n```\n\n### Columns\n\n#### `columns(table?: string): Promise<{ table: string, column: string }[]>`\n\nRetrieve all columns in a given table, or all columns if no table is specified\n\n```ts\nawait inspector.columns();\n// => [\n//   {\n//     \"table\": \"articles\",\n//     \"column\": \"id\"\n//   },\n//   {\n//     \"table\": \"articles\",\n//     \"column\": \"title\"\n//   },\n//   {\n//     \"table\": \"images\",\n//     \"column\": \"id\"\n//   }\n// ]\n\nawait inspector.columns('articles');\n// => [\n//   {\n//     \"table\": \"articles\",\n//     \"column\": \"id\"\n//   },\n//   {\n//     \"table\": \"articles\",\n//     \"column\": \"title\"\n//   }\n// ]\n```\n\n#### `columnInfo(table?: string, column?: string): Promise<Column[] | Column>`\n\nRetrieve all columns from a given table. Returns all columns if `table` parameter is undefined.\n\n```ts\nawait inspector.columnInfo('articles');\n// => [\n//   {\n//     name: \"id\",\n//     table: \"articles\",\n//     data_type: \"VARCHAR\",\n//     default_value: null,\n//     max_length: null,\n//     numeric_precision: null,\n//     numeric_scale: null,\n//     is_nullable: false,\n//     is_unique: false,\n//     is_primary_key: true,\n//     has_auto_increment: true,\n//     foreign_key_column: null,\n//     foreign_key_table: null,\n//     comment: \"Primary key for the articles collection\"\n//   },\n//   { ... },\n//   { ... }\n// ]\n\nawait inspector.columnInfo('articles', 'id');\n// => {\n//    name: \"id\",\n//    table: \"articles\",\n//    data_type: \"VARCHAR\",\n//    default_value: null,\n//    max_length: null,\n//    numeric_precision: null,\n//    numeric_scale: null,\n//    is_nullable: false,\n//    is_unique: false,\n//    is_primary_key: true,\n//    has_auto_increment: true,\n//    foreign_key_column: null,\n//    foreign_key_table: null,\n//    comment: \"Primary key for the articles collection\"\n//  }\n```\n\n#### `primary(table: string): Promise<string>`\n\nRetrieve the primary key column for a given table\n\n```ts\nawait inspector.primary('articles');\n// => \"id\"\n```\n\n### Foreign Keys\n\nRetrieve all configured foreign key constraints.\n\n```ts\nawait inspector.foreignKeys();\n// => [\n//   {\n//     table: 'directus_files',\n//     column: 'folder',\n//     foreign_key_table: 'directus_folders',\n//     foreign_key_column: 'id',\n//     constraint_name: 'directus_files_folder_foreign',\n//     on_update: 'CASCADE',\n//     on_delete: 'SET NULL'\n//   },\n//   {\n//     table: 'directus_files',\n//     column: 'modified_by',\n//     foreign_key_table: 'directus_users',\n//     foreign_key_column: 'id',\n//     constraint_name: 'directus_files_modified_by_foreign',\n//     on_update: 'CASCADE',\n//     on_delete: 'SET NULL'\n//   }\n// ]\n```\n\n### Misc.\n\n#### `withSchema(schema: string): void`\n\n_Not supported in MySQL_\n\nSet the schema to use. Note: this is set on the inspector instance and only has to be done once:\n\n```ts\ninspector.withSchema('my-schema');\n```\n\n## Contributing\n\nPull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.\n\nPlease make sure to update tests as appropriate.\n\n### Tests\n\nFirst start docker containers:\n\n```shell\n$ docker-compose up -d\n```\n\nThen run tests:\n\n```shell\n$ npm test\n```\n\nStandard mocha filter (grep) can be used:\n\n```shell\n$ npm test -- -g '.tableInfo'\n```\n\n## License\n\n[MIT](https://choosealicense.com/licenses/mit/)\n","readmeFilename":"README.md"}