Skip to content

Retrieves the schema of your database and outputs it to a file or object.

License

Notifications You must be signed in to change notification settings

mattjcowan/knex-schema-reader

Repository files navigation

Knex Schema Reader

Build Status Dependency Status

Knex Schema Reader is a utility for exporting a database schema to an object (API) and/or json file (CLI)

Install

Install from github at this time.

$ npm install git+https://git@github.com/mattjcowan/knex-schema-reader

Also, install the provider you need:

$ npm install mssql
$ npm install mysql
$ npm install pg

Features

Exports a database schema to either an object (api) or to a json file (cli). Useful in a code generation pipeline or to build tooling.

Supported database dialects:

  • mysql
  • mssql
  • postgresql

CLI

Run the command as:

$ node ./node_modules/knex-schema-reader/bin/cli.js extract --knexfile ./knexfile.json -o ./schema.json

Or if you just cloned this repo, you can run the pre-built version as:

$ node ./bin/cli.js extract --knexfile ./test/knexfiles/knexfile.mysql.sakila.json -o ./samples/mysql/sakila.json

Sample Outputs

API

To extract a database using the api, import the module and call the extract method.

The following example extracts the AdventureWorks schema from sql server and outputs it to a file 'db.json'.

import fs from 'fs';
import Knex from 'knex';
import SchemaReader from 'knex-schema-reader';

const knex = Knex({
    client: 'mssql',
    connectionstring: 'mssql://aw-username:aw-password@localhost:1433/AdventureWorks2014'
});
SchemaReader(knex).extract()
  .then(function (db) {
    knex.destroy();
    fs.writeFileSync('db.json', JSON.stringify(db), 'utf-8');
  })
  .catch(function (err) {
    knex.destroy();
    console.error(err.stack);
  });

Documentation

github.io site to host documentation ... Currently, pretty much a re-hash of this readme.

Development/Contributions

Build

To build, run:

$ npm run build

Lint

To lint, run:

$ npm run lint

Testing

To run tests, you'll need to make sure that you have valid connections to the 6 sample databases. Check out the knexfiles here for connection information.

Running the tests currently generates the same sample JSON files listed above and available for viewing here

To run the tests, run:

$ npm run test

Code Coverage

To test your code coverage, run:

$ npm run test:cover

Travis CI code coverage is not reliable at the moment, as we don't have any migrations setup with database provisioning to run the included tests. And I don't think Travis CI supports SQL Server at this time either.

The local code coverage is in the 90%:

=============================== Coverage summary ===============================
Statements   : 93.15% ( 530/569 )
Branches     : 76.42% ( 470/615 )
Functions    : 94.08% ( 159/169 )
Lines        : 93.31% ( 516/553 )
================================================================================

That said, there are many many more tests that could be written to solidify the framework, the tests right now skim the surface.

Notable Dependencies

  • knex.js — A query builder for PostgreSQL, MySQL and SQLite3, designed to be flexible, portable, and fun to use.

Similar Projects

Get in Touch

License

Copyright © 2016 Matt Cowan, MJCZone Inc. This source code is licensed under the MIT license found in the LICENSE.txt file. The documentation to the project is licensed under the CC BY-SA 4.0 license.

About

Retrieves the schema of your database and outputs it to a file or object.

Resources

License

Stars

Watchers

Forks

Packages