Best Practices: When to Use Library?

I know Libraries have been in Wappler for a long time and the basic premise is that they are re-usable.

But regular API stuff is also re-usable. And API stuff is generally not used more than once in a project anyways.

When do you use Libraries? What are some good examples?

I think I may be missing out on a useful tool.

As much as possible :smiley:

Two things will happen when you start working with libraries.

a) You will design for reusability which will eventually lead to less work.
b) Having a) in mind you will start building better overall.

But what is an example use case?

I can re-use any of my server connect action just from the API as it is?

Anything that you are planning on reusing.
Security stuff for instance, external API calls, your own REST API, etc

1 Like

As an example, I have a series of steps including queries to retrieve branding, user info, parent details (if a child) etc before finally sending an email that combines the above with subject, content and so on. I set up a library action to do it and now I can simply use it to send an email when processing payments, making bookings, sending reminders by using some parameters. Boom!

4 Likes

Another example, similar to one in the tutorial I think - image resizing. You might use use this in a number of APIs in a project:

image

You need only define the steps once, and use only a single line in any API calling it. All the parameters for the steps can be sent from the calling API, which will be more concise and easier to manage etc:

image

4 Likes

Ah, so you don’t really have to put the whole API action in the library - just parts of it and then you use it in a regular API action?

Hmmm, interesting.

Yes. It might help for you to think of the api as a workflow, and the library as tasks within the workflow.

4 Likes

We use library in the same way as a function in general programming.
As one of my teachers taught me: A function should be cohesive and independent.
And library fulfills that perfectly when using as Exec.

Two days ago, I had a SA which generates a zip of images. I then had to use the exact same logic in another SA. So I moved (manual JSON copy paste) it to a library, and called them via Exec step in both the places.

The original SA now just has one exec step.

Using library SA via Include method is also a good option, but not something that I use frequently. The use case here is more like a server side include or a partial.
Code gets added to existing code on runtime, and the whole code executes as one.

1 Like

I’d say, from my perspective, that it’s one of the most important updates to Wappler so far. Once you embrace it, it saves time, improves consistency, improves reliability…

In the case of the email above which I use in several SC API files, if I want to adjust the template, make an improvement or address a bug in my process all I need do is change it in one place rather than traipsing through lots of actions hoping to find every place I need to adjust.

1 Like

Yes, they are amazing…

So for example, I have accounting functions in my design.

So I have one which will reconcile a payment to any number of unpaid invoices. The maths and conditions are pretty complex.

I call it as part of the server action when a payment is made, when an invoice amount is changed or when a booking is moved to another event.

Lots of great feedback here - I need to start implementing these more widely.

Excuse my ignorance here, but with a library action, is it expected that I can use data from a DB query from a previous step in the applied include or exec library action? Noting - the call and naming will all be 100% the same.

I have about 25 API’s that display data on a page (directory listings) - I’d like to use a library action to take the same data that is output and then run some other actions in the library call. Is this possible, or will they as Sid mentions be independent from any previous steps?

How we are handling this is running the query in individual SAs, and then passing that information as a PARAM to EXEC step.
The library in exec can then use SA’s query result as a PARAM input.

You will have to set the PARAM’s schema to use it via pickers etc in the library. Or you can just code it in and it will work in runtime.

1 Like

Ok, interesting. Thank you @sid

So each SA has the same query output, e.g.:

id
company_id

However, it will output 25 of these pairs per query.

If you don’t mind a couple of follow-on questions:

  • How would you suggest I best pass that output as a PARAM that can then be reprossed by the LIB action.

  • How do you actually pass that information into a PARAM that can then be used? I’m used to passing data using PARAM’s from the F/E to the B/E but not within multiple B/E actions.

Appreciate any guidance here. This probably isn’t the easiest first lib action to work on, but it does have high leverage to the project.

Matt

Just pass the query as is. Define an object type PARAM in library SA. Then in EXEC step, that should show up as an input. Now, if the name of the query is query1, the value of EXEC input should be just {{query1}}.
Then in library SA, you can access the complete JSON object as is via $_PARAM.xxx.

Not sure if this question is any different to the first one.
When talking about libraries, its all B/E. So no involvement of client side here. You cannot call library actions directly in client side. They can be called only in SA, using EXEC or INCLUDE steps.

1 Like

Thank you Sid, they were two different questions - just asked poorly :sweat_smile:

I have enough insight from your reply here to have a crack at this. I’ll try tonight/tomorrow. Thank you!

1 Like

@sid just wanted to extend a thanks. This worked perfectly, thank you.

1 Like