Comparing version 0.8.0 to 0.8.1
@@ -8,3 +8,6 @@ /* SQB | ||
*/ | ||
/** | ||
* Module dependencies. | ||
* @private | ||
*/ | ||
const assert = require('assert'); | ||
@@ -24,88 +27,95 @@ | ||
/** | ||
* @class | ||
* @public | ||
* Expose `DatabaseMetaData`. | ||
*/ | ||
class DatabaseMetaData { | ||
/** | ||
* | ||
* @param {DbPool|Connection} dbobj | ||
*/ | ||
constructor(dbobj) { | ||
this.dbobj = dbobj; | ||
} | ||
module.exports = DatabaseMetaData; | ||
select(...columns) { | ||
return new MetaDataSelect(this.dbobj, columns); | ||
} | ||
/** | ||
* @param {DbPool|Connection} dbobj | ||
* @constructor | ||
*/ | ||
function DatabaseMetaData(dbobj) { | ||
this.dbobj = dbobj; | ||
} | ||
class MetaDataSelect { | ||
const proto = DatabaseMetaData.prototype = {}; | ||
proto.constructor = DatabaseMetaData; | ||
constructor(dbobj, columns) { | ||
this.dbobj = dbobj; | ||
this.dbpool = dbobj.isConnection ? dbobj.dbpool : dbobj; | ||
this._columns = columns; | ||
} | ||
proto.select = function(column) { | ||
const o = Object.create(MetaDataSelect.prototype); | ||
MetaDataSelect.apply(o, arguments); | ||
return o; | ||
}; | ||
from(tableName) { | ||
tableName = String(tableName); | ||
assert(['schemas', 'tables', 'columns', 'primary_keys', | ||
'foreign_keys'].includes(tableName | ||
.toLowerCase()), 'Unknown table "' + tableName + '"'); | ||
let subSelect; | ||
let sourceFields; | ||
const listFields = []; | ||
/** | ||
* | ||
* @param {Object} dbobj | ||
* @param {...*} columns | ||
* @constructor | ||
*/ | ||
function MetaDataSelect(dbobj, columns) { | ||
this.dbobj = dbobj; | ||
this.dbpool = dbobj.isConnection ? dbobj.dbpool : dbobj; | ||
this._columns = columns; | ||
} | ||
const plugin = this.dbpool.nastedPool; | ||
if (tableName === 'schemas') { | ||
assert(typeof plugin.metaDataSelectSchemas === 'function', | ||
'Connecting plugin does not support "metaDataSelectSchemas"'); | ||
const proto2 = MetaDataSelect.prototype = {}; | ||
proto2.constructor = MetaDataSelect; | ||
sourceFields = SchemaFields; | ||
subSelect = plugin.metaDataSelectSchemas(this.dbobj); | ||
proto2.from = function(tableName) { | ||
tableName = String(tableName); | ||
assert(['schemas', 'tables', 'columns', 'primary_keys', | ||
'foreign_keys'].includes(tableName | ||
.toLowerCase()), 'Unknown table "' + tableName + '"'); | ||
var subSelect; | ||
var sourceFields; | ||
const listFields = []; | ||
} else if (tableName === 'tables') { | ||
assert(typeof plugin.metaDataSelectTables === 'function', | ||
'Connecting plugin does not support "metaDataSelectTables"'); | ||
sourceFields = TableFields; | ||
subSelect = plugin.metaDataSelectTables(this.dbobj); | ||
const plugin = this.dbpool.nastedPool; | ||
if (tableName === 'schemas') { | ||
assert(typeof plugin.metaDataSelectSchemas === 'function', | ||
'Connecting plugin does not support "metaDataSelectSchemas"'); | ||
} else if (tableName === 'columns') { | ||
assert(typeof plugin.metaDataSelectColumns === 'function', | ||
'Connecting plugin does not support "metaDataSelectColumns"'); | ||
sourceFields = ColumnFields; | ||
subSelect = plugin.metaDataSelectColumns(this.dbobj); | ||
sourceFields = SchemaFields; | ||
subSelect = plugin.metaDataSelectSchemas(this.dbobj); | ||
} else if (tableName === 'primary_keys') { | ||
assert(typeof plugin.metaDataSelectPrimaryKeys === 'function', | ||
'Connecting plugin does not support "metaDataSelectPrimaryKeys"'); | ||
sourceFields = PrimaryKeyFields; | ||
subSelect = plugin.metaDataSelectPrimaryKeys(this.dbobj); | ||
} else if (tableName === 'tables') { | ||
assert(typeof plugin.metaDataSelectTables === 'function', | ||
'Connecting plugin does not support "metaDataSelectTables"'); | ||
sourceFields = TableFields; | ||
subSelect = plugin.metaDataSelectTables(this.dbobj); | ||
} else if (tableName === 'foreign_keys') { | ||
assert(typeof plugin.metaDataSelectForeignKeys === 'function', | ||
'Connecting plugin does not support "metaDataSelectForeignKeys"'); | ||
sourceFields = ForeignKeyFields; | ||
subSelect = plugin.metaDataSelectForeignKeys(this.dbobj); | ||
} | ||
} else if (tableName === 'columns') { | ||
assert(typeof plugin.metaDataSelectColumns === 'function', | ||
'Connecting plugin does not support "metaDataSelectColumns"'); | ||
sourceFields = ColumnFields; | ||
subSelect = plugin.metaDataSelectColumns(this.dbobj); | ||
for (let col of this._columns) { | ||
col = String(col).toLowerCase(); | ||
if (sourceFields.includes(col) && !listFields.includes(col)) | ||
listFields.push(col); | ||
} | ||
} else if (tableName === 'primary_keys') { | ||
assert(typeof plugin.metaDataSelectPrimaryKeys === 'function', | ||
'Connecting plugin does not support "metaDataSelectPrimaryKeys"'); | ||
sourceFields = PrimaryKeyFields; | ||
subSelect = plugin.metaDataSelectPrimaryKeys(this.dbobj); | ||
const query = this.dbobj | ||
.select(...listFields) | ||
.from(subSelect); | ||
if (subSelect._onfetchrow && subSelect._onfetchrow.length) | ||
for (const fn of subSelect._onfetchrow) | ||
query.onFetchRow(fn); | ||
return query; | ||
} else if (tableName === 'foreign_keys') { | ||
assert(typeof plugin.metaDataSelectForeignKeys === 'function', | ||
'Connecting plugin does not support "metaDataSelectForeignKeys"'); | ||
sourceFields = ForeignKeyFields; | ||
subSelect = plugin.metaDataSelectForeignKeys(this.dbobj); | ||
} | ||
} | ||
this._columns.forEach(function(col) { | ||
col = String(col).toLowerCase(); | ||
if (sourceFields.includes(col) && !listFields.includes(col)) | ||
listFields.push(col); | ||
}); | ||
module.exports = DatabaseMetaData; | ||
const query = this.dbobj.select.apply(this.dbobj, listFields) | ||
.from(subSelect); | ||
if (subSelect._onfetchrow && subSelect._onfetchrow.length) | ||
subSelect._onfetchrow.forEach(function(fn) { | ||
query.onFetchRow(fn); | ||
}); | ||
return query; | ||
}; | ||
{ | ||
"name": "sqb", | ||
"description": "Plugin-driven, multi-dialect SQL query builder and Database connection framework for JavaScript", | ||
"version": "0.8.0", | ||
"version": "0.8.1", | ||
"author": "Panates Ltd.", | ||
@@ -23,7 +23,8 @@ "contributors": [ | ||
"dependencies": { | ||
"debug": "^3.1.0", | ||
"errorex": "^1.0.5", | ||
"putil-flattentext": "^1.0.1", | ||
"putil-flattentext": "^1.1.1", | ||
"putil-isplainobject": "^1.0.1", | ||
"putil-promisify": "^1.1.0", | ||
"putil-taskqueue": "^1.0.3", | ||
"putil-taskqueue": "^1.1.0", | ||
"putil-waterfall": "^1.1.0" | ||
@@ -30,0 +31,0 @@ }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
106456
3553
7
+ Addeddebug@^3.1.0
+ Addeddebug@3.2.7(transitive)
+ Addedms@2.1.3(transitive)
Updatedputil-flattentext@^1.1.1
Updatedputil-taskqueue@^1.1.0