Restructure data from SQL base to Realtime Firebase - firebase

I'm trying to set up a structure for my app moving from SQL structure to Firebase structure. At the moment I'm using the following:
-SQL-
Product: {id, name, workPrice}
Material: {id, name, unitCost}
Product_Material{ pId, mId, amount}
I'm using a table to set the number of materials used so I can get an overall cost of the product.
I read about firebase structuring but I don't know how to apply to this case. What is recommended when associating the two would be the following:
-FB-
Product: {
boxId: {
name: "Wooden box"
workPrice: "5"
materials: {
"woodId": true
}
}
},
Material: {
woodId: {
name: "wood",
unitCost: "10"
}
}
But since I need an amount, it doesn't fit. How would this apply to my case? Do I need to make a third object the same as the third table in SQL?

Since Firebase is a No-SQL database, you can relation two objects by id's, for example, if a product has materials, you can reference the material id of that product, then you query for the product and get that material id, and with that material id, you go to the material node and lookup for that material.
Example
Product: {
name: "Wooden box"
workPrice: "5"
materials: {
"materialId": true,
"materialId2": true
}
},
Material: {
materialId: {
name: "wood",
unitCost: "10"
},
materialId2: {
name: "Plastic",
unitCost:"15"
}
}
So, in this example, lets say you query the wooden box product, when you iterate over the materials sub node of product, you can get each material ID, and then you can relation those material id's with each product.
you can generate random materials ids with the .push() method in Realtime Database or .add() with Firestore

Related

firebase what is the best way/structure to retrieve by unique child key

I have a firebase database like this structure:
-groups
--{group1id}
---groupname: 'group1'
---grouptype: 'sometype'
---groupmembers
----{uid1}:true
----{uid2}:true
--{group2id}
---groupname: 'group2'
---grouptype: 'someothertype'
---groupmembers
----{uid1}:true
----{uid3}:true
----{uid4}:true
Now, I am trying to pull groups of authenticated user. For example for uid1, it should return me group1id and group2id, and for example uid3 it should just return group2id.
I tried to do that with this code:
database().ref('groups/').orderByChild('groupMembers/' + auth().currentUser.uid).equalTo('true').on('value' , function(snapshot) {
console.log('GROUPS SNAPSHOT >> ' + JSON.stringify(snapshot))
})
but this returns null. if I remove "equalTo" and go it returns all childs under 'groups'.
Do you know any solution or better database structure suggestion for this situation ?
Your current structure makes it easy to retrieve the users for a group. It does not however make it easy to retrieve the groups for a user.
To also allow easy reading of the groups for a user, you'll want to add an additional data structure:
userGroups: {
uid1: {
group1id: true,
group2id: true
},
uid2: {
group1id: true,
group2id: true
},
uid3: {
group2id: true
},
uid3: {
group2id: true
}
}
Now of course you'll need to update both /userGroups and /groups when you add a user to (or remove them from) a group. This is quite common when modeling data in NoSQL databases: you may have to modify your data structure for the use-cases that your app supports.
Also see:
Firebase query if child of child contains a value
NoSQL data modeling
Many to Many relationship in Firebase

How to insert data in to datatstore

How to insert data in to datastore?
The data could be like the one below:
{
'food': [{
"item_name": item,
'price': price
}, {
"item_name": item,
'price': price
}],
'beverages': [{
''
'beverage_name': beverage,
'beverage_price': b_price
}, {
''
'beverage_name': beverage,
'beverage_price': b_price
}]
}
The data that you are trying to add to the Google Cloud Datastore is a JSON string. The way you have it in your question is wrong structured. The proper JSON example would be:
{
"food": [
{ "food_name":"NAME1", "food_price":"PRICE1" },
{ "food_name":"NAME2", "food_price":"PRICE2" },
{ "food_name":"NAME3", "food_price":"PRICE3" }
],
"beverages":[
{ "beverage_name":"NAME1", "beverage_price":"PRICE1" },
{ "beverage_name":"NAME2", "beverage_price":"PRICE2" }
]
}
To add the data from the JSON string to the Datastore you have to:
Load the JSON string as JSON object to be able to go through its fields
Create a client to access Google Datastore
Set the key food for the Kind value in Datastore
Use the entity to add the data to the Datastore
Set the key beverages for the Kind value in Datastore
Use again entity to add the data to the Datastore
For further information, you can refer to Google Cloud Data Store Entities, Properties, and Keys documentation.
I have done a little bit coding myself and here is my code example in GitHub for Python. You can take the idea of how it works and test it. It will create two different Kind values in Datastore and add the food data in foods and beverage data to beverages.

Firestore features and limitations on different data structure model

