Architecture Design and Extensibility of JianghuJS
120021. Architecture
The architecture of JianghuJS can be briefly summarized into the following three main layers:
Frontend (doUiAction)
- On the frontend, the _ui table is used to uniformly manage frontend actions, including UI changes, data requests, etc.
- After enabling uiAction, you can execute all methods (functions) defined in the uiActionConfig of the _ui table by calling doUiAction("uiActionId", uiActionParamObj).
Backend (Resource-related & Middleware)
- The backend mainly involves handling Resource-related processes and the use of middleware.
- Resource is used to control the processing of business logic, providing users with two options: one is to handle it directly using SQL, and the other is to use Service for processing. This design allows for flexibility by choosing the appropriate method based on actual circumstances.
- Middleware is a code segment that executes before or after the request reaches the processing logic, which can be used to handle common logic, permission verification, etc.
Database (View Table)
- The database layer introduces views (View) to standardize development, simplify code, and achieve data joins, simplified queries, custom statistics, and other functionalities.
- The use of views helps to simplify complex query logic, encapsulate data join operations, and improve development efficiency.
This architectural design enables JianghuJS to provide a clear set of specifications and tools at the frontend, backend, and database levels, aimed at improving development efficiency, reducing code complexity, and providing flexibility to accommodate different development needs.
2. Extensibility
- Frontend Extensibility
In JianghuJS frontend development, there are several aspects of extensibility that allow developers to customize according to actual needs:
Custom Login Page: Developers can override the loginV4.html file in the app/view/page directory to implement a custom login page. This flexibility allows the appearance and interaction of the login page to be tailored to project requirements.
Custom Navigation Bar: In the app/view/component directory, developers can override the jhMenuV4.html file to customize the navigation bar. This includes the style, layout, and the links and menu items contained within, allowing the navigation bar to better align with the overall style and needs of the project.
Custom Template: In the app/view/template directory, developers have the opportunity to override the jhTemplateV4.html file to customize the template. Templates typically include the overall structure, layout, and interaction methods with backend data, allowing developers to tailor them to better meet business requirements.
- Backend Extensibility
Custom Middleware: Middleware is a function that can be executed during the request processing. Developers can write middleware based on business needs and apply it to specific routes or globally to achieve various custom functionalities. For more details, refer to 05_Custom Middleware in JianghuJS.
Custom Service: Developers can override part or all of the service logic in the app/service.js file to implement custom services. The built-in router, controller, and service in the framework can all be customized by users, following JianghuJS's specifications for handling input and output parameters.
loadConfig() {
super.loadConfig();
}
loadRouter() {
// super.loadRouter();
this.initJianghuRouter();
}
initJianghuRouter() {
this.loadController({
directory: this.getLoadUnits().map(unit => path.join(unit.path, 'app/controller')),
// Allow controller to support override
override: true,
});
this.getLoadUnits().reverse().forEach(unit =>
this.loadFile(path.join(unit.path, 'app/router.js'))
);
}
loadService(opt) {
opt = Object.assign({
override: true,
}, opt);
super.loadService(opt);
}- Override Login Logic: Simply override passwordLogin in app/service.js. According to the login resource configuration, if there is no service or action, create the corresponding service and action.
- First, create user.js under
app/service.js. - In user.js, override the corresponding passwordLogin method in JianghuJS.
- First, create user.js under
async passwordLogin() {
// Implement logic
}- Database Extensibility
In the database layer of JianghuJS, developers have the flexibility to choose and replace databases. Although JianghuJS defaults to using MySQL, users can choose excellent relational databases such as MariaDB, Percona Server, Amazon Aurora, TiDB, etc., based on actual needs to replace MySQL.
This database extensibility means that users can select suitable relational databases according to the specific requirements of the project and the performance characteristics of the database, meeting the project's needs for performance, reliability, and scalability. This also reflects JianghuJS's loosely coupled design at the database level, making the project more customizable and extensible.