How to insert data in to datatstore - google-cloud-datastore

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.

Related

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

Model entities with firebase

I have the following situation:
I have a team entity, in each team we have one or more users.
At first I thought about creating an array of IDS inside team. And then download all team and use the javascript to go through these IDS and fetch the corresponding user.
Something like that:
"teams": {
"xxxxxxx": {
"ids": [0: "bKvysPZZCudBKbbjLYV8ZKr1NUo1", 1: XOvysPZZCudBKbbjLYV8ZKr1NUo1]
}
}
But I do not know if it is the best solution. I would like your opinion.
Tks.
I would recommend making a dictionary of IDs where each ID maps with the boolean value of true. I.e.:
"team-users": {
"team1": [
"uid1": true,
"uid2": true,
...
]
}
if you want to get a team that a user is a part of, then use a parallel structure in you database. Add the following node:
"user-teams": {
"uid1": [
"team1": true,
"team2": true,
...
]
}
Reading from this separate node is faster than querying.

Firebase :: Best way to structure this data

im trying to use firebase to store and retrieve data for my application.. i know that it is recommended to denormalize data and that it may require data replication..
my scenario is as follows:
there are a number of users in the system..
there are a number of posts in the system..
any user should be able to get a list of posts for a particular user..
each posts has a number of users as participants..
i am tempted to use the following structure for this:
users: {
abc: {
name: 'UserA',
profilePicture: 'imageA.png'
},
pqr: {
name: 'UserB',
profilePicture: 'imageB.png'
},
xyz: {
name: 'UserC',
profilePicture: 'imageC.png'
},
...,
...,
...
},
posts: {
def: {
title: 'PostA',
users: {
abc: true,
def: true,
ghi: true,
...,
...,
...
}
},
stu: {
title: 'PostB',
users: {
abc: true,
xyz: true,
...,
...,
...
}
},
...,
...,
...
}
the issue with this is that if i need to show a list of users with each post, i will need to make a query to POST, and then make sequential calls to USER for each user inside that post to get the name/profilePicture data..
if i replicate the user info inside posts as well, the issue becomes that if a user later changes her profilePicture or name, then existing posts will still show the old data..
how can i structure this data better so these cases are efficient?
thanks..
Don't replicate data inside posts. Read Firebase Docs about structuring data
Best practices:
Avoid nesting data
Flatten data structures
if you include data in post you are breaking those 2 rules (and you don't want it).
Multiple calls are not bad.

Structure a NoSQL database for a chat application (using FireBase)

Coming from years of using relational databases, i am trying to develop a pretty basic chat/messaging app using FireBase
FireBase uses a NoSQL data structure approach using JSON formatted strings.
I did a lot of research in order to understand how to structure the database with performance in mind. I have tried to "denormalize" the structure and ended up with the following:
{
"chats" : {
"1" : {
"10" : {
"conversationId" : "x123332"
},
"17": {
"conversationId" : "x124442"
}
}
},
"conversations" : {
"x123332" : {
"message1" : {
"time" : 12344556,
"text" : "hello, how are you?",
"userId" : 10
},
"message2" : {
"time" : 12344560,
"text" : "Good",
"userId" : 1
}
}
}
}
The numbers 1, 10, 17 are sample user id's.
My question is, can this be structured in a better way? The goal is to scale up as the app users grow and still get the best performance possible.
Using the document-oriented database structure such Firestore, you can store the conversations as below;
{
"chat_rooms":[
{
"cid":100,
"members":[1, 2],
"messages":[
{"from":1, "to":2, "text":"Hey Dude! Bring it"},
{"from":2, "to":1, "text":"Sure man"}
]
},
{
"cid":101,
"members":[3, 4],
"messages":[
{"from":3, "to":4, "text":"I can do that work"},
{"from":4, "to":3, "text":"Then we can proceed"}
]
}
]
}
Few examples of NoSQL queries you could run through this structure.
Get all the conversations of a logged-in user with the user id of 1.
db.chat_rooms.find({ members: 1 })
Get all the documents, messages sent by the user id of 1.
db.chat_rooms.find({ messages: { from: 1 } })
The above database structure is also capable of implementing in RDMS database as table relationships using MySQL or MSSQL. This is also can be implemented for group chat room applications.
This structure is optimized to reduce your database document reading usage which can save your money from paying more for infrastructure.
According to our above example still, you will get 2 document reads since we have 4 messages but if you store all the messages individually and run the query by filtering sender id, you will get 4 database queries which are the kind of massive amount when you have heavy conversation histories in your database.
One case for storing messages could look something like this:
"userMessages":
{ "simplelogin:1":
{ "simplelogin:2":
{ "messageId1":
{ "uid": "simplelogin:1",
"body": "Hello!",
"timestamp": Firebase.ServerValue.TIMESTAMP },
"messageId2": {
"uid": "simplelogin:2",
"body": "Hey!",
"timestamp": Firebase.ServerValue.TIMESTAMP }
}
}
}
Here is a fireslack example this structure came from. This tutorial builds an app like slack using firebase:
https://thinkster.io/angularfire-slack-tutorial
If you want something more specific, more information would be helpful.

Getting all root properties

Let's say I have a DocumentDB collection populated with documents that have this shape:
[{ "Name": "KT", "Dob": "5/25/1990", "Children": [], "IsMale": false },
{ "Name": "Chris", "Dob": "10/1/1980", "Children": [], "IsMale": true }]
Now let's say I don't the structure of the documents above.
Is there a query I can write that will return me a distinct list of those property names ("Name", "Dob", "Children", "IsMale")?
In other words, is there a way for be to sniff out the schema of those documents?
This might be a duplicate of this question. In any case, the answers there might give you some ideas.
tl;dr; The only way to do it is to read all of the docs. You can pull them all back to your machine or you can read them inside of a stored procedure and only send the calculated schema back to your machine.
You need a dynamic ORM or ODM for Azure DocumentDB like Slazure to do something like this. Example follows:
using SysSurge.Slazure.AzureDocumentDB.Linq;
using SysSurge.Slazure.Core;
using SysSurge.Slazure.Core.Linq.QueryParser;
public void EnumProperties()
{
// Get a reference to the collection
dynamic storage = new QueryableStorage<DynDocument>("URL=https://contoso.documents.azure.com:443/;DBID=DDBExample;TOKEN=VZ+qKPAkl9TtX==");
QueryableCollection<DynDocument> collection = storage.TestCustomers;
// Build collection query
var queryResult = collection.Where("SignedUpForNewsletter = true and Age < 22");
foreach (DynDocument document in queryResult)
{
foreach (KeyValuePair<string, IDynProperty> keyValuePair in document)
{
Console.WriteLine(keyValuePair.Key);
}
}
}

Resources