totaljs / framework

Node.js framework
http://www.totaljs.com
Other
4.36k stars 450 forks source link

Database Builder NOSQL Between date with format string timestamp? #716

Closed aalfiann closed 5 years ago

aalfiann commented 5 years ago

I've read in documentation, the between is builder.between(name, valueA, valueB); which is valueA and valueB is number.

users.nosql

{"id":"19070212570001dnd1","name":"Aziz","date_created":"2019-07-01 05:57:58.476"}
{"id":"19070212570002dnd0","name":"Tika","date_created":"2019-07-03 05:57:58.476"}

i've tried like this but result is not found builder.between('date_created', 1561935478, 1562108278);

How to solve this?

petersirka commented 5 years ago

Try to append date instead of Number ...

builder.between('date_created', new Date(1561935478), new Date(1562108278));
aalfiann commented 5 years ago

Still not working.

the result response is not found or []

petersirka commented 5 years ago

Yes because:

console.log(new Date(1561935478));
console.log(new Date(1562108278));
1970-01-19T01:52:15.478Z
1970-01-19T01:55:08.278Z
aalfiann commented 5 years ago

Finally I found the way..

builder.between('date_created', "2019-07-01 05:57:58", "2019-07-03 05:57:58");

data is found.

So builder.between the valueA or valueB for the date timestamp must be in type of string.

Thank you.

petersirka commented 5 years ago

No no. This is not good. Correctly you need add Date object. This is the best way.

aalfiann commented 5 years ago

Ok, of course I will use Date object to convert it into string.

Thank you.

petersirka commented 5 years ago

You need to convert a String to Date object. You can do it like this:

builder.between('date_created', YOUR_DATE_A.parseDate(), YOUR_DATE_B.parseDate());
aalfiann commented 5 years ago

users.nosql

{"id":"19070212570001dnd1","name":"Aziz","date_created":"2019-07-01 05:57:58.476"}
{"id":"19070212570002dnd0","name":"Tika","date_created":"2019-07-03 05:57:58.476"}

I've tried using YOUR_DATE_A.parseDate() is won't work because my timestamp format is different.

so I need to custom format to make this work like this YOUR_DATE_A = new Date(YOUR_DATE_A).toISOString().replace("T", " ").replace("Z", "");

Is something wrong?

petersirka commented 5 years ago

If you have custom date format then write a custom parser for it or prepare date format according to Total.js documentation: YEAR-MONTH-DAY HOUR:MINUTE:SECOND or DAY.MONTH.YEAR HOUR:MINUTE:SECOND. Nothing more.