npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@outroll/schema

v1.0.6

Published

Utility for extracting information about existing DB schema

Downloads

6

Readme

@directus/schema

Utility for extracting information about the database schema

Usage

The package is initialized by passing it an instance of Knex:

import knex from 'knex';
import schema from '@outroll/schema';

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 = schema(database);

export default inspector;

Examples

import inspector from './inspector';

async function logTables() {
	const tables = await inspector.tables();
	console.log(tables);
}

API

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

Tables

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

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",
//     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"
// }

primary(table: string): Promise<string>

Retrieve the primary key column for a given table

await inspector.primary('articles');
// => "id"

Misc.

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');

Contributing

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.

Tests

First start docker containers:

$ docker-compose up -d

Then run tests:

$ npm test

Standard mocha filter (grep) can be used:

$ npm test -- -g '.tableInfo'