mysql: Added .format() overload

Added the .format() overload thats accepts an object for the values
parameter
This commit is contained in:
Alexander Horn
2015-08-24 21:00:14 +02:00
parent c5a2f44bab
commit a22c78d619
2 changed files with 7 additions and 0 deletions

View File

@@ -109,6 +109,10 @@ var sql = "SELECT * FROM ?? WHERE ?? = ?";
var inserts = ['users', 'id', userId];
sql = mysql.format(sql, inserts);
var sql = "INSERT INTO posts SET ?";
var post = { id: 1, title: 'Hello MySQL' };
sql = mysql.format(sql, post);
connection.config.queryFormat = function (query, values) {
if (!values) return query;
return query.replace(/\:(\w+)/g, function (txt: string, key: string) {

3
mysql/mysql.d.ts vendored
View File

@@ -15,6 +15,7 @@ declare module "mysql" {
function escape(value: any): string;
function format(sql: string): string;
function format(sql: string, values: Array<any>): string;
function format(sql: string, values: any): string;
interface IMySql {
createConnection(connectionUri: string): IConnection;
@@ -24,6 +25,7 @@ declare module "mysql" {
escape(value: any): string;
format(sql: string): string;
format(sql: string, values: Array<any>): string;
format(sql: string, values: any): string;
}
interface IConnectionStatic {
@@ -69,6 +71,7 @@ declare module "mysql" {
format(sql: string): string;
format(sql: string, values: Array<any>): string;
format(sql: string, values: any): string;
on(ev: string, callback: (...args: any[]) => void): IConnection;
on(ev: 'error', callback: (err: IError) => void): IConnection;