how do i connect an existing database to sequelize.js? - javascript

I have a database I generated through postgres. How do I connect it to sequelize for node.js?
I know the basic syntax for sequelize connections, but how do I connect these together?

In node.js after installing the sequelize and pg packages through npm you can do this:
var sequelize = new Sequelize('your_database_name', 'user', 'password', {
host: "localhost", //your server
port: 12345 //server port
dialect: 'postgres'
});
Note, in order to use Sequelize with Postgres you'll_need the postgres package. The Sequelize homepage contains instructions on changes you might need to make in order to use them together.

Related

Requests to nodejs server deployed on Azure App Service timing out when trying to access Azure Postgres flexible database

I have a node.js express server deployed on Azure App Services, which connects to an Azure flexible postgresql database to serve requests. When running the server locally the postgres database can access the database fine, but when the server is deployed to an azure app service, all requests time out:
The server uses a pool to make requests, this is what my server config file looks like:
const {Pool} = require('pg');
require('dotenv').config();
const config = {
host: process.env.HOST,
user: process.env.USER,
password: process.env.PASSWORD,
database: process.env.DB_NAME,
port: process.env.PORT,
ssl: true,
max: 50,
idleTimeoutMillis: 10000,
allowExitOnIdle: true,
}
module.exports = new Pool(config);
So far I've tried:
Allowing all IP addresses to access the database
Allowing all services within Azure to access the database
Giving the server's App service contributor permissions to the database.
But none of these solutions have prevented requests from timing out, so any help would be greatly appreciated.
Thank you
I have thankfully found the solution to this problem!
I suspect that the Azure App Service overrides the PORT env variable to 8080, and so the port variable used to connect to the database ends up being wrong when a connection is attempted.
I've renamed the .env variable to something else and it is now working correctly.

password authentication failed for user "root", postgresSQL and docker

I'm trying to connect to my database using postgres on docker but it says that the password authentication failed.
My code:
const { Client } = require('pg');
const client = new Client({
host: 'localhost',
port: 5432,
user: 'root',
password: 'root',
database: 'mycontacts',
});
client.connect();
docker command to start the postgres container:
docker run --name postg -e POSTGRES_USER=root2 -e POSTGRES_PASSWORD=root2 -p 5432:5432 -d postgres
Try creating another Postgres container to try and connect and it will work.
Else. password authentication failed while trying to connect into my docker database
It ended up being the postgres that i had installed on my pc.
I uninstalled it and now its working!

How to add database environment variables to Javascript

I want to add my database environment variables to a .env file and use that file in my Javascript program in order to create a connection to my database using Node.js.
So here is my database info which I use to create connection with:
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "my password",
database: "mydatabase"
});
Then I try to check if I am connected to my database using these lines:
(It should print "Connected!").
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
});
I want to put the first block of code in another file and require that file in my Node.js program.
How should I do this? How should I require the file? Thank you in advance :)
I suggest using dotenv package.
Taken straight from the readme:
As early as possible in your application, require and configure dotenv.
require('dotenv').config()
Create a .env file in the root directory of your project. Add
environment-specific variables on new lines in the form of NAME=VALUE. For example:
DB_HOST=localhost
DB_USER=root
DB_PASS=s1mpl3
Usage (process.env now has the keys and values you defined in your .env file.)
var db = require('db')
db.connect({
host: process.env.DB_HOST,
username: process.env.DB_USER,
password: process.env.DB_PASS
})
NOTE: Make sure your .gitignore has an entry to ignore .env files.
You can use a module called dotenv which will load an environments variables defined in a .env file or any file you specify. You would then load the .env in two ways:
In the main script file, call require('dotenv').config()
Via npm script: "dev": "node -r dotenv/config app.js"
Either works, but you do not want to commit .env to source control. Always keep sensitive credentials out. You can create a an .env.example to alert new users the required variables.
Using dotenv package. Install dotenv package.
npm i dotenv
Create a new .env file in project root directory.
touch .env
Add environment variables to .env file
API_HOST=HOST-PLACEHOLDER-URL
API_KEY=TOP-SECRET
APP_NAME=node_crud
APP_ENV=local
APP_KEY=base64:RIjL2Uw/Wdve+HJEvRNp6LHhzoHtLbplQcUp60CBIvs=
APP_DEBUG=true
APP_URL=http://localhost
LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=node_restapi
DB_USERNAME=root
DB_PASSWORD=
PORT = 5000
Add config/database.js file
const dotenv = require('dotenv');
const mysql = require('mysql');
// configraration with env.
dotenv.config();
module.exports = mysql.createConnection({
host: process.env.DB_HOST,
user: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE
});
config/database.js connect with app.js file
// Database Connection
const conn = require('./config/database');
// Shows Mysql Connect
conn.connect((err) =>{
if(err) throw err;
console.log('Mysql Connected with App...');
});
A .env file is needed for a clean separation of environment-specific configurations.
The dotenv packaged is used to read a .env file at runtime containing environment variables and append them on the process.env object.
Creating an example for a .env file to document the mandatory variables speeds up project setup time.
Never commit a .env file to version control.