I have created app whose structure looks like this. Current structure for one company only.
let current = {
products: {
product1: {}//...
},
customers: {
customer1: {},// ...
},
orders: {
order1: {},// ...
},
}
Now I have design data structure to make it multi company app. Suppose companies are ABC, PQR, XYZ but the customers are same. So, a customer can see products from different companies.
Option 1: Add Company property in every lists doc.
let option1 = {
company: {
products: {
product1: {
company: 'ABC'
},
},
customers: { //Also we can put it at root with field company as array. Customers are not primary concern
customer1: {
company: 'PQR'
}
},
orders: {
order1: {
company: 'ABC'
}
},
}
}
My Remarks: I have to put company property in every list which may be more than these. It doesn't look like right solution. Querying products in different companies looks easy.
Option 2: Copy the current root structure for different companies.
let option2 = {
company1: {
products: {
product1: {}
},
customers: {//Also we can put it at root with field company as array. Customers are not primary concern
customer1: {},
},
orders: {
order1: {},
},
},
company2: {
products: {
product1: {}
},
customers: {
customer1: {},
},
orders: {
order1: {},
},
},
// ...
}
My Remarks: I don't know firestore limitations and upcoming features. Querying products in different companies may not be easy.
let option3= {} //your suggestions.
In same firestore project, assume customer handling will not be a problem.
What can be done here? What are things I am missing?
Products
It is unusual for several companies to sell the same product, without wanting to create their own stock item, description, price, etc. Also, if you have an array / map of companies who sell the product, within the product document, customers will see where they can buy the product and there may be no loyalty to any one company. If this is what you're hoping to achieve, then your options could work for you.
Customers
Companies will also want to keep certain data about their customers separate from other companies.
My proposal
I would suggest that you create a Cloud Firestore collection companies and have a document for each company. Within that document, you can create sub-collections for customers, orders and products.
A separate users collection at the root level will allow users to maintain their own data and allow companies to collect whatever they need to keep their records up to date. Adding public and private sub-collections of the user data can manage this easily for you.
Todd Kerpelman from the Firebase team, has made an excellent video which will really help with data modelling in Cloud Firestore.
I hope that this helps

In Sequelize, How do you perform a where query on an association inside of an $or statement?

I have three models, User, Project and ProjectMember. Keeping things simple, the models have the following attributes:
User
- id
Project
- id
- owner_id
- is_published
ProjectMember
- user_id
- project_id
Using sequelize.js, I want to find all projects where the project owner is a specific user, or where there is a project member for that project whose user is that user, or where the project is published. I imagine the raw SQL would look something like this:
SELECT p.*
FROM Project p
LEFT OUTER JOIN ProjectMember m
ON p.id = m.project_id
WHERE m.user_id = 2
OR p.owner_id = 2
OR p.is_published = true;
There are plenty of examples out there on how to perform a query on an association, but I can find none on how to do so conditionally. I have been able to query just the association using this code:
projModel.findAll({
where: { },
include: [{
model: memberModel,
as: 'projectMembers',
where: { 'user_id': 2 }
}]
})
How do I combine this where query in an $or to check the project's owner_id and is_published columns?
It's frustrating, I worked for hours to try to solve this problem, and as soon as I ask here I found a way to do it. As it turns out, sequelize.js developers recently added the ability to use raw keys in your where query, making it (at long last) possible to query an association inside of the main where clause.
This is my solution:
projModel.findAll({
where: {
$or: {
'$projectMembers.user_id$': 2,
owner_id: 2,
is_published: true
}
},
include: [{
model: memberModel,
as: 'projectMembers'
}]
})
Note: this solution breaks if you use 'limit' in the find options. As an alternative, you can fetch all results and then manually limit them afterwards.

How can I achieve this Many-to-many relationship in Firebase?

I know Firebase does not support JOINs between nodes (like SQL Server does between tables), but that is exactly what I need to accomplish. Here's my situation:
I have a transactions node in Firebase like this (where I am including the category name for each transaction):
"transactions":
{
"-Jruazf35b9a_gAVmZBe":
{
payee: "McDonalds", amount: "2.35", category: "Eating Out"
}
"-JruadR11b4a_aTVmZFi":
{
payee: "Walmart", amount: "78.12", category: "Household"
}
"-Jruazf35b9a_AgvNWCq":
{
payee: "CapitalOne", amount: "150.00", category: "Debt"
}
"-JryJF2c33ijbjbBc24p":
{
payee: "FootLocker", amount: "107.54", category: "Personal Blow"
}
"-Jrz0T-aL61Vuw4SOqRb":
{
payee: "Starbucks", amount: "2.88", category: "Eating Out"
}
}
And I have a Categories node like this (where I am including the transactions under each category):
"categories":
{
"-Jruazf35b2a_gAVmZRy":
{
categoryname: "Eating Out",
categorytype: "Expense"
}
"transactions": {
"-Jruazf35b9a_AgvNWCq": {
payee: "McDonalds", amount: "2.35"
}
.
.
.
}
}
}
So far so good. My data is flat. I'm able to show the list of transactions with the category name (screenshot below) and I can show the list of transactions under each category in the expenses per category section (screenshot not shown here).
The problem I have is that if I rename a category the change is only reflected for future transactions. Past transactions show the old category name.
This is obvious due to the way I'm saving the data. So my first logical reaction was to save the category unique ID in the transactions node instead of the category name. However, that presents the challenge where, in my SQL Server little brain, I would need a JOIN so I can get the list of transactions and also include the name of the category for each transaction.
How can I structure my data so that I can:
show a list of transactions including the name of the category (as it does today)
allow a user to rename a category and show the change reflected for ALL transactions (past and future)
show a list of transactions under each category (I think the current approach would still be valid)
Joining data from two lists is inherently a slow operation, especially on NoSQL databases.
I would recommend keeping the categoryName and adding a categoryId. That way you can show your current screen with a single read, but also link to the category. For how to deal with updating the categoryName in each transaction, see How to write denormalized data in Firebase and https://medium.com/#collardeau/es6-promises-with-firebase-76606f36c80c.
Alternatively: the list of categories is likely to be relatively small. So you could also pre-load it and perform a client-side lookup while you're iterating the transactions.

Resources