Creating Your SQLite Canister
Configure your canister
To integrate SQLite into your backend Rust Canister project, you will need to add the ic-rusqlite
dependency and adjust the build process.
The first part is easy:
cargo add ic-rusqlite
The second part requires you to update the dfx.json
to specify path to wasm
binary, set cansiter type
to custom
, and
specify custom build steps to enforce compilation to the wasm32-wasip1
target.
Finally, use wasi2ic
to produce a Wasm executable on the Internet Computer.
Example dfx.json
:
{
"canisters": {
"backend": {
"candid": "can.did",
"package": "backend",
"build": [
"cargo build --release --target wasm32-wasip1",
"wasi2ic target/wasm32-wasip1/release/backend.wasm target/wasm32-wasip1/release/backend_nowasi.wasm"
],
"wasm": "target/wasm32-wasip1/release/backend_nowasi.wasm",
"type": "custom",
"metadata": [
{
"name": "candid:service"
}
]
}
},
"dfx": "0.28.0",
"version": 1
}
Accessing the Database
The database connection is established with the first call to ic_rusqlite::with_connection(...)
, so there is no need to explicitly initialize or create a database.
Internally, the ic-rusqlite
uses stable structures with the memory manager. The virtual memories 101..119
are reserved for the file system.
The virtual memory with the ID 120
is the default storage memory for the database database file is /DB/main.db
. These settings are default, but can be change via the set_connection_config(...)
function.
The ability to associate a file with a virtual memory is a special feature of stable-fs
. This allows to create dedicated files with fast I/O access.
Using File System
ic-rusqlite
is compiled to the WASI target and then processed by the wasi2ic
workflow, embedding ic-wasi-polyfill
and stable-fs
into the output binary. This enables the use of standard Rust I/O APIs for file operations.
By default the main database file is stored in the root folder: /DB/main.db
and there are a few additional helper files that can be created by the databsae engine.
Using Stable Structures
You can freely create other stable structures for your extra storage needs, just make sure to use a virtual memory ID that is not yet occupied.
Make sure you are using the memory manager or you will destroy the database and the file system stored in the stable memory.