Skip to content

Commit

Permalink
feat: update turso resource output type in turso example (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshua-mo-143 authored Apr 23, 2024
1 parent 2662c36 commit e3bf54a
Showing 1 changed file with 22 additions and 23 deletions.
45 changes: 22 additions & 23 deletions axum/turso/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use std::sync::Arc;

use axum::{extract::State, response::IntoResponse, routing::get, Json, Router};
use libsql::Connection;
use libsql::Database;
use serde::{Deserialize, Serialize};

async fn get_posts(State(client): State<Arc<Connection>>) -> Json<Vec<User>> {
let mut rows = client
.query("select * from example_users", ())
.await
.unwrap();
async fn get_posts(State(client): State<Arc<Database>>) -> Json<Vec<User>> {
let conn = client.connect().unwrap();

let mut rows = conn.query("select * from example_users", ()).await.unwrap();
let mut users = vec![];
while let Some(row) = rows.next().await.unwrap() {
users.push(User {
Expand All @@ -26,34 +25,34 @@ struct User {
}

async fn create_users(
State(client): State<Arc<Connection>>,
State(client): State<Arc<Database>>,
Json(user): Json<User>,
) -> impl IntoResponse {
client
.execute(
"insert into example_users (uid, email) values (?1, ?2)",
[user.uid, user.email],
)
.await
.unwrap();
let conn = client.connect().unwrap();
conn.execute(
"insert into example_users (uid, email) values (?1, ?2)",
[user.uid, user.email],
)
.await
.unwrap();

Json(serde_json::json!({ "ok": true }))
}

#[shuttle_runtime::main]
async fn axum(
#[shuttle_turso::Turso(addr = "libsql://your-db.turso.io", token = "{secrets.TURSO_DB_TOKEN}")]
client: Connection,
client: Database,
) -> shuttle_axum::ShuttleAxum {
let client = Arc::new(client);

client
.execute(
"create table if not exists example_users ( uid text primary key, email text );",
(),
)
.await
.unwrap();
let conn = client.connect().unwrap();

conn.execute(
"create table if not exists example_users ( uid text primary key, email text );",
(),
)
.await
.unwrap();

let router = Router::new()
.route("/", get(get_posts).post(create_users))
Expand Down

0 comments on commit e3bf54a

Please sign in to comment.