PG (Node-Postgres) Pool Hangs on Connect ... But Only Inside Gatsby? - node.js

NOTE: This is mainly a question about the pg or Node-PostgreSQL module. It has details from Gatsby and Postgraphile, but I don't need expertise in all three, just pg.
I have a database that works great with a PostGraphile-using Express server. I can also acces it via node at the command line ...
const { Pool } = require("pg");
const pool = new Pool({ connectionString: myDbUrl });
pool.connect().then(() => console.log('connected'));
// logs 'connected' immediately
The exact same database also previously worked great with Gatsby/PostGraphile via the gatsby-source-pg plug-in ... but recently I changed dev machines, and when I try to build or run a dev server, Gatsby hangs on the "source and transform nodes" step. When I debug it, it's hanging on a call to pool.connect().
So I literally have two codebases both using PostGraphile, both with the same config, and one works and the other doesn't. Even stranger, if I edit the source code of the Gatsby plug-in in node_modules, to make it use the exact same code (which I can run at the command line successfully) ... it still hangs.
The only thing I can think of is that some other Gatsby plug-in is using up all the connections and not releasing them, but as far as I can tell (eg. by grep-ing through node_modules) no other plug-in even uses pg.
So really I have two questions:
A) Can anyone help me understand why connect would hang? Bonus points if you can help me understand why it would do so with a known-good config and only inside Gatsby (after some environmental factor changed)?
B) Can anyone help me fix it? If it might be some sort of "previous code forgot to release connections" issue, is there any way I can test for that? If I could just log new Pool().areYouBroken() somehow that would be amazingly useful.

Try:
npm install pg#latest
This is what got my pool/connection to start working as expected.

