Skip to content

Commit 0ec8ed4

Browse files
1. Made the query for bulk update a bit readable. (#8555)
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.
1 parent 423e328 commit 0ec8ed4

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

frameworks/JavaScript/nodejs/app.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
const cluster = require('cluster');
2-
const numCPUs = require('os').cpus().length;
1+
const cluster = require('node:cluster');
2+
const { availableParallelism } = require('node:os');
3+
const numCPUs = availableParallelism();
34

45
process.env.NODE_HANDLER = 'postgres';
56

@@ -13,18 +14,18 @@ if (process.env.TFB_TEST_NAME === 'nodejs-mongodb') {
1314
process.env.NODE_HANDLER = 'mysql-raw';
1415
} else if (process.env.TFB_TEST_NAME === 'nodejs-postgres') {
1516
process.env.NODE_HANDLER = 'sequelize-postgres';
16-
}else if (process.env.TFB_TEST_NAME === 'nodejs-postgresjs-raw') {
17+
} else if (process.env.TFB_TEST_NAME === 'nodejs-postgresjs-raw') {
1718
process.env.NODE_HANDLER = 'postgres';
1819
}
1920

20-
if (cluster.isPrimary) {
21+
if (numCPUs > 1 && cluster.isPrimary) {
2122
console.log(`Primary ${process.pid} is running`);
2223

2324
// Fork workers.
2425
for (let i = 0; i < numCPUs; i++) {
2526
cluster.fork();
2627
}
27-
28+
2829
cluster.on('exit', (worker, code, signal) => {
2930
console.log(`worker ${worker.process.pid} died`);
3031
process.exit(1);

frameworks/JavaScript/nodejs/create-server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Forked workers will run this code when found to not be
22
// the master of the cluster.
33

4-
const http = require('http');
4+
const http = require('node:http');
55
const parseurl = require('parseurl'); // faster than native nodejs url package
66

77
// Initialize routes & their handlers (once)

frameworks/JavaScript/nodejs/handlers/postgres.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ const dbfind = async (id) =>
1717
(arr) => arr[0]
1818
);
1919

20-
const dbbulkUpdate = async (worlds) =>
20+
const dbbulkUpdate = async (worlds) => {
21+
const sorted = sql(worlds
22+
.map((world) => [world.id, world.randomNumber])
23+
.sort((a, b) => (a[0] < b[0] ? -1 : 1)));
2124
await sql`UPDATE world SET randomNumber = (update_data.randomNumber)::int
22-
FROM (VALUES ${sql(
23-
worlds
24-
.map((world) => [world.id, world.randomNumber])
25-
.sort((a, b) => (a[0] < b[0] ? -1 : 1))
26-
)}) AS update_data (id, randomNumber)
25+
FROM (VALUES ${sorted}) AS update_data (id, randomNumber)
2726
WHERE world.id = (update_data.id)::int`;
27+
};
2828

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

0 commit comments

Comments
 (0)