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.
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.
Running the backend generator is similar to how you run any other 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.
- 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.
- The second one is the sql_alchemy.py file containing the SQLAlchemy ORM models, which establish mappings between Python classes and database tables.
- 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.
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.
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.
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?????
Hi:
What about a generator for creating applications using Wikidata[1] as persistence API? Ideally, choosing an EntitySchema[2].
Also, it would be awesome being able to create an application backend based on EntitySchemas using any Wikibase[3] instance using the Wikibase API[4].
If you are interested, please reach me.
[1] https://www.wikidata.org/
[2] https://www.wikidata.org/wiki/Wikidata:Database_reports/EntitySchema_directory
[3] https://wikiba.se/
[4] https://doc.wikimedia.org/Wikibase/master/js/rest-api/
Hi Ismael.
If I understand correctly, the idea is that from a selected EntitySchema, the code generator produces the backend code for CRUD operation (using the wikidata persistence API)? Could you please give us more details about what you have in mind?
Sure I can.
For example, we have a current tool named Cradle which does something similar, but AFAIK it doesn’t implement EntitySchemas fully: https://cradle.toolforge.org/
The tool creates a form for the selected schema, making easier to add new Wikidata items with a little more friendly UI.
For example, for the E10[1] schema you get https://cradle.toolforge.org/?#/shex/E10
[1] https://www.wikidata.org/wiki/EntitySchema:E10
In an ideal world the editor would be more powerful for browsing and modifying items and not only creating new ones.
Here it is a proposal for writing a new version of Cradle: https://drive.google.com/file/d/10dymKaXA3g5Bm6jk730OvNERsJMFNPjp/view
In this case, when I’m referring to Cradle is to be used with an associated Wikibase instance, like Wikidata or in a private server.
If you have more interest, we can talk by Telegram/Guasap/videoconf/etc 🙂
Thank you for sharing this. It’s much clearer now and the idea is quite interesting. We would be happy to contribute. If you have someone interested in working on its development, we could offer our help and support, and we could set up a meeting to discuss it further.