Knex Schema Reader is a utility for exporting a database schema to an object (API) and/or json file (CLI)
Install from github at this time.
$ npm install git+https://[email protected]/mattjcowan/knex-schema-reader
Also, install the provider you need:
$ npm install mssql
$ npm install mysql
$ npm install pg
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
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
-
SQL Server samples:
-
MySQL samples:
-
PostgreSQL samples:
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);
});
github.io site to host documentation ... Currently, pretty much a re-hash of this readme.
To build, run:
$ npm run build
To lint, run:
$ npm run lint
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
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.
- knex.js — A query builder for PostgreSQL, MySQL and SQLite3, designed to be flexible, portable, and fun to use.
- Loopback (Discovery API/CLI) — LoopBack makes it simple to create models from an existing relational database.
- DatabaseSchemaReader — A simple, cross-database facade over .Net 2.0 DbProviderFactories to read database metadata.
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.