rubenjgarcia / jdbc-metadata

NodeJS JDBC Metadata module
MIT License
5 stars 2 forks source link

JDBC Metadata

This nodule module helps you to get the metadata information from a database. Using a jdbc driver in Java you can access to the database and collect the information about tables, columns, primary keys, foreign keys, ...

Example


var metadata = require('jdbc-metadata');

var jdbcConfig = {
    libpath: __dirname + '/../jar/mysql-connector-java-5.1.32-bin.jar',
    drivername: 'com.mysql.jdbc.Driver',
    url: 'jdbc:mysql://localhost:3306/test',
    user: 'root',
    password: ''
};

var jdbcMetadata = new metadata(jdbcConfig);

jdbcMetadata.metadata(function (err, metadata) {

    console.log('Getting tables...');

    jdbcMetadata.tables(null, function (err, tables) {
        console.log(tables);

        jdbcConn.close(function(err) {
          console.log('Connection closed');
        });
    });
});

Configuration

Create an object with the following properties


var jdbcConfig = {
    libpath: __dirname + '/../jar/mysql-connector-java-5.1.32-bin.jar',
    drivername: 'com.mysql.jdbc.Driver',
    url: 'jdbc:mysql://localhost:3306/test',
    user: 'root',
    password: ''
};

Metadata Object

First you must create the metadata object passing a config object and initialize it by calling metadata method

var jdbcMetadata = new metadata(jdbcConfig);
jdbcMetadata.metadata(function (err, metadata) {
    console.log('Metadata object initialized');
});

Tables

To get the tables in the database you can use the tables method. You can filter the result using an options object with the following properties

var options = {schema: 'test', types: ['TABLE', 'VIEW']};
jdbcMetadata.tables(options, function (err, tables) {
    console.log(tables);
});

The result is an array containing the objects representing the tables properties

Columns

To get the columns in tables you can use the columns method. You can filter the result using an options object with the following properties

var options = {schema: 'test', table: 'mytable'};
jdbcMetadata.columns(options, function (err, columns) {
    console.log(columns);
});

The result is an array containing the objects representing the columns properties

Primary Keys

To get the primary keys in tables you can use the primaryKeys method. You can filter the result using an options object with the following properties

var options = {schema: 'test', table: 'mytable'};
jdbcMetadata.primaryKeys(options, function (err, primaryKeys) {
    console.log(primaryKeys);
});

The result is an array containing the objects representing the primary keys properties

Foreign keys

To get the Foreing keys (Imported keys) in tables you can use the importedKeys method. You can filter the result using an options object with the following properties

var options = {schema: 'test', table: 'mytable'};
jdbcMetadata.importedKeys(options, function (err, importedKeys) {
    console.log(importedKeys);
});

The result is an array containing the objects representing the foreign keys properties

Exported keys

To get the foreign key columns that reference the given table's primary key columns (the foreign keys exported by a table) you can use the exportedKeys method. You can filter the result using an options object with the following properties

var options = {schema: 'test', table: 'mytable'};
jdbcMetadata.exportedKeys(options, function (err, exportedKeys) {
    console.log(importedKeys);
});

The result is an array containing the objects representing the foreign keys properties

Close connection

Once you've finished with the connection you must close it by calling method close

jdbcMetadata.close(function (err) {
    console.log('Connection closed');
});