Structurizing data in firebase - javascript

I have recently started to use Firebase. However, since I am not familiar with NoSQL databases I am having a little trouble structurizing it.
I am developing a timesheet application, several users can input their starting and ending hours each day they go to work and it will be saved into a database.
At the moment the structure of my firebase looks like this :
However, I am having some trouble accessing this data in my application. On top of that this just doesn't feel right. First I wanted to just add a new entry under 'timesheet' every time a user inputs something, but obviously, I do not want a user to be able to add 2 entries for one day either.
I know that there's probably some complex way to stop a user from doing this, but I feel that this could all be solved in an easier way if I just saw how I should best structure this database.
Later I want to loop through all the days in the current month for a specific user to show him in a table all his starting/ending hours for each day of the month.
Update: I was thinking about denormalizing my database, but would that really help anything?

You should read this blog post, written by the Firebase team.
TL;DR is that you should denormalize your data because Firebase is optimized for certain kinds of operations (you don't need to fully understand the nitty gritty of that optimization to use Firebase properly).
"I am having some trouble accessing this data in my application"
What kind of trouble?
"I do not want a user to be able to add 2 entries for one day either."
That isn't really a denormalization problem; with your structure now, you could just check a path for null.
new Firebase('path/to/timesheets/April/'+ queryDate) === null, where queryDate is the date you want to check for.
If the above returns true, your user hasn't submitted a timesheet. If so, you shouldn't allow them to.
"I want to loop through all the days in the current month for a specific user to show him in a table all his starting/ending hours for each day of the month."
You can! Iterate with a for loop through all the values nested under the object that's returned when you ask for new Firebase("path/to/April").

Related

SQL Database Design Trains and Containers

I am trying to figure out the best database schema for the following. I will be using Postgres along with Nodejs unless there is something better suited to this task.
I apologize if the answer is obvious. I am new to all of this.
I have a list of train-id's and a list of intermodal containers. I need to be able to query by intermodal container to check which train-id it is on, and also to query by train-id to get a list of all intermodal containers on this train. I would also like to be able to query historical information.
The issue I have is the train-id's repeat once per month so I can't use them as a primary key, or query just based on train-id as it would return containers on the train-id from previous months as well.
The best that I have come up with so far is to create a composite key consisting of the date of departure and the train-id and query based on this to get the list of containers. The day of departure is already included in the train-id, however the month of departure is not. I'm not sure how to make this user friendly though as preferably the user would not have to specify the month of departure.
For querying by container-id I believe I could just limit the result to 1 to only get the most recent train it is on, or is there a better way of doing this?
There will be other details stored in this database as well such as ETA's, car numbers, etc. However the above is what I'm having difficulty with currently.

Is there an alternative to a TTL Index in MongoDB that won’t permanently delete documents?

I am building a job board using Node.JS / MongoDB. After a job listing is purchased by the user, it is added to the database and, using a TTL index, it deletes after 30 days. I’m wondering if there’s a way to change a field vs. deleting the entire document? I ask because I would want to give the user the option to “renew” their listing after the expiration period. What would be the best way to approach this?
You can add a field called "renewalDate", initialize it to the create date and then query items that have a renewalDate that is less than 30 days back from now for items to display. Then, to "renew" a listing, you just set the renewalDate to a more current date so it will appear in the query again.
You could then run a periodic task (once a night or once a week) to permanently delete any documents that are old enough that they aren't even eligible for renewal any more. Or you could use the TTL feature to manage this.

architecture for get and store api request data

This is more of a architectural questions. An external platform had product and price information for let's say, books. There is an API available to get this information.
What I read is that it should be possible to create a function in Javascript and connect the Javascript to a page where you want to show the data on my own website. This would mean that for each page request an API-call is made. Since the requested information only changes once a day maximum this does not sound the most efficient solution.
Can someone advise a better solution? Something into the direction of a similar php or javascript function that does the request on the background, schedule an update and import the data into mysql? If so, what language would be most common.
I need the solution for a Joomla/php/mysql environment
Here's a simple idea - fetch and store results from the API (ones you think aren't gonna change in a day), either on disk, or in the database, and later use these stored results to retrieve what you otherwise would've fetched from the API.
Since storing anything in frontend JS across page reloads isn't easy, you need to make use of PHP for that. Based on what's given, you seem to have two ways of calling the API:
via the frontend JS (no-go)
via your PHP backend (good-to-go)
Now, you need to make sure your results are synced every (say) 24 hours.
Add a snippet to your PHP code that contains a variable $lastUpdated (or something similar), and assign it the "static" value of the current time (NOT using time()). Now, add a couple of statements to update the stored results if the current time is at least 24 hours greater than $lastUpdated, followed by updating $lastUpdated to current time.
This should give you what you need with one API call per day.
PS: I'm not an expert in PHP, but you can surely figure out the datetime stuff.
It sounds like you need a cache, and you're not the first person to run into that problem - so you probably don't need to reinvent the wheel and build your own.
Look into something like Redis. There's an article on it available here as well: https://www.compose.com/articles/api-caching-with-redis-and-nodejs/

