Do I always have to make one Sequelize model file per table? - javascript

I feel like I've progressed a bit far from day 0 in learning node-express + mysql + sequelize, BUT, I'm still confused like if I'm still on the right path.
I'm designing a User role models, one user has only one role model.
Questions are:
do I always have to make one sequelize model file per table?
I read about normalizing database tables, so I'm thinking not making a role_id column in my Users table, and instead make a new table. Is this right?
For instance, I have tables in my scheme design (not sure if it's the correct term, I drew my db design on https://dbdiagram.io/) namely:
Users table
Roles table
UserRoles table
USERS has id(pk), full_name, email.
ROLES has id(pk), description, user_id(foreign key????)
USERROLES has id(pk), user_id, role_id

Yes this will be very useful because:
you'll always be sure where to find a certain model - just find a file with that name in models folder.
you;ll be ale to automatically register all models at once simply by reading all files in a models folder and register them in sequelize
Yes you need a join-table (many-to-many relationship) to link users and roles.

Related

Form based on Product Types

If you have a seller accounts at amazon or ever happened to look at their product upload form, I am sure you must have noticed that for each product type you wish to upload they have a slightly different form.
If you want to sell Table Fans, you are presented with this form
and in case you wish to sell shirts, this is what is presented you.
both forms are different yet do the same thing, i.e. upload products (which has some common and few specific attributes)
What intrigues me about this design is the thought that the end user, a layman, wouldn't be expected to understand jargon like product attributes or its use. You give him a product specific upload form and he will fill it up as necessary.
Looking at a few other carts, take for instance opencart
opencart has one form for all product types and if you need to add a few extra attributes it lays the onus on you (the end user) to first understand what attributes are then work as needed.
(this is opencart's add attributes as needed)
I like the way amazon (and a few other ecommerce site, I believe ebay too does as amazon) worked this up.
I wish to replicate this for my project but honestly speaking I have never seen this before. I do not even understand what do you call this. I tried searching google for "form based on product type" and could not find anything to help me.
Can you please let me know how amazon or ebay do this.
There are many different ways of implementing this. In general, you probably won't be able to do something this complicated with HTML and Javascript alone, you'll need a database and some sort of scripting language like PHP or Ruby on your server to dynamically generate the pages.
It's not clear to me what languages you'd want to use, so I'll give a general description:
On the back-end, in a database I'd create a table for the general product. This table would have a field for product type. Then I would create additional tables for the specific product types, and have these tables store all the additional fields, and the primary key referencing the entry in the main, general product table. I would then create a table for product type, which has the product ID's used in the field in the general product table, and a second field referencing the name of the table in the database that stores this type of product.
The code that is all in common between all types of products, I would have reference the general product table. I would then allow the user to specify the product type, and when this is clicked, send the user to a new page, which would run a script. The script would query the database to retrieve the table name for that data type, and then query the database for a description of additional fields specific to that data type, and display an appropriate form, dynamically generated based on what the database returns.

JHipster - add a relationship to jhi_user_authority

I'm acutually trying to make a one-to-many relationship between the auto generated table "jhi_user_authority" and my own table called socialMediaFilter.
My situation is the following:
On the one side I have users, these are saved in the table "jhi_user" these users have roles and the roles for themselves are saved in a table called "jhi_authority". Between users and roles is a many-to-many relation, that means that many users can have many roles. These relationships are saved in a join-table called "jhi_user_authority". All these 3 tables are auto-generated by Jhipster when I created the project.
In my project I need to add some filters for filtering data from social media networks, twitter, fb and so on. A filter belongs to a role and a role can have many filters. So between these two tables is a one-to-many relationship and I'm struggling implementing this in my project.
I cannot access these auto-generated tables with the jhipster:entity generator. When I'm using for example yo jhipster:entity authority jhipster tries to generate a new entity. And as far as I know - these auto-generated tables are not meant to be accessed by the entity-generator. So when I want to edit them I need to edit them manually, so I can not follow the instructions in the tutorial.
Is there maybe someone who can pushes me to right direction? :D
thanks,
Alex
EDIT:
okay...after thinking for a while i figured out that i need to do it different
the relationships i need to implement should be like this uml
but i still have problems realizing it
Good morning, you have to do an n-m relation with the "Authority" class, in which you must specify that the relationship table is "jhi_user_authority"

JavaScript - save, store and update an array

So I am working on an application and once a user connects (via soundcloud), the following object:
{userid: userid, username: username, genre: genre, followings: followings}
is pushed into an array:
var users = [];
What I want to be able to do is for when a new user connects, to store this new user's profile object in the users array, however I want this new connector's profile to append previously logged in users in the array. So that basically the more users that log in, the bigger the array gets, creating a database of users if that makes sense?
Is there a way of doing this in JavaScript?
Thanks!
The best way to accomplish this is what you somewhat hinted at - a database. There are several databases that you could essentially store locally or in the browser cache, jStorage is one I've heard good things about. A simple search for javascript database will probably give you many other good answers.
You could also create a remote db using sqlite or any other database engine to create your container. Note that would would have to either work with an API or define some sort of content management system so you could perform the CRUD operations on the database.
The layout you provided ( {userid: userid, username: username, genre: genre, followings: followings} ) would work fairly well in a database table. You will have to define what your data types are for each of these fields, probably text or number for the user id, text for username, etc, so you can create the tables with the correct data type.
The followings field seems like it will have more than one entry, i.e. it will be a list or array, so you would probably want to create another table to house those entries and then use a primary key or some other identifier to link to it in your first table.
This question may be of some use to you: How to store a list in a column of a database table
You should create a database and store this information in it. I dont think there is a way to save users info only with JS.

How do I create a one to many relationship in Parse?

I'm trying to set up relationships in parse, but I don't know how. For instance, could I get explicit instructions on how I would link the two tables below?
Branch table consist of objectId, branchName, company, address.
Company table consist of objectId, companyName.
I would like to make a relation between Branch's company field and Company's objectId field in the manner that If I were to pull a record for a specific Branch, the string in the company field would relate to the objectId in the Company table. In real terms 1 company can have multiple branch's, but a branch can only relate to one company.
I have primarily used Access and understand how to make relationships and calls there, but cant figure out how to do it in parse. Do I need to wrote code in my main.js file? Can I do this from the data tab in my dashboard on parse.com?
Relationships in Parse are handled through the Relation data type. To make a class that uses a relation, do the following:
Go into your Parse account, open the app you want to add this class to.
Click on 'Core'.
Click on "data".
Click "Add Class:
For Class #1, call it Company.
Make sure that class is highlighted on the left side, and click "Add Col": Add the companyName as a column, and choose string as the data type.
Do these same instructions again, this time for the Branch class.
Add more columns for the branch class for branchName and address. For the company name relation, do the following:
Add a new column. For the "Select a Type" dropdown, choose "Relation";
On the middle dropdown, it will ask you what you want to relate it to, tell it which Parse class (Company), and on the right, name the column (in your case, it would be "CompanyId").

Backendless : Relationship between tables issue and advice

Good evening,
I currently have 3 tables on my API at backendless.
**Users** (objectId, email, username, password)
**figures** (objectId, figure_name, figure_cat, figure_image, figure_info)
My figures data has rows kind of like a product.
Basically, I would like to link entries in the figures table to users but i'm not sure the best way to go about it.
I can create relationships of one-to-one and one-to-many but being new to that i'm not sure the best way to go about it.
Any help or advice would be much appreciated :)
does a user have many figures?
Can multiple users share the same Figure?
Assuming that a user has many figures, so you will have a userid column in the figures table (1:M rel between user->figures). I also assume your figure_cat is a relatively static list, so consider a lookup table for that so it has a fkey in the figures table as well. Also depending on the size of images, it may be better to put images in a different table so you can put them in a different file group.
Assuming figures are shared by users, you may need a M:N table in between users and figures that stores the userid, to figureid mapping (sometimes called a "tie table" I think)
What database are you using?
You can create a relationship between the tables either directly in the console or by making an API call where you save an object which references other (related) objects. See the documentation for details: http://backendless.com/documentation/data/js/data_relations.htm

Categories