Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update nextjs guides #8281

Open
wants to merge 99 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
99 commits
Select commit Hold shift + click to select a range
74192fa
update nextjs guides
beerose Jan 30, 2025
3bb1448
Add `edb::env-switcher`, `edb::split-section` and `edb::split-point` …
jaclarke Feb 4, 2025
fe6e198
Add index directives for keywords, symbols, sql equivalents, etc. in …
jaclarke Jan 30, 2025
79d9549
Add docs preview deploys (#8289)
jaclarke Feb 4, 2025
d9f0631
Fix deploy failed message
jaclarke Feb 4, 2025
40a0f09
Fix indentation in string docs
jaclarke Feb 4, 2025
b8acef2
Allow any type of content on right side of split pages
jaclarke Feb 5, 2025
0619bd9
Move JS/Python/Go docs into the core
1st1 Feb 7, 2025
df9985b
Update docs site deployment branch
jaclarke Feb 8, 2025
381feec
Return errors from script if deployment fails
jaclarke Feb 8, 2025
7ec9113
docs: Move guides/cloud -> cloud
1st1 Feb 13, 2025
57da0af
Implement basic Sphinx subsitution |Gel| -> node to custom render
1st1 Feb 14, 2025
90b0bb3
Replace EdgeDB->Gel in all :edb-alt-title:
1st1 Feb 14, 2025
d779fa9
Fix docs/tests to go green
1st1 Feb 14, 2025
83efcee
Get rid of trailing whitespace for good
1st1 Feb 14, 2025
aa6a6a7
Get rid of weird unicode ' symbols
1st1 Feb 14, 2025
cd8076d
Massive (yet careful) rename EdgeDB -> Gel
1st1 Feb 14, 2025
87a1c31
Rename ``.esdl`` -> |.gel|
1st1 Feb 14, 2025
ec61dad
Rename ``edgedb.toml`` to |gel.toml|
1st1 Feb 14, 2025
8b5b2b3
``edgedb`` -> |gelcmd|
1st1 Feb 14, 2025
a681be6
Kill links to (temporarily) Easy EdgeDB and Live Tutorial
1st1 Feb 14, 2025
10968b9
Make doc test stricter; fix bug
1st1 Feb 14, 2025
696acdd
Add a custom role for inline '$ gel ..' commands
1st1 Feb 14, 2025
446e876
Start using :gelcmd:
1st1 Feb 14, 2025
2b2f88d
Fix what's new 6
1st1 Feb 14, 2025
f6738c6
Use :gelcmd: in more places
1st1 Feb 14, 2025
98be319
Replacing some ``edgedb ...`` with :gelcmd:...
1st1 Feb 14, 2025
8e14100
Update some guides
1st1 Feb 14, 2025
63deaaf
Another batch of updates
1st1 Feb 14, 2025
554d601
Another round of EdgeDB -> |Gel|
1st1 Feb 14, 2025
9f0c952
Enable |Gel's| substitution
1st1 Feb 14, 2025
87ea9bd
Gel's -> |Gel's|
1st1 Feb 14, 2025
4679a9b
More renames
1st1 Feb 15, 2025
13f1c72
'gel watch'
1st1 Feb 15, 2025
6a60a84
More edgedb commands -> gel
1st1 Feb 15, 2025
1667f1d
More changes
1st1 Feb 15, 2025
00ddfda
edgedb> -> gel>
1st1 Feb 15, 2025
2671879
edgedb error: -> gel error:
1st1 Feb 15, 2025
7ca895a
Drop Phoenix tutorial (per nik's suggestion)
1st1 Feb 15, 2025
bde0104
Start renaming references
1st1 Feb 15, 2025
98641e0
More refs
1st1 Feb 15, 2025
d8d3f0a
an |Gel| -> a |Gel|
1st1 Feb 15, 2025
d915e54
"$ edgedb" -> "$ gel"
1st1 Feb 15, 2025
46755a1
More renames
1st1 Feb 15, 2025
6000fb5
@edgedb -> @gel
1st1 Feb 15, 2025
d124194
Fix another ref name
1st1 Feb 15, 2025
d4501bf
edgedb-auth-token, edgedb-pkce-verifier -> gel
1st1 Feb 15, 2025
c4d6f06
More renames
1st1 Feb 15, 2025
b4999d1
"$ edgedb" -> "$ gel"
1st1 Feb 15, 2025
1e09d11
Handle .esdl transition
1st1 Feb 15, 2025
e07e9f7
More changes
1st1 Feb 15, 2025
3299349
github link renames
1st1 Feb 15, 2025
725020e
Another batch of renames
1st1 Feb 15, 2025
8981b7c
More renames
1st1 Feb 15, 2025
399f6b2
More renames
1st1 Feb 15, 2025
6912bf1
More renames
1st1 Feb 15, 2025
72733d0
More renames -- almost there
1st1 Feb 15, 2025
a8e0949
More renames
1st1 Feb 15, 2025
db9d8ee
Kill Java
1st1 Feb 15, 2025
b58e9ca
Rename X-Gel-User back to X-EdgeDB-User
1st1 Feb 15, 2025
a2357b5
Bulk-rename ``EDGEDB_NAME`` to :gelenv:`NAME`
1st1 Feb 15, 2025
9f77ed7
More inline ENV vars renames
1st1 Feb 15, 2025
cd62bc2
More environment variables renamed
1st1 Feb 15, 2025
393a049
More env var renames
1st1 Feb 15, 2025
e7bf1a8
Remaining env vars renamed
1st1 Feb 15, 2025
7b5398f
Rename CLI rest filed from edgedb_* to gel_*
1st1 Feb 15, 2025
5e87c4e
Add redirects file
1st1 Feb 15, 2025
3119021
Rename edgedb_toml.rst to gel_toml.rst
1st1 Feb 15, 2025
f07de4d
Update CLI curl URL to new scheme
1st1 Feb 15, 2025
6c6e50a
Commit forgotten new gel_toml.rst
1st1 Feb 15, 2025
b1121dc
Kill versionchanged:_default versioning
1st1 Feb 15, 2025
6f6629b
Kill debug
1st1 Feb 15, 2025
ffc182e
Drop all :version-lt: snippets
1st1 Feb 15, 2025
05d05e0
Some branches<>databases footwork
1st1 Feb 15, 2025
5187a6b
Cleanup version_added < 5
1st1 Feb 15, 2025
5631250
Kill :versionreplace:
1st1 Feb 19, 2025
511454a
Refactor Indexes
1st1 Feb 15, 2025
b62a9dc
Refactor functions
1st1 Feb 15, 2025
5c60f4d
Refactor modules
1st1 Feb 15, 2025
6d7f493
Unify triggers
1st1 Feb 15, 2025
0389a3e
Merge constraints
1st1 Feb 16, 2025
e44afc2
Merge annotations
1st1 Feb 16, 2025
1920531
Merge links
1st1 Feb 16, 2025
e0574ee
Add a clarification about DDL commands
1st1 Feb 16, 2025
517d0c7
Merge extensions
1st1 Feb 16, 2025
08d6670
Merge access policies
1st1 Feb 16, 2025
3597114
Fix up links. OK, I'm not trusting LLMs anymore.
1st1 Feb 17, 2025
5653abc
Fix up constraints
1st1 Feb 17, 2025
967fa4d
Refactor aliases
1st1 Feb 17, 2025
dc15c45
Merge mutation rewrites
1st1 Feb 19, 2025
d3800a3
Upgrade and merge the "Future behavior" section
1st1 Feb 19, 2025
2fec4b4
Kill 3.0 syntax from SDL/index (it's covered in what's new)
1st1 Feb 19, 2025
af6f9d8
Merge globals
1st1 Feb 19, 2025
f23a9d6
Merge primitives/scalars
1st1 Feb 19, 2025
121ebf6
Merge objects
1st1 Feb 19, 2025
c086931
Quickstart tutorial with Next.js (#8269)
scotttrinh Feb 19, 2025
5d1bd8d
Update next guide
beerose Feb 19, 2025
870b60d
Merge branch 'new-gel-docs' into nextjs-guide
beerose Feb 19, 2025
d7339b2
fix format issue
beerose Feb 19, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions docs/guides/tutorials/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ Using EdgeDB with...
.. toctree::
:maxdepth: 1

nextjs_app_router
nextjs_pages_router
nextjs
rest_apis_with_fastapi
rest_apis_with_flask
jupyter_notebook
Expand Down
270 changes: 270 additions & 0 deletions docs/guides/tutorials/nextjs.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,270 @@
.. _ref_guide_nextjs:

=======
Next.js
=======

:edb-alt-title: Using EdgeDB with Next.js

This guide shows how to integrate EdgeDB with Next.js applications.

.. note::

If you have an existing Next.js project, skip to :ref:`Adding EdgeDB to an existing project <adding_edgedb_existing>` section.

Creating a new project
-----------------------

We recommend using the ``@edgedb/create`` package to set up a complete Next.js + EdgeDB project.

.. code-block:: bash

$ npx @edgedb/create

It's a guided CLI tool that helps you configure your new app. Follow these prompts:

1. Enter your project name
2. Choose ``Next.js`` as the web framework
3. Choose whether to use EdgeDB Auth (optional)
4. Choose TypeScript (recommended)
5. Select your preferred router (App or Pages)
6. Choose whether to use Tailwind CSS
7. Configure project structure preferences
8. Let the tool initialize EdgeDB and git repo

The tool will create a new directory with your project name containing a fully configured Next.js + EdgeDB setup.

.. _adding_edgedb_existing:

Adding EdgeDB to an Existing Project
-------------------------------------

If you already have a Next.js project, follow these steps to add EdgeDB:

1. Initialize EdgeDB in your project directory:

.. code-block:: bash

$ npx edgedb project init

2. Install the EdgeDB client library:

.. code-block:: bash

$ npm install edgedb
# or yarn add edgedb
# or pnpm add edgedb

3. Install the query builder generator (recommended for TypeScript projects):

.. code-block:: bash

$ npm install --save-dev @edgedb/generate
# or yarn add --dev @edgedb/generate
# or pnpm add --dev @edgedb/generate

Updating the schema
-------------------

Start by adding new types to your EdgeDB schema:

.. code-block:: sdl
:caption: dbschema/default.esdl

module default {
type BlogPost {
required title: str;
required content: str {
default := ""
}
}
}

Then create and apply your first migration:

.. code-block:: bash

$ npx edgedb migration create
$ npx edgedb migrate

You can now run queries against your new schema. Run the following command to open the EdgeDB REPL:

.. code-block:: bash

$ npx edgedb

You can then execute the following ``insert`` statements:

.. code-block:: edgeql-repl

edgedb> insert BlogPost {
....... title := "This one weird trick makes using databases fun",
....... content := "Use EdgeDB"
....... };
{default::BlogPost {id: 7f301d02-c780-11ec-8a1a-a34776e884a0}}
edgedb> insert BlogPost {
....... title := "How to build a blog with EdgeDB and Next.js",
....... content := "Let's start by scaffolding our app..."
....... };
{default::BlogPost {id: 88c800e6-c780-11ec-8a1a-b3a3020189dd}}


Alternatively, you can use the EdgeDB UI to manage your schema and data. Open the EdgeDB UI by running:

.. code-block:: bash

$ npx edgedb ui


Generating the query builder​
-----------------------------

This step is optional but recommended for TypeScript projects. The query builder generates TypeScript types for your EdgeQL queries:

.. code-block:: bash

$ npx @edgedb/generate edgeql-js

The command introspects your schema and generates a query builder in the ``dbschema/edgeql-js`` directory.

Using EdgeDB in Next.js
-----------------------

EdgeDB with React Server Components (App Router)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we show both server components and server actions?

================================================

Create a server component that fetches data directly from EdgeDB:

.. code-block:: tsx
:caption: app/page.tsx

import { createClient } from 'edgedb';
import e from '@/dbschema/edgeql-js';

const client = createClient();

export default async function Posts() {
const posts = await e.select(e.BlogPost, () => ({
id: true,
title: true,
content: true,
})).run(client);

return (
<div>
{posts.map(post => (
<article key={post.id}>
<h2>{post.title}</h2>
<p>{post.content}</p>
</article>
))}
</div>
);
}

With API Routes
===============

Create an API route and fetch data from the client side:

.. code-block:: tsx
:caption: pages/api/posts.ts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should update this to be an app-router Route Handler, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking to have two options there - with app router and with pages router. I'll make it clear in the title.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to support the pages router still? There is nothing particularly special about this code, and if we show an app router Route Handler, it should be pretty easy for someone two write the equivalent pages router API route, I would think.


import type { NextApiRequest, NextApiResponse } from 'next';
import { createClient } from 'edgedb';
import e from '@/dbschema/edgeql-js';

const client = createClient();

export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
const posts = await e.select(e.BlogPost, () => ({
id: true,
title: true,
content: true,
})).run(client);

res.status(200).json(posts);
}

.. code-block:: tsx
:caption: pages/api/posts.ts

import { useEffect, useState } from 'react';

export default function Posts() {
const [posts, setPosts] = useState(null);

useEffect(() => {
fetch('/api/posts')
.then(res => res.json())
.then(setPosts);
}, []);

if (!posts) return <div>Loading...</div>;

return (
<div>
{posts.map(post => (
<article key={post.id}>
<h2>{post.title}</h2>
<p>{post.content}</p>
</article>
))}
</div>
);
}

Deployment
-----------

First, add a ``prebuild`` script to your ``package.json`` to generate the query builder during deployment:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should move this to gel.toml hooks

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But don't we need this, too? So that t works with a cloud provider when building the app. As I understand, the hooks will work after applying the migration etc, so if I deploy to e.g. Vercel and connect to the Cloud with env variables, it won't run the generator.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, good point, we should do both. We probably need some additional direction to get it to even work during deployment since it needs access to a live database. I think it works fine in the Vercel integration which I'm pretty sure sets the EDGEDB_INSTANCE and EDGEDB_SECRET_KEY environment variables correctly.

Maybe this section should be merged into the sections below?


.. code-block:: json

{
"scripts": {
"prebuild": "npx @edgedb/generate edgeql-js"
}
}

Using Vercel Marketplace (Recommended)
======================================

The easiest way to deploy your Next.js application with EdgeDB is through the `Vercel Marketplace <https://vercel.com/blog/introducing-the-vercel-marketplace>`_:

1. Open your project's dashboard in Vercel
2. Navigate to the Storage tab
3. Select EdgeDB from the Marketplace
4. Follow the prompts to provision your database

Benefits of using Vercel Marketplace integration:

- Seamless authentication with EdgeDB Cloud using your Vercel account
- Automatic configuration of environment variables
- Integration with Vercel Preview deployments
- Consolidated billing through your Vercel account
- GitHub integration for continuous deployment

.. note::

The pricing remains the same whether you access EdgeDB through Vercel or directly via `EdgeDB Cloud <https://cloud.edgedb.com>`_.

Alternative Deployment Options
==============================

If you prefer to manage your EdgeDB deployment separately, you can:

1. Use EdgeDB Cloud directly through `EdgeDB Cloud <https://cloud.edgedb.com>`_
2. Self-host on your preferred cloud provider (`AWS <https://www.edgedb.com/docs/guides/deployment/aws_aurora_ecs>`_, `GCP <https://www.edgedb.com/docs/guides/deployment/gcp>`_, `Azure <https://www.edgedb.com/docs/guides/deployment/azure_flexibleserver>`_)

For these options, you'll need to configure the appropriate environment variables in your Vercel project settings.

Next Steps
----------

- Explore the `EdgeDB documentation <https://www.edgedb.com/docs>`_ for more advanced queries and features
- Check out the `query builder documentation <https://docs.edgedb.com/libraries/js>`_ for TypeScript integration
- View example projects in the `edgedb-examples repository <https://github.com/edgedb/edgedb-examples>`_
Loading