Skip to content

An example of how to use `@harperdb/nextjs` to build a Next.js application on HarperDB

Notifications You must be signed in to change notification settings

HarperDB/nextjs-example

Repository files navigation

HarperDB Next.js Example

This is an example of how to use @harperdb/nextjs to develop a Next.js application with HarperDB.

The Next.js application can interact with the database through the Resource API directly instead of relying on network operations. This is significantly more efficient and enables a better application development experience.

Get Started

  1. Clone this repo locally
  2. Run npm install
    1. If HarperDB is already installed on your machine (we recommend it be installed globally), then run npm link harperdb.
  3. Run npm run dev
  4. Open http://localhost:3000 🎉

How does it work?

This example in and of itself is a HarperDB Component, and is reliant on the @harperdb/nextjs protocol extension component in order to access to the HarperDB Globals. The globals are only available on server-side code paths such as server actions and server components. Any code paths using HarperDB globals must first import the harperdb package (i.e. import('harperdb')).

Tip

Use the harperdb-nextjs CLI (part of the @harperdb/nextjs package) to replace the Next.js CLI. For example, next dev becomes harperdb-nextjs dev. This CLI handles running HarperDB and providing sensible configuration values for the @harperdb/nextjs component.

Based on Next.js best practices, it is recommended to use this in server actions so that server and client components can both access the same functions. This example demonstrates this pattern by defining two server actions, listDogs and getDog (located in ./app/actions.js). These are then used throughout the application, in both Client and Server components!

About

An example of how to use `@harperdb/nextjs` to build a Next.js application on HarperDB

Topics

Resources

Stars

Watchers

Forks