How to use the escapeId function from mysql

Find comprehensive JavaScript mysql.escapeId code examples handpicked from public code repositorys.

mysql.escapeId is a function in the mysql module that escapes and quotes a MySQL identifier (such as a table or column name) to prevent SQL injection attacks.

2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
                const indexField = indexs.map((index) => `[${index}]`).join('');
                return `${mysql.escapeId(arrayField)}->${mysql.escape(`$${[indexField, ...subKeys].join('.')}`)}`;
            }
            return `${mysql.escapeId(field)}->${mysql.escape(`$.${subKeys.join('.')}`)}`;
        }
        return mysql.escapeId(keys[0]);
    }
    return mysql.escapeId(key);
}
whereClip(where) {
fork icon0
star icon0
watch icon1

41
42
43
44
45
46
47
48
49
50
this.escape = function(val, stringifyObjects, timeZone) {
    return Mysql.escape(val, stringifyObjects, timeZone || connection.config.timezone);
};

this.escapeId = function(val, forbidQualified) {
    return Mysql.escapeId(val, forbidQualified);
};

this.startTransaction = function() {
    Extraload._incrementTasks('mysql');
fork icon0
star icon0
watch icon0

+ 11 other calls in file

How does mysql.escapeId work?

Sure! mysql.escapeId is a function in the mysql module that escapes and quotes a MySQL identifier to prevent SQL injection attacks. In MySQL, an identifier is a name used to identify a database object such as a table, column, or index. Identifiers can be specified using various syntaxes, such as backticks or double quotes. However, if an identifier contains special characters, it can be used to inject malicious SQL code into a query. mysql.escapeId is used to escape and quote an identifier to prevent this from happening. To use mysql.escapeId, you simply call it with a single argument: the identifier you want to escape and quote. mysql.escapeId then returns the escaped and quoted identifier as a string. For example, consider the following code: javascript Copy code {{{{{{{ const mysql = require('mysql'); const tableName = 'users; DROP TABLE users'; const escapedTableName = mysql.escapeId(tableName); console.log(escapedTableName); // '`users; DROP TABLE users`' In this example, we first require the mysql module. We then define a tableName variable containing an identifier that includes a semicolon and a SQL injection attack. We call mysql.escapeId with tableName as the argument. mysql.escapeId escapes the semicolon and quotes the identifier using backticks, and returns the resulting string. The resulting string is 'users; DROP TABLE users', which is the escaped and quoted version of the original tableName variable. This code demonstrates how mysql.escapeId can be used to escape and quote a MySQL identifier to prevent SQL injection attacks.

69
70
71
72
73
74
75
76
77
78

async insert(table, values) {
  const keys = Object.keys(values);
  const valuesList = Object.values(values);

  const sql = `INSERT INTO ${table} (${keys.map(key => mysql.escapeId(key)).join(', ')}) VALUES (${valuesList.map(() => '?').join(', ')})`;

  const result = await this.execute(sql, valuesList);
  return result.insertId;
}
fork icon0
star icon0
watch icon0

Ai Example

1
2
3
4
5
6
const mysql = require("mysql");

const tableName = "users; DROP TABLE users";
const escapedTableName = mysql.escapeId(tableName);

console.log(escapedTableName); // '`users; DROP TABLE users`'

In this example, we first require the mysql module. We then define a tableName variable containing an identifier that includes a semicolon and a SQL injection attack. We call mysql.escapeId with tableName as the argument. mysql.escapeId escapes the semicolon and quotes the identifier using backticks, and returns the resulting string. The resulting string is 'users; DROP TABLE users', which is the escaped and quoted version of the original tableName variable. This code demonstrates how mysql.escapeId can be used to escape and quote a MySQL identifier to prevent SQL injection attacks.