The popularity and adoption of low-code tools and frameworks continue to rise. The main advantage of these tools is the reduction in time for developing and deploying custom applications thanks to the use of automatic code-generators. For example, BESSER, an open-source low-code platform for smart software development, offers code generators for various technologies such as SQLAlchemy, Django, Python, and more.

And today, we release an even more interesting generator that, instead of targeting a single artefact, aims to generate the full backend of your application! This new feature is now available in the latest release of the platform.

Step 1 – Model your application with BESSER

BESSER features a modeling language called B-UML for specifying different aspects of a domain or application. B-UML (short for BESSER’s Universal Modeling Language) is heavily inspired by UML but does not aim to be fully compliant with it. In other words, it is composed of a set of sublanguages that modelers can choose to ‘activate’ for a given project depending on their modeling needs. Currently, the types of models that can be created in BESSER include structural models, object models, graphical user interface models, and OCL specifications (you can find the description in BESSER’s documentation).

To generate the backend software for your application, you need to specify a structural model describing the domain concepts in a class diagram.

BESSER can understand structural models built with PlantUML. So, we are going to implement the example model in Figure 1 (the typical model of Library, Book, and Author) to generate its full backend software. You can refer to this documentation for guidance on utilizing PlantUML models within BESSER.

Figure 1. Domain model

 

Step 2 – Generate your Backend

To manage the data conforming to the previous schema you will need at least database where that data can be stored and retrieved. Obviously, the schema of such database will be generated from the structural model. But a database is not enough, you will probably also want to provide a REST API access to the data so you can better control who and how access the data. And not any API but one that has some validation for the input data before trying to save it in the database in order to minimize errors.

Well, our new BESSER version provides all this: our Backend Generator combines multiple specialized generators from the BESSER suite to create a full backend for you, as shown in Figure 2. More specifically, our backend generator facilitates the creation of dynamic and scalable API endpoints thanks to the REST API Generator (relying on the FastAPI framework), efficient ORM transformation for database interactions thanks to the SQL Alchemy Generator, and robust data validation thanks to the Pydantic Generator, ensuring data integrity and backend security.

Figure 2. Overview of the BESSER backend generator

Running the backend generator is similar to how you run any other generator:

Listing 1. Execution of the backend code generator

You can tailor the code generation process to meet your application’s specific needs by selecting appropriate HTTP methods like GET, POST, PUT, and DELETE. This selective approach helps generate only the necessary API components, enhancing efficiency. If your B-UML model includes read-only classes, the generator automatically omits PUT and DELETE methods, maintaining the immutability of certain data elements as defined in your model.

The nested_creations parameter further refines API handling of entity relationships in requests. When enabled, it allows the API to link existing entities using identifiers and create new nested entities within the same request. If disabled, the API restricts itself to linking existing entities, simplifying interactions but limiting flexibility. The default setting is disabled, focusing on straightforward data linkages.

For the Library example previously described, the generator will create three different files using different components of the generators.

  1. The main file generated is main_api.py, which includes the initialization of the database and the REST API endpoints defining how the server responds to client requests.
  2. The second one is the sql_alchemy.py file containing the SQLAlchemy ORM models, which establish mappings between Python classes and database tables.
  3. And the final one, the pydantic_classes.py file crucial for data validation and serialization.

Once main_api.py is launched, the server becomes operational, allowing clients to interact with the backend service through the specified REST API endpoints. Additionally, the openapi_specs.json file is generated. This JSON file contains the documentation following the OpenAPI specification to describe the structure and behavior of the API, including the list of available endpoints, the parameters they accept, the types of data they expect, the possible response codes and other important details. Figure 3 shows an excerpt of the OpenAPI specification generated for the Library example.

Figure 3. OpenAPI specification generated

With the server up and running, and leveraging the OpenAPI specification, you can access the interactive API documentation provided by Swagger UI. Figure 4 displays a snapshot of the graphical interface, through which you can also explore available endpoints, accepted parameters, response codes, and other details.

Figure 4. Interactive API documentation

An SQLite database is automatically constructed according to the models produced in the sql_alchemy.py file. Figure 5 presents a screenshot of the database generated for our Library example with some books entries created.

Figure 5. Generated database

 

What’s next

BESSER strives to be an open-source low-code platform for developing smart software. The code generator presented here is another step forward for BESSER in that regard. This customizable generator provides a solution to streamline the development of backend applications, including their database, REST API endpoints, and OpenAPI specification. With this solution, we showcase BESSER’s ability not only to build new code generators but also its capacity to reuse existing generators as part of a comprehensive solution that involves code generation for various technologies.

We have plenty of ideas about what could be next, but we would love to hear from you as well. What would you like to see in the next BESSER release?????

Want to build better software faster?

Want to build better software faster?

Read about the latest trends on software modeling and low-code development

You have Successfully Subscribed!

Pin It on Pinterest

Share This