Shopify App: Where to store my app data?
Intro
Building Shopify apps used to have only one option for storing your data, your own database. But Shopify platform has come a long way since then now we have got metafields and metaobjects, allowing us to store data in many ways across Shopify platform. Not only can we store data but also have control over the ownership and permissions models too! But where is the best place to store your applications data? This blog post will explore the options available to you, and the best practices for storing your app data.
Questions
Theres some key questions about what your app is going to store and be used for, that can help you decide the best option of where to store your data.
You only need a rough idea of the answers to these questions.
- What's the context of the data? i.e. does it align to a Shopify model would it make sense to store it on order/product etc or not
- What's the volume of data like? i.e. are you storing a couple of key data points and config or thousands of records
- Do you need to aggregate the information? i.e. compute totals across orders for example
- Are there webhooks for the Shopify model? i.e. can you easily get a copy of data out of Shopify
- Where will you need to access the data? i.e. only inside your app, on the storefront, on headless sites etc.
- What's the access of that data look like? i.e. will there be constant or sporadic access of the data
Database
You will likely want a database to store some data regardless of the other information you'll want to store. As at the very minimum you will want to keep the store information, billing status, offline access token and contact info for your use and ease of access. This allows for a quick overview of the stores using the app, the billing plans in use, and provides the ability to contact them if necessary.
Always ensure you have secured this information, particularly the offline access token. You will want to encrypt the data at rest and in transit and ensure you are following best practices for securing your database.
Now we have covered the basics of what you will want to store in a database. Let us look at your answers to the questions above and where a database fits.
If you have a large volume of data to store, hundreds, thousands or more of items, you will likely hit limits on using metafields or metaobjects in Shopify so probably not best approach. A database using SQL or NoSQL would work best here as then the limits are all in your control.
If you need to aggregate the data regularly whether it be for dashboard, reports, or core app functionality, then a SQL based database is likely the right option. It will be much more efficient to do these aggregations than NoSQL. At the time of writing, you cannot aggregate across metafields or metaobjects in Shopify.
If you need to allow access to the data in your app from all over Shopify, whether it be your app, in liquid, headless etc. Then using your own database here would add complications for both you and the merchant in exposing the data, so a database might not be the right option here.
What is the access of the data you would store in a database? With constant requests, will you hit Shopify rate limits trying to access the metafields? If it is going to be hit with substantial number of requests consistently then NoSQL database likely makes sense, just for scale.
Lastly how confident are you in what the data will look like, and will it need to change a lot over time? If not, then NoSQL might give you more flexibility as it does not need a specific schema and easier to change. But SQL you will have to migrate the tables, and this could be annoying if you have to do that frequently.
Metafields
So, let us look at some of the options Shopify give us if we want to look beyond storing the data ourselves.
If the context of the data aligns to a Shopify model. Then you have some options, can you use the Shopify thing itself which saves you having to think of the format of the data and its lifecycle, like using draft orders or products etc and customising them. But consider if other apps or the merchant might be using these for something else, will there be any impact and can you differentiate your versions of these, so it is clear to other apps and merchants what is create by your app or not. Or does it make sense to extend the model using metafields, for example adding additional fields to a product or order. This is a great way to add additional information a Shopify model, in a structured way, instead of adding information to notes or tags.
Metafields with a definition can be viewed by the merchant in Shopify Admin, so you can easily add more information and context to Orders or Products for example. They can also be easily accessed from Liquid, Storefront, or other APIs inside of Shopify, making them ideal to access information across different areas.
Shopify have introduced some ownership and permissions for metafields now, so you can better control who can access that data and edit it. Giving you much better control over the data use inside of Shopify.
Metaobjects
If the data you want to store does not fit to the Shopify model, i.e. it is not directly related to an existing Shopify model, like products or orders. Then a metaobject might make sense, as here you can shape the data you want to store and allow customers to manage and edit that information as well.
These can also be easily viewed by the merchant in Shopify Admin under Content, so you can easily add more information. They can also be easily accessed from Liquid, Storefront, or other APIs inside of Shopify, making them ideal to access information across different areas.
Shopify have introduced some ownership and permissions for metaobjects now, so you can better control who can access that data and edit it. Giving you much better control over the data use inside of Shopify.
Not only that but the metaobjects can be translated as well, so you can support a more localised experience for the merchants if you are using the data on the shop front.
One size does not fit all
It may be the case that just one option does not fit all of what you need and that is also fine!
You could use a database for keeping the store information, and some data you need to aggregate. If you can get data out of Shopify, leveraging webhooks for example. This could work really well, leveraging metafields, metaobjects in Shopify for your primary data storage and then keeping a copy of the data, you'd like to aggregate in a SQL Database. Meaning you get the best of both worlds! Of course, you can also always use metaobjects and metafields together, to add even more context and information than using just one of these independently.
Summary
In summary, you can use Shopify Platform to easily store data for your application. You have got plenty of options to choose from, whether you use just one or multiple in combination make sure you pick the option that works best for you and your customers and merchants.