Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Serde Serialize struct when inserting #36

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ edition = "2018"

[dependencies]
reqwest = { version = "0.11", features = ["json"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

[dev-dependencies]
json = "0.12"
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Add this to your `Cargo.toml`:

```toml
[dependencies]
postgrest = "1.0"
postgrest = "1.1.1"
```

Simple example:
Expand Down Expand Up @@ -74,7 +74,7 @@ let body = resp
use postgrest::Postgrest;
use dotenv;

dotenv::dotenv().ok();
dotenv::dotenv().ok();

let client = Postgrest::new("https://your.supabase.endpoint/rest/v1/")
.insert_header(
Expand All @@ -92,7 +92,7 @@ let body = resp

### Building Queries

These examples assume you've already initialized the client. The methods `.from()` and `.rpc()` initalizes the query builder inside the client.
These examples assume you've already initialized the client. The methods `.from()` and `.rpc()` initalizes the query builder inside the client.

Using filters:

Expand Down
10 changes: 10 additions & 0 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use reqwest::{
header::{HeaderMap, HeaderValue},
Client, Error, Method, Response,
};
use serde::Serialize;

/// QueryBuilder struct
pub struct Builder<'a> {
Expand Down Expand Up @@ -337,6 +338,15 @@ impl<'a> Builder<'a> {
self
}

pub fn insert_json<T>(mut self, body: T) -> Self
where T: Serialize, {
self.method = Method::POST;
self.headers
.insert("Prefer", HeaderValue::from_static("return=representation"));
self.body = Some(serde_json::to_string(&body).unwrap());
self
}

/// Performs an upsert of the `body` (in JSON) into the table.
///
/// # Note
Expand Down
28 changes: 28 additions & 0 deletions tests/client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use postgrest::Postgrest;

use std::error::Error;
use serde::Serialize;

const REST_URL: &str = "http://localhost:3000";

Expand Down Expand Up @@ -54,6 +55,33 @@ async fn insert() -> Result<(), Box<dyn Error>> {
Ok(())
}

#[derive(Serialize)]
struct Message {
message: String,
channel_id: u16,
username: String
}

#[tokio::test]
async fn insert_json() -> Result<(), Box<dyn Error>> {
let client = Postgrest::new(REST_URL);
let fake_message = Message {
message: "Test message 0".to_string(),
channel_id: 1,
username: "kiwicopple".to_string()
};
let resp = client
.from("messages")
.insert_json(&fake_message)
.execute()
.await?;
let status = resp.status();

assert_eq!(status.as_u16(), 201);

Ok(())
}

#[tokio::test]
async fn upsert() -> Result<(), Box<dyn Error>> {
let client = Postgrest::new(REST_URL);
Expand Down