Sequelize: how to get datetime column value as string - node.js

I'm using Sequelize with MySQL on my Apollo GraphQL server.
Apollo part is not very important, but here's the deal.
Problem is, Sequelize is returning datetime values as Date object, when you make queries. And Apollo-Server turns the Date object to seconds(using valueOf() function of Date object) after it gets the data from the resolver. However, Apollo server only allow String datatype for Date object, so on my client's side I'm getting some thing like "385823948287" for date values.
I need to get date values as ISO String. Or any other form that moment.js accepts.
I have to make either Sequelize return DATETIME values as ISOString, or Apollo Server to make Date Objects as ISO String, not seconds in quotes.
If you know how to do just one of those jobs, please let me know.

I solved this using Graphql ISO Date. Now I'm getting proper date string.
Install the module, add scalar DateTime and change the data types of date values to DateTime in your schema. Import the module and add it as DateTime on your type resolver.

Related

Node application and insertion in Mongodb timestamp, problem with Date type and locale time

I have a node application that receives data from MQTT, where various parameters and timestamp are sent to the broker. The server and hardware are correctly configured with local time (Spain).
When I am going to do the insertion in the database, in this case MongoDB, I format the timestamp to be able to insert it in the mongo date format.
The problem is that only the javascript Date() can insert the date format in mongo and I always get two hours less than my local time.
//Format timestamp to date with dayjs
let date = dayjs(data.channels.timestamp * 1000).format()
//Output: 2021-09-16T13:32:33+02:00
//Format date to insert in MongoDB
let timestamp_ = new Date(date);
//Output: 2021-09-16T11:32:33.000Z
The correct date is: 2021-09-16T13:32:33+02:00
I tried momentjs and I have the same problem.
It is normal to have date in timezone GMT+0. Date object assumes the date is always GMT+0. Keep it like that and store everything in GMT+0.
Manipulate your query according to the time zone, not the data stored.

What does Google Big Query expects in date column?

I'm sending a JSON with some data to Google Big Query, all string and numeric columns are accepting my data just fine, however, whenever I try to send a data to the Date column it returns a error saying:
'invalid',
location: 'creationdate',
debugInfo: '',
message: "Invalid date: '2020-10-05T00:00:00.000Z'"
I'm using Node.js to work with the data and sending it to Google Big Query through the insertAll route, following the format told by them on their docs: https://cloud.google.com/bigquery/docs/reference/rest/v2/tabledata/insertAll
If I send the data as Null it works fine, I've also tried to send it as a string and as an integer, but it returns the same error.
To confirm is the data type on the BigQuery schema Date or Datetime?
You may need to truncate to fit the date format or switch to using datetime.
For some reason the Date format in Big Query only accepts YYYY-MM-DD, without hours or timezone, I had to create another column in my table so I would be able to store the hours and timezone.

Nodejs Service returning datetime instead of just date when trying to cast datetime field to date of a SQL Server Query

My database is SQL Server. I have the following sql query
SELECT RQI,RQIOver,PCI,PCIOver,PQI,PQIOver,SR,SROver,IRI,IRIOver,RUT,RUTOver,cast(DateCollected as date) as DateCollected,cast(DateCollectedOver as date) as DateCollectedOver from dbo.PF_Condition where SegmentId=12665
The result I am getting for DateCollected is just the date, which I need, when run it in SQL Management Studio. However when I put the same SQL query in a nodejs service which I created the result is not the same.
I needed to extract YYYY-MM-DD from datetime field DateCollected and DateCollectedOver as mentioned in the query above.
Can someone help me with it?
I made it work with the slice operator of javascript and sliced the date part only and displayed in the frontend where I need to. The results returned are of type string so the slice operator worked.
If someone can help why the cast function is not working in the sql query when I put it in the nodejs service it will be really helpful
After casting as DATE, you can then use the LEFT function to truncate the result to 10 characters:
LEFT(cast(DateCollected as date), 10) as DateCollected
which would return a string with the format 'YYYY-MM-DD'

How to compare date in Jooq

In the mysql db I have a field name date type date
the value of the field is as following 2019-11-05
how to query in jooq where date is mentioned as above
I tried following
java.util.Date date = SimpleDateFormat("yyyy-MM-dd").parse("2019-11-05")`
then
.where(DAILY_TRX_SUMMARY.DATE.eq(java.sql.Date(date.getTime()))
But it is not returning result.
What is the problem and how to fix it?
Use java.sql.Date.valueOf("2019-11-05") instead. Otherwise, you inherit JDBC's bad API design decisions, where a java.sql.Date might have timestamp information in some time zone.

Handling SQL date as string with node-mssql

I'm using node-mssql to get rows from a table that includes a date column (YYYY-MM-DD). I want to pass the date to a client application as a string in that same format. node-mssql is creating date objects, which I'm having to convert and slice at significant cost to get the format that I started with. sql.map.register(String, sql.Date) doesn't seem to work here (or I'm using it wrong). Is there a way to change how node-mssql handles the SQL date data type?
I'm having the same problem. sql.map.register is only for converting JS types to SQL types and I don't think there is any way to avoid SQL Date fields being converted into JS Date objects with node-mssql alone. I just changed my SQL query to convert to a formatted varchar instead of a Date type, e.g.: convert(varchar, birthday, 105) as dob.

Resources