Nodejs sequelize many to many relation ship mapping in single table with add, update, delete - node.js

I have multiple type of posts (Post, Product, Image) and one category option that has multiple categories. I want to assign multiple category to post but i don't want to create more than one table for store relationship information with post type.

Related

Save product images in external table or in JSON column and parse them - PostgreSQL

I'm working on an e-commerce website and there are products and every product has many images related to it.
There is a table called products to save the products and every product should have many images, the product images are the links for images uploaded on external storage something like Amazon S3 so I only save the image link in the database and every product should have an array of strings and every string is the product image url.
for example: ["domain.com/images/product_image_1.jpg", "domain.com/images/product_image_2.jpg"]
I tried 2 solutions to save the product images, and those solutions are working well but I'm confused about which one is better or which one I should follow:
Solution 1:
Created a table called product_images and this table has 5 columns (id, product_id, image_url, created_at, updated_at) and storing the images links in it and every product has many images related to it and then creating an API to make the CRUD operations on product images.
Solution 2:
Created a column in the products table called images and set the type JSON and saved the images array in it so I can use them directly using the product CRUD APIs.
I don't know which one is the best solution but I think the second solution will be simple and faster
I saw everyone create a second table to save the product images links and join them when fetching the products list or the product details but I think the join will affect the performance and the query time.

What is best way to delete all rows within an arraykey?

I have an entity Contact which has an array entity Specialties. Just a standard one to many relationship. One contact many specialties. (Specialties entity has few columns, might not be relevant). I have a screen to add list of Specialties to a contact on the PCF. I want to add a custom Remove All button on the Contact screen which will delete all values on the array against the specific contact. A contact can have large number of specialties (~10000)
What is best way to delete all the elements in the array?
Currently, I have the below function on the action property of the button and it is clocking and timing out.
for(spec in contact.Specialties)
{contact.removeFromSpecialties(spec) //OOTB remove from array method}
Any other better way to remove ~10000 records from the array entity?
From your question above, I assume that you will have a "Remove All" button in PCF screen with an action that deletes all the speciality records associated with that particular contact, but you will not delete partial records (like one or few records).
Also I assume the entity type of "Speciality" entity is "Editable" or "Retireable".
Keeping the above assumption in mind, you can create a new function and call that function from "Remove All" button action property.
Given below the code that will hard delete the entire records from database table just like you execute a delete query against a DB table,
function removeAllSpecialitiesForContact(contactFromPCF : Contact){
var specialityQuery = Query.make(entity.Speciality).compare(Speciality#Contact, Equals, contactFromPCF.ID)
var deleteBuilder = new com.guidewire.pl.system.database.query.DeleteBuilder.DeleteBuilder(specialityQuery)
deleteBuilder.execute()
}
This new approach might not get timeout after PCF action as you faced already.

Handling Record List operations

I have a requirement where I need to maintain records of data in database table via SharePoint, provide powerapp or other solution based interface so that I could do the following+
Add, Update or Edit the Package records.
Pick desired records and create Parent to group them.
I heard that above mentioned grouping desired records ( building parent child ) relation is not supported in SharePoint either OOTB or Custom approach?
Request suggestion or direction that could be taken, if anyone has done something similiar.
You can't directly create parent-child relationships in sharepoint, what you can do is work around the IDs. Let's say you want to create a database of books... Every book has an author, and you want to store the data of the author as well.
You can create a table for authors, and a table for books. Each author you add to the list will have a name, surname, email, etc, and an unique Author ID.
Now you want to add a book for that author. In the book table you would give the book an ID, name, published date, etc, and instead of the name of the author, you would give it the ID of the author referencing the author data in the other table. If you want the author's name (for instance), all you need to do is search in the Author's table for that author ID.
This has been my workaround when doing parent-child tables in Sharepoint. Hope this makes sense to you. Best regards

Snaplogic querying two sources and joining data together

I am trying to build a Pipeline which queries out my Sales records (as one Read activity)
Now in this Sales schema there are fields that reference a People table however its not a direct connection as there is a Many-to-Many relationship.
So what I need to do is query my PeopleToSales table for all related records and populate them in a flat structure in my subsequent JSON object.
How can I built two objects together and join them based on Sales ID? Also in the event there are multiple matches how can I choose the first one?
You can read both the Sales records and the PeopleToSales table and then use the Join snap to merge the relevant documents based on whatever ID that defines the relation between them.
After that, you can use the Group By Fields snap to group the documents based on Sales ID.
You can add the Sales ID field (say - $sales_id) in the Fields list in the settings and it will group documents based on the Sales ID.
Also, when using the Group By Fields snap, you first have to sort the documents based on the keys. So, use a Sort snap before the Group By Fields snap.
As far as getting the first object is concerned, after the group-by, you can just get the 0th element of the list (say group[0]).
Please refer to - SnapLogic Docs - Group By Fields

Creating database tables with same columns or one master table

I am building a website with large database, there's 6 types of data, so 6 forms to pass data to database.
Each form has unique parameters, and 4 of 6 forms have the same fields and the fields can contain multiple data: email, address and phone can be multiple on 4 forms.
For the first i wanted to created 4 different tables like: store_contacts, warehouse_contacts, delivery_contacts, etc. to keep different types separated.
so i would have 4 similar tables containing the same fields:
id, phone, email, address, store_id/delivery_id/etc
I have read that better practice to create one table containing them, table Contacts:
id, type, type_id, phone, email, address
from similar questions:
Two tables with same columns or one table with additional column?
https://softwareengineering.stackexchange.com/questions/302573/one-wide-table-or-multiple-themed-tables
https://dba.stackexchange.com/questions/46852/multiple-similar-tables-vs-one-master-table
But i'm not sure if tables will change later and new fields will be added for store only or only for delivery. and apart from contacts i have similar situation for other fields.
Would it be comfortable to make queries with type every time i need to pull data for certain type or when i need to delete them? Won't it get messy when a lot of rows will be inserted? And if a new field will be created for 'store', it is okay that others will contain NULL on that field?
Probably you should read a bit about Relational Entities or Object Orientation - inheritance, depending on the paradigm you are working.
For example, you can get aware about it in articles like this
Usually you should store contacts in a separate and exclusive entity, for a plenty of reasons. Sector-specific fields can be stored in each table, only if you are sure that there would be no use for them in another entities. For example: warehouse_contacts would have an imaginary employee id field to represent an employee in warehouse repsonsible for attending a given contact. Even though, proably the best practice would be to build a third table managing this information.
Nevertheless, if performance is an issue, I mean, if you have millions of records and dozens and dozens of simultaneous access in your website, maybe your Data Base would run faster in fewer tables, not so normalized. But this situation is quite improbable for most enterprises and users. Rather, this situation is kind a common practice in large-scale and legacy systems.
Good luck.

Resources