Deploying a NextJS app using StatelyDB on Netlify
We recently published an open source starter template that helps developers get started quickly building with StatelyDB on the Netlify platform. This tutorial will walk you through the example application the starter template uses and how to get your own copy deployed to Netlify.
What We’re Building
Our example app is a simple “Link in Bio” profile page, similar to Linktree or about.me, which displays a name, photo and a collection of links. Here’s what our sample site looks like:
Live demo: https://statelydb-demo.netlify.app/.
Github Source: https://github.com/StatelyCloud/netlify-starter-template
You’re going to need a Store
The very first thing we need to do is get our own Stately Cloud account so we can set up a StatelyDB Store. We’ll do that by visiting the console. If you don’t have an account yet, go through the sign-up process and follow the instructions in the console to get a new Store created.
You’ll want to grab the Store ID listed in the console:
You will also need your Client ID and Client Secret, which should be provided by the Stately team.
Defining a Schema
Our app defines a basic Schema in the schema/schema.ts
file, which looks like this:
|
|
We have two Item Types: Profile and Link.
A Profile contains a unique identifier and a full name, both of which are strings. Our app isn’t used by multiple users, so we use a default value for the identifier (default
). The Profile has a Key Path like /p-default
.
Every Profile has a collection of Links. A Link contains a field that references the Profile identifier, as well as a title, url and emoji. Note that the structure of the Key Path for a Link looks like /p-default/l-1
, which means each link is nested under a Profile and can be efficiently retrieved using the List operation with a prefix.
Here’s an example of visualizing this structure:
We’ve already done the work of generating Typescript client code that lets us use these Item Types, which you can view in the src/lib/generated
folder if you’re curious.
Using the Schema types
Our app defines a set of helper functions for interacting with our StatelyDB Items at src/lib/actions.ts
:
|
|
This single file handles all of our database modifications — the rest of the app code is UI! You’ll notice that the fetchProfileWithLinks
function uses the beginList
operation and supplies a Key Path prefix that will match both the Link and Profile Items. This means we’re able to query multiple records, of different Item types, by Key Path prefix. The other functions show how easy it is to use the types from our Schema to create, edit and delete Items without needing SQL or other boilerplate.
Deploying to Netlify
First we’re going to check out the starter repo:
|
|
Next we’re going to create a new Netlify site. You will need to be logged in to the Netlify CLI first:
|
|
Now we have a site, but it needs to be configured before trying to deploy. Change the environment variables here to match the values from your Stately Cloud account:
|
|
…and now install the npm
dependencies and run a local server:
|
|
Since we set the environment variable NEXT_PUBLIC_EDITABLE
to true
we should be able to open up the edit page at http://localhost:8888/edit and add a few test links! Once you’ve had a chance to make some changes you like, let’s make the site read-only again and deploy it to Netlify.
|
|
And that’s it!
Takeaway
If you’ve followed the steps above you’re well on your way to getting started using StatelyDB with NextJS on Netlify. We know that StatelyDB can handle high scale, but it’s also great for smaller sites because of the flexibility of Elastic Schema. But even better, with StatelyDB you won’t have to worry about planning for the future because you can change your mind later about your data model.
Want to learn more about StatelyDB?
We'd love to hear from you! Book a demo with our team to learn more about how you can iterate faster using StatelyDB with Elastic Schema.