Wasm-rust and postgres



有没有一种方法可以使用wasm从postgres中的数据库中获取数据?。我曾试图使用rust中的库来获得它,但当我使用";wasm包构建——目标web";。这个想法是在lib.rs文件中构建一个函数,从数据库返回数据。我在lib.rs中有以下代码:

use postgres::{Client, Error, NoTls};
use wasm_bindgen::prelude::*;
...
struct Author {
_id: i32,
name: String,
}
#[wasm_bindgen]
pub fn select_name(name: &String) -> Result<(), Error> {
let mut client = Client::connect("postgresql://user:1234@localhost:5432/db", NoTls)?;    
for row in client.query(
"SELECT id, name FROM author WHERE name = $1",
&[&name],
)? {
let author = Author {
_id: row.get(0),
name: row.get(1),
};
println!(
"Select_Name => Author {} :",
author.name
);
}
Ok(())
}

但我有一些错误:

error[E0432]: unresolved import `crate::sys::IoSourceState`
error[E0432]: unresolved import `crate::sys`
...

这不可能直接实现(正如Njuguna Mureithi正确地写的那样(,但可以绕过。

我们可以使用该项目:https://github.com/PostgREST/postgrest并将API公开给我们的sql服务器。

我们下载最新版本的postgresthttps://github.com/PostgREST/postgrest/releases/tag/v9.0.0在Linux的情况下,我们打开

tar -xf postgrest-v9.0.0-linux-static-x64.tar.xz

然后运行帮助

./postgrest -h

为创建配置文件/进展后

postgrest -e > cfg.psqlrest

在配置文件中更改数据库的用户和密码。例如使用

db-uri = "postgres://user:pasword@localhost:5432/dbname"

到您的dbname数据库访问用户:pasword配置

db-uri = "postgres://postgres:zaqwsxc@localhost:5432/dbname"

并运行服务器,该服务器将向我们的postgres 发布api

./postgrest cfg.psqlrest

地址http://localhost:3000将开始访问数据库dbname,该数据库必须事先在数据库服务器中创建。

以下是使用API调用查询所需的库的描述。

https://rustwasm.github.io/wasm-bindgen/examples/fetch.html

API示例https://postgrest.org/en/stable/api.html

最新更新