Comparing version 0.1.0 to 0.2.0
@@ -9,3 +9,5 @@ module.exports = Column; | ||
'getDataType', | ||
'getMaxLength' | ||
'getMaxLength', | ||
'isPrimaryKey', | ||
'getDefaultValue' | ||
]; | ||
@@ -12,0 +14,0 @@ |
module.exports = Column; | ||
var util = require('util'); | ||
var dbmUtil = require('../util'); | ||
var BaseColumn = require('../column'); | ||
@@ -8,3 +9,3 @@ var dataType = require('../data-type'); | ||
function Column(props) { | ||
this.meta = props; | ||
this.meta = dbmUtil.lowercaseKeys(props); | ||
} | ||
@@ -26,30 +27,11 @@ util.inherits(Column, BaseColumn); | ||
Column.prototype.getDataType = function() { | ||
switch (this.meta.data_type) { | ||
case 'integer': | ||
case 'int': | ||
case 'int4': | ||
return dataType.integer; | ||
case 'boolean': | ||
case 'bool': | ||
return dataType.boolean; | ||
case 'text': | ||
return dataType.text; | ||
case 'varchar': | ||
case 'character varying': | ||
return dataType.varchar; | ||
case 'real': | ||
case 'float4': | ||
return dataType.float; | ||
case 'double precision': | ||
case 'float8': | ||
return dataType.double; | ||
case 'time': | ||
case 'timetz': | ||
return dataType.time; | ||
case 'timestamp': | ||
case 'timestamptz': | ||
return dataType.timestamp; | ||
default: | ||
return this.meta.data_type.toUpperCase(); | ||
} | ||
return this.meta.data_type.toUpperCase(); | ||
}; | ||
Column.prototype.isPrimaryKey = function () { | ||
return this.meta.primary_key === true; | ||
}; | ||
Column.prototype.getDefaultValue = function () { | ||
return this.meta.column_default; | ||
}; |
@@ -1,2 +0,1 @@ | ||
var util = require('util'); | ||
var pg = require('pg'); | ||
@@ -37,4 +36,39 @@ var Table = require('./table'); | ||
Driver.prototype.getColumns = function (tableName, callback) { | ||
var handler = handleResults.bind(this, Column, callback); | ||
var handler = handleResults.bind(this, Column, findPrimaryKeys.bind(this)); | ||
this.client.query("SELECT * FROM information_schema.columns WHERE table_name = $1", [tableName], handler); | ||
function findPrimaryKeys(err, columns) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
this.client.query("select cu.column_name " + | ||
"from information_schema.key_column_usage cu, " + | ||
"information_schema.table_constraints tc " + | ||
"where tc.table_name = cu.table_name " + | ||
"and tc.constraint_type = 'PRIMARY KEY' " + | ||
"and tc.constraint_name = cu.constraint_name " + | ||
"and tc.table_name = $1", | ||
[tableName], | ||
onPrimaryKeysResult.bind(this, columns) | ||
); | ||
} | ||
function onPrimaryKeysResult(columns, err, result) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
var primaryKeys = result.rows.map(function (row) { | ||
return row.column_name; | ||
}); | ||
columns.forEach(function (column) { | ||
if (arrayContains(primaryKeys, column.getName())) { | ||
column.meta.primary_key = true; | ||
} | ||
}); | ||
callback(null, columns); | ||
} | ||
}; | ||
@@ -58,1 +92,10 @@ | ||
} | ||
function arrayContains(arr, item) { | ||
for (var i = 0; i < arr.length; i++) { | ||
if (arr[i] === item) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} |
module.exports = Table; | ||
var util = require('util'); | ||
var dbmUtil = require('../util'); | ||
var BaseTable = require('../table'); | ||
function Table(props) { | ||
this.meta = props; | ||
this.meta = dbmUtil.lowercaseKeys(props); | ||
} | ||
@@ -9,0 +10,0 @@ util.inherits(Table, BaseTable); |
@@ -11,3 +11,3 @@ { | ||
"description": "Relational database metadata extraction library", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"license": "MIT", | ||
@@ -32,4 +32,4 @@ "main": "./lib/db-meta.js", | ||
"scripts": { | ||
"test": "mocha" | ||
"test": "mocha test/*.js test/**/*.js" | ||
} | ||
} |
# db-meta | ||
Relational database metadata extraction library, currently supporting only PostgreSQL. | ||
Relational database metadata extraction library | ||
The following databases are currently supported: | ||
* SQLite3 - via: [node-sqlite3](https://github.com/developmentseed/node-sqlite3) | ||
* MySQL - via: [node-mysql](https://github.com/felixge/node-mysql) | ||
* PostgreSQL - via: [node-postgres](https://github.com/brianc/node-postgres) | ||
## Installation | ||
@@ -97,5 +103,12 @@ | ||
Returns the max length of the column, if constrained | ||
Returns the max length of the column, if constrained. Only valid for textual columns. | ||
### Column.isPrimaryKey() | ||
Returns true if the column is the primary key, false otherwise | ||
### Column.getDefaultValue() | ||
Returns the default value for the column | ||
## License | ||
@@ -102,0 +115,0 @@ |
@@ -19,3 +19,3 @@ var expect = require('chai').expect; | ||
done(); | ||
}) | ||
}); | ||
@@ -45,20 +45,20 @@ it('should implement the getName method', function (done) { | ||
it('should implement the getDataType method', function(done) { | ||
expectDataType('INTEGER', ['integer', 'int', 'int4']); | ||
expectDataType('BOOLEAN', ['boolean', 'bool']); | ||
expectDataType('TEXT', ['text']); | ||
expectDataType('VARCHAR', ['varchar', 'character varying']); | ||
expectDataType('FLOAT', ['real', 'float4']); | ||
expectDataType('DOUBLE', ['double precision', 'float8']); | ||
expectDataType('TIME', ['time', 'timetz']); | ||
expectDataType('TIMESTAMP', ['timestamp', 'timestamptz']); | ||
expectDataType('FOO', ['foo']); | ||
var c = new Column({ column_name: 'col', data_type: 'integer' }); | ||
expect(c.getDataType()).to.equal('INTEGER'); | ||
done(); | ||
}); | ||
}); | ||
function expectDataType(outputType, metaTypes) { | ||
metaTypes.forEach(function (type) { | ||
var c = new Column({ column_name: 'col', data_type: type }); | ||
expect(c.getDataType()).to.equal(outputType); | ||
it('should implement the isPrimaryKey method', function(done) { | ||
var c = new Column({ column_name: 'col', primary_key: true }); | ||
expect(c.isPrimaryKey()).to.be.true; | ||
var c = new Column({ column_name: 'col', primary_key: false }); | ||
expect(c.isPrimaryKey()).to.be.false; | ||
done(); | ||
}); | ||
} | ||
it('should implement the getDefaultValue method', function(done) { | ||
var c = new Column({ column_name: 'col', column_default: '30' }); | ||
expect(c.getDefaultValue()).to.equal('30'); | ||
done(); | ||
}); | ||
}); |
@@ -8,7 +8,7 @@ var expect = require('chai').expect; | ||
before(function(done) { | ||
pg.connect({ database: 'db-meta-test' }, onConnect); | ||
pg.connect({ database: 'db_meta_test' }, onConnect); | ||
function onConnect(err, dbDriver) { | ||
driver = dbDriver; | ||
driver.client.query('CREATE TABLE person (id INTEGER PRIMARY KEY, name VARCHAR(255) NOT NULL, email VARCHAR(100), age INTEGER);', 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);', done); | ||
} | ||
@@ -58,2 +58,3 @@ }); | ||
expect(idColumn.getMaxLength()).to.be.null; | ||
expect(idColumn.isPrimaryKey()).to.be.true; | ||
@@ -65,3 +66,4 @@ var nameColumn = getColumnByName(columns, 'name'); | ||
expect(nameColumn.getMaxLength()).to.equal(255); | ||
expect(nameColumn.getDataType()).to.equal('VARCHAR'); | ||
expect(nameColumn.getDataType()).to.equal('CHARACTER VARYING'); | ||
expect(nameColumn.isPrimaryKey()).to.be.false; | ||
@@ -73,4 +75,8 @@ var emailColumn = getColumnByName(columns, 'email'); | ||
expect(emailColumn.getMaxLength()).to.equal(100); | ||
expect(emailColumn.getDataType()).to.equal('VARCHAR'); | ||
expect(emailColumn.getDataType()).to.equal('CHARACTER VARYING'); | ||
expect(emailColumn.isPrimaryKey()).to.be.false; | ||
var ageColumn = getColumnByName(columns, 'age'); | ||
expect(ageColumn.getDefaultValue()).to.equal('30'); | ||
done(); | ||
@@ -77,0 +83,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
33153
28
787
137
1