How to store a time value in MongoDB using SailsJS v0.11.3? - node.js

I'm working with SailsJS and MongoDB and I have an API that has two models and I want to store Date and Time separately but as in the official documentation said it doesn't have a typeof Time attribute, just Date and DateTime. So, I'm using DateTime to store Time values.
I send the values in the request to be stored in the database, I have no problem to store dates, I just send a value like:
2015-12-16
and that's it, it is stored in the table with no problem, but when I want to store a Time value like:
19:23:12
it doesn't works. The error is:
{
"error": "E_VALIDATION",
"status": 400,
"summary": "1 attribute is invalid",
"model": "ReTime",
"invalidAttributes": {
"value": [
{
"rule": "datetime",
"message": "`undefined` should be a datetime (instead of \"19:23:12\", which is a string)"
}
]
}
}
So, any idea how to send the time value to be stored in a DateTime attribute?
I also have tried to send it in different formats like:
0000-00-00T19:23:12.000Z
0000-00-00T19:23:12
T19:23:12.000Z
19:23:12.000Z
19:23:12
But any of them works fine.
Also I was thinking to store both values (Date and Time) in plain text, I mean typeof String attributes. But I need to make some queries and I don't know if it will affect the performance with the waterline ORM.
Please any kind of help will come in handy.
Thanks a lot!

You have two options here. The best would be to simply store the date and time as a datetime and parse them into separate values when you need to use them. MongoDB will store this in BSON format and this will be the most efficient method.
Otherwise, you could use string and create a custom validation rule as described in the Sails documentation.

Related

JMeter: Connect to PostGresSQL in JSR using groovy and then compare values from multiple tables in DB with API response

Sorry for the long post, but I really need some guidance here. I need to compare values from an API response with the values from multiple tables in the DB.
Currently, I am doing it as follows:
Use a JDBC Connect Configuration to connect to Postgres DB and then use the JDBC Sampler to execute queries. I use it three times to query 3 different tables. I store this data in variables (lets call them DBVariables). Please see this image for current Jmeter setup. https://i.stack.imgur.com/GZJyF.png
In JSR Assertion, I have written code that takes data from various DBVariables and compares it against the API response.
However, my issue is the API response can have an array of records and then nested arrays inside each (please see API response sample below). And these array elements can be sorted in any order. This is where I have issues.
I was wondering what would be the most efficient way of writing this JSR Assertion to validate all data elements returned by the API are the same as what is in the DB.
I am very new to groovy, but I think if I can query the DB inside the JSR assertion (instead of using the JDBC sampler), then the comparison can be done by storing API response in a map and then the DBResponse in another map and sorting both and comparing the items.
My questions are:
How can I connect to postgressql using groovy and then execute query statements in it? I have not done that before and was hoping if someone can provide a sample code.
How can I store API response and DB responses in Map and sort them and compare them in groovy?
The API response is of the following type:
{
"data":{
"response":{
"employeeList":[
{
"employeeNumber":"11102",
"addressList":[
{
"addrType":"Home",
"street_1":"123 Any street"
},
{
"addrType":"Alternate",
"street_1":"123 Any street"
}
],
"departmentList":[
{
"deptName":"IT"
},
{
"deptName":"Finance"
},
{
"deptName":"IT"
}
]
},
{
"employeeNumber":"11103",
"addressList":[
{
"addrType":"Home",
"street_1":"123 Any street"
},
{
"addrType":"Alternate",
"street_1":"123 Any street"
}
],
"departmentList":[
{
"deptName":"IT"
},
{
"deptName":"Finance"
},
{
"deptName":"IT"
}
]
}
]
}
}
}
Have you seen Working with a relational database chapter of Groovy documentation? Alternatively you can obtain a Connection instance from the JDBC Configuration Element like
def connection = org.apache.jmeter.protocol.jdbc.config.getConnection('your-pool-name')
With regards to "sort" There is DefaultGroovyMethods class which provides sort() function for any "sortable" entity. With regards to "compare" - we don't know how the object from the database looks like hence cannot provide a comprehensive solution.
Maybe an easier option would be converting the response from the JDBC Sampler to JSON using JsonBuilder and once you have 2 JSON structures use the library like JSONassert which doesn't care about order and depth
You haven't asked, but if you're "very new to groovy" maybe it worth extracting individual values from API using JSON Extractor, do the same for the database with the JDBC elements and compare individual JMeter Variables using Response Assertion?

Selecting appropriate Noda time structures

