Postgres pool doesn't connect to the client - node.js

My postgres pool is not connecting to the client and i'm almost sure that i passed the right properties to it.
That's the pool instance:
const { Pool } = require('pg')
module.exports = new Pool({
host: 'localhost',
port: 5432,
user: 'laura',
password: 123,
database: 'launchstore'
})
And i created the postgres service with docker-compose:
version: '3.3'
services:
postgres:
container_name: postgres
image: postgres
environment:
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_DB: ${POSTGRES_DATABASE}
ports:
- "5432:5432"
restart: unless-stopped
My env file:
POSTGRES_DATABASE=launchstore
POSTGRES_PASSWORD=123
POSTGRES_USER=laura
When i enter the page, it keeps loading and that's the result why i try to log the pool instance:
Pool {
_events: [Object: null prototype] {},
_eventsCount: 0,
_maxListeners: undefined,
options: {
Client: [Function: Client] { Query: [Function: Query] },
host: 'localhost',
port: 5432,
user: 'laura',
password: 123,
database: 'postgres',
max: 10,
idleTimeoutMillis: 10000
},
log: [Function (anonymous)],
Client: [Function: Client] { Query: [Function: Query] },
Promise: [Function: Promise],
_clients: [],
_idle: [],
_pendingQueue: [],
_endCallback: undefined,
ending: false,
ended: false,
[Symbol(kCapture)]: false
}
If i'm passing the right properties and my container is running, why my client can't connect to the pool?

The PG dependency wasn't installed correctly and that's why the pool wasn't working too. So it wasn't related to any configuration error.

Related

mongoose cannot connect when replica set is enabled

