How to access mysql using javascript? - javascript

I know that most people say, "OH! It's not possible, use php" or whatever...
That would be a waste of my time. I don't want to hear that it's not possible.
I'm looking for anyway possible that I can access mysql using javascript. I don't care how much of a drawn out process it is or that it would take node.js.
Just give me a way to do it. Node.js or AJAX is something I'm willing to look into, but I'd rather just use javascript and nothing else.
I do know PHP, Node.js, and AJAX, so I'm not looking for an easy way out. I just want to find out how.
* edit *
I guess this would be more of what I am looking for:
Is there any other types of sql or some sort of database that is accessable by in browser javascript?

You should checkout https://mongolab.com/home They provide full access to a NoSQL database using JSON objects which are directly accessible with jQuery calls. I saw these guys at a Hackathon and ended up winning a prize! They are cool and will probably give you direct help if you click the support link.

Certainly possible. Web Application servers best suit that role, e.g. PHP, Cold Fusion, RubyOnRails, java(JSP), .net(ASP), etc.
You use javascript to send a request that the application server than uses to access the mysql server and... usually server up some of the results in a web page :)

To connect mysql using javascript, you can use the mysql package from npm.
npm i mysql
import mysql from "mysql";
var connection = mysql.createConnection({
host: "localhost",
user: "root",
password: "",
database: "social_media",
});
connection.connect();
const getUserById = (id) => {
connection.query(`SELECT * FROM users where id = ${id}`, function (err, results, fields) {
if (err) throw err;
console.log("The solution is: ", results);
});
};
getUserById(7);
connection.end();

Related

How can I reference data from MariaDB on web browser JS application?

I am trying to create a webpage that uses data from a MariaDB. My current idea (which has been giving me a lot of trouble) is to just connect to the database from the app.js file, which is the main script for my index.html.
const dotenv = require("dotenv");
dotenv.config();
const mariadb = require("mariadb");
const pool = mariadb.createPool({
database: process.env.DATABASE,
host: process.env.HOST,
user: process.env.USER_TOKEN,
password: process.env.PASSWORD,
});
// the rest of the code involves selecting from the db, and parsing the data
However, I have been running into many issues. I'm not too knowledgeable on all this, but I found that I need to webpack the file if I want to be able to use the "require" keyword. But I could not figure that out as I kept running into weird issues when using Browserify; I think there may be an incompatibility with MariaDB. I also looked into using JS modules, but I am not sure if that is possible with MariaDB.
I am trying to come up with another solution, potentially using some sort of API to a back end, which would make the GET request to the database, but I feel like it should not have to be that complicated for my sake (I also wouldn't really know where to start with this). All I basically want to do, is make a GET request to a MariaDB when the page loads on the client's browser and display that data on the webpage. Is there a simple way to do this?
I suggest you use nodejs to connect and query database as it will greatly resolve a lot of overhead for you..
The easiest way i can think of is using a prisma starter template here
https://github.com/prisma/prisma-examples/tree/latest/javascript/script
It also gives the added advantage of the ORM function...
Hope it helps.

Have multiple production environment for multiple customers in NodeJS Server

After researching a lot I can't find anything similar to help me solve this problem.
I have a Node server, with several environments (dev, test, demo, prod). This server is deployed in production on a Linux server, via a service.
I need to be able to have several production environments for several different customers.
Example:
I have 2 urls: https://customer1.com and https://customer2.com.
The code of these two clients are identical, only the url changes.
For the server, it must be able to recognize which client is sending it a request, because the data to be sent back to the client is not the same.
The customer1 will have its database on a different url than that of customer2. The server will therefore have to make the distinction in order to return only the data concerning the client making the request.
My question: how to achieve this?
I would like to avoid deploying 1 server per client, which I know would be simpler, but less maintainable.
Currently, I am using Express-Session to define environments.
In fact, I have a middleware which will look in mysql for the environment variables of each client:
con.connect(function(err) {
if (err) throw err;
con.query(`SELECT * FROM environments WHERE CLIENT_URL = '${req.headers.origin}'`, function(err, result) {
if (err) throw err;
delete result[0].ID;
for (var prop in result[0]) {
req.session[prop] = result[0][prop];
}
next();
});
con.end();
});
It seems to work but it doesn't seem very stable or very reliable to me, am I wrong?
What better can I use to separate the data so that there is no customer1 that can receive the data from customer2?
Thank you for your help!
Following all comments under your original post, you need to do something like this:
SELECT * FROM environments WHERE CLIENT_URL = '${req.headers.origin}' AND CUSTOMER_NAME
LIKE yourUserCustomerFromSession
Before, any user could query data for any customer as long as they use the URL for that customer, now this is no longer possible.
Even better way of doing it, if you don't want to hold the Client name in the session, you can do 2 queries - the first one to get the Client name for the logged in User and the second one similar to the code above:
SELECT * FROM environments WHERE CLIENT_URL = '${req.headers.origin}' AND
CUSTOMER_NAME LIKE theClientNameYouJustGotForTheLoggedInUser

I am very green when it comes to node.js integration

So I figured the best way to learn is to try and fail over and over. I am building a webapp, at least trying to. I am curious how to go about using node to query my db. I am able to make a connection to the db with my single app.js file.
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'xxxxxxxx-us-east-1.rds.amazonaws.com',
port : '3306',
user : 'user',
password : 'password',
database : 'app'
});
connection.connect(function(err){
if(!err) {
console.log("Database is connected ... ");
} else {
console.log("Error connecting database ... ");
}
});
My problem, or lack of understand begins when I try to integrate this into my client-side js code. For instance say I wanted to trigger the db connection when a user uploads a photo.
var upload = s3.putObject({
Bucket: albumBucketName,
Key: photoKey,
Body: file,
ACL: "public-read",
});
var promise = upload.promise();
Can I include the app.js node file?
Sorry if this is a dumb question. I feel like I am missing some fundamental understanding of how to integrate the functionality of node with my current client side JS. Any help or further reading is appreciated--I am even curious about PHP solutions.
X
Server and client side code are separate. However you can create a Node module that harnesses the AWS and returns an appropriate response to the client after completed.
To do this, you need to create an endpoint that you post your data to from the client, then process with the same AWS modules only for Node. You also need to be able to access the connection instance from a different NodeJS module. This can be accomplished several ways. First, if the library that instantiates the connection tracks all of the connections, you should be able to require the library in a different module, then use the library's API to access one of the connections. Second, if you create only one instance of the connection and allow it to export, then you can import the module with that connection. Third, you can use something like a request/response pattern between the two modules, with the pattern instance declared globally.

