How to use mysql

Comprehensive mysql code examples:

How to use mysql.default:

28
29
30
31
32
33
34
35
36
37
exports.getUserStats = exports.deleteComment = exports.insertComment = exports.getComments = exports.getResource = exports.getItemsByCategory = exports.getSearchItems = exports.deleteResource = exports.updatePendingStatus = exports.getPendingSubmittals = exports.insertRersource = exports.getUserByEmail = exports.getUserById = exports.insertUser = exports.connection = void 0;
const dotenv = __importStar(require("dotenv"));
const mysql_1 = __importDefault(require("mysql"));
const uuid_1 = require("uuid");
dotenv.config();
exports.connection = mysql_1.default.createConnection(process.env.DATABASE_URL);
const insertUser = (firstName, lastName, email, hashedPassword, dateJoined, role) => {
    return new Promise((resolve, reject) => {
        const userId = (0, uuid_1.v4)();
        const sql = "INSERT INTO person (id, first_name, last_name, email, password, date_joined, role) VALUES (?, ?, ?, ?, ?, ?, ?)";

How to use mysql.raw:

229
230
231
232
233
234
235
236
237
238
239
240
        const query = mysql.format(`DELETE FROM ?? WHERE ?`, [table, where])
        return await this.query(query, {})
    }


    raw(rawQuery) {
        return mysql.raw(rawQuery)
    }


}

How to use mysql.createQuery:

111
112
113
114
115
116
117
118
119
120
    t.end()
  })
  var sql = 'SELECT 1 + 1 AS solution'
  factory(function () {
    agent.startTransaction('foo')
    var query = mysql.createQuery(sql, basicQueryCallback(t))
    queryable.query(query)
    t.equal(agent.currentSpan, null, 'mysql span should not bleed into calling code')
  })
})

How to use mysql.escapeId:

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) {

How to use mysql.createPoolCluster:

538
539
540
541
542
543
544
545
546
547
    cluster.end()
    cluster = undefined
  }
}

var cluster = mysql.createPoolCluster()
cluster.add(connectionOptions)
cluster.getConnection(function (err, conn) {
  if (err) throw err
  queryable = conn

How to use mysql.format:

41
42
43
44
45
46
47
48
49
50
if (err) throw err;

// search for existing users in db
const sqlSearch =
  "SELECT * FROM accounts WHERE username = ? AND activestatus = 'active'";
const search_query = mysql.format(sqlSearch, [username]);

await connection.query(search_query, async (err, result) => {
  // connection.release(); // to prevent pool from being depleted
  if (err) throw err;

How to use mysql.createPool:

504
505
506
507
508
509
510
511
512
513
      pool.end()
      pool = undefined
    }
  }

  var pool = mysql.createPool(connectionOptions)
  queryable = pool

  cb()
})

How to use mysql.escape:

63
64
65
66
67
68
69
70
71
72
});

data = "SELECT * " +
    "FROM users " +
    "WHERE username = " + mysql.escape(req.body.name) +
    "AND password = " + mysql.escape(req.body.pass);


connectDB.query(data, (err, result) => {
    if (err) throw err;

How to use mysql.createConnection:

488
489
490
491
492
493
494
495
496
497
      queryable.end()
      queryable = undefined
    }
  }

  queryable = mysql.createConnection(connectionOptions)
  queryable.connect()

  cb()
})