Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node.js updates #8555

Merged
merged 1 commit into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading