> Note: This step uses a custom dev & build script to initialize Tigris database and collection for
> the app and requires [ts-node](https://www.npmjs.com/package/ts-node#installation) to be installed.
:tada: All done. You should be able to use app on `localhost:3000` in browser. Feel free to play
around or do a [code walk-through](#code-walkthrough) next :tada:
</details>
# 📖 How to use
Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init), [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/), or [pnpm](https://pnpm.io) to bootstrap the example:
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)).
# 👀 Code walkthrough
<details>
<summary> 📂 File structure</summary>
```text
├── package.json
├── lib
│ ├── tigris.ts
├── models
│ └── tigris
│ └── todoStarterApp
│ └── todoItems.ts
└── pages
├── index.tsx
└── api
├── item
│ ├── [id].ts
└── items
├── index.ts
└── search.ts
```
</details>
<details>
<summary> 🪢 Tigris schema definition</summary>
[models/tigris/todoStarterApp/todoItems.ts](models/tigris/todoStarterApp/todoItems.ts) - The to-do list app
has a single collection `todoItems` that stores the to-do items in `todoStarterApp` database. The
Database and Collection get automatically provisioned by the [setup script](scripts/setup.ts).
This is an inspiration from Next.js based file system router. Create a folder or drop a schema file
inside database folder under `models/tigris/`, and you're able to instantly create Databases and
Collections in Tigris for your application.
</details>
<details>
<summary> 🌐 Connecting to Tigris</summary>
[lib/tigris.ts](lib/tigris.ts) - Loads the environment variables you specified previously in creating a Vercel project
section and uses them to configure the Tigris client.
</details>
<details>
<summary> ❇️ API routes to access data in Tigris collection</summary>
All the Next.js API routes are defined under `pages/api/`. We have three files exposing endpoints:
'deleteTodoItem' requires an id value of the TodoItem. When the user presses the 'delete'(cross) button from a TodoItem, this method is invoked.
It calls the endpoint 'api/item/<id>' with the 'DELETE' method. Read the method 'handleDelete' under pages/api/item/[id]' to learn more how the api handles deletion.
*/
constdeleteTodoItem=(id?:number)=>{
setIsLoading(true)
fetch('/api/item/'+id,{
method:'DELETE',
}).then(()=>{
setIsLoading(false)
if(viewMode==='list'){
fetchListItems()
}else{
searchQuery()
}
})
}
// Update Todo-item (mark complete/incomplete)
/*
'updateTodoItem' takes the TodoItem object, inverts the 'completed' boolean and calls the same endpoint as 'deletion' but with a different method 'PUT'.
Navigate to 'api/item/[id]' and read more how the api handles updates under the 'handlePut' method.