Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to manage a db connection in javascript


//put these lines in a seperate file
const mysql = require('mysql2');

const connection = mysql.createPool({
    host: "localhost",
    user: "",
    password: "",
    database: ""
    // here you can set connection limits and so on
});

module.exports = connection;

//put these on destination page
const connection = require('../util/connection');

async function getAll() {
    const sql = "SELECT * FROM tableName";
    const [rows] = await connection.promise().query(sql);
    return rows;
} 
exports.getAll = getAll;
Comment

PREVIOUS NEXT
Code Example
Javascript :: typescript class constructor overload 
Javascript :: embed video by javascript 
Javascript :: javascript override shortcut 
Javascript :: urlencoded limit 
Javascript :: toggle button javascript 
Javascript :: npm add latest version to package json 
Javascript :: javascript password hashing 
Javascript :: Toggle checkbox checking in jquery 
Javascript :: nodejs event 
Javascript :: json objects 
Javascript :: input type styled components 
Javascript :: A <Route is only ever to be used as the child of <Routes element, never rendered directly. Please wrap your <Route in a <Routes. 
Javascript :: remove id attribute javascript 
Javascript :: how to get the data from url in javascript 
Javascript :: js arithmetic operators 
Javascript :: how to replace strings with react components 
Javascript :: js get html content 
Javascript :: count the number of elements in an array javascript 
Javascript :: Group array of strings by first letter 
Javascript :: react scroll 
Javascript :: on() jquery 
Javascript :: linear gradient react js 
Javascript :: object json parse nestjs 
Javascript :: js array backwards 
Javascript :: random coordinates js 
Javascript :: capacitor.ionicframework.com to apk 
Javascript :: round decimal js 
Javascript :: string concatenation javascript 
Javascript :: error An unexpected error occurred: "https://registry.yarnpkg.com/@material-ui/icons/-/icons-4.11.2.tgz: ESOCKETTIMEDOUT". 
Javascript :: angular passing data to child component 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =