Unverified Commit 420d7850 authored by Thomas Ballinger's avatar Thomas Ballinger Committed by GitHub

Update Convex example. (#38850)

parent 8fe44c31
......@@ -20,16 +20,28 @@ yarn create next-app --example with-convex with-convex-app
pnpm create next-app --example with-convex with-convex-app
```
After creating a [Convex account](https://www.convex.dev) and getting a beta key,
Log in to Convex,
```bash
npx convex init --beta-key <your beta key>
npx convex login
```
Next, push the Convex functions for this project.
initialize a new Convex project,
```bash
npx convex init
```
and push the Convex functions for this project.
```bash
npx convex push
```
Now you can run your code locally with a Convex backend with
```bash
npm run dev
```
Deploy it to the cloud with [Vercel](https://vercel.com/new?utm_source=github&utm_medium=readme&utm_campaign=next-example) ([Documentation](https://nextjs.org/docs/deployment)).
# Welcome to your functions directory
# Welcome to your Convex functions directory!
Write your convex functions in this directory.
Write your Convex functions here.
A query function (how you read data) looks like this:
A query function looks like:
```typescript
// getCounter.ts
// myQueryFunction.ts
import { query } from './_generated/server'
export default query(async ({ db }): Promise<number> => {
const counterDoc = await db.table('counter_table').first()
console.log('Got stuff')
if (counterDoc === null) {
return 0
}
return counterDoc.counter
export default query(async ({ db }) => {
// Load data with `db` here!
})
```
A mutation function (how you write data) looks like this:
A mutation function looks like:
```typescript
// incrementCounter.ts
// myMutationFunction.ts
import { mutation } from './_generated/server'
export default mutation(async ({ db }, increment: number) => {
let counterDoc = await db.table('counter_table').first()
if (counterDoc === null) {
counterDoc = {
counter: increment,
}
db.insert('counter_table', counterDoc)
} else {
counterDoc.counter += increment
db.replace(counterDoc._id, counterDoc)
}
// Like console.log but relays log messages from the server to client.
console.log(`Value of counter is now ${counterDoc.counter}`)
export default mutation(async ({ db }) => {
// Edit data with `db` here!
})
```
The convex cli is your friend. See everything it can do by running
The Convex CLI is your friend. See everything it can do by running
`npx convex -h` in your project root directory. To learn more, launch the docs
with `npx convex docs`.
......@@ -9,7 +9,7 @@
"next": "latest",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"convex-dev": "0.1.4"
"convex": "^0.1.6"
},
"devDependencies": {
"@types/node": "~16.11.12",
......
import '../styles/globals.css'
import type { AppProps } from 'next/app'
import { ConvexProvider, ConvexReactClient } from 'convex-dev/react'
import { ConvexProvider, ConvexReactClient } from 'convex/react'
import convexConfig from '../convex.json'
const convex = new ConvexReactClient(convexConfig.origin)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment