I have a table - available_trucks
where I want to update the column - available_date
.
This available date I am planing to be update every day at midnight but now for test purposes I try to update every one minute with this library in nodejs
https://www.npmjs.com/package/node-schedule
I am using also knex js as sql builder.
My code
SERVER JS( MAIN FILE )
const truckJob = require('./jobs/trucks-job');
schedule.scheduleJob('*/1 * * * *', (fireDate) => {
console.log('This job was supposed to run at ' + fireDate + ', but actually ran at ' + new Date());
truckJob.updateAvailableTrucks();
})
TRUCKS-JOB
const db = require('../db/knex');
function updateAvailableTrucks() {
db("available_trucks").update('available_date', '2020-12-13T04:47:56.126').where('truck_number',
'228');
}
So here for test purposes I am putting hardcoded date value, where truck_number
is 228. I want to mention that I checked and the data in my available_trucks
- table exist so here I am try to update existing column in my table. But after one minute when the script is again executed, the column in the table is not updated. Also when I try to get the data for example from some table then everything is working well.
For example
function getAll() {
return db.select('*').from('available_trucks');
}
My knex configuration KNEX JS
const config = require('../knexfile.js');
const pg = require('pg');
const PG_DECIMAL_OID = 1700;
// workaround that ensures numeric types are read as numbers, not strings
pg.types.setTypeParser(PG_DECIMAL_OID, parseFloat);
module.exports = require('knex')(config);
KNEX FILE JS
const config = {
client: 'pg',
connection: {
host: process.env.POSTGRES_HOST || 'localhost',
database: process.env.POSTGRES_DB || 'some_db_name',
user: process.env.POSTGRES_USER || 'postgres',
password: process.env.POSTGRES_PASSWORD || 'xx
},
pool: { min: 0, max: 10 },
migrations: { directory: './db/migrations' }
};
module.exports = config;