How to set the Application Name for a Sequelize application - node.js

I've got a nodejs application that uses Sequelize as it's ORM. I've successfully got Sequelize connected to the database, but I haven't found anything in the documentation that explains how to set the application name. To clarify I'm looking to set a unique Application Name attribute for my app's connection string. That way when a DBA is looking at traffic they can pick out my application's queries from the rest.
Is this something that Sequelize can even do? Or does this need to be done at the tedious level? Failing that, is there a way in nodejs to specify connection string attributes?

Tedious allows setting the app name with the appName config param. You should be able to set this via the dialectOptions object when creating your Sequelize connection:
var conn = new Sequelize('my_db', 'my_user', 'my_pass', {
host: 'my_server',
dialect: 'mssql',
dialectOptions: {
appName: 'my_app_name'
}
});

For those finding this when they're looking for how to set the name for Postgres, you use application_name in the dialectOptions, eg
{
username: process.env.DB_USER,
password: process.env.DB_PASS,
database: process.env.DB_NAME,
port: process.env.DB_PORT,
host: DB_HOST,
dialect: 'postgresql',
dialectOptions: {
application_name: 'My Node App',
},
},

Related

Nodejs Postgress Error no pg_hba.conf entry for host

I'm creating a new pool object a follows:
import { Pool, PoolConfig } from "pg";
const options = {
host: process.env.DB_HOST,
port: Number.parseInt(process.env.DB_PORT as any),
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
connectionLimit: Number.parseInt(process.env.DB_CONNECTION_LIMIT as any),
ssl: false,
} as PoolConfig;
export const pool = new Pool(options);
But when i try to do pool.query I'm getting the following error:
"There was an error processing your request: no pg_hba.conf entry for host \"172.31.0.1\", user \"admin\", database \"todos\", no encryption"
I've also tried to set the ssl to true but it seems not working.
What maybe posibly the problem here

TypeORM: Generate migrations without dedicated ormconfig file

Of the several ways of connecting to a database offered by TypeORM (json, env, yml...) I choose to set my Connection Options programatically within my project's code using a regular TypeScript file as follows:
// src/database/createConnection.ts
import { createConnection } from 'typeorm';
const connectionOptions = {
type: 'postgres',
host: POSTGRES_HOST_PROD,
port: Number(POSTGRES_PORT),
username: POSTGRES_USER,
password: POSTGRES_PASSWORD,
database: POSTGRES_DB,
entities: ['build/entities/**/typeDef.js'],
migrations: ['build/database/migrations/*.js'],
cli: {
migrationsDir: 'src/database/migrations/'
}
};
await createConnection(connectionOptions);
With the values fed by a regular .env file.
My problem is that when trying to create migration files through npx typeorm migration:generate -n initial I get an error as the command expects a specific orm configuration file to be present (ormconfig.env, ormconfig.json, etc).
As I already have my connection options set, this is not only redundant, it would also be processed instead of the configuration I have already set in the .ts file, and the only solution I see would be to rename the variables in my (non-typeorm-specific) .env file to match the specific TypeORM variable names, which I'd prefer not to do.
TL:DR Is there a way of generating TypeORM migrations without creating a dedicated orm config file?
Oh, turns out TypeORM already has a built-in option for this which does not require an extra ormconfig file, one can just add the property migrationsRun: true to the Connection Options (making sure synchronize is set to false).
For instance:
import { createConnection } from 'typeorm';
const connectionOptions = {
type: 'postgres',
host: POSTGRES_HOST_PROD,
port: Number(POSTGRES_PORT),
username: POSTGRES_USER,
password: POSTGRES_PASSWORD,
database: POSTGRES_DB,
migrationsRun: true,
entities: ['build/entities/**/typeDef.js'],
migrations: ['build/database/migrations/*.js'],
cli: {
migrationsDir: 'src/database/migrations/'
}
};
await createConnection(connectionOptions);

How can i to disable step CREATE SCHEMA to sequelize migrations?

I have a postgresql database, schema and username:password.
If i run:
sequelize db:migrate --config=./dist/options/db.options.js --migrations-path=./dist/migrations --env=main
...then i catch error:
CREATE SCHEMA IF NOT EXISTS my_schema;
**ERROR**: permission denied for database my_database
My user cannot create schema, but this schema already exists.
How i can to disable this step on sequelize migrations options?
It is my sequelize config:
const CONNECTION: any = {
dialect: 'postgres',
host: process.env.PG_HOST,
port: parseInt(process.env.PG_PORT, 10),
database: process.env.PG_DATABASE,
username: process.env.PG_USERNAME,
password: process.env.PG_PASSWORD,
models: [
...
],
autoLoadModels: true,
sync: false,
migrationStorage: "sequelize",
migrationStorageTableName: genTableName('migrations'),
migrationStorageTableSchema: process.env.PG_SCHEMA,
logging: (...msg) => console.log(msg)
};
Google couldn't help me...
I found answer on my question. In my situation need to remove key migrationStorageTableSchema from config.

