snippets_js
Integration
New integration from credential
const mysqlClient = yepcode.integration.mysql('credential-slug')
New integration from plain authentication data
const mysql = require('mysql2');
const mysqlClient = mysql.createConnection({
user: 'mysql',
password: '1234abcd',
host: 'localhost',
database: 'testdb',
port: 3306,
connectTimeout: 2000
});
Connect
Connect (callback)
mysqlClient.connect((err) => {
if(err){
throw err
}
// Your query here
})
Disconnect
Disconnect (callback)
mysqlClient.end(function(err) {
if(err){
throw err
}
console.log('Connection closed!')
});
SELECT Text Only
SELECT Text only with callback
mysqlClient.query('SELECT id, name, price FROM products', function (error, rows, fields) {
if(error){
console.error(error.stack)
throw error
}
console.log(rows.forEach((row) => console.log(row.name)));
});
SELECT Parametrized
SELECT Parameterized (callback)
mysqlClient.query(
`SELECT id, name, price
FROM products
WHERE price > ? AND stock > ?`,
[100, 200],
function (error, rows, fields) {
if (error) {
console.error(error.stack);
throw error;
}
console.log(rows.forEach((row) => console.log(row.name)));
}
);
SELECT Query Object
SELECT Query object (callback)
const query = {
sql: `SELECT id, name, price
FROM products
WHERE price > ? AND stock > ?`,
timeout: 40000,
values: [100, 200],
};
mysqlClient.query(query, function (error, rows, fields) {
if (error) {
console.error(error.stack);
throw error;
}
console.log(rows.forEach((row) => console.log(row.name)));
});
INSERT Text Only
INSERT (callback)
mysqlClient.query(
"INSERT INTO products(id, name, price, stock) VALUES('12345', 'FOO', 12, 50)",
function (error, results, fields) {
if (error) {
console.error(error.stack);
throw error;
}
});
INSERT Parameterized
INSERT (callback)
mysqlClient.query(
'INSERT INTO products(id, name, price, stock) VALUES(?, ?, ?, ?)',
['12345', 'FOO', 12, 50],
function (error, results, fields) {
if (error) {
console.error(error.stack);
throw error;
}
});
INSERT Query object (callback)
const query = {
sql: 'INSERT INTO products(id, name, price, stock) VALUES(?, ?, ?, ?)',
timeout: 40000,
values: ['12345', 'FOO', 12, 50]
};
mysqlClient.query(query, function (error, results, fields) {
if (error) {
console.error(error.stack);
throw error;
}
});