Unlock the Power of Multi-Model Databases with ArangoDB
In today’s fast-paced development landscape, managing complex data types can be a daunting task. But what if you could harness the strengths of different database paradigms in a single, flexible solution? Enter ArangoDB, a cutting-edge, open-source database that combines the benefits of graph, document, and relational databases.
Getting Started with ArangoDB
To begin, sign up for a new Oasis account on cloud.arangodb.com and create a new ArangoDB project. Be sure to select the OneShard basic service to stay within the free tier. Choose a name for your project, select your cloud provider and region, and wait a few minutes for your database to boot up.
Familiarizing Yourself with Arango Query Language
Arango Query Language (AQL) is intuitive and easy to learn. Let’s create a new example collection called “cheese” and insert some data using the following code:
INSERT { name: "Cheddar" } INTO cheese
To retrieve data from the cheese collection, run the query:
FOR c IN cheese RETURN c
ArangoDB Node.js Drivers
Before integrating ArangoDB with our React and Next.js project, let’s explore how drivers work in Node.js. Create a new folder called “arangotest” and install the ArangoDB JavaScript Driver. Establish a database connection using the following code:
const db = new Database('your-database-connection-string');
Creating a Collection and Adding Data
Define a function that creates a new collection in our database or prevents an error from occurring if the collection already exists:
async function getCollection(collectionName) {... }
Let’s add a new item, “cat,” to our collection using the following function:
async function addItem(collectionName, itemName) {... }
Querying Results
To query the results, we’ll use an AQL query that returns an asynchronous ArrayCursor object. Loop over the query cursor and push the results into an array:
async function getCats() {... }
Using ArangoDB in Your React Application
To avoid exposing your database credentials on your application’s frontend, create an API with your favorite Node.js framework or use serverless functions. We’ll use Next.js to create an API to access ArangoDB from within our React application.
Setting Up Your Next.js Application
Generate a new Next.js application and install arangojs. Create a folder called “utils” and add a file called “db.js” to wrap our connection code in its own function.
Creating an API Route in Next.js
Create a file in pages/api called “cats.js” and define an API route to query cats:
export default async function handler(req, res) {... }
Getting Data from a React Component
Use the standard practice of getting data from an API. Declare state to hold the API data with the useState Hook and declare a function to request the data with fetch and update the state.
The Future of Database Management
Multi-model databases like ArangoDB are revolutionizing the way we manage complex data. With its flexible data models and cloud-hosted database service, Oasis, ArangoDB makes it easy to build and scale your application.