Upload AI Search Cover
Author Cloudapp
E.G.

Next.js 14 — Sync Neon.Tech Postgres DB with Azure AI Search

January 11, 2025
Table of Contents

In the fast-evolving world of search capabilities, the ability to connect and synchronize data across various platforms is key. This article will guide you through connecting a Neon.Tech Postgres database to Azure AI Search using Next.js 14, providing you with an API route that seamlessly handles the sync.

What is Neon.Tech Postgres?

Neon.Tech is an innovative cloud-based Postgres database provider that delivers high-performance data solutions with powerful scaling options. Built for modern applications, Neon.Tech Postgres provides fully managed database services with real-time features and integration capabilities. It’s a great choice for developers seeking a Postgres-based solution that can scale with their needs.

What is Azure AI Search?

Azure AI Search is Microsoft’s robust search service that helps organizations quickly add advanced search capabilities to their applications. Powered by AI, Azure AI Search offers features like natural language processing, contextual understanding, and OCR, which makes it ideal for applications requiring powerful and flexible search functionalities.

Installing Necessary NPM Packages

In your Next.js 14 project, you need to install the following packages for database interaction and Azure AI Search:

These packages allow us to connect to Neon.Tech Postgres and interact with Azure AI Search in our Next.js app.

To create a new Azure AI Search resource, you’ll need an active Azure account and the Azure CLI installed on your machine. If you haven’t already, log in with az login.

Create a Resource Group (if you don’t already have one):

Create an Azure AI Search Service:

  • Replace <your-search-service-name> with a unique name for your Azure Search service.

  • The sku free option provides a free tier with basic features. You can choose other SKUs based on your needs.

Create an Index in Azure AI Search: Define the structure of the index to match the fields you’ll be indexing. Here’s an example schema for the users table, which can be saved in a JSON file (e.g., indexSchema.json):

Use the following command to create the index from the JSON schema:

  • This command uploads the index schema, creating an index named users-index.

Retrieve API Keys and Service Name:

To interact with Azure AI Search from your application, you’ll need an API key and the service endpoint URL.

Get the Admin Key for your Azure AI Search service:

  • The output will contain primaryKey and secondaryKey. Use the primaryKey for your API key in the code example.

Configuring Environment Variables

In your Next.js project, create a .env.local file in the root directory and add the following environment variables:

  • AZURE_SEARCH_SERVICE_NAME: Your Azure AI Search service name.

  • AZURE_SEARCH_API_KEY: The primary admin key you retrieved.

  • AZURE_SEARCH_INDEX_NAME: The name of the index you created (users-index).

Synchronizing Data: The Next.js API Route

To implement the synchronization between Neon.Tech Postgres and Azure AI Search, we’ll set up an API route in our Next.js 14 application. This API route will pull data from a table in our Neon.Tech Postgres DB and index it into Azure AI Search, allowing for robust search features.

Explanation of the Code

  1. Environment Variables: We set up environment variables for the Azure AI Search service. These variables include the service name, API key, and index name, keeping sensitive data outside the codebase.

  2. SQL Query Execution: Using a standard SELECT * query, we retrieve data from the users table in Neon.Tech Postgres. This is achieved with the query function from the imported database library ("@/lib/postgresdb").

  3. Document Preparation: We format the retrieved data to match the structure expected by Azure AI Search, ensuring that each document has required fields like id, name, email, and more.

  4. Indexing in Azure AI Search: Finally, we use the uploadDocuments method to index these documents into our Azure AI Search index. Error handling is built in to capture and log any issues that might arise during the indexing process.

Running the Synchronization

To test the sync, send a POST request to this API route. Once triggered, the API route will retrieve the data from Neon.Tech Postgres, format it and upload it to Azure AI Search, where it’s ready for querying.

Conclusion

This setup allows you to maintain up-to-date data in Azure AI Search while benefiting from the performance and flexibility of Neon.Tech Postgres. With this approach, adding powerful search capabilities to your application becomes straightforward and efficient.

Let me know if this guide helped you synchronize Neon.Tech Postgres with Azure AI Search!

Cloudapp-dev, and before you leave us

Thank you for reading until the end. Before you go:

Please consider clapping and following the writer! 👏 on our Medium Account

Or follow us on twitter -> Cloudapp.dev

Related articles