Beyond Version Control: Easily Make Immutable Files Dynamic
Written by: Justin Hunter
Republished from here: https://bit.ly/3Z2fN67
One of Pinata’s key differentiators from other file storage APIs is that we believe immutability is a first-class citizen. Every file uploaded to Pinata is immutable. This means if you want to change something, you don’t lose the original version. You can, of course, delete the original, but by default you will have both copies. This creates a nice version history, but if you stop there, you have to map the history yourself. Fortunately, there’s an easier way.
The Hot Swaps plugin in our plugin marketplace allows you to do two things:
Let’s break that down.
Mapping One File To Another
The primary functionality of the Hot Swaps plugin is to allow developers to point a file (we’ll call this ‘File A’ for simplicity) to any other file. For example, let’s say a developer wants to build a JSON index of every Pokémon card they have collected. When they collect a new card, they need to update the JSON file. With an immutable file system like Pinata, that would mean uploading the new version of the JSON (File B) and using the new content identifier for File B in any app the developer is building around this data.
With Hot Swaps, the developer can instead upload the new JSON file and “swap” File B for the old content of File A. The benefit of this is the developer doesn’t need to maintain their own mapping of file history. The original content identifier the app is using will now return the File B data.
Let’s see how this looks in practice.
This is how simple it is to swap your original file via its content identifier (CID) with another file. The implications of this are important to understand.
Let’s go back to the Pokémon collection data. Suppose you have built a simple app for yourself to track your collection. The first time you uploaded the JSON file with your collection data, you can store the file’s CID as an environment variable in your code. Now, your app can load the JSON file via that CID even when the data changes. You don’t have to worry about making additional queries to find and load the newest version of the JSON. Your app is always up to date.
Viewing History
Some people might read the above and think they can do this same thing with a mutable file system. For example, if you upload a JSON file through a more traditional file storage API, you’re probably able to refer to that file by name. If you change the file, you can still refer to it by name and get the new version.
The problem with this methodology is that you lose history. You are literally overwriting the data each time you make a change in a mutable file system. With Pinata’s immutable storage and Hot Swaps, you not only get the benefits of being able to change the underlying data while still referencing the same CID, you also get a full history of the data changes.
When you call the swap history endpoint, you will get the entire history of the file’s swaps. With this response, you can grab any of the previously mapped files and see the history over time. This is powerful for both an app functionality standpoint but also as a protection tool. If you accidentally updated your JSON data file, you can revert back with a simple API call.
Why Does It Matter?
Immutable data is neither new, nor novel. However, it is often treated as an after-thought. If you are using a legacy cloud storage service like AWS’s S3, you can add immutability by jumping through hoops and customizing your configuration. But immutability is powerful enough that it should be a primary tool in the toolbox.
With Pinata, you get immutability along with the benefits of being able to update your data through Hot Swaps. Simplify your code, track version history, and more.