Now we talked a lot about how we can use the stitch to query data and how we can authenticate users,
dive into the official docs if you want to learn more about that and how you can lock down access even
more and how you have finegrained control.
Now I want to talk about some other stitch features, briefly at least, to be precise
I want to talk about functions and triggers because these are also two other major features. Now functions,
this allows you to write some code that you can execute,
let's click on create new function and let's name it greet,
it will not do that much useful stuff but it will simply greet users. With that, default settings are
ok, you can click save and now you can write your function code.
Now you write javascript code and for all the features, again the official docs are the right place to
go. In the end what you can do here is you can execute any javascript code, you can access other services, you
can access your database,
here's a snippet that shows you how that would work
and you can also simply log something so that I can show you that this works. Not a super useful function
but at least I can show you how we can call this. So I'll save my function code here
and now I want to call this function and we can call it from inside our frontend, from our client application
through that stitch sdk.
Now let's say I want to call that function whenever my app loads,
so essentially I can do this in the app.js file in the constructor after I initialized my client. Here
I can use this client and then execute call function
and here I simply pass a name to that function or the name of that function and that is of course greet
and it will only look into my the stitch app which I initialize here and then find that function.
You can also pass some arguments as a second parameter,
so this would be an array of data you pass into the function, you can also have a look at this, can pass Max
in there
and now if I go back and edit my function slightly, I can also output arg here and see what I get
there.
So now let me save that and let me now reload my app and this should execute the function.
I don't see anything here but on the backend, let's go to logs and there, we see a bunch of logs but
we also see two function calls because the app was loaded twice because I save that before already.
And there indeed you see greet
Max as the output, so you see Max was the argument that was passed to the function and hence, I could also
use it here.
So it's as simple as that,
that is already how I can call a function from inside my code.
Important is that this function runs totally on a server,
users cannot see your code in there,
so if you got any code you want to execute that user should not be able to see,
you can put it into a function and call that function from your frontend
and then this code is invisible to your users and you can also put longer taking tasks in here for
example, that you don't want to execute in the browser,
stuff like that.
Now you can also execute a function from inside or upon a trigger
I should say, so we can go to triggers and we could add an authentication trigger to basically say
ok, when a user was created or logs in or is deleted then I want to execute something, some function
or a database trigger.
Now let's go with a database trigger here,
you can give it any name you want,
I'll name it product insert, should be enabled and I'll listen to an event in the shop database and
there in the products collection and I'll listen to an insert. Whenever something is inserted into this
collection in this database,
then I want to execute a function, to that function I'll pass the full document which was inserted as an argument
and then I'll execute my greet function let's say. You can now click save and that is already it, what
you can now do is you can go back to your app and log in real quick and if I now add a product
and I'll really just type in some dummy data here so that we can quickly create it.
Now that I created it,
if we go back to the logs here in our stitch console, you'll see product insert, that trigger was executed
and you see the output of the result is greet and then a more complex object in the end because that
function receives the full document and some other metadata and therefore
this is not output here
essentially. If you want to see what's being passed in detail, you can of course dive into the official
docs or maybe write this object into some dummy collection,
you can insert that object into a dummy collection to then also see what's in there.
And these are functions and triggers, very useful features that allow you to write code that runs on the
server and even control that this code should always run when something happens to one of your collections.