diff --git a/index.js b/index.js index 583d790..f4e8de1 100644 --- a/index.js +++ b/index.js @@ -5,9 +5,9 @@ module.exports = pgMetadata module.exports.createQuery = createQuery module.exports.createMetadataObject = createMetadataObject var informationSchemaFields = module.exports.informationSchemaFields = [ - 'column_name', - 'udt_name', - 'data_type', + 'column_name', + 'udt_name', + 'data_type', 'character_maximum_length', 'table_name', 'table_schema', @@ -49,11 +49,11 @@ function pgMetadata(connection, options, callback) { connection.query(query, function (err, result) { if (err) { callback(err) - } else { + } else { var schema = createMetadataObject(result.rows) debug(schema) - callback(null, schema) - } + callback(null, schema) + } }) } @@ -73,7 +73,7 @@ function createQuery(opts) { if (opts.schema) { whereClause.push('table_schema=%L') values.push(opts.schema) - } + } if (opts.database) { whereClause.push('table_catalog=%L') @@ -92,7 +92,7 @@ function createMetadataObject(resultSet) { for (var i = 0; i < resultSet.length; i++) { var row = resultSet[i] - + var key = row.column_name var table = row.table_name var schema = row.table_schema @@ -106,18 +106,18 @@ function createMetadataObject(resultSet) { var schema = database[row.table_schema] if (!schema) { - database[row.table_schema] = schema = {} + database[row.table_schema] = schema = {} } var table = schema[row.table_name] - + if (!table) { schema[row.table_name] = table = {} } var s = table[key] = { type: row.udt_name, - required: row.is_nullable ? true : false + required: row.is_nullable === 'NO' || row.is_nullable === false } if (s.type.indexOf('char')>=0 || s.type.indexOf('text')>=0) { diff --git a/test.js b/test.js index 44675c6..676aff6 100644 --- a/test.js +++ b/test.js @@ -14,7 +14,7 @@ describe('pg-metadata', function () { table_name: 'atable', table_schema: 'aschema', table_catalog: 'adb', - is_nullable: true, + is_nullable: false, numeric_precision: null, numeric_scale: null, numeric_precision_radix: null, @@ -22,7 +22,7 @@ describe('pg-metadata', function () { interval_type: null, interval_precision: null }, - { + { character_maximum_length: 244, column_name: 'b', udt_name: 'varchar', @@ -71,6 +71,7 @@ describe('pg-metadata', function () { table_name: 'ctable', table_schema: 'cschema', table_catalog: 'cdb', + is_nullable: 'NO', numeric_precision: 24, numeric_scale: null, numeric_precision_radix: 2, @@ -84,6 +85,7 @@ describe('pg-metadata', function () { table_name: 'ctable', table_schema: 'cschema', table_catalog: 'cdb', + is_nullable: 'YES', numeric_precision: 12, numeric_scale: 2, numeric_precision_radix: 10, @@ -182,7 +184,7 @@ describe('pg-metadata', function () { describe('creates a metadata object from a query result set that contains all', function () { it('the databases', function () { var actual = pgMetadata.createMetadataObject(resultSet) - + actual.should.have.property('adb') actual.should.have.property('bdb') actual.should.have.property('cdb') @@ -192,7 +194,7 @@ describe('pg-metadata', function () { it('schemas in a database', function () { var actual = pgMetadata.createMetadataObject(resultSet) - + actual.adb.should.have.property('aschema') actual.bdb.should.have.property('bschema') actual.cdb.should.have.property('cschema') @@ -202,7 +204,7 @@ describe('pg-metadata', function () { it('tables in each schema', function () { var actual = pgMetadata.createMetadataObject(resultSet) - + actual.adb.aschema.should.have.property('atable') actual.adb.aschema.should.have.property('btable') @@ -211,7 +213,7 @@ describe('pg-metadata', function () { it('columns in each table', function () { var actual = pgMetadata.createMetadataObject(resultSet) - + actual.adb.aschema.atable.should.have.property('a') actual.adb.aschema.atable.should.have.property('b') actual.bdb.bschema.btable.should.have.property('a') @@ -219,7 +221,7 @@ describe('pg-metadata', function () { it('types of data and length for each columns', function () { var actual = pgMetadata.createMetadataObject(resultSet) - + actual.adb.aschema.atable.a.should.have.property('type', 'varchar') actual.adb.aschema.atable.a.should.have.property('length', 244) actual.adb.aschema.atable.a.should.not.have.property('scale') @@ -236,7 +238,7 @@ describe('pg-metadata', function () { actual.cdb.should.be.eql({ cschema: { ctable: { - a: { type: 'float4', required: false, precision: 24, scale: null, precision_radix: 2 }, + a: { type: 'float4', required: true, precision: 24, scale: null, precision_radix: 2 }, b: { type: 'numeric', required: false, precision: 12, scale: 2, precision_radix: 10 } } } @@ -269,7 +271,7 @@ describe('pg-metadata', function () { .which.have.property('atable') .which.have.property('a') .which.have.property('type', 'varchar') - + done() }) }) @@ -285,7 +287,7 @@ MockConnection.prototype.query = function(q, cb) { setImmediate(function () { if (self.error) cb(new Error('lalala')) - else + else cb(null, { rows: self.resultSet }) }) }