Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const main = (event, context, callback) => {
// This is the main execution scope. All non-system require(...)
// statements must be put here.
// This missing module is normally not allowed for inline lambda. But
// autorequire will install the module automatically for us.
const mysql = require('mysql2/promise');
// Create pool of database connections.
const pool = mysql.createPool({
host: 'iotdb.culga9y9tfvw.us-west-2.rds.amazonaws.com',
user: 'root',
password: 'iotattp4me',
database: 'iotdb',
});
function handler(input, context2, callback2) {
// This is the main program flow after resolving the missing modules.
if (input.domain) delete input.domain; // TODO: Contains self-reference loop.
console.log('Input:', JSON.stringify(input, null, 2));
console.log('Context:', context2);
// Don't index response to set desired state.
if (input.state && input.state.desired) {
return callback2(null, 'Ignoring response to set desired state');
}
let device = null;
console.log('after second sleep');
let start = +new Date();
console.log(
await Promise.all([
c.execute('select sleep(2.5)'),
c.execute('select sleep(2.5)')
])
);
console.log(
'after 2+3 parallel sleep which is in fact not parallel because commands are queued per connection'
);
let end = +new Date();
console.log(end - start);
await c.end();
const p = mysql.createPool({
port: 3306,
user: 'testuser',
namedPlaceholders: true,
password: 'testpassword'
});
console.log(await p.execute('select sleep(0.5)'));
console.log('after first pool sleep');
start = +new Date();
console.log(
await Promise.all([
p.execute('select sleep(2.5)'),
p.execute('select sleep(2.5)')
])
);
console.log('after 2+3 parallel pool sleep');
end = +new Date();
public constructor(client: IMClient) {
this.client = client;
for (const db of client.config.databases) {
const range = db.range;
delete db.range;
const pool = mysql.createPool(db);
for (let i = range.from; i <= range.to; i++) {
this.pools.set(i, pool);
}
this.dbCount = Math.max(this.dbCount, range.to);
}
console.log(`We're connected to ${this.dbCount} db shards on ${client.config.databases.length} different servers`);
setInterval(() => this.syncDB(), 10000);
}
_cmdExecStmnt(requestId, sqlStr, inputParams) {
if (!this.pool) {
this.pool = mysql.createPool(Object.assign(this.options, this.poolConfig));
}
return this.pool.query(sqlStr.toString(), inputParams).then(([rows, fields]) => {
return {rowCount: rows.length, rows: rows};
});
}
function getDatabase() {
return require("mysql2/promise").createPool({
host: process.env.DATABASE_HOSTNAME || "localhost",
user: process.env.DATABASE_USERNAME || "root",
password: process.env.DATABASE_PASSWORD || "",
database: process.env.OJP_DATABASE_NAME || "raptor",
connectionLimit: 5,
});
}
return module.exports = (databaseConfig) => mysql.createPool(databaseConfig);
constructor(config) {
super(config);
this.db_config = {
connectionLimit: 10,
host:this.config._target.host,
port:this.config._target.port,
user:this.config._target.user,
password:this.config._target.password,
database:this.config._target.database
};
this.pool = mysql.createPool(this.db_config);
}
async createPool(): Promise {
return mysql2.createPool(this.getMysqlOptions());
}
const mysql = require('mysql2/promise');
const pool = mysql.createPool({
host: '',
user: '',
password: '',
database: '',
});
module.exports = pool;
constructor() {
try {
const pool: mysql.Pool = mysql.createPool({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
})
if (pool) logger.verbose('Database OK')
this.pool = pool
} catch (err) {
logger.error('Database NOT OK')
}
}