Meteor.js + external Mongo. Meteor cannot login into Mongo

I have a meteor.js app + mongo db (2.6).
I've created a user in mongo like this:
use meteor
db.createUser(
{
user: "meteor",
pwd: "password",
roles:
[
{
role: "userAdmin",
db: "meteor"
}
]
}
)
here is my mongodb.conf:
dbpath=/data/db
logpath=/var/log/mongodb/mongodb.log
logappend=true
port = 27017
when i set auth = true then my meteor app cannot connect to mongo anymore. It says
Exception in callback of async function: MongoError: auth failed
Same error when i try to connect with RoboMongo when auth is true. When auth is false i can connect with RoboMongo. So it's not about Firewall or something.
I don't understand, what I can do to switch on authorization in Mongo, so that it would let me login. Please help.
It depends on how you start Meteor. You need to tell it which Mongo instance to use and also provide proper credentials like this:
export MONGO_URL=mongodb://<username>:<password>#<host>:<port>/<db>
That's the way I've been doing it with my Meteor apps and a dedicated MongoDB and it works nicely. But if RoboMongo is not able to connect, there may be something wrong with the Mongo configuration. You can use this command to enable authentication in your config:
mongod --auth --config /path/to/mongodb.conf
I assume you have an admin user already which was used to create the meteor user, you will run into issues if you start Mongo without authentication, then add a meteor user anonymously and restart with auth=true.

Finding MongoDB details after hosting on Ubuntu

I'm completely new to setting up servers, MongoDB, and still a little new to Javascript.
I'm trying to upload a Deployd server onto an online server. There is limited information on this, so at the moment, I set up a simple AWS Ubuntu server by doing the following tutorials:
http://zenborgium.blogspot.com/2012/12/how-to-setup-deployd-on-ubuntu-server.html
http://terraltech.com/how-to-setup-deployd-on-ubuntu-server/
However, I'm stuck at creating the production.js. There's a guide on it here. I'm specifically stuck at this line of code:
var server = deployd({
port: process.env.PORT || 5000,
env: 'production',
db: {
host: 'my.production.mongo.host',
port: 27105,
name: 'my-db',
credentials: {
username: 'username',
password: 'password'
}
}
});
server.listen();
Where do I find the host, port, name, and credentials that I should use from MongoDB? The tutorials say I need to use my own data, but I don't know where or how to find them.
First of all, you have to have MongoDB installed. I haven't used Deployd myself, but I will give you some information regarding the config file.
If you are running Mongo on the same ubuntu server as your application, you can use localhost to connect.
Default Mongo install runs on port 27017, in other words localhost:27017.
The 'name' parameter is just a name you give your database. So here you can put whatever you want, ex my-db.
With a clean Mongo install, you don't need any credentials. You have to set that up yourself if you want. I suppose leaving them out of the config file is ok, if not needed.
Your config file should therefor look something like this:
var server = deployd({
port: process.env.PORT || 5000,
env: 'production',
db: {
host: 'localhost',
port: 27017,
name: 'my-db'
}
});
update
I had a quick look at the tutorial you linked to. In one of the tutorials they created a user for mongodb. If you followed this step, you need to put that login information into you connect-object under credentials.
update 2
To get information about your mongodb install, check this SO post

Categories

Resources