pg-promise reads integers as string, even after being parsed - node.js

I'm having the following error:
I have a post form to insert some values to a postgres database using pg-promise. Those values are being converted to integers on the server. But when I try to insert the values to postgres, it says:
invalid input syntax for integer: ""
What I'm trying to do:
Every field that its left blank, convert it to NULL and insert it to the database, here's the code:
var tier_1 = parseInt(req.body.tier_1);
if (isNaN(tier_1)) {
console.log("Not a Number");
tier_1= null;
}
and the query:
"insert into products(tier_1) values (${tier_1})"
but postgres still reads tier_1 as a string.
Any ideas?
Thank you!

Related

Query generated in sequelize is returning values in dbeaver but not working in the program?

I have some sequelize code which is generating a query in terminal, which when I run on dbeaver for Postgres is giving me the results i need from the database, but in the program itself it is failing to fetch the results and is returning null. Am putting the code and the query here:
let whereClauseArr1 = [];
let currentDate = moment(dateNow).format('YYYY-MM-DD');
whereClauseArr1.push(db.sequelize.where(db.sequelize.fn('date',db.sequelize.col('activity_date')),'=', currentDate));
whereClauseArr1.push(db.sequelize.where(db.sequelize.col('emp_id'), emp.id));
var empData = await db.UserActivity.findAll({ where:whereClauseArr1});
The two whereClause conditions are the two limitations that we need for getting data from UserActivity table,the emp_id and activity_date. And the entire codeBlock is placed inside for loop where emp belongs to data array of different employee ids.Now empData should return a value if eg. user_activity has the date: 2022-11-05 00:00:00.000 +0530, and the current date is : 2022-11-05. The program generates the following query in the terminal:
select "x", "y", "z"
from "user_activities" as "UserActivity"
where ("UserActivity"."deletedAt" is null
and (date("activity_date") = '2022-11-05'
and "user_id" = '34d1fa38-4e0d-11ed-9641-53842d47c054'));
Now when I run this query in dbeaver, I get the proper result if I put the current date in the Postgres database, but for some reason , in the terminal, the output is always coming as null, for empData , even if I put today's date in the database.How can the query generated in the terminal run properly in the database, but fail to work for the program?Could anyone maybe please point out a solution to this situation and why there's a difference in output between dbeaver and the terminal here?

Sequlize ORM :getting Invalid date in postgres after create

I am trying to insert a string into tables that having the sequlize data type is DATE
await Image.create(
{
path: actualPath,
image_name: name,
camera_name: cameraName,
image_created_time: createdTime,
},
{ transaction }
)
a sample value of image_created_time field is 2021-11-24 17.31.35.
After insertion, I m getting the Invalid date is for the field image_created_time
Not sure how to resolve this issue,
Can somebody help me on this?
Indicate date-time values in ISO 8601 and that does the trick:
2021-11-24 17:31:35
Check inserted values directly in DB using pgAdmin or DBeaver

Fetch jsonb column of postgres db

I am using node-postgres to select and insert data into postgres. I have some column of jsonb type which I am fetching from db by using below query
getEmployee() {
return SELECT empId, empData FROM employee WHERE empId = $1;
}
where empData is jsonb type of column. Below is code snippet which use above query.
const employee = await DBService.query(pgObj.getEmployee(), [empId]);
when I am trying to get empData from employee I am getting empty value.
const { empData } = employee;
I am not sure what I am missing here. Is this the correct way to fetch josnb column of postgreas db in nodejs?
Are you sure empdata is even populated, in the database? Maybe it's empty.
Also, what are the jsonb fields of empdata?
To get the actual sub-fields of empdata, you need the ->> operator. eg:
get the whole json object as text
SELECT empId, empData::text
FROM employee where empId = $1
get individual attributes
SELECT empId, empData->>annual_pay as salary
FROM employee WHERE empId = $1;
etc...
You can also try
Have a look here: https://kb.objectrocket.com/postgresql/how-to-query-a-postgres-jsonb-column-1433
I haven't tried these out, I'm not in front of postgres right now.

Inserting Timestamp Into Snowflake Using Python 3.8