mongoDB use timestamp from _id to evaluate whether its been accessed within last 24 hours

first time posting something, until now lurking always brought some answers. I hope i can also give back this way, but just started out programming a few months ago.
so here we go: I run a website without a Database backend yet and want to add some functionality. The website is currently running on a vps with nodejs and nginx. The database JSON files should look like that:
{account:"1241423",
counter:0,
_id:"someid"}
evertime someone passes me an account number through the webform i want to check if its inside the DB, no problem with that. When i find it i also want to check when its last been accessed (using the timestamp of the _id), if it was more or less than 24 hours ago. if its been more than 24 hours ago i want to increment the counter by 1 and also want to give it a new _id so the time of access gets updated as well. I went through some of the documentation and a few articles and yt videos but didnt quite find what i was looking for.
so lets say i save the JSON file to a temporary js object called "accountInfo":
how do i check whether the creation/update date is more or less than 24 hours in the past by using the accountInfo._id property?
edit: iam currently using mongodb with nodejs.
ObjectId carries only one timestamp and, normally, it is of its creation (obviously). If you want to check when an item was last updated (or accessed), you have to have separate fields for this, and also write code in your app that keeps these timestamps accurate (touches updated_at when you update a document, etc.)
You can extract the timestamp portion by using getTimestamp() method
my_accountinfo._id.getTimestamp()

Implementing an Employee Schedule in MongoDB

Hi Stackoverflow Family,
So this is a pretty big question; be prepared to read quite a bit.
Basically my team and I working on an Employee scheduling application (we're already well into it, so please nothing about changing our stack or anything like that). Anyways, we're building this with PHP, Mongo, JavaScript, JQuery, and Bootstrap; and I'm working more on the db side of things.
What I really want to find out after asking this question is to see if my current approach to the db is right; I've basically created several collections and documents that reference each other in order to access or reference specific data that I'm looking for. If that doesn't really make sense check out my schema below:
Employee Collection - Contains indexes such as name, employeeNumber, address, etc, employee availability, position & department.
(My issue here is I want it to reference other collections which contain their Shift information, but I can only really do that when I insert a document).
Shift Collection - Contains indexes such as shiftNumber, shiftStartTime, endTime (This collection, I basically want to reference to employee, such that for creating each employee I have their shift time connected to it).
**Schedule Collection ** - Now for the Schedule collection, this is the one that confuses me the most; I basically want our Calendar UI to be able to look through our schedule collection and be able to pull all the shifts in a certain day, or in a specific week. But I have no idea as to how I can approach this from the backend.
So far what I've done with the Schedule Collection is that I've mathematically created a Calendar year and placed that within the Schedule; basically it contains a document called Year, and in that Year it contains every day of the week with information such as day number, week number, leapYear, etc.
Anyways, I hope this is enough information; my main confusion is the with the main schedule, I think I nearly have the Employee collection functioning properly since it references the Department class with no issues. I just mainly can't figure out how to implement a full schedule in mongo!
Thanks guys!

Categories