I have the following data which i was querying with .net time and ran into issues with timezones and spans. I was recommended to use Noda Time.
"MarketStates": {
"dataTimeZone": "America/New_York",
"monday": [
{
"start": "04:00:00",
"end": "09:30:00",
"state": "premarket"
},
{
"start": "09:30:00",
"end": "16:00:00",
"state": "market"
}
],
"holidays": [
"1/1/1998",
"1/1/1999",
"1/1/2001"
],
"earlyCloses": {
"7/3/2000": "13:00:00",
"7/3/2001": "13:00:00"
}
}
I am writing a function IsMarketOpen providing a time to test against, and the above MarketStates json database - it returns true if the current time is during market open and false if a holiday or earlyClose.
For the market states (monday above) I will use a LocalTime.
For the earlyCloses I plan to use ZonedDateTime.
For the passed time into this method, I will use a ZonedDateTime.
For holidays would I need to keep the timezone? I cannot find a ZonedDate, only OffsetDate or LocalDate?
In summary, should I keep everything ZonedDateTime (since i have the time zone specified in the json database snippet above), or use a LocalDateTime and then perform the conversion/testing at that point?
Please bear with me for the above question I didn't realize that time is actually so hard and need guidance for structure selection, I will adapt as per comments if extra context is needed. Thank you.
Your earlyCloses looks like it's really a Dictionary<LocalDate, LocalTime>, and holidays is a List<LocalDate>. (As a side note, it's pretty awful that it's not using ISO-8601 for the date format... I can't tell whether those early closes are July 3rd or March 7th.)
The time zone is specified by dataTimeZone, but I'd suggest keeping it as a string in the model, and converting it to a DateTimeZone when you need to.
The thrust of what I'm saying is that I'd encourage you to make the values in your direct model (loaded from JSON and saved to JSON) match what's actually stored in the JSON. You could have a wrapper around that model which (for example) converted the early closes into ZonedDateTime values... but I've generally found it really useful to keep the "plain model" simple, so you can immediately guess the representation in the JSON just from looking at it.

Mongodb and Node.js datetime timezone issue

I am having hard time to understand the concept of querying date in mongodb/node.js, even after going through many SO and other articles in google.
While storing the date from a react datepicker  in mongodb it gets stored a day less as per my time zone but when it is fetched it is shown the same,I can understand that it is again being converted into locale
but how do query responds?
When I query  mongodb from node.js/mongoose - I don't find the correct resultSo my question is while querying the mongodb - how dates are passed to mongodb ?
for example :
post.find({publish_date:{ $gte:new Date()}}
What is the new Date value - localtime zone of the server? or  browser?
is the new date value converted to utc while comparing the database or compared as passed from the server ?
I have a post model where I want the post to be published on the published_date as per my timezone.
for example - if I put 2021-01-21 mongo stores 2021-01-20T18:30:00.000+00:00 - it is understood that it stored as UTC
Now when I am querying the post to be published when the server date/time is 2021-01-21 - it is not fetch any document!
When I change the publish_date as 2021-01-22 mongo stores 2021-01-21T18:30:00.000+00:00 then query produces on document.
I am not sure how to solve this problem
Any help in this regard would be helpful
Thanks.
In MongoDB all Date values are stored as UTC-Times only.
Your client application is responsible to display these UTC times as local times, if required.
MongoDB does not preserve the input time zone, if you need this for any reason then you have to store it in a separate field.
In Mongo you can display locals date/times values with $dateToString:
{ $dateToString: {
date: <dateExpression>,
format: <formatString>,
timezone: <tzExpression>,
onNull: <expression>
} }
Whenever one has to work with date/time values then I recommend the moment.js library.
The query could be this for example:
{ $gte: moment().startOf('day').toDate() }
I am in Switzerland where we have UTC+01:00, thus moment().startOf('day').toDate() returns ISODate("2021-01-20T23:00:00Z")

Forcing a string field to DateTime in WCF query with Azure Table Storage

So, a quick overview of what I'm doing:
We're currently storing events to Azure Table storage from a Node.js cloud service using the "azure-storage" npm module. We're storing our own timestamps for these events in storage (as opposed to using the Azure defined one).
Now, we have coded a generic storage handler script that for the moment just stores all values as strings. To save refactoring this script, I was hoping there would be a way to tweak the query instead.
So, my question is, is it possible to query by datetime where the stored value is not actually a datetime field and instead a string?
My original query included the following:
.where( "_timestamp ge datetime'?'", timestamp );
In the above code I need to somehow have the query treat _timestamp as a datetime instead of a string...
Would something like the following work, or what's the best way to do it?
.where( "datetime _timestamp ge datetime'?'", timestamp );
AFAIK, if the attribute type is String in an Azure Table, you can't convert that to DateTime. Thus you won't be able to use .where( "_timestamp ge datetime'?'", timestamp );
If you're storing your _timestamp in yyyy-MM-ddTHH:mm:ssZ format, then you could simply do a string based query like
.where( "_timestamp ge '?'", timestamp );
and that should work just fine other than the fact that this query is going to do a full table scan and will not be an optimized query. However if you're storing in some other format, you may get different results.

Wrong time format fetched

I am using Node for fetching data from MySQL. In database, i got record like : 2013-08-13 15:44:53 . But when Node fetches from Database , it assigns value like 2013-08-19T07:54:33.000Z.
I just need to get time format as in MySQL table. Btw ( My column format is DateTime in MySQL)
In Node :
connection.query(post, function(error, results, fields) {
userSocket.emit('history :', {
'dataMode': 'history',
msg: results,
});
});
When retrieving it from the database you most likely get a Date object which is exactly what you should work with (strings are only good to display dates, but working on a string representation of a date is nothing you want to do).
If you need a certain string representation, create it based on the data stored in the Date object - or even better, get some library that adds a proper strftime-like method to its prototype.
The best choice for such a library is moment.js which allows you to do this to get the string format you want:
moment('2013-08-19T07:54:33.000Z').format('YYYY-MM-DD hh:mm:ss')
// output (in my case on a system using UTC+2 as its local timezone):
// "2013-08-19 09:54:33"
However, when sending it through a socket (which requires a string representation) it's a good idea to use the default one since you can pass it to the new Date(..) constructor on the client side and get a proper Date object again.

Resources