Annoying answer: because of a bug (thank you #charmander). For further details see: https://github.com/brianc/node-postgres/issues/2300
P.S. I never did find any sort of new Pool().areYouBroken() function.

Related

vite rebuilds dev server on every http request, causing graphql schema duplicates instantly crashing server -- build and production works, repo inside

I'm trying to build a graphql server using nestjs and using vite + swc as the compiler/builder for performance reasons, webpack would take 50-60+ seconds on each rebuild on a big project, SWC/vite seems to cut that down by a factor of 5 at least.
Here's a repository that reproduces the issue with a basic 'health check' endpoint and graphql query.
The main tools concerning this:
"#nestjs/graphql": "10.0.9",
"#nestjs/apollo": "10.0.9",
"typescript": "4.7.4",
"vite-plugin-node": "1.0.0",
"vite": "2.9.13",
"#swc/core": "1.2.207",
"vite-tsconfig-paths": "3.5.0"
Now, I have played around with these fixed versions trying out various combinations of older versions. But I've narrowed down the flaw to be a problem with vite specifically.
There's this github issue opened over a month ago that's probably directly related, with this being merely a symptom of that issue.
If you build the app and serve it, everything works fine, because the production version calls the bootstrap() function which is not handled by the vite development server.
This is also a nestjs-specific problem due to nestjs doing the code-first approach.
I'm trying to patch this issue somehow by attempting three things:
stop the development server from rebuilding on every request
configure the development server to cleanup after itself on every request
configure nestjs's graphql in a way that only builds the schema 1 time, something as simple as:
let built = false;
if(!built) { buildSchema(); built = true; }
I'm counting on that built variable not changing between requests, but if it does, I might find a way to tie it to the start command via a file outside of vite's scope.
Thank you.

Unable to set up sample React project correctly?

I got an interview assessment next week and they provided me a sample code project of what to expect. The problem is I'm having a LOT of trouble simply setting it up to run as expected...
Edited: Removed project for privacy
I cloned the project and installed all dependencies.
Problem 1
I followed the README in which I created a Database called messenger and then created a .env file in the server directory. The problem is I can't retrieve any values from that .env file through process.env.REACT_APP_CUSTOM_VAR. Console.logging "process.env" does not show ANY custom variables. It's as if they weren't even created...
Problem 2
On the frontend side, it should have been simple (npm install and then npm start). When starting, I found that there was NO CSS applied (despite reading the code and there's Material UI used). When I inspected the page, I'm getting the error 'The server responded with a status of 431 (Request header fields too large).
I have a hard time believing the company gave me super broken code to the point where I can't even run the sample code properly... Can anyone please help me and try installing the code project above? Please let me know if you got the same problem or found any solutions!
Submit an issue requesting that they add .nvmrc and package-lock.json such that you can ensure your machine is prepared to absorb dependencies and properly posture with the correct version of node. It's a crap-shoot of testing various versions of node against their build otherwise.
Your best bet is to check the first publish date of the client (javascript) code against the latest versions of node from that time. It sucks, but that's what I would do.
The project runs fine.
Theres not really styling applied, but that seems to be intentional.
Most likely you would benefit from using whatever database you have access to that already works (in my case, mssql) by changing the db.js
const db = new Sequelize('dbname', 'user', 'password', {
host: 'host',
port: 1433,
logging: false,
dialect: 'mssql',
dialectOptions: {
encrypt: true,
},
});
What you should however not do if you want the job, is critique it. It works just fine as the baseline for a recruitment assignment. Your enviroment is the issue in this case.
Do not forget to run the seed on the backend.
For the problem 1 on the server, I've create an .env file on the ./server directory and logging process.env logged the variables from that file. Also, on the backend, you don't need the REACT_APP_CUSTOM_VAR prefix when accessing environment variables.
For the problem 2 on the frontend, after the instalation, the client loaded the css from mui properly. I'm using node 14.17.0.

Relay/GraphQL Schema cache not updating when I update schema on server side

I have a React app using Relay and a remote GraphQL server. When I start the webpack server, I have it fetch the latest schema and feed it into the babel-relay-plugin.
It works great....except when I make a schema change. It appears React or Relay or webpack or something is caching the schema, because I'll get a Schema validation error in the browser console when I run the app. However, when I run the query manually against the GraphQL server using GraphIQL, the query is successful. So it would have to be some sort of cache on the react, relay, webpack side I'm thinking?
Things I've tried:
List item
Restarting webpack server
Removing node_modules and npm install
I've even tried restarting my computer (that actually seemed to work, but may be coincidence)
Thanks in advance for your help.
Turns out, of course, it was human error. I had cacheDirectory as true in my babel-loader query. You can read about it on the babel-loader readme (just do a find on page for 'cacheDirectory') https://github.com/babel/babel-loader
Once I changed that to false, which is the default. The problem went away. Hope that helps others.
This happened to me when I switched to Webpack 2.
The solution in my case was to move the babelRelayPlugin to be the first plugin to execute in .babelrc.
I'm not exactly sure on the why though.

ExpressJS, NodeJs, and MongoDB CRUD Skeleton - Mac

I have recently started working with MongoDB and had it working for a small web test with node JS However. I new my implementation wasn't following a decent MVC structure. I began searching and found this website and read through it. It's implementation look good as well as following a good MVS skeleton structure.
Express/NodeJS/MongoDB CRUD Skeleton
I got to the part where he says 'HOORAY! We've got a functioning web server that is talking to Mongo. Part 1 is accomplished." Unfortunately, I went the local host specified and it says the 'site cannot be reached'. I am still fairly new to NodeJs, Mongo and Express and cannot figure out why it is not working.
I am not sure if I have done a step wrong time and time again which I doubt or if there something is missing that is stopping me from having it working.
The part before it says:
Awesome. Now to test it's all working in the terminal type npm start and you shouldn't see any errors:
kcoleman-mbp:nodewebapp kcoleman$ npm start
> nodewebapp#0.0.1 start /Users/kcoleman/Documents/projects/nodewebapp
> node ./bin/www
That part I get the same outcome with:
nodewebApp and node./bin/www
The page says to navigate to 127.0.0.0:3000, but the localhost usualy defaults to 127.0.0.1. I didn't see any code in the page where he defines the port to 127.0.0.0.
So try connecting to 127.0.0.1:3000.
Coming to nodewebApp and node./bin/www, it could be because the command might be present in pacakge.json. If you delete the nodewebApp from package.json, you won't be seeing the same in the command.

Output to Chrome console from Node.js

I'm looking for a way to output Node variables directly into the google chrome browser console. The same way a console.log() works on the client side. Something like this for php. This would greatly speed up development.
NOTE:
Since the old answer (written in september 2014) refers to an older version of node-inspector, my instructions are not relevant anymore in 2017. Also, the documentation has gotten a lot better, so I have updated my original answer:
node-inspector is what you need.
It opens up an instance of Chrome with its developer tools for debugging.
It's also easy to use:
1. Install
$ npm install -g node-inspector
2. Start
$ node-debug app.js
Source: https://github.com/node-inspector/node-inspector
You might want to try NodeMonkey - https://github.com/jwarkentin/node-monkey
I know it's an old question but came on top of my Google search so maybe somebody will find my answer useful.
So you can use node --inspect-brk index.js
Now, all you have to do is basically just type chrome://inspect in your Chrome address bar and click Open dedicated DevTools for Node
In DevTools, now connected to Node, you’ll have all the Chrome DevTools features you’re used to:
Complete breakpoint debugging, stepping w/ blackboxing
Source maps for transpiled code
LiveEdit: JavaScript hot-swap evaluation w/ V8
Console evaluation with ES6 feature/object support and custom object formatting
Sampling JavaScript profiler w/ flamechart
Heap snapshot inspection, heap allocation timeline, allocation profiling
Asynchronous stacks for native promises
Hope that helped.
The closest thing to this I've seen is Node JS console object debug inspector
See this post for usage and potential issues: http://thomashunter.name/blog/nodejs-console-object-debug-inspector/
For users with nodejs on linux via ssh-shell (putty):
Problem with nodejs on linux-ssh-shell is, that you have no browser connected.
I tried all this solutions, but didnt get it to work.
So i worked out a solution with firebase (https://firebase.google.com), because my project uses firebase.
If you are familiar with firebase, than this is a great way. If not, firebase is worth using in combination with nodejs - and its free!
In the server-side-script (started with node) use a own function log():
// server-side:
// using new firebase v3 !
var fbRootRef = firebase.database();
var fbConsoleRef = fbRootRef.ref("/console");
var log = function(args) {
fbConsoleRef.set({'obj': args});
}
// inside your server-code:
log({'key':'value'});
On client-side you create a firebase-reference on this console-object:
// client side:
fbRootRef.child('/console').on('value', function(d) {
var v = d.val();
console.log(v);
});
Now everything logged on server-side with the log() - function is transferred in realtime to the firebase-database and from there triggering the client-console-reference and logged into the browsers console.
If anyone needs help, i will explain in more detail and could give a more extended version of this logging with types (console./log/warn/info), grouping with title-info (i.e. server says: (filename + line).
Setting up firebase for your project is done in max 30 minutes, inserting the console-function in 30 minutes. I think its worth the time!
You can use bonsole, a simple way to log something in browser. Even in Linux, you can go to the LAN's ip to check it.
The most simple way with least dependencies is using a WebSocket connection to send the messages to the browser. Any WebSocket example you can find on the internet will suffice to accomplish this. Everything else requires to be heavily integrated into the host system and wouldn't work if you want to actually run this on a remote server. You can also send commands to the server directly from the browser console this way.
Links:
https://www.npmjs.com/package/websocket
https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_client_applications

Resources