Make sdk create consistent (#4667)

This commit is contained in:
Raphael Darley 2024-09-03 06:01:37 -07:00 committed by GitHub
parent b0cde2a40a
commit ff3c3bcc2b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 25 additions and 18 deletions

View file

@ -337,7 +337,7 @@ pub trait RpcContext {
return Err(RpcError::InvalidParams);
};
let one = data.is_single();
let one = what.is_thing_single();
let mut res = match what {
Value::None | Value::Null => {
@ -375,8 +375,9 @@ pub trait RpcContext {
let Ok((what, data)) = params.needs_one_or_two() else {
return Err(RpcError::InvalidParams);
};
let what = what.could_be_table();
// Return a single result?
let one = what.is_thing_single();
let one = what.is_thing_single() || what.is_table();
// Specify the SQL query string
let sql = if data.is_none_or_null() {
"CREATE $what RETURN AFTER"
@ -385,7 +386,7 @@ pub trait RpcContext {
};
// Specify the query parameters
let var = Some(map! {
String::from("what") => what.could_be_table(),
String::from("what") => what,
String::from("data") => data,
=> &self.vars()
});
@ -540,7 +541,7 @@ pub trait RpcContext {
return Err(RpcError::InvalidParams);
};
// Return a single result?
let one = kind.is_thing_single();
let one = from.is_single() && to.is_single();
// Specify the SQL query string
let sql = if data.is_none_or_null() {
"RELATE $from->$kind->$to"

View file

@ -1168,7 +1168,7 @@ impl Value {
pub fn is_single(&self) -> bool {
match self {
Value::Object(_) => true,
Value::Array(a) if a.len() == 1 => true,
t @ Value::Thing(_) => t.is_thing_single(),
_ => false,
}
}

View file

@ -26,7 +26,7 @@ use crate::{
Connect, Response as QueryResponse, Result, Surreal,
},
method::Stats,
opt::{IntoEndpoint, Resource as ApiResource, Table},
opt::{IntoEndpoint, Table},
value::Notification,
};
use channel::Sender;
@ -559,7 +559,7 @@ async fn router(
data,
} => {
let mut query = Query::default();
let one = matches!(what, ApiResource::RecordId(_));
let one = what.is_single_recordid();
let statement = {
let mut stmt = UpsertStatement::default();
stmt.what = resource_to_values(what);
@ -578,7 +578,7 @@ async fn router(
data,
} => {
let mut query = Query::default();
let one = matches!(what, ApiResource::RecordId(_));
let one = what.is_single_recordid();
let statement = {
let mut stmt = UpdateStatement::default();
stmt.what = resource_to_values(what);
@ -635,7 +635,7 @@ async fn router(
data,
} => {
let mut query = Query::default();
let one = matches!(what, ApiResource::RecordId(_));
let one = what.is_single_recordid();
let statement = {
let mut stmt = UpdateStatement::default();
stmt.what = resource_to_values(what);
@ -654,7 +654,7 @@ async fn router(
data,
} => {
let mut query = Query::default();
let one = matches!(what, ApiResource::RecordId(_));
let one = what.is_single_recordid();
let statement = {
let mut stmt = UpdateStatement::default();
stmt.what = resource_to_values(what);
@ -672,7 +672,7 @@ async fn router(
what,
} => {
let mut query = Query::default();
let one = matches!(what, ApiResource::RecordId(_));
let one = what.is_single_recordid();
let statement = {
let mut stmt = SelectStatement::default();
stmt.what = resource_to_values(what);
@ -689,7 +689,7 @@ async fn router(
what,
} => {
let mut query = Query::default();
let one = matches!(what, ApiResource::RecordId(_));
let one = what.is_single_recordid();
let statement = {
let mut stmt = DeleteStatement::default();
stmt.what = resource_to_values(what);

View file

@ -4,7 +4,8 @@ use crate::{
};
use std::ops::{self, Bound};
use surrealdb_core::sql::{
Edges as CoreEdges, IdRange as CoreIdRange, Table as CoreTable, Thing as CoreThing,
Edges as CoreEdges, Id as CoreId, IdRange as CoreIdRange, Table as CoreTable,
Thing as CoreThing,
};
#[cfg(any(feature = "protocol-ws", feature = "protocol-http"))]
@ -104,6 +105,12 @@ impl Resource {
Resource::Unspecified => CoreValue::None,
}
}
pub fn is_single_recordid(&self) -> bool {
match self {
Resource::RecordId(rid) => !matches!(rid.into_inner_ref().id, CoreId::Range(_)),
_ => false,
}
}
}
impl From<RecordId> for Resource {

View file

@ -1,6 +1,6 @@
use super::common::{self, Format, Socket, DB, NS, PASS, USER};
use http::header::{HeaderMap, HeaderValue};
use assert_fs::TempDir;
use http::header::{HeaderMap, HeaderValue};
use serde_json::json;
use std::future::Future;
use std::pin::Pin;
@ -380,10 +380,9 @@ async fn create() -> Result<(), Box<dyn std::error::Error>> {
)
.await?;
assert!(res.is_object(), "result: {res:?}");
assert!(res["result"].is_array(), "result: {res:?}");
let res = res["result"].as_array().unwrap();
assert_eq!(res.len(), 1, "result: {res:?}");
assert_eq!(res[0]["value"], "bar", "result: {res:?}");
assert!(res["result"].is_object(), "result: {res:?}");
let res = res["result"].as_object().unwrap();
assert_eq!(res["value"], "bar", "result: {res:?}");
// Verify the data was created
let res = socket.send_message_query("SELECT * FROM tester").await?;
assert!(res[0]["result"].is_array(), "result: {res:?}");