简体   繁体   中英

Google Cloud Datastore for a Products and Stock

I'm trying Google Cloud Datastore, but I have some doubts. I know that the ideal is to use a relational database for make a shop online, but I would like to try using Google Cloud Datastore.

How would a database of 2 tables be made? Stock and Products. The stock table has 2 columns (ref and units) and the product table has 3 columns (name, ref and price).

How would you do to get all the products that have stock?... like a join, I know that we do not have joins, that's why my doubt.

There has to be an efficient way to get the stock related to the products.

There are no tables in the Datastore, you have just entities with properties. And, depending on the client library you use, you might have entity models.

The Stock entities can have a Key property pointing to the corresponding Product entities. You query for the Stock entities, from the results you obtain the Product keys with which you pull the respective entities.

Or, if they're always in a 1:1 relationship I could use the exact same entity IDs for the corresponding Stock and Product entities, so I can make a Stock query and from the Stock entities in the result (or rather from their keys/IDs as I'd probably make keys_only queries) I can immediately compute the Product keys and get the respective entities (see re-using an entity's ID for other entities of different kinds - sane idea? ).

But, in general, you might want to reconsider the general SQL approach of querying data to generate a report when you need it (and expecting that to be fast) and instead make the habit of performing the necessary computations ahead of time - whenever the data used in those computations changes. This is a much more scalable approach which works hand in hand with the datastore (and I guess with nosql in general). And for which you do not need to perform equivalent to SQL-style join ops. Basically raise the stock empty flag for a product right when you decrement its stock value, when you already know the product in question, so that you don't have to query for it later. While there also add it to the report (so that you'll have it ready when needed) and maybe trigger the restocking activity as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM