how to make relation for no sql database? - javascript

how to make relation for no sql database?
You can say it for firebase database, where database is in json
format.

A NO SQL database means that database has no relations. Either go with SQL database and convert JSON format or face the truth that in NOSQL there are no relations.

Before anything, I will give you an example of how to try to do this, but I want to tell you that I wouldn't actually do this again. Firebase is no good replace for MySQL.
Making relations for a noSQL database is not possible, but you could always make them "manually" and decide how to work with it.
What I meant with "manually" is that you can duplicate data for that, but that's not a very good option. For example, I made long ago an Android app to manage neightbour communities, and because of the time I had to do it I decided to make it with Firebase.
And I will never do it again, to be honest. I didn't want to lose time on an API, but I lost it anyway trying to structure everything nicely and with all the changes I had to make every 2 days so everything wouldn't fail.
Here you have an example. The database has 2 nodes, the communities and the users.
The users have these fields:
And, meanwhile, the communities have an incidences list, and those store the email of its author as any other field (image not relatable, they are random ones).
So, TLDR: No, you can't make relations. The only way to do what you want is with duplicated data, like the email of its author on the incidence.
PS: I made a chat app with Firebase in my company, I would share the DB with you so you can see the structure, but it's confidential, you know.

Related

HTML5 QRCodeScanner for attendance system

I want to youse HTML5QR Code Scanner library in one of my projects.
First I want to say sorry for asking those very basic questions, but I am new to coding in JavaScript, I normally write in PHP.
I'd like to use this library for my school as attendance system.
My idea is: every student owns a qr code
The teacher scans the code with his mobile device. After collecting all codes, the decoded values (name of each student) are written into a database.
I have two problems with that:
1: Can I collect a list of all decoded values?
My idea is that scanning, collecting and than writing the values into the database is much faster than doing this process for every student individually.
2. How can I pass those values to PHP, which I use to write the values into my database?
Thank you in advance for your help!
Kind regards
Dan
If i caught you well i would suggest you to use the following library https://github.com/cozmo/jsQR then implement a very simple interface to allow lecturers (Non-Tech persones) to easily scan the student card and get directly a feedback as the attendance has been taken.
To enhance Fexibility, avoid saving all students details on the the QR datas but instead just save his unique identifier i.e Roll Number and then as soon as his attendance is taken, retrieve all other infomation from database.
For fasten the process, You will need to send the request using Asynchronous Request,check on https://www.w3schools.com/js/js_ajax_http_send.asp to know more about it.
Finally, Avoid complicated UI on the record page for lecturer, this will avoid misunderstanding.
I've previously worked on attendance system feel free to ask me any other question.

NodeJS/Mongoose - Logical separation of same schema + multi-tenancy

I have 2 requirements in my application:
I have multiple clients, which should be completely separated
Each client can have multiple subsidiaries that he should be able to switch between without re-authenticating but the data should be separated (e.g. all vendors in subsidiary 1 should not be shown in subsidiary 2)
As for the first requirement, I'm thinking of using a multi-tenancy architecture. That is, there will be one API instance, one frontend instance per customer and one database per customer. Each request from the frontend will include a tenant ID by which the API decides which database it needs to connect to / use. I would use mongoose's useDb method for this.
Question 1: is this method a good approach and/or are there any known drawbacks performance wise? I'm using this article as a reference.
As for the second requirement, I would need to somehow logically separate certain schemas. E.g., I have my mongoose vendorSchema. But I would need to somehow separate the entries per subsidiary. I could only imagine to add a field to each of these "shared schemas" e.g.
const vendorSchema = new mongoose.Schema({
/* other fields... */
subsidiary {
type: mongoose.Schema.Types.ObjectId,
ref: "Subsidiary",
required: true
}
})
and then having to use this a subsidiary in every request to the API to use in the mongoose query to find the right data. That seems like a bad architectural decision and an overhead though, and seems little scalable.
Question 2: Is there a better approach to achieve this logical separation as per subsidiary for every "shared" schema?
Thanks in advance for any help!
To maybe answer part of your question..
A multi tenant application is, well normal.. I honestly don't know of any web-app that would be single tenant, unless it's just a personal app.
With that said the architecture you have will work but as noted in my comments there is no need to have a separate DB for each users, this would be a bit overkill and is the reason why SQL or Mongo queries exist.
Performance wise, in general database servers are very performant, that's what they are designed for, but this will rely on many factors
Number of requests
size of requests
DB optimization
Query optimization
Resources of DB server
I'm sure there are many more I didn't list but you get the idea..
To your second question, yes you could add a 'Subsidiary' field, this would say be the subsidiary ID, so then when you query Mongo you use where subsidiar = 'id' this would then return only the items for said user...
From the standpoint of multiple request to mongo for each API call, yah you want to try and limit the number of calls each time but thats where caching comes in, using something like redis to store the responses for x minutes etc. Then the response is mainly handled by redis, but again this is going to depend a lot on the size of the responses and frequency etc.
But this actually leads into why I was asking about DB choices, Mongo works really well for frequently changing schemas with little to no relation to each other. We use Mongo for an a chat application and it works really well for that because it's more or less just a JSON store for us with simply querying for chats but the second you need to have data relate to each other it can start to get tricky and end up costing you more time and resources trying to hack around mongo to do the same task.
I would say it could be worth doing an exercise where you look at your current data structure, where it is today and where it might go in the future. If you can foresee having your data related in anyway in the future or maybe even crypto ( yes mongo does have this but its only in the enterprise version) then it may be something to look at.

