Databse connection examples

master
Juan 3 years ago
parent 4bb439dfff
commit c4b7c5e7c1

@ -0,0 +1,12 @@
CREATE DATABASE IF NOT EXISTS `nodeapi`
DEFAULT CHARACTER SET utf8
DEFAULT COLLATE utf8_general_ci;
GRANT USAGE ON *.* TO "nodeapi"@"localhost" IDENTIFIED BY "RDSGF435FG"
WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;
GRANT USAGE ON *.* TO "nodeapi"@"%" IDENTIFIED BY "RDSGF435FG"
WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;
GRANT ALL PRIVILEGES ON `nodeapi`.* TO 'nodeapi'@'%';
GRANT ALL PRIVILEGES ON `nodeapi`.* TO 'nodeapi'@'localhost';

@ -0,0 +1,81 @@
{
"name": "mariadb-test",
"version": "0.1.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"@types/geojson": {
"version": "7946.0.8",
"resolved": "https://registry.npmjs.org/@types/geojson/-/geojson-7946.0.8.tgz",
"integrity": "sha512-1rkryxURpr6aWP7R786/UQOkJ3PcpQiWkAXBmdWc7ryFWqN6a4xfK7BtjXvFBKO9LjQ+MWQSWxYeZX1OApnArA=="
},
"@types/node": {
"version": "14.17.21",
"resolved": "https://registry.npmjs.org/@types/node/-/node-14.17.21.tgz",
"integrity": "sha512-zv8ukKci1mrILYiQOwGSV4FpkZhyxQtuFWGya2GujWg+zVAeRQ4qbaMmWp9vb9889CFA8JECH7lkwCL6Ygg8kA=="
},
"denque": {
"version": "1.5.1",
"resolved": "https://registry.npmjs.org/denque/-/denque-1.5.1.tgz",
"integrity": "sha512-XwE+iZ4D6ZUB7mfYRMb5wByE8L74HCn30FBN7sWnXksWc1LO1bPDl67pBR9o/kC4z/xSNAwkMYcGgqDV3BE3Hw=="
},
"iconv-lite": {
"version": "0.6.3",
"resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.6.3.tgz",
"integrity": "sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==",
"requires": {
"safer-buffer": ">= 2.1.2 < 3.0.0"
}
},
"long": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/long/-/long-4.0.0.tgz",
"integrity": "sha512-XsP+KhQif4bjX1kbuSiySJFNAehNxgLb6hPRGJ9QsUr8ajHkuXGdrHmFUTUUXhDwVX2R5bY4JNZEwbUiMhV+MA=="
},
"mariadb": {
"version": "2.5.4",
"resolved": "https://registry.npmjs.org/mariadb/-/mariadb-2.5.4.tgz",
"integrity": "sha512-4vQgMRyBIN9EwSQG0vzjR9D8bscPH0dGPJt67qVlOkHSiSm0xUatg1Pft4o1LzORgeOW4PheiY/HBE9bYYmNCA==",
"requires": {
"@types/geojson": "^7946.0.7",
"@types/node": "^14.14.28",
"denque": "^1.5.0",
"iconv-lite": "^0.6.3",
"long": "^4.0.0",
"moment-timezone": "^0.5.33",
"please-upgrade-node": "^3.2.0"
}
},
"moment": {
"version": "2.29.1",
"resolved": "https://registry.npmjs.org/moment/-/moment-2.29.1.tgz",
"integrity": "sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ=="
},
"moment-timezone": {
"version": "0.5.33",
"resolved": "https://registry.npmjs.org/moment-timezone/-/moment-timezone-0.5.33.tgz",
"integrity": "sha512-PTc2vcT8K9J5/9rDEPe5czSIKgLoGsH8UNpA4qZTVw0Vd/Uz19geE9abbIOQKaAQFcnQ3v5YEXrbSc5BpshH+w==",
"requires": {
"moment": ">= 2.9.0"
}
},
"please-upgrade-node": {
"version": "3.2.0",
"resolved": "https://registry.npmjs.org/please-upgrade-node/-/please-upgrade-node-3.2.0.tgz",
"integrity": "sha512-gQR3WpIgNIKwBMVLkpMUeR3e1/E1y42bqDQZfql+kDeXd8COYfM8PQA4X6y7a8u9Ua9FHmsrrmirW2vHs45hWg==",
"requires": {
"semver-compare": "^1.0.0"
}
},
"safer-buffer": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz",
"integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg=="
},
"semver-compare": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/semver-compare/-/semver-compare-1.0.0.tgz",
"integrity": "sha1-De4hahyUGrN+nvsXiPavxf9VN/w="
}
}
}

