CouchDB Date functions - datetime

We are in the process of converting a rather large PHP/MySQL project to Angular/Node.js/CouchDB. The main problem I am running into now is that our MySQL queries are rather complicated, using a lot of date functions (like DATE_DIFF, DATE_FORMAT, etc.) and I don't know how to convert them to this new architecture.
How do most devs handle those types of functions in CouchDB? Do they just pull the raw data from the database and leave all of the calculations up to the controller/front-end?
Example query:
SELECT DATE_DIFF(NOW(),table.datefrom) as how_long, DATE_FORMAT(table.datefrom,'%m/%d/%Y') as formatted_date FROM table ORDER BY datefrom
How would that query be handled with CouchDB?

Datetimes are not a "native" type in CouchDB. However, you have several good options that you can choose between depending on the situation.
You can use a "timestamp" numeric value. (either in the native milliseconds, or converted to seconds if needed) You can get a timestamp for "now" with (new Date()).valueOf().
You can also break up the parts of your datetimes into an array. ([ year, month, day, hour, minute, second ]) This will enable you to use grouping to "drill down" into increasingly specific time-frames as well as query based on individual parts of the date.
If you want date manipulation and formatting from a tested library, you can pull in a 3rd party module like moment.js as a CommonJS module that you can use in your view/show/list/etc.
I can see one potential issue with your example query above. You are basically getting a "seconds since" via DATE_DIFF(NOW(), ...). In a view function, you won't be able to use a "transient" value like NOW() since views need to remain unaffected by outside variables/conditions. However, this is solved by adding a list function that can take your view results and transform the output to have "relative" values like what you are trying to achieve, and can also receieve querystring arguments to further add dynamism to your view.

Related

Preventing magic numbers used in firebase realtime database

So I want to specify a time after which a post gets deleted. The time is 3 months, in my code I would define this as
const THREE_MONTHS_IN_MS = 7889400000
export const TIME_AFTER_WHICH_USER_IS_DELETED = THREE_MONTHS_IN_MS
How can I define this in my database without resorting to the use of a magic number? Basically it looks like this right now:
timeAfterWhichUserIsDeleted: 7889400000
Or as a direct screenshot of the database: https://gyazo.com/67abfdc329e1e36aae4e66b0da4b4f75
I would like to avoid this value in the database and instead have it be more readable.
Any tips or suggestions?
The 7889400000 is a UNIX timestamp, indicating a number of milliseconds since the epic. While you can store a value indicating the same moment in a different format, you'll want to make sure that the format you use still allows you to query the value.
A common format that is both readable and queryable it ISO-8859-1, and my current time in that format would be 2022-03-22 06:50:48.
I noticed after re-reading your question that your timeAfterWhichUserIsDeleted is actually an interval and not a moment. If the interval is always going to be in months, you could store countOfMonthsAfterWhichUserIsDeleted: 3 as a more readable form of the same intent. Just note that 3 is equally magic as 7889400000 and the main difference is that I've named the field more meaningfully.

MS Project: How to set daily actual work for a task using a JavaScript Add-In?

I want to synchronize data for actual work from a web-based application of my company with MS Project. I am currently developing an Add-In with JavaScript in order to achieve this:
The red circle in my screenshot shows the data that I want to set programmatically. However, I have no idea how to achieve this.
I understand that I can get Task GUIDs and then set task fields using the task GUID and the field ID. This way I can save the cumulative actual work, but not per day like in my screenshot.
The API Docs on the MS Office Website are rather hard to read and navigate. Any help would be apprechiated!
Let's first separate the language from the operation.
Operationally, based on your circle, you want to set work for a task to happen on individual days? This is done using timeScaleData, see https://learn.microsoft.com/en-us/previous-versions/office/developer/office-2003/aa206255(v=office.11) . When I did something similar (in VBA), I had to (1) get an array of time scale values, then (2) walk/iterate through that array and set work to those days:
set timeScaleValsArry = myTask.Assignments(1).TimeScaleData(startDay, endDay, pjAssignmentTimeScaledWork, daily)
for a = 1 to timeScaleValsArry.Count
timeScaleValsArry[a].value = hoursToWorkThatDay
next
Breaking down the elements above:
myTask is the task (of type task) I want to manipulate.
Assignments is an array representing each resource assigned to the task; for my purposes, I only ever had 1 resource assigned, hence the index of (1).
TimeScaleData is the function that returns the the array starting on the day startDay (whatever you want that to be), endDay, pjAssignmentTimeScaledWork which tells this function what data we want to work with (being work, but there are alternates ), and daily which is the frequency you want to work with (for instance you can go down to minutes, or up to years).
Then the returned array timeScaleValsArry is walked, and inside the loop the daily assignment for each value is manipulated. You'd need to customize this part to meet your needs; alternatively, you don't even need to loop if you always had three days: just hard code the array indices.
As far as language, clearly this is do-able in VBA. Doing this in C# as a VSTO addin has very similar syntax. I'd presume for JavaScript (what are you using, ScriptLab?) would also have similar syntax.