Caching query results, to do or not to do, overkill or performance energizer?

Good evening,
my project uses the MEAN Stack and has a few collections and a single database from which the data is retrieved.
Thinking about how the user would interface itself with the webapp I am going to build, I figured that my idea of the application is quite a bit of a waste.
Now, the application is hosted on a private server on the LAN, making it very fast on requests and it's running an express server.
The application is made around employee management, services and places where the services can take place. Just describing, so to have an idea.
The "ring to rule them all" is pretty much the first collection, services, which starts the core of the application. There's a page that let's you add rows, one for each service that you intend to do and within that row you choose an employee to "run the service", based on characteristics that this employee has, meaning that if the service is about teaching Piano, the employee must know how to play Piano. The same logic works for the rest of the "columns" that will build up my row into a full service recognized by the app as such.
Now, what I said above is pretty much information retrieval from a database and logic to make the application model the information retrieved and build something with it.
My question or rather my doubt comes from how I imagined the querying to work for each field that is part of the service row. Right now I'm thinking about querying the database (mongodb) each time I have to pick a value for a field, but if you consider that I might want to add a 100 rows, each of which would have 10 fields, that would make up for a lot of requests to the database. Now, that doesn't seem elegant, nor intelligent to me, but I can't come up with a better solution or idea.
Any suggestions or rule of thumbs for a MEAN newb?
Thanks in advance!
EDIT: Answer to a comment question which was needed.
No, the database is pretty static (unless the user willingly inserts a new value, say a new employee that can do a service). That wouldn't happen very often. Considering the query that would return all the employees for a given service, those employees would (ideally) be inside an associative array, with the possibility to be "pop'd" from it if chosen for a service, making them unavailable for further services (because one person can't do two services at the same time). Hope I was clear, I'm surely not the best person at explaining oneself.
It would query the database on who is available when a user looks at that page and another query if the user assigns an employee to do a service.
In general 1 query on page load and another when data is submitted is standard.
You would only want to use an in memory cache for
frequent queries but most databases will do this automatically.
values that change frequently like:
How many users are connected
Last query sent
Something that happens on almost every query (>95%)

single query for fetching data from multiple collection in Mongodb/Mongoose

In a Node.js App, i use Mongodb/Mongoose. MongoDB doesn't has Joins, and this is a big problem for me. because i store data in separate collections. i have two collections. Users and Books. i need a query like this:
SELECT Books.name FROM Users,Books WHERE Users.name='john' and Books.lang='en'
im sure that we cannot do this by one single Mongoose query. Mongoose populate has also lots of problems. for example if i do this:
Users.find({user_name:'john'}).pupulate({path:'books', match:{lang:'en'}})
and if i change this code to this, it gets to another problem:
Users.find({user_name:'john'}).pupulate({path:'books', match:{lang:'en'}}).limit(6)
it fetches first 6 users item and then populate books, if there no population then result will be null. i need fetch first 6 item that belong to an specific user and has an english book.
With this code, All users with name of john will be fetched and a few of them has a book with english language. imagine users with name john is 1 million and one of them has an english book! we have to fetch that huge data and with a secondary block of code, filter that one item. its not clear and good way.
is there any other way? i has a lot of search about this but no good result. in this scenario what annoys me is fetching all users and then filter it again. this can be an slow architecture. whats the best way to me? what can i do.
i'm wondering a database engine that claims to be an enterprise one has this disadvantage. other people doesn't has this problem? there is something i'm missing?

NodeJS, express and mongoDB - mapping logic

Nothing important in this paragraph - I am still new to programming and getting the hang of nodejs, so for this question I am only asking for someone to explain the logical process I should take. I can figure it out from there and I feel as though this would be a great example for beginners like myself so I will post the code for the final solution.
The Situation
-I have one mongo database containing customers(there are 100's of customers. It has 3 fields (First, Last, Age)
-The Customers receive a monthly service. Everyday the company assigns to each of their small number of employees a random list of these customers to take care of.
-I have already created a second Schema in the mongo database that has four fields; the employee assigned to the daily list, the date, an array filled with the ID fields of many customers, and a Boolean value (employee, date, array, boolean) - {i need the second schema for archive purposes}
The problem -
I need to query the employee list (2nd) database for incomplete lists( aka false boolean values); Then create, show or active a link to a view for each of the incomplete lists, the individual views will be populated by querying the customer database(1st database) by the arguement of the ID fields that I retrieve from the array in the 2nd database. And I need to create that link and populate the view for each of incomplete lists. I am using NodeJS, Express and Jade, but like i said in the paragraph you skipped if you could just map out the logic I can get started and will post my final result.
Thank You for any attempts. It is now almost 9pmEST, I will be monitoring my post until atleast 11pmEST if anyone needs any clarification. and again tomorrow morning
-Steven R
Having what I understand, is that they have 2 databases. Parese to me a little more simple to do. Use ObjectId with a field "boolesean" or numeric, this field should modify or change the connection to the database. So solve the problem of having multiple databases and you can relate. If you use ObjectId, you can do research in other collections and in this case in other databases. use Mongoose That will help you build better databases, and search on different levels.

Categories

Resources