1 00:00:02,880 --> 00:00:07,650 Now we talked a lot about how we can use the stitch to query data and how we can authenticate users, 2 00:00:07,860 --> 00:00:12,450 dive into the official docs if you want to learn more about that and how you can lock down access even 3 00:00:12,450 --> 00:00:15,680 more and how you have finegrained control. 4 00:00:15,720 --> 00:00:20,550 Now I want to talk about some other stitch features, briefly at least, to be precise 5 00:00:20,550 --> 00:00:27,160 I want to talk about functions and triggers because these are also two other major features. Now functions, 6 00:00:27,240 --> 00:00:30,210 this allows you to write some code that you can execute, 7 00:00:30,450 --> 00:00:34,220 let's click on create new function and let's name it greet, 8 00:00:34,230 --> 00:00:40,530 it will not do that much useful stuff but it will simply greet users. With that, default settings are 9 00:00:40,530 --> 00:00:44,240 ok, you can click save and now you can write your function code. 10 00:00:44,250 --> 00:00:49,530 Now you write javascript code and for all the features, again the official docs are the right place to 11 00:00:49,530 --> 00:00:55,770 go. In the end what you can do here is you can execute any javascript code, you can access other services, you 12 00:00:55,770 --> 00:00:57,050 can access your database, 13 00:00:57,090 --> 00:00:59,530 here's a snippet that shows you how that would work 14 00:00:59,670 --> 00:01:05,790 and you can also simply log something so that I can show you that this works. Not a super useful function 15 00:01:05,940 --> 00:01:10,840 but at least I can show you how we can call this. So I'll save my function code here 16 00:01:11,010 --> 00:01:17,940 and now I want to call this function and we can call it from inside our frontend, from our client application 17 00:01:18,010 --> 00:01:19,890 through that stitch sdk. 18 00:01:20,160 --> 00:01:23,260 Now let's say I want to call that function whenever my app loads, 19 00:01:23,430 --> 00:01:30,510 so essentially I can do this in the app.js file in the constructor after I initialized my client. Here 20 00:01:30,510 --> 00:01:34,010 I can use this client and then execute call function 21 00:01:34,290 --> 00:01:39,870 and here I simply pass a name to that function or the name of that function and that is of course greet 22 00:01:40,170 --> 00:01:45,980 and it will only look into my the stitch app which I initialize here and then find that function. 23 00:01:46,020 --> 00:01:49,750 You can also pass some arguments as a second parameter, 24 00:01:49,860 --> 00:01:56,430 so this would be an array of data you pass into the function, you can also have a look at this, can pass Max 25 00:01:56,430 --> 00:01:57,440 in there 26 00:01:57,820 --> 00:02:05,070 and now if I go back and edit my function slightly, I can also output arg here and see what I get 27 00:02:05,070 --> 00:02:05,540 there. 28 00:02:05,730 --> 00:02:11,990 So now let me save that and let me now reload my app and this should execute the function. 29 00:02:12,180 --> 00:02:18,670 I don't see anything here but on the backend, let's go to logs and there, we see a bunch of logs but 30 00:02:18,670 --> 00:02:25,110 we also see two function calls because the app was loaded twice because I save that before already. 31 00:02:25,330 --> 00:02:27,040 And there indeed you see greet 32 00:02:27,160 --> 00:02:34,770 Max as the output, so you see Max was the argument that was passed to the function and hence, I could also 33 00:02:34,770 --> 00:02:35,520 use it here. 34 00:02:36,630 --> 00:02:37,910 So it's as simple as that, 35 00:02:38,010 --> 00:02:40,980 that is already how I can call a function from inside my code. 36 00:02:40,980 --> 00:02:44,140 Important is that this function runs totally on a server, 37 00:02:44,190 --> 00:02:46,350 users cannot see your code in there, 38 00:02:46,440 --> 00:02:50,740 so if you got any code you want to execute that user should not be able to see, 39 00:02:50,760 --> 00:02:54,290 you can put it into a function and call that function from your frontend 40 00:02:54,390 --> 00:03:00,450 and then this code is invisible to your users and you can also put longer taking tasks in here for 41 00:03:00,450 --> 00:03:02,700 example, that you don't want to execute in the browser, 42 00:03:02,760 --> 00:03:04,370 stuff like that. 43 00:03:04,410 --> 00:03:08,780 Now you can also execute a function from inside or upon a trigger 44 00:03:08,790 --> 00:03:14,520 I should say, so we can go to triggers and we could add an authentication trigger to basically say 45 00:03:14,520 --> 00:03:22,500 ok, when a user was created or logs in or is deleted then I want to execute something, some function 46 00:03:22,860 --> 00:03:24,740 or a database trigger. 47 00:03:24,750 --> 00:03:27,340 Now let's go with a database trigger here, 48 00:03:27,340 --> 00:03:28,680 you can give it any name you want, 49 00:03:28,710 --> 00:03:37,490 I'll name it product insert, should be enabled and I'll listen to an event in the shop database and 50 00:03:37,490 --> 00:03:42,720 there in the products collection and I'll listen to an insert. Whenever something is inserted into this 51 00:03:42,720 --> 00:03:44,520 collection in this database, 52 00:03:44,640 --> 00:03:51,090 then I want to execute a function, to that function I'll pass the full document which was inserted as an argument 53 00:03:51,750 --> 00:03:58,640 and then I'll execute my greet function let's say. You can now click save and that is already it, what 54 00:03:58,640 --> 00:04:06,020 you can now do is you can go back to your app and log in real quick and if I now add a product 55 00:04:06,160 --> 00:04:10,850 and I'll really just type in some dummy data here so that we can quickly create it. 56 00:04:10,900 --> 00:04:12,280 Now that I created it, 57 00:04:12,430 --> 00:04:21,400 if we go back to the logs here in our stitch console, you'll see product insert, that trigger was executed 58 00:04:21,580 --> 00:04:26,950 and you see the output of the result is greet and then a more complex object in the end because that 59 00:04:26,950 --> 00:04:31,770 function receives the full document and some other metadata and therefore 60 00:04:31,870 --> 00:04:33,080 this is not output here 61 00:04:33,100 --> 00:04:37,840 essentially. If you want to see what's being passed in detail, you can of course dive into the official 62 00:04:37,840 --> 00:04:41,980 docs or maybe write this object into some dummy collection, 63 00:04:42,010 --> 00:04:46,550 you can insert that object into a dummy collection to then also see what's in there. 64 00:04:47,200 --> 00:04:54,070 And these are functions and triggers, very useful features that allow you to write code that runs on the 65 00:04:54,070 --> 00:04:59,710 server and even control that this code should always run when something happens to one of your collections.