Comparing version 0.2.0 to 0.3.0
@@ -6,3 +6,2 @@ module.exports = Column; | ||
var BaseColumn = require('../column'); | ||
var dataType = require('../data-type'); | ||
@@ -9,0 +8,0 @@ function Column(props) { |
var mysql = require('mysql'); | ||
var Table = require('./table'); | ||
var Column = require('./column'); | ||
var Index = require('./index'); | ||
@@ -40,5 +41,10 @@ exports.connect = function (options, callback) { | ||
Driver.prototype.getIndexes = function(tableName, callback) { | ||
var handler = handleResults.bind(this, Index, callback); | ||
this.client.query("show indexes from " + tableName, handler); | ||
}; | ||
Driver.prototype.close = function(callback) { | ||
this.client.end(callback); | ||
} | ||
}; | ||
@@ -55,2 +61,2 @@ function handleResults(obj, callback, err, result) { | ||
callback(null, objects); | ||
} | ||
} |
@@ -6,3 +6,2 @@ module.exports = Column; | ||
var BaseColumn = require('../column'); | ||
var dataType = require('../data-type'); | ||
@@ -9,0 +8,0 @@ function Column(props) { |
var pg = require('pg'); | ||
var Table = require('./table'); | ||
var Column = require('./column'); | ||
var Index = require('./index'); | ||
@@ -75,2 +76,18 @@ exports.connect = function (options, callback) { | ||
Driver.prototype.getIndexes = function(tableName, callback) { | ||
var handler = handleResults.bind(this, Index, callback); | ||
var sql = "select t.relname as table_name, " + | ||
"i.relname as index_name, " + | ||
"a.attname as column_name " + | ||
"from pg_class t, pg_class i, pg_index ix, pg_attribute a " + | ||
"where t.oid = ix.indrelid " + | ||
"and i.oid = ix.indexrelid " + | ||
"and a.attrelid = t.oid " + | ||
"and a.attnum = ANY(ix.indkey) " + | ||
"and t.relkind = 'r' " + | ||
"and t.relname = $1"; | ||
this.client.query(sql, [tableName], handler); | ||
}; | ||
Driver.prototype.close = function(callback) { | ||
@@ -77,0 +94,0 @@ this.client.end(); |
@@ -14,2 +14,2 @@ module.exports = Table; | ||
return this.meta.table_name; | ||
}; | ||
}; |
@@ -6,3 +6,2 @@ module.exports = Column; | ||
var BaseColumn = require('../column'); | ||
var dataType = require('../data-type'); | ||
@@ -9,0 +8,0 @@ function Column(props) { |
var sqlite3 = require('sqlite3'); | ||
var Table = require('./table'); | ||
var Column = require('./column'); | ||
var Index = require('./index'); | ||
@@ -40,3 +41,36 @@ exports.connect = function (options, callback) { | ||
Driver.prototype.close = function(callback) { | ||
Driver.prototype.getIndexes = function (tableName, callback) { | ||
this.client.all("pragma index_list(" + tableName + ")", function (err, result) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
var indexCount = 0; | ||
var numIndexes = result.length; | ||
var indexes = []; | ||
for (var i = 0; i < numIndexes; i++) { | ||
var indexName = result[i].name; | ||
this.client.all("pragma index_info(" + indexName + ")", function (err, result) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
for (var j = 0; j < result.length; j++) { | ||
var indexMeta = result[j]; | ||
indexMeta.index_name = indexName; | ||
indexMeta.table_name = tableName; | ||
indexes.push(new Index(indexMeta)); | ||
} | ||
if (++indexCount === numIndexes) { | ||
callback(null, indexes); | ||
} | ||
}); | ||
} | ||
}.bind(this)); | ||
}; | ||
Driver.prototype.close = function (callback) { | ||
this.client.close(); | ||
@@ -43,0 +77,0 @@ callback(); |
@@ -14,2 +14,2 @@ module.exports = Table; | ||
return this.meta.table_name; | ||
}; | ||
}; |
@@ -7,2 +7,2 @@ exports.lowercaseKeys = function (obj) { | ||
return newObj; | ||
} | ||
}; |
@@ -11,3 +11,3 @@ { | ||
"description": "Relational database metadata extraction library", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"license": "MIT", | ||
@@ -25,3 +25,6 @@ "main": "./lib/db-meta.js", | ||
"chai": "~1.1.0", | ||
"mocha": "~1.2.1" | ||
"mocha": "~1.2.1", | ||
"pg": "~0.7.2", | ||
"mysql": "~0.9.6", | ||
"sqlite3": "~2.1.5" | ||
}, | ||
@@ -28,0 +31,0 @@ "optionalDependencies": {}, |
@@ -11,2 +11,3 @@ # db-meta | ||
## Installation | ||
@@ -16,2 +17,15 @@ | ||
You'll also need to install the appropriate driver for the database you want to | ||
use db-meta with. None are installed by default. | ||
# PostgreSQL | ||
$ npm install pg | ||
# MySQL | ||
$ npm install mysql | ||
# SQLite3 | ||
$ npm install sqlite3 | ||
## Example Usage | ||
@@ -115,2 +129,10 @@ | ||
## Testing | ||
To run the tests you'll need a local installation of PostgreSQL, MySQL, and SQLite3 with default user | ||
accounts. You'll also need to create a schema named `db_meta_test` beforehand (excluding SQLite3) and | ||
install the npm dev dependencies. Finally, you can run all the tests with | ||
$ npm test | ||
## License | ||
@@ -117,0 +139,0 @@ |
@@ -17,3 +17,2 @@ var expect = require('chai').expect; | ||
expect(t.meta).not.to.be.null; | ||
expect(t.meta.column_name).to.equal('col'); | ||
done(); | ||
@@ -20,0 +19,0 @@ }); |
@@ -12,4 +12,8 @@ var expect = require('chai').expect; | ||
driver = dbDriver; | ||
driver.client.query('CREATE TABLE person (id INTEGER PRIMARY KEY NOT NULL, name VARCHAR(255) NOT NULL, email VARCHAR(100), age INTEGER DEFAULT 30);', done); | ||
driver.client.query('CREATE TABLE person (id INTEGER PRIMARY KEY NOT NULL, name VARCHAR(255) NOT NULL, email VARCHAR(100), age INTEGER DEFAULT 30);', createIndex); | ||
} | ||
function createIndex(err) { | ||
driver.client.query('CREATE INDEX person_name_idx ON person (name, age)', done); | ||
} | ||
}); | ||
@@ -82,2 +86,17 @@ | ||
}); | ||
it('should return all indexes in the database for a given table', function(done) { | ||
driver.getIndexes('person', onResult); | ||
function onResult(err, indexes) { | ||
expect(indexes.length).to.equal(3); | ||
expect(indexes[1].getName()).to.equal('person_name_idx'); | ||
expect(indexes[1].getTableName()).to.equal('person'); | ||
expect(indexes[1].getColumnName()).to.equal('name'); | ||
expect(indexes[2].getName()).to.equal('person_name_idx'); | ||
expect(indexes[2].getTableName()).to.equal('person'); | ||
expect(indexes[2].getColumnName()).to.equal('age'); | ||
done(); | ||
} | ||
}); | ||
}); | ||
@@ -84,0 +103,0 @@ |
@@ -17,4 +17,2 @@ var expect = require('chai').expect; | ||
expect(t.meta).not.to.be.null; | ||
expect(t.meta.id).to.equal(1); | ||
expect(t.meta.table_name).to.equal('tbl'); | ||
done(); | ||
@@ -21,0 +19,0 @@ }); |
@@ -17,3 +17,2 @@ var expect = require('chai').expect; | ||
expect(t.meta).not.to.be.null; | ||
expect(t.meta.column_name).to.equal('col'); | ||
done(); | ||
@@ -20,0 +19,0 @@ }); |
@@ -12,4 +12,8 @@ var expect = require('chai').expect; | ||
driver = dbDriver; | ||
driver.client.query('CREATE TABLE person (id INTEGER PRIMARY KEY NOT NULL, name VARCHAR(255) NOT NULL, email VARCHAR(100), age INTEGER DEFAULT 30);', done); | ||
driver.client.query('CREATE TABLE person (id INTEGER PRIMARY KEY NOT NULL, name VARCHAR(255) NOT NULL, email VARCHAR(100), age INTEGER DEFAULT 30);', createIndex); | ||
} | ||
function createIndex(err) { | ||
driver.client.query('CREATE INDEX person_name_idx ON person (name, age)', done); | ||
} | ||
}); | ||
@@ -82,2 +86,17 @@ | ||
}); | ||
it('should return all indexes in the database', function(done) { | ||
driver.getIndexes('person', onResult); | ||
function onResult(err, indexes) { | ||
expect(indexes.length).to.equal(3); | ||
expect(indexes[1].getName()).to.equal('person_name_idx'); | ||
expect(indexes[1].getTableName()).to.equal('person'); | ||
expect(indexes[1].getColumnName()).to.equal('name'); | ||
expect(indexes[2].getName()).to.equal('person_name_idx'); | ||
expect(indexes[2].getTableName()).to.equal('person'); | ||
expect(indexes[2].getColumnName()).to.equal('age'); | ||
done(); | ||
} | ||
}); | ||
}); | ||
@@ -84,0 +103,0 @@ |
@@ -17,4 +17,2 @@ var expect = require('chai').expect; | ||
expect(t.meta).not.to.be.null; | ||
expect(t.meta.id).to.equal(1); | ||
expect(t.meta.table_name).to.equal('tbl'); | ||
done(); | ||
@@ -21,0 +19,0 @@ }); |
@@ -17,3 +17,2 @@ var expect = require('chai').expect; | ||
expect(t.meta).not.to.be.null; | ||
expect(t.meta.name).to.equal('col'); | ||
done(); | ||
@@ -20,0 +19,0 @@ }); |
@@ -12,4 +12,8 @@ var expect = require('chai').expect; | ||
driver = dbDriver; | ||
driver.client.run('CREATE TABLE person (id INTEGER PRIMARY KEY NOT NULL, name VARCHAR(255) NOT NULL, email VARCHAR(100), age INTEGER DEFAULT 30);', done); | ||
driver.client.run('CREATE TABLE person (id INTEGER PRIMARY KEY NOT NULL, name VARCHAR(255) NOT NULL, email VARCHAR(100), age INTEGER DEFAULT 30);', createIndex); | ||
} | ||
function createIndex(err) { | ||
driver.client.run('CREATE INDEX person_name_idx ON person (name, age)', done); | ||
} | ||
}); | ||
@@ -81,2 +85,17 @@ | ||
}); | ||
it('should return all indexes in the database for a given table', function(done) { | ||
driver.getIndexes('person', onResult); | ||
function onResult(err, indexes) { | ||
expect(indexes.length).to.equal(2); | ||
expect(indexes[0].getName()).to.equal('person_name_idx'); | ||
expect(indexes[0].getTableName()).to.equal('person'); | ||
expect(indexes[0].getColumnName()).to.equal('name'); | ||
expect(indexes[1].getName()).to.equal('person_name_idx'); | ||
expect(indexes[1].getTableName()).to.equal('person'); | ||
expect(indexes[1].getColumnName()).to.equal('age'); | ||
done(); | ||
} | ||
}); | ||
}); | ||
@@ -83,0 +102,0 @@ |
@@ -5,3 +5,3 @@ var expect = require('chai').expect; | ||
describe('pg table', function () { | ||
describe('sqlite3 table', function () { | ||
it('should implement all the methods defined in the base table interface', function (done) { | ||
@@ -18,4 +18,2 @@ var t = new Table({ table_name: 'tbl' }); | ||
expect(t.meta).not.to.be.null; | ||
expect(t.meta.id).to.equal(1); | ||
expect(t.meta.table_name).to.equal('tbl'); | ||
done(); | ||
@@ -22,0 +20,0 @@ }); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
42382
34
1007
159
0
5