Skip to content

Commit

Permalink
1. Made the query for bulk update a bit readable. (TechEmpower#8555)
Browse files Browse the repository at this point in the history
2. Updated the cluster code to use availableParallelism as used nowdays witth the recent node.js versions.
3. Updated to the ESM require modules which are used in all latest node.js projects instead of old CJS modules.
  • Loading branch information
srisaiswaroop authored Nov 20, 2023
1 parent 423e328 commit 0ec8ed4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
11 changes: 6 additions & 5 deletions frameworks/JavaScript/nodejs/app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const cluster = require('cluster');
const numCPUs = require('os').cpus().length;
const cluster = require('node:cluster');
const { availableParallelism } = require('node:os');
const numCPUs = availableParallelism();

process.env.NODE_HANDLER = 'postgres';

Expand All @@ -13,18 +14,18 @@ if (process.env.TFB_TEST_NAME === 'nodejs-mongodb') {
process.env.NODE_HANDLER = 'mysql-raw';
} else if (process.env.TFB_TEST_NAME === 'nodejs-postgres') {
process.env.NODE_HANDLER = 'sequelize-postgres';
}else if (process.env.TFB_TEST_NAME === 'nodejs-postgresjs-raw') {
} else if (process.env.TFB_TEST_NAME === 'nodejs-postgresjs-raw') {
process.env.NODE_HANDLER = 'postgres';
}

if (cluster.isPrimary) {
if (numCPUs > 1 && cluster.isPrimary) {
console.log(`Primary ${process.pid} is running`);

// Fork workers.
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}

cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
process.exit(1);
Expand Down
2 changes: 1 addition & 1 deletion frameworks/JavaScript/nodejs/create-server.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Forked workers will run this code when found to not be
// the master of the cluster.

const http = require('http');
const http = require('node:http');
const parseurl = require('parseurl'); // faster than native nodejs url package

// Initialize routes & their handlers (once)
Expand Down
12 changes: 6 additions & 6 deletions frameworks/JavaScript/nodejs/handlers/postgres.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ const dbfind = async (id) =>
(arr) => arr[0]
);

const dbbulkUpdate = async (worlds) =>
const dbbulkUpdate = async (worlds) => {
const sorted = sql(worlds
.map((world) => [world.id, world.randomNumber])
.sort((a, b) => (a[0] < b[0] ? -1 : 1)));
await sql`UPDATE world SET randomNumber = (update_data.randomNumber)::int
FROM (VALUES ${sql(
worlds
.map((world) => [world.id, world.randomNumber])
.sort((a, b) => (a[0] < b[0] ? -1 : 1))
)}) AS update_data (id, randomNumber)
FROM (VALUES ${sorted}) AS update_data (id, randomNumber)
WHERE world.id = (update_data.id)::int`;
};

const dbgetAllWorlds = async () => sql`SELECT id, randomNumber FROM world`;

Expand Down

0 comments on commit 0ec8ed4

Please sign in to comment.