-
Notifications
You must be signed in to change notification settings - Fork 409
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
base: master
Are you sure you want to change the base?
update nextjs guides #8281
Changes from 1 commit
74192fa
3bb1448
fe6e198
79d9549
d9f0631
40a0f09
b8acef2
0619bd9
df9985b
381feec
7ec9113
57da0af
90b0bb3
d779fa9
83efcee
aa6a6a7
cd8076d
87a1c31
ec61dad
8b5b2b3
a681be6
10968b9
696acdd
446e876
2b2f88d
f6738c6
98be319
8e14100
63deaaf
554d601
9f0c952
87ea9bd
4679a9b
13f1c72
6a60a84
1667f1d
00ddfda
2671879
7ca895a
bde0104
98641e0
d8d3f0a
d915e54
46755a1
6000fb5
d124194
d4501bf
c4d6f06
b4999d1
1e09d11
e07e9f7
3299349
725020e
8981b7c
399f6b2
6912bf1
72733d0
a8e0949
db9d8ee
b58e9ca
a2357b5
9f77ed7
cd62bc2
393a049
e7bf1a8
7b5398f
5e87c4e
3119021
f07de4d
6c6e50a
b1121dc
6f6629b
ffc182e
05d05e0
5187a6b
5631250
511454a
b62a9dc
5c60f4d
6d7f493
0389a3e
e44afc2
1920531
e0574ee
517d0c7
08d6670
3597114
5653abc
967fa4d
dc15c45
d3800a3
2fec4b4
af6f9d8
f23a9d6
121ebf6
c086931
5d1bd8d
870b60d
d7339b2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
================================================ | ||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should update this to be an app-router Route Handler, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should move this to There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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>`_ |
There was a problem hiding this comment.
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?