sql-client
Version:
A dirt-simple SQL client abstraction (currently) supporting PostgreSQL, MySQL and SQLite.
105 lines (92 loc) • 3.76 kB
JavaScript
// Generated by CoffeeScript 2.6.0
(function() {
var DEBUG, DEBUG_CONNECT, DEBUG_DDL, SQLClient, ref, ref1, ref2,
splice = [].splice;
DEBUG = /(^|,)SQLClient($|,)/i.test(typeof process !== "undefined" && process !== null ? (ref = process.env) != null ? ref.NODE_DEBUG : void 0 : void 0);
DEBUG_DDL = /(^|,)SQLClient\.DDL($|,)/i.test(typeof process !== "undefined" && process !== null ? (ref1 = process.env) != null ? ref1.NODE_DEBUG : void 0 : void 0);
DEBUG_CONNECT = /(^|,)SQLClient\.connect($|,)/i.test(typeof process !== "undefined" && process !== null ? (ref2 = process.env) != null ? ref2.NODE_DEBUG : void 0 : void 0);
SQLClient = class SQLClient {
constructor(...options1) {
var factory, ref3;
ref3 = options1, [...options1] = ref3, [factory] = splice.call(options1, -1);
this.connect = this.connect.bind(this);
this.disconnect = this.disconnect.bind(this);
this.execute = this.execute.bind(this);
this.options = options1;
this.factory = factory;
this.created_at = Date.now();
this.pooled_at = null;
this.borrowed_at = null;
this.connected_at = null;
}
connect(options, callback) {
if (typeof options === "function" && (callback == null)) {
[callback, options] = [options, callback];
}
if (DEBUG_CONNECT) {
console.log(`SQLClient.connect. ? ${this.connection != null}`);
}
if (this.connection == null) {
return this.factory.open_connection(...this.options, (err, connection) => {
if (err != null) {
return typeof callback === "function" ? callback(err) : void 0;
} else {
this.connection = connection;
this.connected_at = Date.now();
return typeof callback === "function" ? callback() : void 0;
}
});
} else {
return typeof callback === "function" ? callback() : void 0;
}
}
disconnect(options, callback) {
if (typeof options === "function" && (callback == null)) {
[callback, options] = [options, callback];
}
if (DEBUG_CONNECT) {
console.log(`SQLClient.disconnect. ? ${this.connection != null}; options:`, options);
}
if (this.connection != null) {
return this.factory.close_connection(this.connection, (err) => {
if (err != null) {
return typeof callback === "function" ? callback(err, this.connection) : void 0;
} else {
this.connection = null;
this.connected_at = null;
return typeof callback === "function" ? callback() : void 0;
}
});
} else {
return typeof callback === "function" ? callback() : void 0;
}
}
execute(sql, bindvars, callback) {
if ((callback == null) && typeof bindvars === 'function') {
callback = bindvars;
bindvars = null;
}
if (this.connection == null) {
return this.connect((err) => {
if (err != null) {
return callback(err);
} else {
return this.execute(sql, bindvars, callback);
}
});
} else {
return this.factory.pre_process_sql(sql, bindvars, (err, sql, bindvars) => {
if (err != null) {
return typeof callback === "function" ? callback(err) : void 0;
} else {
if (DEBUG && (DEBUG_DDL || !(/^\s((create)|(drop))\s/i.test(sql)))) {
console.log("SQLClient executing:", sql, bindvars);
}
return this.factory.execute(this.connection, sql, bindvars, callback);
}
});
}
}
};
exports.SQLClient = SQLClient;
}).call(this);