mirror of
https://github.com/fluencelabs/fluid
synced 2025-04-05 03:41:12 +00:00
41 lines
1.1 KiB
Rust
41 lines
1.1 KiB
Rust
|
use fluence::sdk::*;
|
||
|
use serde_json::value::RawValue;
|
||
|
|
||
|
use api::Request;
|
||
|
use api::Response;
|
||
|
|
||
|
use crate::api::AppResult;
|
||
|
use crate::errors::err_msg;
|
||
|
|
||
|
pub mod database;
|
||
|
pub mod errors;
|
||
|
pub mod ffi;
|
||
|
|
||
|
fn init() {
|
||
|
logger::WasmLogger::init_with_level(log::Level::Info).unwrap();
|
||
|
}
|
||
|
|
||
|
#[invocation_handler(init_fn = init)]
|
||
|
fn run(nickname: String) -> String {
|
||
|
// Create table for messages storage
|
||
|
database::query("CREATE TABLE messages(msg text, handle text)".to_string());
|
||
|
|
||
|
// Insert message 'Hello, username!' using `nickname` as author's handle
|
||
|
database::query(format!(
|
||
|
r#"INSERT INTO messages VALUES("{}","{}")"#,
|
||
|
"Hello, username!", nickname
|
||
|
));
|
||
|
|
||
|
// Get all messages
|
||
|
let messages = database::query("SELECT * FROM messages".to_string());
|
||
|
log::info!("messages: {}", messages);
|
||
|
|
||
|
// Get all messages as JSON via SQLite's JSON extension
|
||
|
database::query(
|
||
|
"SELECT json_group_array(
|
||
|
json_object('msg', msg, 'handle', handle)
|
||
|
) AS json_result FROM (SELECT * FROM messages)"
|
||
|
.to_string(),
|
||
|
)
|
||
|
}
|