How to set the entire date/time for an instance?

I'm adding an extension function to Moment and it needs to change the entire Moment instance to a new date/time value. However, the available Set methods only seem to allow setting specific units (ie. day, month, hour, second).
I understand that it would probably be possible to do:
this.set('year', year);
this.set('month', month);
...
but this seems ugly and possibly error-prone (if the values adjust for temporarily invalid date/times).
You can get Date from that moment values and then use setMinutes, setYear, setMonth etc. functions on that date object.

Access 2010 Query with Parameter and Sort

I have a problem that I've been going round and round with in Access 2010. Imagine a table with these columns:
Name Date Time
Now, I have a query that asks the user to input a begin date and an end date and returns all records that are between those two dates. This works fine. However, as soon as I add a sort to the Date column things go awry. Once you put a sort on a column with a parameter the user gets asked to enter the parameter twice. From what I've been able to find out this is normal (although annoying) behavior in Access.
If I add the Date column in a second time and show the column with the sort and don't show the column with the parameter it works fine. The query would look something like:
Name Date (shown & sorted) Date (not shown & parameters) Time
Now when I run the query it all works well and comes out the way I want it to. This would obviously be a great solution then. However, there's another problem. When I save the query, leave, and reopen the query the two columns are merged back into each other. Thus, the change is lost and the user again sees two inputs.
My question is this: what can I do differently to achieve the desired results?
Some possible things I've thought about but don't know the answer to are:
Is there a way to make it so the columns don't merge? Do I have to use a form with the input boxes and take the data from that (I'd prefer not to do that as it will require a lot of additional work to handle the various things I am doing in the database). Is there some obvious thing I'm missing?
Thanks for any suggestions.
FYI: Here is the SQL from the query
SELECT Intentions.Intention, Intentions.MassDate, Intentions.[Time Requested], Intentions.[Place Requested], Intentions.[Offered By], Intentions.Completed
FROM Intentions
WHERE (((Intentions.MassDate) Between [Enter start date] And [Enter end date]))
ORDER BY Intentions.MassDate, Intentions.[Time Requested];
It is true that sometimes the Query Designer in Access will "reorganize" a query when you save it. However, I don't recall an instance where such a reorganization actually broke anything.
For what it's worth, the following query seems to do what you desire. After saving and re-opening it looks and behaves just the same:
For reference, the SQL behind it is
PARAMETERS startDate DateTime, endDate DateTime;
SELECT NameDateTime.Name, NameDateTime.Date, NameDateTime.Time
FROM NameDateTime
WHERE (((NameDateTime.Date) Between [startDate] And [endDate]))
ORDER BY NameDateTime.Date DESC , NameDateTime.Time DESC;
I have had the same problem and I have discovered the reason:
If, after you have run your query, sort a collumn in the result grid and the say yes to save changes to the query the sort action will be stored with the query. This will actually cause the query to run twice. First to create the result and then one more time to sort. You'll therefore be asked twice for the parameters.
SOLUTION: Run the query (entering your parameters twice ;-) ). Then remove the Sorting by clicking on the AZ-eraser symbol in the task bar above (in the sorting compartment).
Then open your query in design-mode and add the sorting order to the appropriate collumn.
Your are then good to go.
Regards
Jan

Control input of SQLite attribute to date format only.

I have been reading all about converting TEXT fields into date formats and ways to use Python to create date objects but my question remains.
My table has a dateTime column that is specified as TEXT, I would like to build a constraint that forces input to be in dateTime format, but as SQLite doesn't do dates (as I would like) I haven't worked out how to do it.
My current ideas: 1. Limit number of characters
2. Set separate attributes for day, month and year and constrain their domains
3. It is silly to do this on the database side just do it in the user interface
I would appreciate your opinions on these or other options.
Thanks :)
I've been trying to solve the same issue and the method I came up with was to use the date/time functions to parse the value and check that it hasn't changed. Essentially the following:
CREATE TABLE dts_test (
dts TEXT CHECK (dts IS datetime(dts))
);
If the default format is not what you want you can use the strftime or similar to design whatever format you want, but it must be something that the built-in date and time functions can parse.

Resources