Skip to content

Commit

Permalink
Patch (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
juchiast authored Nov 30, 2023
1 parent d70ec9f commit bb1f86d
Show file tree
Hide file tree
Showing 5 changed files with 194 additions and 19 deletions.
105 changes: 105 additions & 0 deletions crates/cmds-std/node-definitions/postgrest/new_rpc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
{
"type": "native",
"data": {
"node_definition_version": "0.1",
"unique_id": "",
"node_id": "postgrest_new_rpc",
"version": "0.1",
"display_name": "New RPC",
"description": "https://docs.rs/postgrest/latest/postgrest/struct.Postgrest.html#method.rpc",
"tags": ["database", "postgrest", "supabase"],
"related_to": [
{
"id": "",
"type": "",
"relationship": ""
}
],
"resources": {
"source_code_url": "",
"documentation_url": ""
},
"usage": {
"license": "Apache-2.0",
"license_url": "",
"pricing": {
"currency": "USDC",
"purchase_price": 0,
"price_per_run": 0,
"custom": {
"unit": "monthly",
"value": "0"
}
}
},
"authors": [
{
"name": "Space Operator",
"contact": ""
}
],
"design": {
"width": 0,
"height": 0,
"icon_url": "",
"backgroundColorDark": "#000000",
"backgroundColor": "#fff"
},
"options": {}
},
"targets": [
{
"name": "url",
"type_bounds": ["string"],
"required": false,
"defaultValue": "https://base.spaceoperator.com/rest/v1/",
"tooltip": "",
"passthrough": false
},
{
"name": "schema",
"type_bounds": ["string"],
"required": false,
"defaultValue": "public",
"tooltip": "",
"passthrough": false
},
{
"name": "function",
"type_bounds": ["string"],
"required": true,
"defaultValue": null,
"tooltip": "",
"passthrough": false
},
{
"name": "params",
"type_bounds": ["free"],
"required": true,
"defaultValue": null,
"tooltip": "",
"passthrough": false
}
],
"sources": [
{
"name": "quey",
"type": "object",
"defaultValue": "",
"tooltip": ""
}
],
"targets_form.json_schema": {
"type": "object",
"title": "New RPC",
"properties": {
"table": {
"title": "table",
"type": "string"
}
}
},
"targets_form.ui_schema": {
"ui:order": ["table"]
}
}
1 change: 1 addition & 0 deletions crates/cmds-std/src/postgrest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pub mod builder_select;
pub mod builder_update;
pub mod execute_query;
pub mod new_query;
pub mod new_rpc;
48 changes: 48 additions & 0 deletions crates/cmds-std/src/postgrest/new_rpc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use flow_lib::command::prelude::*;

const NAME: &str = "postgrest_new_rpc";

#[derive(Deserialize, Debug)]
struct Input {
url: Option<String>,
schema: Option<String>,
function: String,
params: JsonValue,
}

#[derive(Serialize, Debug)]
struct Output {
query: postgrest::Builder,
}

async fn run(ctx: Context, input: Input) -> Result<Output, CommandError> {
let url = input
.url
.unwrap_or_else(|| format!("{}/rest/v1", ctx.endpoints.supabase));
let mut pg = postgrest::Postgrest::new(url);
if let Some(schema) = input.schema {
pg = pg.schema(schema);
}
let query = pg.rpc(input.function, serde_json::to_string(&input.params)?);
Ok(Output { query })
}

fn build() -> BuildResult {
Ok(
CmdBuilder::new(flow_lib::node_definition!("postgrest/new_rpc.json"))?
.check_name(NAME)?
.build(run),
)
}

flow_lib::submit!(CommandDescription::new(NAME, |_| build()));

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_build() {
build().unwrap();
}
}
18 changes: 12 additions & 6 deletions crates/flow/src/command/rhai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@ impl CommandTrait for Command {
}

async fn run(&self, ctx: Context, input: ValueSet) -> Result<ValueSet, CommandError> {
let registry = ctx
.get::<FlowRegistry>()
.ok_or_else(|| anyhow::anyhow!("FlowRegistry not found"))?;
// TODO: stop token
registry
ctx.get::<FlowRegistry>()
.ok_or_else(|| anyhow::anyhow!("FlowRegistry not found"))?
.run_rhai(run_rhai::Request {
command: self.inner.clone(),
ctx: ctx.clone(),
Expand All @@ -39,7 +36,16 @@ impl CommandTrait for Command {

pub fn build(nd: &NodeData) -> Result<Box<dyn CommandTrait>, CommandError> {
let inputs: Vec<Input> = nd.targets.iter().cloned().map(Into::into).collect();
let outputs: Vec<Output> = nd.sources.iter().cloned().map(Into::into).collect();
let outputs: Vec<Output> = nd
.sources
.iter()
.cloned()
.map(|s| Output {
// TODO: we did not upload this field to db
optional: true,
..Output::from(s)
})
.collect();
let source_code_name = inputs
.get(0)
.ok_or_else(|| CommandError::msg("no source code input"))?
Expand Down
41 changes: 28 additions & 13 deletions lib/flow-lib/src/config/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,19 @@ pub struct Source {
pub optional: bool,
}

impl Into<CmdOutputDescription> for Source {
fn into(self) -> CmdOutputDescription {
CmdOutputDescription {
name: self.name,
r#type: self.r#type,
optional: self.optional,
impl From<Source> for CmdOutputDescription {
fn from(
Source {
name,
r#type,
optional,
..
}: Source,
) -> Self {
Self {
name,
r#type,
optional,
}
}
}
Expand All @@ -164,13 +171,21 @@ pub struct Target {
pub passthrough: bool,
}

impl Into<CmdInputDescription> for Target {
fn into(self) -> CmdInputDescription {
CmdInputDescription {
name: self.name,
type_bounds: self.type_bounds,
required: self.required,
passthrough: self.passthrough,
impl From<Target> for CmdInputDescription {
fn from(
Target {
name,
type_bounds,
required,
passthrough,
..
}: Target,
) -> Self {
Self {
name,
type_bounds,
required,
passthrough,
}
}
}
Expand Down

0 comments on commit bb1f86d

Please sign in to comment.