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

Feature: Procedures or Functions #247

Closed
2 tasks done
mathe42 opened this issue Sep 21, 2022 · 4 comments
Closed
2 tasks done

Feature: Procedures or Functions #247

mathe42 opened this issue Sep 21, 2022 · 4 comments
Assignees
Labels
feature New feature or request
Milestone

Comments

@mathe42
Copy link

mathe42 commented Sep 21, 2022

Is your feature request related to a problem?

I have different use cases for this. Basicly make a callable list of surrealQL instructions with arguments etc.

This would allow us to reduce our API-Size to a minimum.

Describe the solution

My proposal contains a few extensions to statements

  • CALL
  • DEFINE PROCEDURE (with RETURN keyword)

Define a procedure to get a person

DEFINE PROCEDURE get_person ($firstname, $lastname, $birthdate) {
  LET $person = SELECT * FROM person WHERE firstname = $firstname AND lastname = $lastname AND birthdate = $birthdate;
  
  IF (COUNT($person)==1) THEN
     RETURN 1;
  ELSE
    RETURN CREATE person {
       firstname: $firstname,
       lastname: $lastname,
       birthdate: $birthdate
    }
  END
}

and call it by

LET $myPerson = CALL get_person('Sebastian', 'Krüger', '2022-09-21')

Alternative methods

The specifc syntax doesn't matter the above is only an example that I thought about.

There are some things things to look at:

  1. types,
  2. permissions (we might want to allow to run SQL of procedures even if for that statement the user has no permission but for the call of the procedure)
  3. Transaction I would expect that the Procedure is automaticly wraped in a transaction if there is an error at any statement it should fail and commit nothing to db.

(#225 might be silightly releated)

SurrealDB version

surreal 1.0.0-beta.7 for windows on x86_64

Contact Details

No response

Is there an existing issue for this?

  • I have searched the existing issues

Code of Conduct

  • I agree to follow this project's Code of Conduct
@MatthewAry
Copy link

If this feature gets added, it would be nice to be able to call a procedure in an event. DEFINE EVENT

@kearfy
Copy link
Member

kearfy commented Dec 6, 2022

+1 for this. I currently created a kinda hacky table structure with a table that drops all info and some events that allow me to reuse certain queries but this would make it miles easier

@tobiemh tobiemh added this to the v1.0.0-beta.9 milestone Feb 19, 2023
@tobiemh tobiemh self-assigned this Feb 19, 2023
@kearfy
Copy link
Member

kearfy commented Feb 20, 2023

Would like to comment on this: I don't think it would make sense to have a CALL statement to execute the procedures, as this will force you to use subqueries to invoke them, eventually exhausting the limits faster. I'd personally say that prefixing them like this: prodedure::name_of_procedure(arg1, arg2, ...) to invoke them, makes more sense.

Per your example: LET $myPerson = procedure::get_person('Sebastian', 'Krüger', '2022-09-21')

tobiemh added a commit that referenced this issue Feb 21, 2023
tobiemh added a commit that referenced this issue Feb 21, 2023
tobiemh added a commit that referenced this issue Feb 21, 2023
tobiemh added a commit that referenced this issue Feb 21, 2023
@Nezteb
Copy link

Nezteb commented Mar 25, 2023

This is the one major feature I was waiting to be added before giving SurrealDB a try. You are a wizard @tobiemh, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants