Skip to content

Commit cd839b1

Browse files
Merge branch 'canary' into add-with-zustand-example
2 parents 756911e + b8c49ae commit cd839b1

File tree

11 files changed

+446
-0
lines changed

11 files changed

+446
-0
lines changed

examples/with-knex/.env.local.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PG_URI=postgres://admin:pass@localhost:5432/mydb

examples/with-knex/.gitignore

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
21+
# debug
22+
npm-debug.log*
23+
yarn-debug.log*
24+
yarn-error.log*
25+
26+
# local env files
27+
.env.local
28+
.env.development.local
29+
.env.test.local
30+
.env.production.local

examples/with-knex/README.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
## Example app using Knex
2+
3+
[Knex](http://knexjs.org/) is a SQL query builder that works with a variety of SQL databases including Postgres and MySQL. This example shows you how to use Knex with Next.js to connect and query a Postgres database. The same code can also connect to all other databases supported by Knex.
4+
5+
## Deploy your own
6+
7+
Once you have access to the environment variables you'll need, deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example):
8+
9+
[![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-knex&env=PG_URI&envDescription=Required%20to%20connect%20the%20app%20with%20Postgres)
10+
11+
## How to use
12+
13+
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:
14+
15+
```bash
16+
npx create-next-app --example with-knex with-knex
17+
# or
18+
yarn create next-app --example with-knex with-knex
19+
```
20+
21+
## Configuration
22+
23+
### Install dependencies
24+
25+
```bash
26+
npm install
27+
# or
28+
yarn
29+
```
30+
31+
### Set up a Postgres database
32+
33+
Set up a Postgres database locally or use a DBaaS provider such as AWS or Digital Ocean
34+
35+
### Configure environment variables
36+
37+
Copy the `.env.local.example` file in this directory to `.env.local` (this will be ignored by Git):
38+
39+
```bash
40+
cp .env.local.example .env.local
41+
```
42+
43+
Set the `PG_URI` variable in `.env.local` to the connection uri of your postgres database.
44+
45+
### Apply migrations
46+
47+
You can create, apply and rollback migrations using the scripts in `package.json`. For now we will run the example migrations in the `knex/migrations` folder, which will add some Todos to the database.
48+
49+
```bash
50+
npm run migrate:latest
51+
# or
52+
yarn migrate:latest
53+
```
54+
55+
### Start Next.js in development mode
56+
57+
```bash
58+
npm run dev
59+
# or
60+
yarn dev
61+
```
62+
63+
Your app should now be up and running on [http://localhost:3000](http://localhost:3000)! If it doesn't work, post on [GitHub discussions](https://github.com/zeit/next.js/discussions).
64+
65+
You should now see a list of Todos that were fetched from the database via the API Route defined in `/pages/api/todos.js`.
66+
67+
## Deploy on Vercel
68+
69+
You can deploy this app to the cloud with [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)).
70+
71+
#### Deploy Your Local Project
72+
73+
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).
74+
75+
**Important**: When you import your project on Vercel, make sure to click on **Environment Variables** and set them to match your `.env.local` file.
76+
77+
#### Deploy from Our Template
78+
79+
Alternatively, you can deploy using our template by clicking on the Deploy button below.
80+
81+
[![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-knex&env=PG_URI&envDescription=Required%20to%20connect%20the%20app%20with%20Knex)

examples/with-knex/knex/index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import knex from 'knex'
2+
import config from '../knexfile.js'
3+
4+
/**
5+
* Global is used here to ensure the connection
6+
* is cached across hot-reloads in development
7+
*
8+
* see https://github.com/vercel/next.js/discussions/12229#discussioncomment-83372
9+
*/
10+
let cached = global.pg
11+
if (!cached) cached = global.pg = {}
12+
13+
export function getKnex() {
14+
if (!cached.instance) cached.instance = knex(config)
15+
return cached.instance
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
exports.up = async function (knex) {
2+
await knex.schema.createTable('todos', (table) => {
3+
table.increments('id')
4+
table.string('text').notNullable()
5+
table.boolean('done').notNullable()
6+
})
7+
8+
await knex('todos').insert([
9+
{ text: 'Buy milk', done: true },
10+
{ text: 'Wash car', done: false },
11+
])
12+
}
13+
14+
exports.down = async function (knex) {
15+
await knex.raw('DROP TABLE todos CASCADE')
16+
}

examples/with-knex/knexfile.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const { loadEnvConfig } = require('@next/env')
2+
3+
const dev = process.env.NODE_ENV !== 'production'
4+
const { PG_URI } = loadEnvConfig('./', dev).combinedEnv
5+
6+
module.exports = {
7+
client: 'pg',
8+
connection: PG_URI,
9+
migrations: {
10+
directory: './knex/migrations',
11+
},
12+
seeds: {
13+
directory: './knex/seeds',
14+
},
15+
}

examples/with-knex/package.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "with-knex",
3+
"version": "0.1.0",
4+
"scripts": {
5+
"dev": "next dev",
6+
"build": "next build",
7+
"start": "next start",
8+
"migrate:make": "knex migrate:make",
9+
"migrate:latest": "knex migrate:latest",
10+
"migrate:up": "knex migrate:up",
11+
"migrate:down": "knex migrate:down",
12+
"migrate:status": "knex migrate:status",
13+
"seed:create": "knex seed:make",
14+
"seed:run": "knex seed:run"
15+
},
16+
"dependencies": {
17+
"knex": "0.21.6",
18+
"next": "latest",
19+
"pg": "8.4.1",
20+
"react": "^16.13.1",
21+
"react-dom": "^16.13.1"
22+
},
23+
"license": "MIT"
24+
}

examples/with-knex/pages/api/todos.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { getKnex } from '../../knex'
2+
3+
export default async function handler(req, res) {
4+
const knex = getKnex()
5+
const todos = await knex('todos')
6+
res.status(200).json(todos)
7+
}

0 commit comments

Comments
 (0)