I need to pass username and password to typeORM on runtime

On NodeJS (NestJS) I used TypeORM to connect to Database (Oracle). I would like to pass the username and password during runtime of the application.
Due to security reasons, in my company, the security is configured in away that for each user, we create a database schema. He log into The application using his DB credential. I know I t is not very popular practice in the industry.
const connection = await createConnection({
type: "oracle",
host: "localhost",
port: 3306,
username: "test", // this need to pass during run time
password: "test", // same thing for the password
database: "test"
});
Could you please share with me any reference/ hints on how to achieve this on typeORM and nodeJs/NestJs?
Thanks
You can use register TypeORM in the root level, and use a configuration service to provide the connection details:
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
useFactory: (config: ConfigService) => ({
host: config.get('DB_HOST'),
username: config.get('DB_USER'),
password: config.get('DB_PASSWORD'),
database: config.get('DB_NAME'),
entities: [__dirname + '/**/*.entity{.ts,.js}'],
port: 3306,
type: 'oracle',
}),
inject: [ConfigService],
}),
ConfigService is just a simple service that implements a get function that provides the specific requested configuration.
More on that here: https://docs.nestjs.com/techniques/database#async-configuration

How to set up postgresql config with heroku and nodejs?

This is my first time try to host nodeJS application - built with hapi.js, typeorm and postgresql - on heroku. I've create two apps on heroku - for "staging" (server-staging) and "production" (server-prod) - that using same code but will use different configuration. Why different configuration? because each application on heroku will use different postgres credential, as it's attached as an add-ons.
objective
My objective/main question is How and where I have to set the database config for my application?
I use .env file (which I ignore in .gitignore - I don't want to put the credential in my repo) to connect the application to my local database. Here is how the .env looks like:
HOST=localhost
PORT=3001
TYPEORM_CONNECTION=postgres
TYPEORM_HOST=localhost
TYPEORM_USERNAME=postgres
TYPEORM_PASSWORD=password
TYPEORM_DATABASE=database
TYPEORM_PORT=5432
TYPEORM_SYNCHRONIZE=true
TYPEORM_LOGGING=false
In the application, I never do/write code such process.env.TYPEORM_USERNAME since its done by the typeorm node_modules. What I do to start the connection is by doing this:
const server = new Hapi.Server({
port: process.env.PORT,
host: process.env.HOST,
routes: {
cors: Cors,
},
});
await server.register(Plugins);
server.route(Router);
await createConnection();
await server.start();
And my application automatically connected to the specified database as defined in the .env. Now, in heroku, the credential is lies here:
All information lies there, but, [Q1] I don't know how to tell my application (of course, without store the credential in my code/repo) that I have to use the config as defined in above picture? Also, as stated in above image, "Heroku rotates credentials periodically and updates applications where this database is attached.". Does it means the credentials will changed periodically? [Q2] If yes, is there any way to make my application auto recognise the new credential?
Sorry if my explanation make confused. If you did not understand what I am trying to achieve, please ask things that you don't understand, so I can fix/update my question to make it understandable.
Anyway, I found this example first-example and second-example. But, they are using process.env.DATABASE_URL, which contain credential. I think, it means that they not ignore their .env file in their repo?
*) Note: Q1 means Question 1, and so for the rest
Write a ormconfig.js file in the root of your repo. This way you can access the environment variables like the url provided from heroku and you don't have credentials in your repo.
require('dotenv').config();
module.exports = [
{
name: 'default',
type: 'postgres',
url: process.env.DATABASE_URL,
synchronize: false,
logging: false,
entities: ['dist/entities/*.*'],
migrations: ['dist/database/migrations/**/*.js'],
subscribers: ['dist/database/subscribers/**/*.js'],
cli: {
entitiesDir: 'dist/entities',
migrationsDir: 'dist/database/migrations',
subscribersDir: 'dist/database/subscribers',
},
},
{
name: 'development',
type: 'postgres',
host: process.env.POSTGRES_HOST,
port: process.env.POSTGRES_PORT,
username: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
database: process.env.POSTGRES_DB,
synchronize: true,
logging: true,
entities: ['src/entities/*.*'],
migrations: ['src/database/migrations/**/*.ts'],
subscribers: ['src/database/subscribers/**/*.ts'],
cli: {
entitiesDir: 'src/entities',
migrationsDir: 'src/database/migrations',
subscribersDir: 'src/database/subscribers',
},
},
];
With this configuration you can then get a specific configuration in javascript/typescript:
let connectionOptions: ConnectionOptions;
if(process.env.NODE_ENV ==='development') {
connectionOptions = await getConnectionOptions("development");
} else {
connectionOptions = await getConnectionOptions("default");
}
await createConnection(connectionOptions);

Resources