@ -0,0 +1,18 @@
{
"name": "mariadb-test",
"version": "0.1.0",
"description": "test du connecteur MariaDB",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [
"mariadb",
"test"
],
"author": "",
"license": "ISC",
"dependencies": {
"mariadb": "^2.5.4"
}
}

@ -0,0 +1,79 @@
// testing :
// https://mariadb.com/kb/en/nodejs-connector/
const http = require ('http'); // to create server
const url = require('url'); // to parse url
const BASIC_API_PORT = 3998;
const DB_HOST = "127.0.0.1",
DB_PORT = "3306",
DB_USR = "nodeapi",
DB_PWD = "RDSGF435FG",
DB_NAME = "nodeapi";
const mariadb = require('mariadb');
const pool = mariadb.createPool({
host: DB_HOST,
port: DB_PORT,
user: DB_USR,
password:DB_PWD,
database: DB_NAME,
connectionLimit: 5,
});
//const server = http.createServer(function (req, resp) {
const app = http.createServer
(
async (req, res) =>
{
const parsedURL = url.parse(req.url, true);
var response_text = "";
if(req.method === 'GET' && parsedURL.pathname.startsWith('/api/tables'))
{
console.log('requesting table list');
var cnx = null;
try
{
cnx=await pool.getConnection();
const tables_res = await cnx.query("SHOW TABLES");
console.log(tables_res);
response_text += tables_res.length+" table"+(tables_res.length>1?"s":"")+" found";
//console.log(response_text);
if(tables_res.length > 0)
{
Object.keys(tables_res).forEach(function(key) {
if(key !== 'meta')
{
var row=tables_res[key];
response_text+= "\n - "+row['Tables_in_'+DB_NAME];
}
});
}
}
catch(err)
{
throw(err);
}
finally
{
res.statusCode = 200;
res.end(response_text);
if(cnx)
{
return cnx.end();
}
}
}
else
{
res.statusCode = 400;
res.end("API Endpoint Not Supported");
}
}
);
app.listen(BASIC_API_PORT);
console.log('mariadb test api started');