I have an empty table defined in snowflake as;
CREATE OR REPLACE TABLE db1.schema1.table(
ACCOUNT_ID NUMBER NOT NULL PRIMARY KEY,
PREDICTED_PROBABILITY FLOAT,
TIME_PREDICTED TIMESTAMP
);
And it creates the correct table, which has been checked using desc command in sql. Then using a snowflake python connector we are trying to execute following query;
insert_query = f'INSERT INTO DATA_LAKE.CUSTOMER.ACT_PREDICTED_PROBABILITIES(ACCOUNT_ID, PREDICTED_PROBABILITY, TIME_PREDICTED) VALUES ({accountId}, {risk_score},{ct});'
ctx.cursor().execute(insert_query)
Just before this query the variables are defined, The main challenge is getting the current time stamp written into snowflake. Here the value of ct is defined as;
import datetime
ct = datetime.datetime.now()
print(ct)
2021-04-30 21:54:41.676406
But when we try to execute this INSERT query we get the following errr message;
ProgrammingError: 001003 (42000): SQL compilation error:
syntax error line 1 at position 157 unexpected '21'.
Can I kindly get some help on ow to format the date time value here? Help is appreciated.
In addition to the answer #Lukasz provided you could also think about defining the current_timestamp() as default for the TIME_PREDICTED column:
CREATE OR REPLACE TABLE db1.schema1.table(
ACCOUNT_ID NUMBER NOT NULL PRIMARY KEY,
PREDICTED_PROBABILITY FLOAT,
TIME_PREDICTED TIMESTAMP DEFAULT current_timestamp
);
And then just insert ACCOUNT_ID and PREDICTED_PROBABILITY:
insert_query = f'INSERT INTO DATA_LAKE.CUSTOMER.ACT_PREDICTED_PROBABILITIES(ACCOUNT_ID, PREDICTED_PROBABILITY) VALUES ({accountId}, {risk_score});'
ctx.cursor().execute(insert_query)
It will automatically assign the insert time to TIME_PREDICTED
Educated guess. When performing insert with:
insert_query = f'INSERT INTO ...(ACCOUNT_ID, PREDICTED_PROBABILITY, TIME_PREDICTED)
VALUES ({accountId}, {risk_score},{ct});'
It is a string interpolation. The ct is provided as string representation of datetime, which does not match a timestamp data type, thus error.
I would suggest using proper variable binding instead:
ctx.cursor().execute("INSERT INTO DATA_LAKE.CUSTOMER.ACT_PREDICTED_PROBABILITIES "
"(ACCOUNT_ID, PREDICTED_PROBABILITY, TIME_PREDICTED) "
"VALUES(:1, :2, :3)",
(accountId,
risk_score,
("TIMESTAMP_LTZ", ct)
)
);
Avoid SQL Injection Attacks
Avoid binding data using Python’s formatting function because you risk SQL injection. For example:
# Binding data (UNSAFE EXAMPLE)
con.cursor().execute(
"INSERT INTO testtable(col1, col2) "
"VALUES({col1}, '{col2}')".format(
col1=789,
col2='test string3')
)
Instead, store the values in variables, check those values (for example, by looking for suspicious semicolons inside strings), and then bind the parameters using qmark or numeric binding style.
You forgot to place the quotes before and after the {ct}. The code should be :
insert_query = "INSERT INTO DATA_LAKE.CUSTOMER.ACT_PREDICTED_PROBABILITIES(ACCOUNT_ID, PREDICTED_PROBABILITY, TIME_PREDICTED) VALUES ({accountId}, {risk_score},'{ct}');".format(accountId=accountId,risk_score=risk_score,ct=ct)
ctx.cursor().execute(insert_query)

What are the returned data types from a Knex select() statement?

Hi everyone,
I am currently using Knex.js for a project and a question arise when I make a knex('table').select() function call.
What are the returned types from the query ? In particular, If I have a datetime column in my table, what is the return value for this field ?
I believe the query will return a value of type string for this column. But it is the case for any database (I use SQLite3) ? It is possible that the query returns a Date value ?
EXAMPLE :
the user table has this schema :
knex.schema.createTable('user', function (table) {
table.increments('id');
table.string('username', 256).notNullable().unique();
table.timestamps(true, true);
})
since I use SQLite3, table.timestamps(true, true); produces 2 datetime columns : created_at & modified_at.
when I make a query knex('user').select(), it returns a array of objects with the attributes : id, username, created_at, modified_at.
id is of type number
username is of type string
what will be the types of created_at & modified_at ?
Will it be always of string type ? If I use an other database like PostgreSQL, these columns will have the timestamptz SQL type. The returned type of knex will be also a string type ?
This is not in fact something that Knex is responsible for, but rather the underlying database library. So if you're using SQLite, it would be sqlite3. If you're using Postgres, pg is responsible and you could find more documentation here. Broadly, most libraries take the approach that types which have a direct JavaScript equivalent (booleans, strings, null, integers, etc.) are returned as those types; anything else is converted to a string.
Knex's job is to construct the SQL that the other libraries use to talk to the database, and receives the response that they return.
as I believe it will be object of strings or numbers

Resources