Introduction to CRUD Functionality
This guide introduces the CRUD (Create, Read, Update, Delete) features of FISCO BCOS, designed to help developers build blockchain applications more efficiently.
Why CRUD Was Designed
In FISCO BCOS 1.0, nodes used MPT data structures with LevelDB for local storage, which limited scalability. As business volumes grew, data expanded rapidly, making migration complex and costly. To overcome these limitations, FISCO BCOS 2.0 redesigned its storage architecture with distributed storage, improving both capacity and performance.
With distributed storage adopting a table-based structure, CRUD functionality was introduced to provide a more intuitive development approach. This method aligns with traditional SQL-based business development practices and offers an alternative to Solidity contracts, simplifying blockchain application development.
Advantages of CRUD
CRUD enhances blockchain application development through improved efficiency and performance while reducing complexity and migration costs.
Efficiency Improvement
CRUD resembles traditional SQL programming, lowering the learning curve for contract development. Developers can treat contracts as database stored procedures, converting blockchain data operations into table operations. This approach simplifies development and accelerates application deployment.
Performance Enhancement
CRUD operations are executed via precompiled contracts using distributed storage. Transactions bypass the slower EVM, instead running on high-speed precompiled contract engines. This results in faster data read/write operations and higher application performance.
Reduced Maintenance Complexity
CRUD separates logic from storage, allowing developers to focus on core business logic. Data isnโt tied to specific contracts, simplifying upgrades. New contracts can interact with tables and data created by older contracts.
Lower Migration Costs for SQL-Based Applications
Many traditional applications manage data via database tables. CRUD enables seamless migration of SQL-based business logic to blockchain, reducing transition costs.
How to Use CRUD
CRUD functionality can be accessed through two methods: CRUD contracts and SDK CRUD Service interfaces.
CRUD Contracts
Smart contracts are the primary medium for interacting with blockchain. CRUD is integrated into Solidity contracts by importing the Table.sol abstract interface file provided by FISCO BCOS. Contracts that include this interface are called CRUD contracts.
Key Components of Table.sol
- TableFactory Contract
Used to create and open tables, with a fixed contract address of0x1001. - Entry Contract
Represents a record (row) in a table. - Entries Contract
A collection of Entry objects. - Condition Contract
Specifies filters for query, update, and delete operations. - Table Contract
Performs CRUD operations on tables.
CRUD Contract Development Steps
Import
Table.sol
PlaceTable.solin the same directory as your contract and import it:import "./Table.sol";Create a Table (Optional)
TableFactory tf = TableFactory(0x1001); int count = tf.createTable("t_test", "name", "item_id,item_name");- Tables are created dynamically when accessed.
- Skip this step if using tables created by other contracts.
Perform CRUD Operations
Insert Records:
Table table = tf.openTable("t_test"); Entry entry = table.newEntry(); entry.set("name", name); entry.set("item_id", item_id); entry.set("item_name", item_name); int count = table.insert(name, entry);Query Records:
Condition condition = table.newCondition(); Entries entries = table.select(name, condition);Update Records:
condition.EQ("name", name); condition.EQ("item_id", item_id); int count = table.update(name, entry, condition);Delete Records:
int count = table.remove(name, condition);
SDK CRUD Service Interfaces
For scenarios where writing and deploying contracts is unnecessary, FISCO BCOS SDK provides CRUD Service interfaces that interact with a precompiled CRUD contract.
Java SDK Example
CRUDService crudService = new CRUDService(web3j);
// Create a table
crudService.createTable("t_test", "name", "item_id,item_name");
// Insert a record
crudService.insert("t_test", "name", "item_id,item_name", "value1,value2");
// Query records
List<Map<String, String>> result = crudService.select("t_test", "name = 'value1'");๐ Explore more SDK examples
Comparing CRUD Contract and SDK Approaches
| Feature | CRUD Contract | SDK CRUD Service |
|---|---|---|
| Data Access | Full access (state variables, tables) | Limited to user tables |
| Business Logic | Supports complex logic | Simple data operations |
| Development Overhead | Requires contract deployment | No contract deployment needed |
Best Practices for Effective CRUD Usage
- Prefer CRUD Contracts
Use CRUD contracts for most applications unless only simple table operations are needed. - Do Not Modify
Table.sol
Altering this file can cause transaction failures or unexpected behavior. - Non-Unique Primary Keys
CRUD tables allow duplicate primary keys. Keep primary key values concise for efficiency. - Encapsulate Fields for Large Tables
Use arrays or structs to avoid Solidity's 16-local-variable limit. - Open Tables per Method Call
Avoid storing table objects globally; reopen tables within each method. - Mixed Storage Strategies
Combine table storage with state variables for flexibility. - Implement Permission Controls
Restrict write access to authorized accounts using permission controls.
๐ Learn about permission controls
FAQs
What is the main advantage of CRUD over traditional Solidity contracts?
CRUD simplifies data operations by aligning with SQL-like table operations, reducing development complexity and improving performance.
Can I migrate existing SQL-based applications to FISCO BCOS using CRUD?
Yes, CRUD provides a smooth transition path for SQL-based applications by enabling table-based data management on blockchain.
How does CRUD improve performance?
CRUD operations bypass the EVM, executing via high-speed precompiled contracts and distributed storage.
Is CRUD suitable for all blockchain applications?
While CRUD excels in data-heavy applications, complex logic may still require traditional Solidity contracts.
How do I secure CRUD tables?
Use permission controls to restrict write access to authorized accounts, preventing unauthorized modifications.