@ -0,0 +1,81 @@
{
"name": "mysql-test",
"version": "1.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
"bignumber.js": {
"version": "9.0.0",
"resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.0.0.tgz",
"integrity": "sha512-t/OYhhJ2SD+YGBQcjY8GzzDHEk9f3nerxjtfa6tlMXfe7frs/WozhvCNoGvpM0P3bNf3Gq5ZRMlGr5f3r4/N8A=="
},
"core-util-is": {
"version": "1.0.3",
"resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.3.tgz",
"integrity": "sha512-ZQBvi1DcpJ4GDqanjucZ2Hj3wEO5pZDS89BWbkcrvdxksJorwUDDZamX9ldFkp9aw2lmBDLgkObEA4DWNJ9FYQ=="
},
"inherits": {
"version": "2.0.4",
"resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz",
"integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ=="
},
"isarray": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz",
"integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE="
},
"mysql": {
"version": "2.18.1",
"resolved": "https://registry.npmjs.org/mysql/-/mysql-2.18.1.tgz",
"integrity": "sha512-Bca+gk2YWmqp2Uf6k5NFEurwY/0td0cpebAucFpY/3jhrwrVGuxU2uQFCHjU19SJfje0yQvi+rVWdq78hR5lig==",
"requires": {
"bignumber.js": "9.0.0",
"readable-stream": "2.3.7",
"safe-buffer": "5.1.2",
"sqlstring": "2.3.1"
}
},
"process-nextick-args": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz",
"integrity": "sha512-3ouUOpQhtgrbOa17J7+uxOTpITYWaGP7/AhoR3+A+/1e9skrzelGi/dXzEYyvbxubEF6Wn2ypscTKiKJFFn1ag=="
},
"readable-stream": {
"version": "2.3.7",
"resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.7.tgz",
"integrity": "sha512-Ebho8K4jIbHAxnuxi7o42OrZgF/ZTNcsZj6nRKyUmkhLFq8CHItp/fy6hQZuZmP/n3yZ9VBUbp4zz/mX8hmYPw==",
"requires": {
"core-util-is": "~1.0.0",
"inherits": "~2.0.3",
"isarray": "~1.0.0",
"process-nextick-args": "~2.0.0",
"safe-buffer": "~5.1.1",
"string_decoder": "~1.1.1",
"util-deprecate": "~1.0.1"
}
},
"safe-buffer": {
"version": "5.1.2",
"resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz",
"integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g=="
},
"sqlstring": {
"version": "2.3.1",
"resolved": "https://registry.npmjs.org/sqlstring/-/sqlstring-2.3.1.tgz",
"integrity": "sha1-R1OT/56RR5rqYtyvDKPRSYOn+0A="
},
"string_decoder": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz",
"integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==",
"requires": {
"safe-buffer": "~5.1.0"
}
},
"util-deprecate": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz",
"integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8="
}
}
}

@ -0,0 +1,15 @@
{
"name": "mysql-test",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"mysql": "^2.18.1"
}
}

@ -0,0 +1,62 @@
// testing :
// https://www.npmjs.com/package/mysql
const http = require ('http'); // to create server
const url = require('url'); // to parse url
const BASIC_API_PORT = 3998;
const DB_HOST = "127.0.0.1",
DB_PORT = "3306",
DB_USR = "root",
DB_PWD = "root",
DB_NAME = "nodeapi";
const mysql = require('mysql');
const pool = mysql.createPool({
host: DB_HOST,
port: DB_PORT,
user: DB_USR,
password:DB_PWD,
database: DB_NAME,
connectionLimit: 5,
});
response_text = '';
const app = http.createServer(
async (req, res) => {
const parsedURL = url.parse(req.url, true);
if (req.method === 'GET' && parsedURL.pathname.startsWith('/api/tables'))
{
console.log('requesting table list');
pool.getConnection(function (err, conn) {
conn.query("SHOW tables", function (err, rows) {
console.log(rows);
response_text += rows.length + " table" + (rows.length > 1 ? "s" : "") + " found";
//console.log(response_text);
if(rows.length > 0)
{
Object.keys(rows).forEach(function(key) {
if(key !== 'meta')
{
var row=rows[key];
response_text+= "\n - "+row['Tables_in_'+DB_NAME];
}
});
}
});
res.statusCode = 200;
res.end(response_text);
response_text ="";
});
} else {
res.statusCode = 400;
res.end("API Endpoint Not Supported");
}
}
);
app.listen(BASIC_API_PORT);
console.log('mysql test api started');

@ -0,0 +1,20 @@
var response_text="";
const mysql = require('mysql');
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'root',
database: 'nodeapi'
});
connection.connect((err) => {
if (err) throw err;
console.log('Connected to MySQL Server!');
});
connection.query("SHOW tables", function (err, rows) {
console.log(rows);
response_text += rows.length + " table" + (rows.length > 1 ? "s" : "") + " found";
console.log(response_text);
});

@ -0,0 +1,5 @@
{
"name": "npm-tests",
"version": "1.0.0",
"lockfileVersion": 1
}

@ -0,0 +1,12 @@
{
"name": "npm-tests",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {}
}
Loading…
Cancel
Save