Data model design for Parse - parse-platform

I have currently having 4 classes in Parse.
1) Rangoli - Main item objects
2) Categories - List of all categories
3) Users - List of all users
4) ItemCategories - List of categories that each items are assigned
Below is the structure of all classes. I am facing difficulties in queries when the item has multiple categories. I am getting duplicates.
Whats the best way of designing this data model which will allow us to query by category and get all columns.
For detailed issue on the query, please refer to Relational Query in Parse

You could make category a Relation or Array.
See:
documentation
You could make category in item categories a relation, or you could add a categories column in Rangoli and scrap the ItemCategories table.
edit-
You could make Categories a column in Rangoli. Now when you query for items, you also get all their categories, You could still use your other table to get all the items in a category, and that mapping a lot easier. Now you get both use cases

Related

Laravel models, database and pivot tables question

Hello I am working with Laravel,
I have to create two simple models, let's say Stores and Books.
Stores can have one or multiple Books and Books can belong to many Stores.
Of course I will use a many to many relationship, with a pivot table.
Books the can have different prices depending the store.
I think a separate table can only complicate things, in my mind the pivot table associating books and stores should have a price column, but pivot tables only contains store_id and book_id.
Should I create a book_prices and associate it with books and to stores? What is the best approach?
You are free and able to set other attributes on your pivot table. You can read more about it in the docs.
https://laravel.com/docs/9.x/eloquent-relationships#retrieving-intermediate-table-columns
You have to define the relationship accordingly, the following should clarify how this works. In this example you use the many-to-many relationship and add the price column to every retrieved pivot model.
public function books()
{
return $this->belongsToMany(Book::class)
->withPivot('price')
}
For example, you are able to access the pivot column in a loop like this
foreach ($shop->books as $book)
{
echo $book->pivot->price;
}
You can define additional columns for your pivot table in the migration for the pivot table, and then when defining the relationship use withPivot to define the additional columns so they come through in the model:
return $this->belongsToMany(Book::class)->withPivot('price');
(Adapted from the Laravel documentation, see https://laravel.com/docs/9.x/eloquent-relationships#retrieving-intermediate-table-columns)
Depends on the complexity of your case, but yes, you have two options for it. Let's say that the pivot table is called as book_store:
Directly adds price column to book_store. This is obviously the simpler option. The drawbacks are:
The history of the price changes isn't logged. You'll have to create another table for logging if you want to keep this history information.
Changes made to price will directly change the price of the related book_store record. Meaning that a price is being updated "live" e.g users cannot update the price now but "publish" it some time later just like this example in the doc.
Create a new, different table to store the price. This may seems relatively more complex, but it may also be more future-proof.
Basically, you get 2 things that you miss in the first option above.
Don't think too much about book_store being a pivot table. One way to see it is like this: book_store IS a pivot table from books and stores tables viewpoints, but it's also just a normal SQL table which could relate to any other tables using any kind of relationships.
If you want to implement this, make sure to create a primary-key in the book_store table.
Alast, it all depends on what you need. Feel free to ask if you need more insight about this. I hope this helps.

average aggregate on nested data

What I would like to get is the average ScoreOverall of all the products within the List. I've tried adding some relationships but I can't get an aggregate "endpoint" on a nested level.
The data model:
List
|--ListProducts (linking table, one list to many products)
|--ProductScore (joined view)
|--ScoreWeighted (value I want the average across all products in this List)
Any clues on what the best approach for this would be?
If the built in GQL operations exposed by Hasura don't allow you to get everything you need your best bet is extending the schema with a custom SQL function or view

Is it possible to have repeatable objects in a object by using many to many relationship

My collections in strapi
Orders
Items
My goal
Collection Items
beer
water
sprite
example of a order
3 beer
2 water
2 sprite
My situation
My solution was to use many to many relations between collections orders and items.
So when i create a order like the example from above, i got the following result:
order
1 beer
1 water
1 sprite
It seems that strapi can't handle repeatable (same id) items in a collection. Am i doing something wrong or is there just no way to make this kind of relations between collections.
In your case, I suggest you use a component (repeatable) to do that.
In the Content Type Builder create a new component.
With a number field and a relation field with your products.
In your Order Content Type you add a component field, select the repeatable option and use the component you just created.
With that, in your Orders, you will be able to add a new item, specify which item and the numbers of items (with the interfere field)

Query and comparing 2 tables

i have a table that is stored all records of products, and than i have other table that stores all records that i bought in the table products.
In one of my pages i list all my products, but i will to list only the products that were not bought from the user. How can i query the listing of products to only show products that i didnt bought? How can i make this comparation or intersection in eloquent?
In my case the producst are information from users, or more specifically answers from Inquiries, the identifyer of the user is by email, i query by group of emails, and the email is unique for each survey.
ex:
$survey = $survey->answers->groupBy('email');
As you didn't provide any details of the tables, i will make some assumptions:
The product table has an id coloumn, the second table has some form of product id, to see what has been bought.
You should take a look at this:
DB::table('bought_stuff')->whereNotIn('email', [1, 2, 3])->get();
you can read about it here:
https://laravel.com/docs/5.3/queries#where-clauses under "whereNotIn".
It is a little difficult for me to answer, as i'm not completely sure about what you mean in the last part..

How to model this mysql table relationship in elastic search

I have a large amount of shop items imported into elastic search and I can query them.
I am wondering how best to model the following mysql table relationship into elastic search:
Shop items can have different offers. There are different offer types. And in some shops an item may be on offer, in other shops the item may not be on offer or have a different offer type. Items don't have to have offers. I model this below:
Items table
item_id
Offers table
shop_id, item_id, offer_type, user_id
Where user_id is the id of the user who created the offer.
So as an example, item_id 1 and shop_id's 1,2 and offer_types premium and featured.
Then the offers table could look like:
shop_id, item_id, offer_type, user_id
1,1,featured,45
2,1,premium,33
2,1,featured,45
But it's not the case that every item is on offer. And even if item_id 1 is on offer in shops 1 and 2, it might not be on offer in other shops.
I want to be able to query my /items type and it will only be for one shop at a time but for that shop I want to get all the items in e.g. a certain price range and of a certain category (that i can do all ready), but I need to know for each item in the results what offer they have if any (e.g. if featured, premium or whatever offer_type).
How can I best model this behaviour in elastic search?
One approach is Nested Object relationship - Shop contains set of items with id as your shop id
For your cases
1) Get all items of a shop - GET: http://host/your_index/shops_type/shopid
This will give you all items in a shop along with offer_type. you can filter in your program logic

Resources