Decentralized Database
Learn how to store the application data with a decentralized database on Filecoin.
Store data with Tableland
Tableland is a decentralized database built on the SQLite engine, which offers developers a web3-native, relational database that seamlessly integrates into their EVM-compatible stacks. Under the hood, Tableland records database tables as ERC721 tokens on-chain and enables the execution of SQL statements in a completely decentralized manner through on-chain smart contracts.
To learn more about what is tableland and how to use it, you can visit https://tableland.xyz/.
Ingredients
Ensure that you install and import the necessary dependencies in your projects.
Instructions
Let's take storage deal aggregator/RaaS as an example to demonstrate how to integrate it with Tableland.
When uploading data via aggregator/RaaS providers to the Filecoin network, you can choose to store its metadata in Tableland tables instead of storing it in the chain state. This metadata can then be easily accessed from the Tableland database and utilized directly within your application.
If you require sample datasets to use, you can use the Filecoin Dataset Explorer.
As an example, let's design the deal aggregator table as follows. However, you can certainly add more columns to this table to include additional information, such as RaaS registration.
column | data Type |
---|---|
ID | int |
CID | bytes/string |
deal_ID | int |
miner_ID | int |
status | string |
Create aggregator table
To track all the deal aggregation/RaaS requests submitted to the smart contract, we need to create a database table. You can add the following code to create an aggregator table within the constructor()
function of the aggregator contract. This way, when the aggregator/RaaS contract is deployed, an aggregator table will be created to store the metadata of the aggregation requests.
We will create an
insert
function within the smart contract to add a record whenever an aggregation request is made.
Whenever the submit
or submitRaaS
function is called, a record will be inserted into the aggregator table instead of being stored in the blockchain's state.
we create an
updateRecord
function to modify an aggregator record once thecomplete
function is called after the storage deal has been made on the Filecoin network.
After SP finishes publishing the storage deal on-chain to include an aggregation request, a callback function complete
will be called to notify the contract that a CID is packed into a storage deal. Then we can call updateRecord
to update the details for this CID record in the Tableland database.
Query aggregation records
By using the Tableland SDK, you can easily query the aggregation or RaaS status of all the data stored with the aggregator using SQL statements. For instance, you can retrieve all records associated with a specific CID by executing a SELECT statement.
To learn how to write different select statements using Tableland SDK, you can refer to here.
Last updated