What is the right way to manage connections to mongoDB, using node?

I'm using node.js and mongoDB. Right now, for my test app, the connection to the db is in the main node file, but I guess this is a wrong practice.
What I want/need: a secure way (i.e. not storing password on files users can access) to connect to the db just when needed.
For example: I want several admin pages (users, groups, etc..). Each page should connect to the db, find some data, and display it. It also have a form for adding a document to the db and a delete option.
I thought maybe to create some kind of a connection function - send it what you want to do (add, update, find, delete), to where (collection name) and whatever it needs. But I can't just include this function, because then it'll reveal the password to the db. So what can I do?
Thanks!
I'm going to answer your question bit by bit.
Right now, for my test app, the connection to the db is in the main node file
This is fine, though you might want to put it in a separate file for easier reuse. NodeJS is a continuesly running process, so in theory you could serve all of your HTTP responses using the same connection to the database. In practice you'd want to create a connection pool, but the Mongodb driver for NodeJS already does this automatically.
Each page should connect to the db, find some data, and display it.
When you issue a query on the MongoDB driver, it will automatically use a connection from its internal connection pool, as long as you gave it the credentials when your application was starting up.
What I want/need: a secure way (i.e. not storing password on files users can access) to connect to the db just when needed.
I would advice to keep your application configuration (any variables that depend on the environment in which the app is running) in a separate file which you don't commit to your VCS. A module like node-config can help a great deal with that.
The code you will end up with, using node-config, is something like:
config/default.json:
{
"mongo": null
}
This is the default configuration file which you commit.
config/local.json:
{
"mongo": "mongo://user:pass#host:port/db"
}
The local.json should be ignored by your VCS. It contains secret sauce.
connection.js:
var config = require('config');
var MongoClient = require('mongodb').MongoClient;
var cache;
module.exports = function(callback){
if(cache){
return callback(cache);
}
MongoClient.connect(config.get('mongo'), function(err, db){
if(err){
console.error(err.stack);
process.exit(1);
}
cache = db;
callback(db);
});
}
An incomplete example of how you might handle reusing the database connection. Note how the configuration is gotten using config.get(*). An actual implementation should have more robust error handling and prevent multiple connections from being made. Using Promises would make all that a lot easier.
index.js:
var connect = require('./connection');
connect(function(db){
db.find({whatever: true})
});
Now you can just require your database file anywhere you want, and reuse the same database connection, which handles pooling for you and you don't have your passwords hard-coded anywhere.

Viewing Node.js code

I'm new to web development. I'm writing an application with Node.js and want to use MySQL. I'm not sure how web technology works, but would people visiting my site be able to see my database credentials? According to the MySQL documentation (https://github.com/felixge/node-mysql) this is how to connect to a MySQL database.
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'me',
password : 'secret'
});
connection.connect();
connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
if (err) throw err;
console.log('The solution is: ', rows[0].solution);
});
connection.end();
No, they won't be able to see your code unless you run this on the client, which you shouldn't be able to or should, at all.
The example above runs on the backend and the query and data get displayed in the console, so it will only be accessible from the server and no clients connecting to it should be able see it, also you are not using anything to display a website ( based on the example above ) so you should not worry about it. If you want to, check out express or the http library for node.js.
After a while you should get the hang of it. Good luck!
People won't see your database credentials unless you publish the file in plain .js rather than executing it in node.js.
However, as an extra security measure, please prevent your database from connections outside the server, such as setting up a firewall to prevent port 3306 from outside connections, and use only 'localhost' users in your mysql database.

Categories

Resources