I am attempting to dockerize a nodeJs application, however I cannot connect using mongoose, Mongodb Compass works (from host OS)
The app connects to a Mongodb server ( also on docker) using the following code:
const mongoUri: string = "mongodb://mongo:27017?replicaSet=rs0";
mongoose
.connect(mongoUri)
.then(() => console.log("Connected to database")).catch(err => console.log(err.reason));
I am using the following docker-compose file:
version: "3.8"
services:
mongo:
image: mongo:4.4.0
volumes:
- ./mongo:/etc/mongo
- ./data/db:/data/db
entrypoint: [/etc/mongo/start.sh]
ports:
- 27017:27017
hostname: mongo
backend:
depends_on:
- mongo
build: ./sp_backend
ports:
- 8080:8080
- 8883:8883
- 8884:8884
hostname: backend
The start.sh is setting a custom mongod.conf as following:
mongod --config /etc/mongo/mongod.conf
and in the mongod.conf the replication is set (to enable change streams)
...
replication:
replSetName: "rs0"
...
I also ran rs.initiate() on the mongo instance at the first startup, and I can connect from the host to the mongo server using Mongodb Compass, however mongoose spits out the following error:
TopologyDescription {
type: 'ReplicaSetNoPrimary',
servers: Map(1) {
'127.0.0.1:27017' => ServerDescription {
address: '127.0.0.1:27017',
type: 'Unknown',
hosts: [],
passives: [],
arbiters: [],
tags: {},
minWireVersion: 0,
maxWireVersion: 0,
roundTripTime: -1,
lastUpdateTime: 25342884,
lastWriteDate: 0,
error: [MongoNetworkError],
topologyVersion: null,
setName: null,
setVersion: null,
electionId: null,
logicalSessionTimeoutMinutes: null,
primary: null,
me: null,
'$clusterTime': null
}
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
setName: 'rs0',
maxElectionId: new ObjectId("7fffffff0000000000000001"),
maxSetVersion: 1,
commonWireVersion: 0,
logicalSessionTimeoutMinutes: null
}
My thinking is that the servers map should not contain 127.0.0.1 as a server since the mongo runs on a sepparate server. I also checked to ping the database container from the nodejs container and it works.
Later update:
It seems I was using the an old mongo docker image, after upgrading (mongo:4.4.0) to mongo:latest fixed the problem.

Sequelize.query() promise never resolves

I am trying to run a Sequelize query against a Postgres DB, as follows:
async getRowFromWorkerTable(contact_uri){
console.log(`running query...`);
let result;
try{
result = await sequelize.query("select * from worker where contact_uri='"+contact_uri+"'",
{ type: sequelize.QueryTypes.SELECT});
}
catch(err){
console.err(err);
}
finally{
console.log(`query complete.`);
}
return result;
}
I get the running query... text, but then no error or query complete.
I initialize sequelize prior to this as follows:
const sequelize=new Sequelize(process.env.database,process.env.username,process.env.password,{
dialect:'postgres',
});
Examining the sequelize object with console.log({sequelize}) gives the following:
{
sequelize: <ref *1> Sequelize {
options: {
dialect: 'postgres',
dialectModule: null,
dialectModulePath: null,
host: 'localhost',
protocol: 'tcp',
define: {},
query: {},
sync: {},
timezone: '+00:00',
clientMinMessages: 'warning',
standardConformingStrings: true,
logging: [Function: log],
omitNull: false,
native: false,
replication: false,
ssl: undefined,
pool: {},
quoteIdentifiers: true,
hooks: {},
retry: [Object],
transactionType: 'DEFERRED',
isolationLevel: null,
databaseVersion: 0,
typeValidation: false,
benchmark: false,
minifyAliases: false,
logQueryParameters: false,
dialectOptions: [Object]
},
config: {
database: 'dbname',
username: 'username',
password: 'password',
host: 'localhost',
port: 5432,
pool: {},
protocol: 'tcp',
native: false,
ssl: undefined,
replication: false,
dialectModule: null,
dialectModulePath: null,
keepDefaultTimezone: undefined,
dialectOptions: [Object]
},
dialect: PostgresDialect {
sequelize: [Circular *1],
connectionManager: [ConnectionManager],
QueryGenerator: [PostgresQueryGenerator]
},
queryInterface: QueryInterface {
sequelize: [Circular *1],
QueryGenerator: [PostgresQueryGenerator]
},
models: {},
modelManager: ModelManager { models: [], sequelize: [Circular *1] },
connectionManager: ConnectionManager {
sequelize: [Circular *1],
config: [Object],
dialect: [PostgresDialect],
versionPromise: [Promise [Object]],
dialectName: 'postgres',
pool: [Pool],
lib: [PG],
nameOidMap: {},
enumOids: [Object],
oidParserMap: Map(0) {}
},
importCache: {}
}
}
I am able to successfully run this query through psql. Why am I not getting any query results through Sequelize?
Figured it out--this issue has been addressed in depth here: https://github.com/sequelize/sequelize/issues/12158

MongoClient cannot connect to mongo in another Docker container

I am currently trying to deploy my app with docker but I have some problem with mongodb.
My app is not succeeding to connect to mongodb in another container.
Here is my docker-compose.yml file:
version: "3.7"
services:
mqtt_broker:
build:
context: .
dockerfile: Dockerfile
container_name: mqtt_broker
ports:
- "4040:4040"
links:
- mqtt_db:mqtt_db
depends_on:
- mqtt_db
mqtt_db:
build:
context: ./mongo
dockerfile: Dockerfile
container_name: mqtt_db
ports:
- "27017:27017"
Here is the Dockerfile for the mongo container:
FROM mongo:latest
COPY init_docker.js /docker-entrypoint-initdb.d/
CMD mongod --replSet "rs0" --bind_ip_all
Here is how I connect to mongo in the app container:
const client = new MongoClient(config.get("dbURL"));
const mongo = await client.connect();
const db = client.db(config.get("dbName"));
with the configuration file:
{
"cors": {
"origin": "*"
},
"clientPort": 4040,
"mqttPort": 1883,
"dbURL": "mongodb://mqtt_db:27017",
"dbName": "mqtt_proxy"
}
Here the error I get with the mongo node client:
MongoServerSelectionError: Server selection timed out after 30000 ms
at Timeout._onTimeout (/broker/node_modules/mongodb/lib/sdam/topology.js:305:38)
at listOnTimeout (node:internal/timers:559:17)
at processTimers (node:internal/timers:502:7) {
reason: TopologyDescription {
type: 'Unknown',
servers: Map(1) {
'mqtt_db:27017' => ServerDescription {
_hostAddress: HostAddress { isIPv6: false, host: 'mqtt_db', port: 27017 },
address: 'mqtt_db:27017',
type: 'RSGhost',
hosts: [],
passives: [],
arbiters: [],
tags: {},
minWireVersion: 0,
maxWireVersion: 13,
roundTripTime: 35.24000000000001,
lastUpdateTime: 13896371,
lastWriteDate: 0,
topologyVersion: {
processId: ObjectId { [Symbol(id)]: [Buffer [Uint8Array]] },
counter: 0
},
logicalSessionTimeoutMinutes: 30
}
},
stale: false,
compatible: true,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
commonWireVersion: 13,
logicalSessionTimeoutMinutes: undefined
},
code: undefined,
[Symbol(errorLabels)]: Set(0) {}
}
I can actually ping mqtt_db, the ip is resolved, but I don't see it in /etc/hosts.
I also tried to add networks in my docker-compose but it translated mqtt_db in localhost and it does not work.
Here is the repo of my project: https://gitlab.com/mqtt-broker-sniffer/sniffer
EDIT
I had to specify ?directConnection=true when I was connecting to the db.
Crédit: https://stackoverflow.com/a/70204195/19127936
const client = new MongoClient(
`${config.get("dbURL")}/${config.get("dbName")}?directConnection=true`
);
this.client = await client.connect();
this.db = client.db(config.get("dbName"));

models created are returning empty array

I have created two modules in my express using sequelize with typescript but when I try to log the model using console.log(require('../models')) it's giving an empty array.
{ models: {},
sequelize:
Sequelize {
options:
{ dialect: 'postgres',
dialectModulePath: null,
host: '127.0.0.1',
protocol: 'tcp',
define: {},
query: {},
sync: {},
timezone: '+00:00',
logging: [Function: bound consoleCall],
omitNull: false,
native: false,
replication: false,
ssl: undefined,
pool: {},
quoteIdentifiers: true,
hooks: {},
retry: [Object],
transactionType: 'DEFERRED',
isolationLevel: null,
databaseVersion: 0,
can someone tell me what I'm doing wrong?

Why do my Mongoose models not have access to my database when they live inside an NPM module?

I'm building an API using Node, Express, and Mongo. For my data modeling, I'm using Mongoose. I'd like to move my models to a separate Node module that my API uses so that I can also reuse the models in other projects. However, I'm running into a strange problem using my Mongoose data models in my API.
When my data model source lives in an NPM module, my models cannot access my database, and any .find() or .save() calls hang and do nothing. However, if my data model source lives directly in my project, not under node_modules, then everything is fine. Here is my code:
var mongoose = require('mongoose');
var dbURI = 'mongodb://localhost/myproject-dev'
mongoose.connect(dbURI);
mongoose.connection.on('error', console.error.bind(console, 'database connection error:'));
var productModel = require('myproject-core').models.product;
console.log(productModel);
Here console.log() shows (I've snipped lots of stuff):
{ [Function: model]
base:
{ connections: [ [Object] ],
plugins: [],
models: ...
...
db:
...
replica: false,
hosts: null,
host: null,
port: null,
user: null,
pass: null,
name: null,
options: null,
otherDbs: [],
_readyState: 0,
_closeCalled: false,
However, if I require() the model (with the exact same model code) in my source
var playerModel = require('./models/player');
console.log(playerModel);
I see this:
{ [Function: model]
base:
{ connections: [ [Object] ],
plugins: [],
models: ...
...
db:
...
replica: false,
hosts: null,
host: 'localhost',
port: 27017,
user: undefined,
pass: undefined,
name: 'myproject-dev',
options: { db: [Object], auth: {}, server: [Object], replset: [Object] },
otherDbs: [],
_readyState: 2,
_closeCalled: false,
And idea what's wrong?
Solved. The problem was that I had mongoose listed as the parent-require NPM package helped solve the problem.

Resources