New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

anydb-sql

Package Overview
Dependencies
Maintainers
1
Versions
84
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

anydb-sql - npm Package Compare versions

Comparing version 0.1.6 to 0.1.7

176

anydb-sql.js

@@ -13,107 +13,127 @@ var anyDB = require('any-db');

module.exports = function (opt) {
module.exports = function (opt) {
var dialect = url.parse(opt.url).protocol;
dialect = dialect.substr(0, dialect.length - 1);
if (dialect == 'sqlite3')
dialect = 'sqlite';
sql.setDialect(dialect);
if (dialect == 'sqlite') {
try {
var sqlitepool = require('./sqlite-pool');
var pool = sqlitepool(opt.url, opt.connections);
} catch (e) {
throw new Error("Unable to load sqlite pool: " + e.message);
}
var dialect = url.parse(opt.url).protocol;
dialect = dialect.substr(0, dialect.length - 1);
if (dialect == 'sqlite3')
dialect = 'sqlite';
sql.setDialect(dialect);
if (dialect == 'sqlite') {
try {
var sqlitepool = require('./sqlite-pool');
var pool = sqlitepool(opt.url, opt.connections);
} catch (e) {
throw new Error("Unable to load sqlite pool: " + e.message);
}
else {
var pool = anyDB.createPool(opt.url, opt.connections);
}
}
else {
var pool = anyDB.createPool(opt.url, opt.connections);
}
var self = {};
var self = {};
function extendedTable(table) {
// inherit everything from a regular table.
var extTable = Object.create(table);
function extendedTable(table) {
// inherit everything from a regular table.
var extTable = Object.create(table);
// make query methods return extended queries.
queryMethods.forEach(function (key) {
extTable[key] = function () {
return extendedQuery(table[key].apply(table, arguments));
}
});
// make query methods return extended queries.
queryMethods.forEach(function (key) {
extTable[key] = function () {
return extendedQuery(table[key].apply(table, arguments));
}
});
// make as return extended tables.
extTable.as = function () {
return extendedTable(table.as.apply(table, arguments));
};
extTable.eventEmitter = new EventEmitter();
return extTable;
}
// make as return extended tables.
extTable.as = function () {
return extendedTable(table.as.apply(table, arguments));
};
extTable.eventEmitter = new EventEmitter();
return extTable;
}
function extendedQuery(query) {
var extQuery = Object.create(query);
var self = extQuery;
self.__extQuery = true;
extQuery.execWithin = function (where, fn) {
var query = self.toQuery(); // {text, params}
if (!fn)
return where.query(query.text, query.values);
else
return where.query(query.text, query.values, function (err, res) {
fn(err, res ? res.rows : null);
});
};
function extendedQuery(query) {
var extQuery = Object.create(query);
var self = extQuery;
extQuery.exec = extQuery.execWithin.bind(extQuery, pool);
self.__extQuery = true;
extQuery.all = extQuery.exec;
extQuery.execWithin = function (where, fn) {
var query = self.toQuery(); // {text, params}
if (!fn)
return where.query(query.text, query.values);
else
return where.query(query.text, query.values, function (err, res) {
fn(err, res && res.rows ? res.rows.map(normalizer) : null);
});
};
extQuery.get = function (fn) {
return this.exec(function (err, rows) {
return fn(err, rows && rows.length ? rows[0] : null);
})
};
extQuery.exec = extQuery.execWithin.bind(extQuery, pool);
queryMethods.forEach(function (key) {
extQuery[key] = function () {
var q = query[key].apply(query, arguments);
if (q.__extQuery) return q;
return extendedQuery(q);
}
});
extQuery.all = extQuery.exec;
return extQuery;
}
extQuery.get = function (fn) {
return this.exec(function (err, rows) {
return fn(err, rows && rows.length ? rows[0] : null);
})
};
queryMethods.forEach(function (key) {
extQuery[key] = function () {
var q = query[key].apply(query, arguments);
if (q.__extQuery) return q;
return extendedQuery(q);
}
});
self.define = function () {
return extendedTable(sql.define.apply(sql, arguments));
};
return extQuery;
}
self.close = pool.close.bind(pool);
self.begin = pool.begin.bind(pool);
self.query = pool.query.bind(pool);
self.define = function () {
return extendedTable(sql.define.apply(sql, arguments));
};
self.allOf = function() {
var tables = [].slice.call(arguments);
return tables.reduce(function (all, table) {
var tableName = table.alias || table._name;
return all.concat(table.columns.map(function(c) {
return c.as(tableName + '.' + c.name);
}));
}, []);
};
self.close = pool.close.bind(pool);
self.begin = pool.begin.bind(pool);
self.query = pool.query.bind(pool);
return self;
self.allOf = function() {
var tables = [].slice.call(arguments);
return tables.reduce(function (all, table) {
var tableName = table.alias || table._name;
return all.concat(table.columns.map(function(c) {
return c.as(tableName + '.' + c.name);
}));
}, []);
};
return self;
};
var normalizer = module.exports.normalizer = function normalizer(row) {
var res = {};
for (var key in row) {
var path = key.split('.'), plen = path.length;
for (var k = 0, obj = res; k < plen - 1; ++k) {
var item = path[k];
if (!obj[item]) obj[item] = {};
obj = obj[item];
}
var item = path[plen - 1];
obj[item] = row[key];
}
return res;
}
{
"name": "anydb-sql",
"version": "0.1.6",
"version": "0.1.7",
"description": "anydb-sql combines node-anydb and node-sql",

@@ -5,0 +5,0 @@ "main": "anydb-sql.js",

@@ -19,2 +19,15 @@ var test = require('tap').test;

test('normalizer', function(t) {
var n = anydbsql.normalizer;
t.test('normalizes well', function(t) {
var normalized = n({'a.b':1, 'a.c': 2, b: 3, 'c.a': 1, 'c.b': 2});
t.deepEquals(normalized, {
a: {b: 1, c: 2},
b: 3,
c: {a: 1, b: 2}
});
t.end();
});
});
test('anydb-sql', function(t) {

@@ -72,6 +85,6 @@

.select(userx.id);
console.log(query.toQuery())
//console.log(query.toQuery())
query.get(function(err, res) {
console.log(err, res);
//console.log(err, res);
t.equals(res.id, 1, 'user.id is 1');

@@ -78,0 +91,0 @@ t.end();

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc