Skip to content

Commit db69e47

Browse files
author
Sven Anderson
authored
example application on Redis (Lambda.store) (vercel#15532)
This example application guides you to build a next.js application and integrate lambda.store as data store. See readme for details.
1 parent 6375026 commit db69e47

File tree

20 files changed

+611
-0
lines changed

20 files changed

+611
-0
lines changed

examples/with-redis/.gitignore

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# next.js
12+
/.next/
13+
/out/
14+
15+
# production
16+
/build
17+
18+
# misc
19+
.DS_Store
20+
*.pem
21+
22+
# debug
23+
npm-debug.log*
24+
yarn-debug.log*
25+
yarn-error.log*
26+
27+
# local env files
28+
.env.local
29+
.env.development.local
30+
.env.test.local
31+
.env.production.local
32+
33+
# vercel
34+
.vercel

examples/with-redis/README.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Redis Example
2+
3+
This example showcases how to use Redis as a data store in a Next.js project. [Lambda Store](https://lambda.store/) is used as managed Redis service.
4+
5+
The example is a basic roadmap voting application where users can enter and vote for feature requests. It features the following:
6+
7+
- Users can add and upvote items (features in the roadmap), and enter their email addresses to be notified about the released items.
8+
- The API records the ip-addresses of the voters, so it does not allow multiple votes on the same item from the same IP address.
9+
- To find the id of any item, click the vote button, you will see its id on the url.
10+
11+
## Demo
12+
13+
[https://roadmap-voting-demo.vercel.app/](https://roadmap-voting-demo.vercel.app/)
14+
15+
## Deploy your own
16+
17+
Once you have access to [the environment variables you'll need](#configuration), deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example):
18+
19+
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/import/git?c=1&s=https://github.com/vercel/next.js/tree/canary/examples/with-redis&env=REDIS_URL&envDescription=Required%20to%20connect%20the%20app%20to%20Redis&envLink=https://github.com/vercel/next.js/tree/canary/examples/with-redis%23configuration)
20+
21+
## How to use
22+
23+
Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init) or [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/) to bootstrap the example:
24+
25+
```bash
26+
npx create-next-app --example with-redis with-redis-app
27+
# or
28+
yarn create next-app --example with-redis with-redis-app
29+
```
30+
31+
## Configuration
32+
33+
A data store with Redis is required for the app to work. In the steps below we'll integrate Lambda Store as the data store.
34+
35+
### Without Vercel
36+
37+
If you are planning to deploy your application to somewhere other than Vercel, you'll need to integrate Lambda Store by setting an environment variable.
38+
39+
First, create an account and a database in the [Lambda Store console](https://console.lambda.store/).
40+
41+
To connect to Redis, you will need your Redis connection string. You can get the connection string by clicking on **Connect** in the Database page within the Lambda Store dashboard as below:
42+
43+
![setup without vercel](./docs/lstr6.png)
44+
45+
Next, create a file called `.env.local` in the root directory and copy your connection string:
46+
47+
```bash
48+
REDIS_URL="YOUR_REDIS_CONNECTION_STRING"
49+
```
50+
51+
Your app is now connected to a remote Redis database!
52+
53+
### Using Vercel
54+
55+
You can add the Lambda Store integration to your Vercel account. Once you set up the integration you won't have to visit the Lambda Store console anymore. Follow the next steps to setup the integration:
56+
57+
#### Step 1. Deploy Your Local Project
58+
59+
To deploy your local project to Vercel, push it to GitHub/GitLab/Bitbucket and [import to Vercel](https://vercel.com/import/git?utm_source=github&utm_medium=readme&utm_campaign=next-example).
60+
61+
#### Step 2. Add the Lambda Store Integration to Your Vercel Account
62+
63+
Visit Vercel [Lambda Store Integration](https://vercel.com/integrations/lambdastore) page and click the `Add` button.
64+
65+
#### Step 3. Configure the Integration
66+
67+
The integration requires a [Developer API Key](howto/developerapi.md) that can be created from the [Lambda Store console](https://console.lambda.store).
68+
69+
Enter the API key and your registered email address in the integration setup page:
70+
71+
![setup](./docs/lstr1.png)
72+
73+
#### Step 4. Create a Database
74+
75+
In the next page of the integration setup, your databases will be automatically listed. A new database can be created from the Vercel Integration page as well as in the Lambda Store Console:
76+
77+
![new db](./docs/lstr2.png)
78+
79+
Click the **New Database**, you should be able to see the page below:
80+
81+
![new db form](./docs/lstr3.png)
82+
83+
Fill out the form and click on **Create** to have your new database.
84+
85+
#### Step 5. Link the Database to Your Project
86+
87+
Select your project from the dropdown menu then click on **Link To Project** for any database.
88+
89+
`REDIS_URL` will be automatically set as an environment variable for your application.
90+
91+
![link project](./docs/lstr4.png)
92+
93+
![redis url env](./docs/lstr5.png)
94+
95+
**Important:** You will need to re-deploy your application for the change to be effective.

examples/with-redis/docs/lstr1.png

176 KB
Loading

examples/with-redis/docs/lstr2.png

52.6 KB
Loading

examples/with-redis/docs/lstr3.png

51.8 KB
Loading

examples/with-redis/docs/lstr4.png

36.2 KB
Loading

examples/with-redis/docs/lstr5.png

18.3 KB
Loading

examples/with-redis/docs/lstr6.png

133 KB
Loading

examples/with-redis/package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "with-redis",
3+
"version": "0.1.0",
4+
"scripts": {
5+
"dev": "next dev",
6+
"build": "next build",
7+
"start": "next start"
8+
},
9+
"dependencies": {
10+
"next": "latest",
11+
"react": "^16.13.1",
12+
"react-dom": "^16.13.1",
13+
"react-toastify": "^6.0.8",
14+
"redis": "^3.0.2",
15+
"uuid": "^8.2.0"
16+
},
17+
"license": "MIT"
18+
}

examples/with-redis/pages/_app.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import 'react-toastify/dist/ReactToastify.css'
2+
import '../styles/base.css'
3+
4+
// This default export is required in a new `pages/_app.js` file.
5+
export default function MyApp({ Component, pageProps }) {
6+
return <Component {...pageProps} />
7+
}

0 commit comments

Comments
 (0)