Database

Overview

The Appwrite Database in Patronus is designed to securely store and manage all flight-related data, user preferences, and prediction outputs. It plays a key role in organizing data input from the frontend and efficiently handling backend interactions.

Database Structure in Patronus

In Patronus, our Appwrite Database is set up with specific collections to streamline data retrieval and manipulation. The main collections used are:

  1. Flight Requests: Stores details about each flight search request, including:

    • fromLocation: Departure city or airport code.

    • toLocation: Destination city or airport code.

    • departureDate and arrivalDate: User-selected travel dates.

    • passengerCount: Number of passengers for the search.

  2. Predictions: Holds AI-generated data for optimal booking information, including:

    • predictedPrice: The lowest price predicted by the AI model.

    • predictedDate: The date/time when this price is expected.

    • bookingLink: Direct link to book with the airline or provider.

  3. User Profiles: Stores user data, such as email, preferences, and notification settings.

Creating Database Documents from the Frontend

To populate the Appwrite Database, our frontend leverages Appwrite’s Database API to create new documents in the respective collections. This setup ensures that flight details and requests can be easily captured as users input their travel information.

Here’s how documents are created from the frontend:

  1. Initialize the Appwrite Database Client:

    • The Appwrite client is initialized with proper authentication, allowing our frontend to interact with the database securely.

    import { Client, Databases } from 'appwrite';
    
    const client = new Client()
      .setEndpoint(process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT)
      .setProject(process.env.NEXT_PUBLIC_APPWRITE_PROJECT_ID);
    
    const databases = new Databases(client);
  2. Creating a Flight Request Document:

    • When a user submits a flight query, the following function is triggered to create a document in the Flight Requests collection:

    async function createFlightRequest(fromLocation: string, toLocation: string, departureDate: string, arrivalDate: string, passengerCount: number) {
      try {
        const response = await databases.createDocument(
          'YOUR_DATABASE_ID',
          'Flight Requests Collection ID',
          {
            fromLocation,
            toLocation,
            departureDate,
            arrivalDate,
            passengerCount,
          }
        );
        console.log('Flight Request Created:', response);
      } catch (error) {
        console.error('Error creating flight request:', error);
      }
    }

Backend Interaction with the Database

The backend in Patronus uses the Appwrite SDK to manage data flow and interact with these collections efficiently. Here’s a breakdown of how the backend interacts with the database for essential tasks:

  1. Fetching Flight Request Data:

    • When the flightQueryFunction is triggered, it retrieves relevant flight request details directly from the Flight Requests collection. This data is essential for making requests to external APIs and performing predictions.

    async function getFlightRequestData(requestId) {
      const client = new Client();
      const databases = new Databases(client);
      client.setEndpoint(process.env.APPWRITE_ENDPOINT).setProject(process.env.APPWRITE_PROJECT_ID);
    
      const flightRequest = await databases.getDocument('YOUR_DATABASE_ID', 'Flight Requests Collection ID', requestId);
      return flightRequest;
    }
  2. Saving AI Predictions:

    • After receiving predictions from the AI model, the backend stores this data in the Predictions collection for future reference and notifications. This includes the predicted price, date, and booking link.

    async function savePredictionData(predictionData) {
      const client = new Client();
      const databases = new Databases(client);
      client.setEndpoint(process.env.APPWRITE_ENDPOINT).setProject(process.env.APPWRITE_PROJECT_ID);
    
      const response = await databases.createDocument(
        'YOUR_DATABASE_ID',
        'Predictions Collection ID',
        predictionData
      );
      return response;
    }
  3. Updating and Notifying Users:

    • As predictions update, Patronus can notify users or update their profiles by interacting with the User Profiles collection. For instance, users are notified when a predicted price hits their desired threshold.

Database Security and Permissions

To maintain security, document permissions are carefully set so that only authenticated users can access or modify their own data. Permissions are adjusted based on the collection type:

  • Flight Requests: Only the user who created the document can view or update their requests.

  • Predictions: Permissions are set so that predictions can be read by users who made the request.

  • User Profiles: Only the owner of the profile can access and modify their profile data.

Use Cases in Patronus

  • Flight Request Logging: All user flight requests are stored, enabling easy retrieval for analysis and reporting.

  • Prediction Management: The Predictions collection retains AI results, making it straightforward to track historical price trends.

  • User-Centric Data Storage: Profile and preference data help tailor the user experience, including notification settings and flight preferences.


This section provides a comprehensive look into how Patronus uses the Appwrite Database for data management. Let me know if this aligns with your expectations or if you’d like more details added!

Last updated