- Getting Started
- Domain Modelling
- Schema Design
- Program Design
- Folder Structure
- Technologies Used
- API Contract
- Testing
- Known Issues
- Deployment
Certainly! Here's a basic outline for a "Getting Started" documentation for the backend server.
Welcome to the backend server of my AI Process Builder application. This guide will help you get up and running with the server so that you can start building amazing features.
Before you begin, ensure you have met the following requirements:
- Node.js installed on your development machine.
- A PostgreSQL database (e.g., Supabase) set up and accessible.
- An OpenAI API Key for AI-powered content generation.
-
Clone the repository:
git clone "https://github.com/anujaagarwal/process-builder-server.git" -
Install dependencies:
cd process-builder-server npm install
-
Create a
.envfile in the root directory of the backend server with the following environment variables:# Database configuration DB_USERNAME=your_database_username DB_PASSWORD=your_database_password DB_HOST=your_database_host DB_DATABASE=your_database_name DB_DIALECT=postgres # OpenAI API Key OPENAI_API_KEY=your_openai_api_key
Replace
your_database_username,your_database_password,your_database_host,your_database_name, andyour_openai_api_keywith your actual database and OpenAI API details. -
Ensure that your connection with postresql is running and accessible by running /healthcheck api. Described below in API contracts
To set up the database schema, run the following command:
npx sequelize-cli db:migrateThis command will execute database migrations using Sequelize to create the necessary tables.
To start the backend server, run the following command:
node app.jsThe server should now be running on the specified port (usually 3000).
My backend server provides various API endpoints for frontend application. Refer to the API documentation for details on available endpoints and their functionality.
You can run tests or test using postman
Congratulations! You've successfully set up backend server. You can now integrate it with your frontend application to build powerful features for AI Process Builder.
-
Process Entity: This entity would represent a process in the system. Attributes which includes id, name, and description.
-
Steps Entity: This entity would represent the steps involved in a process. Attributes includes id, title, description, and processID (to link a step to its process).
- Relationship between Process and Steps is 1:N.
Have a look at the diagram below:-
[Process] 1 ---- N [Steps]
| |
| |
| - ProcessID | - StepID
| - ProcessName | - StepName
| - Description | - StepDescription
| | - StepOrder
| - ProcessID(FK)
In this representation, the line between [Process] and [Steps] shows the relationship, with '1' on the Process side and 'N' on the Steps side, indicating that one Process can have many Steps. The bullet points under each entity represent attributes of that entity.
- Constraints is that ProcessId should be unique always.
-
Processes Table:- This table stores the high-level information about each process.
- id: Primary Key, unique identifier for each process.
- name: Text, name of the process.
- description: Text, a brief description of the process.
- created_at: DateTime, timestamp when the process was created.
- updated_at: DateTime, timestamp when the process was last updated.
-
Steps Table This table stores individual steps for each process.
- id: Primary Key, unique identifier for each step.
- process_id: Foreign Key, links to the Processes table.
- title: Text, title of the step.
- description: Text, detailed description of the step.
- order: Integer, the order of the step within the process.
- metadata: Text, for storing any additional information related to the step.
- created_at: DateTime, timestamp when the step was created.
- updated_at: DateTime, timestamp when the step was last updated.
The program design of the backend of my application, uses Express.js along with controllers, routes, services, migrations, and models through Sequelize ORM, follows a typical MVC (Model-View-Controller) architectural pattern. Here's a brief description of each component and their roles in my backend design:
-
Express.js: Express is a fast and minimalist web framework for Node.js that simplifies the creation of robust and scalable web applications. That was the reason I used express for api development.
-
Controllers: Controllers are responsible for handling incoming HTTP requests, processing data, and sending HTTP responses. They act as intermediaries between the routes (endpoints) and the services. Each controller corresponds to a specific resource or entity in my application, such as processes or steps.
-
Routes: Routes define the available endpoints (URL paths) in my application and map them to specific controller methods. They determine how incoming requests should be handled based on the HTTP method (GET, POST, PUT, DELETE) and the URL.
-
Services: Services encapsulate the business logic of my application. They perform operations such as data validation, database interactions, and any other complex tasks required to fulfill a request. Services are typically called by controllers and can interact with models.
-
Migrations: Migrations are scripts that define the structure and schema of the database tables. They are used to create, modify, or update database tables and their relationships. Sequelize migrations help keep my database schema in sync with my application's models.
-
Models: Models define the data structure and relationships of the application's entities. They serve as an abstraction layer for interacting with the database. Sequelize models provide an object-oriented approach to database operations, allowing to create, read, update, and delete records easily.
The typical flow of a request in my backend application follows these steps:
-
An incoming HTTP request hits a specific route defined in my Express.js application.
-
The route maps the request to the appropriate controller method.
-
The controller method, in turn, call one or more services to perform business logic and data processing.
-
Services interact with Sequelize models to read from or write to the database.
-
The controller receives the results from the services and constructs an HTTP response.
-
The HTTP response is sent back to the client with the requested data or an appropriate status code.
Overall, this design separates concerns, making my backend code organized, maintainable, and easy to extend as application grows.
├── package.json
├── package-lock.json
├── README.md
├── app.js
├── controllers
│ ├── processController.js
│ └── stepController.js
├── models
├── process.js
│ └── step.js
├── migrations
├── create-process.js
│ └── create-step.js
├── routes
│ ├── processRoutes.js
│ └── stepsRoutes.js
├── seeders
└── services
├── openaiService.js
├── processService.js
└── stepsService.js- Used for the database.
- Provides a PostgreSQL database with a user-friendly interface.
- Simplifies database management and reduces the need for complex server-side code.
- Chosen as the runtime environment for the backend.
- Known for its speed and scalability.
- A minimal and fast web framework for Node.js.
- Used to create routes, handle HTTP requests, and structure the backend.
- Simplifies the development of RESTful APIs and web services.
- An Object-Relational Mapping (ORM) library for Node.js.
- Simplifies database interactions by using JavaScript objects instead of raw SQL queries.
- Provides data modeling, validation, and migration capabilities.
- Integrated to generate step-by-step instructions from text descriptions.
- Utilized for AI-powered content generation.
- Postman: Used for testing the apis in local
Here is an high level definition of APIs I have used.
Endpoint: /api/create-process
Method: POST
Description: Create a new process.
Request:
{
"name": "Name of Process",
"description": "Description of the process"
}Response (Success):
{
"createdAt": "2023-12-11T04:16:59.616Z",
"updatedAt": "2023-12-11T04:16:59.616Z",
"id": "id of the process",
"name": "Nameof the process",
"description": "Description of process"
}Response (Error):
{
"error": "Error message"
}Endpoint: /api/process-description
Method: POST
Description: Create a new process in the db and generates steps by interacting with AI and stores those step in the database.
Request:
{
"description": "Description of process"
}Response (Success):
{
"processId": 1,
"steps": [
{
"order": 1,
"title": "Title of step",
"description": "Description of step",
"processId": 1
},
{
"order": 2,
"title": "Title of step",
"description": "Description of step",
"processId": 1
}
]
}Response (Error):
{
"error": "Error message"
}Endpoint: /api/update-step/:processId/:order
Method: PUT
Description: Update an existing step.
Request:
{
"title": "Updated Title",
"description": "Updated Description"
}Response (Success):
{
"id": 1,
"processId": 1,
"title": "Updated Title",
"description": "Updated Description",
"order": 1
}Response (Error):
{
"error": "Error message"
}Endpoint: /api/save-all-steps
Method: GET
Description: Retrieve process details by ID.
Request:
{
"steps": [
{
"processId": 1,
"order": 1,
"title": "hey 1",
"description": "hey 1"
},
{
"processId": 1,
"order": 2,
"title": "hey 2",
"description": "hey 2"
}
]
}Response (Success):
{
"message": "Steps saved successfully"
}Response (Error):
{
"error": "Error message"
}Endpoint: /healthcheck
Method: GET
Description: Check the health status of the server.
Response (Success): "I'm healthy!"
Response (Error): "Unable to connect to server"
Start the node server and one by one use the above endpoints and the localhost url to test the apis in postman.
- Now the openAI api is giving response slow, so have to optimise it.
Used Render to deloy my server, here is the link
https://ai-process-builder.onrender.com