Building CRUD Apps with NextJs

0
50
Building CRUD Apps with NextJs


When building full-stack web applications, the ability to create, read, update and delete items is crucial. For example, blog sites need dynamic content management for productivity. With CRUD functionality, we can carry out these functions in our application. We will be using Next.js as our case study to look at how CRUD implementation works.

What is a CRUD application?

A CRUD application refers to an app that can perform four simple functions: Creating, Reading, Updating, and Deleting data. If you have developed programs using databases, you would have likely performed CRUD operations. Applications that keep track of data would require data storage and organization in ways that can be easily accessed, searched, or modified. Hence the need for CRUD functionality in modern-day applications.

How do CRUD operations work?

The user in a database can store the data required by an application. The database may provide an authorization key to allow modifications to the data in storage. CRUD, as mentioned earlier, is made up of four functions: Create, Read, Update, and Delete.

  • Create functionality: This operation inserts new records into the database, adding new data to pre-existing ones stored in the database. For instance, we can add new users to an application to a pre-existing database of the application’s users.
  • Read functionality: This function handles the fetching of data from the database for use by the user. A user can fetch the entire or a particular data set using this operation. It will simply retrieve the resources and display the results. These results can be viewed or have modifications performed on them.
  • Update Functionality: This function lets us modify existing records in the database. All that will be required is information about the position of the data you wish to change in the database.
  • Delete functionality: We use the Delete functionality to remove a record stored in the database. With this, a user can delete one or multiple records.

Benefits of CRUD Application

Developers use CRUD for applications requiring managing data in a database structure. With all the functions offered by CRUD apps, there are some merits to using them.
Here are a few:

  • It facilitates the design of the application making it more scalable and straightforward. With CRUD, you can manage and handle data in complex application structures, making it easier to build large applications.
  • Users may choose to secure data by requesting access credentials for authorization to perform CRUD operations on data stored in an application. Direct access from browsers is restricted, and only authorized users can modify the database.

When should we implement CRUD functionality?

Without CRUD, websites providing e-commerce, blogs, and social media (to name a few examples) would be impossible to build and maintain. It would be best if you used CRUD operations when there is data mutability involved in the business logic of your application.
Examples of this are:

  • Interactive user sites with mutable data like e-learning platforms and social media.
  • Sites requiring frequent updating of information, such as news or e-commerce websites.
  • Creation and management of dynamic content for a website.
  • When a site requires requests to create data to be made on the client’s side and validations to be carried out on the request before the server sends a corresponding response to allow that operation.

Types of databases, differences, pros, and cons

There are primarily two major types of database structures:

  • Server query Language (SQL) database
  • No-Server Query Language (NoSQL) database

SQL Database

An SQL database is a table-based relational data structure that defines and manipulates data using pre-defined schemas to determine its structure. All data to be worked on within the database must follow this pre-determined structure. Examples include Oracle, MySQL, Microsoft SQL Server, and PostgreSQL.

Pros

  • Are best suited for handling complex queries.
  • Follows ACID (atomicity, consistency, isolation, and durability) property which makes them capable of performing different tasks as a single logical operation. ACID refers to a collection of properties of database transactions intended to guarantee the validity of an operation in the event of errors.
  • It has support from vendors for large-scale deployment.
  • Have been around much longer than NoSQL databases and have more tools for database management.

Cons

  • SQL databases are less suitable for frequently changing data compared to NoSQL, and are more suited for handling complex queries. New data to be added to SQL databases will usually require some alterations to the schema, unlike NoSQL databases, where a user can easily add data without any prior step.
  • It isn’t easy to scale when there is a large amount of data to handle as the size of the application increases.
  • Due to its rigid pre-determined structure, it is not flexible as changes in its structure would be both difficult and disruptive.

NoSQL Databases

No SQL database is a non-relational data structure where data is stored in non-relational tables. NoSQL databases include pure document databases (such as JSON documents), key-value stores where data is stored alongside key values to reference them, wide-column databases, and graph databases.

Pros

  • Unlike SQL databases, NoSQL databases offer flexible schemas. It does not follow the rigid structure of SQL and can be easily modified to suit new changes in the data structure. You can create documents without having to pre-define their structures.
  • Can handle storage of structured and semi-structured data due to its non-tabular form.
  • Support large numbers of user access, making it suitable for large-scale applications.
  • NoSQL databases are cheaper to scale compared to SQL databases. SQL databases are vertically scalable and require increasing the hardware capacity on a single server. In contrast, NoSQL databases are horizontally scalable and can be scaled by adding more servers to manage the load.
  • No SQL databases offer the creation of multiple databases with different structures and syntax.

Cons

  • They have been around for a lesser period than SQL databases and are less popular. SQL databases have been around for about 40 years, are more popular, and easier to get support and add-ons to integrate with other services.

How can you use CRUD operations?

With all these fantastic features possible with CRUD, how can one use it within an application? The first thing a developer will require for the application is a database to store data. You may choose an SQL database if you will be handling structured data. On the other hand, you may select NoSQL if the data does not require a pre-defined schema and requires flexibility or scaling as development progresses. After choosing your preferred database type, the next step is to connect your database to your application and set up your user schema to access the database. Finally, build the app’s backend with the four essential functions of creating, reading, updating, and deleting your data.

With this out of the way, what stack can one use in building a flexible and scalable CRUD application? In this tutorial, we will be using the Next.js framework as a use case for CRUD applications.

CRUD with Next.js for front-end

With diverse Javascript frameworks out there, what makes Next.js stand out? With Next.js, you can effortlessly build your front-end, connect your web app to your database, and set up CRUD applications easily using Next.js provisions for API routes. With this, you can create and manage your endpoints for CRUD operations on your database data.

There are two approaches to setting up API endpoints for your CRUD operations:

  • Hardcode API routes for your CRUD app in the API directory provided by Next.js. Here you can create API endpoints with access schema to enable access to the database.
  • Use a helper dependency such as Next.js dependency called Next Crud. Next-Crud is a helper library that can be integrated to make the creation of API routes easier by using a model built with Prisma.

Creating a Basic NextJs Todo App

In this section, we will build a basic to-do app with NextJs to demonstrate CRUD operations. We will be using the FaunaDB database along with NextJs for this.

First, we will be installing these dependencies in CLI with the following commands:

1npx create-next-app todoapp

The above command creates a Next application called Todo in your working directory. Next, we will install the FaunaDB dependency. To do this, we first open the project directory with:

Then install the dependency:

1npm install --save faunadb

With this installed, we can proceed with building our app. First, we will design the Layout of our application with hard-coded data, and then we will set up our Fauna database and link it to our application and perform CRUD operations with it.

In the working directory, create a new folder called components. This folder will contain the various building blocks for our application. In this folder, create two files called Todo.js and TodoItem.js. The first will contain our app layout, while the latter will contain a list of tasks for the application.

In Todo.js, we have the following:

1import { React, useEffect, useState } from "react";

2import styles from "../../styles/Home.module.css";

3import TodoItem from "./TodoItem";

4

5function Todo() {

6 const [newtodo, setnewtodo] = useState("");

7 const handleinput = (e) => {

8 setnewtodo(e.target.value);

9 };

10 const HandleSubmit = (e) => {

11 console.log(newtodo);

12 };

13

14 return (

15 <div className={styles.maincont}>

16 <h1>Todo App</h1>

17 <div className={styles.newtodo}>

18 <h3>Add new todo</h3>

19 <div className={styles.semi}>

20 <input

21 type="text"

22 value={newtodo}

23 onChange={(e) => handleinput(e)}

24 ></input>

25 <button onClick={() => HandleSubmit()}>

26 Add Todo

27 </button>

28 </div>

29 </div>

30 <div>

31 <TodoItem />

32 </div>

33 </div>

34 );

35}

36export default Todo;

The component above contains an input field and button, which we will use to add new Todo Items to our app. It also has the TodoItem component for our tasks. In the TodoItem component, we can define the structure of the tasks:

1import { React, useState } from "react";

2 import styles from "../../styles/Home.module.css";

3

4 function TodoItem() {

5 const [isChecked, setIsChecked] = useState(false);

6

7 const handleDelete = () => {

8 };

9

10 const handlecheck = async () => {

11 };

12

13 return (

14 <div>

15 <span className={styles.eachtodo}>

16 <p className={styles.text}>Eat Breakfast</p>

17 <div>

18 <input

19 type="checkbox"

20 className={styles.toggle}

21 defaultChecked= {false}

22 onChange={handlecheck}

23 onClick={() => setIsChecked(!isChecked)}

24 />

25 <button onClick={handleDelete}>Delete</button>

26 </div>

27 </span>

28 </div>

29 );

30 }

31 export default TodoItem;

Above, we have a task, “Eat Breakfast,” along with a checkbox and a button to delete the task. We will add all these functionalities as we progress.

For the app to be rendered, we will add the Todo component to our index.js file:

1import Head from "next/head";

2import styles from "../styles/Home.module.css";

3import Todo from "./components/Todo";

4

5export default function Home() {

6 return (

7 <div className={styles.container}>

8 <Head>

9 <title>Create Next App</title>

10 <meta

11 name="description"

12 content="Generated by create next app"

13 />

14 <link rel="icon" href="/favicon.ico" />

15 </Head>

16 <Todo />

17 </div>

18 );

19}

To style our app, we will add some CSS to the Home.module.css file:

1.container {

2 padding: 0;

3}

4

5.maincont {

6 background: rgb(8, 8, 49);

7 width: 100%;

8 min-height: 100vh;

9 display: flex;

10 justify-content: center;

11 align-items: center;

12 flex-direction: column;

13 font-family: Montserrat;

14 color: #fff;

15}

16

17.maincont h1 {

18 text-transform: uppercase;

19}

20

21.newtodo h3 {

22 text-align: center;

23}

24

25.newtodo .semi {

26 display: flex;

27 gap: 8px;

28}

29

30.semi input {

31 padding: 5px 7px;

32 border: 2px solid rgb(52, 52, 114);

33 outline: none;

34 border-radius: 4px;

35}

36

37.semi button {

38 border: none;

39 padding: 5px 13px;

40 background: #fff;

41 color: rgb(8, 8, 49);

42 border-radius: 5px;

43 font-weight: bold;

44}

45

46.eachtodo {

47 display: flex;

48 flex-direction: row;

49 align-items: center;

50 justify-content: space-between;

51}

52

53.eachtodo div button {

54 color: red;

55 background: none;

56 border: none;

57 opacity: 70%;

58 font-family: monospace;

59 font-weight: 600;

60}

61

62.maincont button:hover {

63 cursor: pointer;

64}

65

66.text {

67 max-width: 350px;

68}

This concludes the basic structure of our application. We can run our app using the npm run dev command in CLI and navigate in our browser to localhost:3000 to view the results.

Image 1

Open Source Session Replay

Debugging a web application in production may be challenging and time-consuming. OpenReplay is an Open-source alternative to FullStory, LogRocket and Hotjar. It allows you to monitor and replay everything your users do and shows how your app behaves for every issue. It’s like having your browser’s inspector open while looking over your user’s shoulder. OpenReplay is the only open-source alternative currently available.

replayer.png

Happy debugging, for modern frontend teams – Start monitoring your web app for free.

Creating a Fauna database

Navigate in your browser to the Fauna webpage and click on sign up and create a user account, or log in if you already have an existing account. After signing in, click on Create a Database on the dashboard.

Image 2

Enter the database name in the sidebar that pops out and click on Create

Image 3

On the page that opens up, click on Create a new collection and create a ‘todos’ collection.

Image 4

Click on Save to create this collection. With this, we have our database set up complete. We can add data to this by clicking on the new Document button and defining the todo item as follows:

Image 5

The task contains the to-do item, while done will contain the value of our checkbox to determine if a task has been completed. Click on Save to add this to the todos collection.

Fetching data from FaunaDB

To be able to access the data stored on FaunaDB, we will first need to create access credentials. To do this, click on Security on the left navigation pane, then click on the new Key button, and finally click on Save to generate the key. Save the generated key as we will be using it in our application.

Back in our project folder, within the api folder, create a folder called getData and a file index.js in it. This file will contain our Read method to fetch data from FaunaDB. By default, Next.js treats all files within the api directory as API end-points.

1const { Ref } = require("faunadb");

2const faunadb = require("faunadb");

3const secret = "your app secret";

4const q = faunadb.query;

5const client = new faunadb.Client({

6 secret,

7 domain: "db.fauna.com",

8});

9module.exports = async (req, res) => {

10 try {

11 const dbs = await client.query(

12 q.Map(

13 q.Paginate(q.Documents(q.Collection("todos"))),

14 q.Lambda("doc", q.Get(q.Var("doc")))

15 )

16 );

17

18 res.status(200).json(dbs.data);

19 } catch (error) {

20 res.status(500).json({ error: error.message });

21 }

22};

The code above initializes the FaunaDB client and uses the key to access the todos collection. This data is then stored in the dbs variable. If we run our application and run the above via http://localhost:3000/api/getData, we get a result similar to the image below:

Image 6

To show this in the application, add the following code to Todo.js

1const [data, setData] = useState([]);

2

3async function fetchData() {

4 const res = await fetch("../api/getData");

5 const newData = await res.json();

6 setData(newData);

7}

8useEffect(() => {

9 fetchData();

10}, [newtodo]);

Next, we will pass this to our TodoItem component and render it.

1{

2 data &&

3 data.map((todo) => (

4 <TodoItem key={todo.ref["@ref"].id} todo={todo} />

5 ));

6}

Then in TodoItem component

1function TodoItem({ todo }) {

2

3 return (

4 <div>

5 <span className={styles.eachtodo}>

6 <p className={styles.text}>{todo.data.task}</p>

7 <div>

8 <input

9 type="checkbox"

10 className={styles.toggle}

11 defaultChecked={todo.data.done}

12 onChange={handlecheck}

13 onClick={() => setIsChecked(!isChecked)}

14 />

15 <button onClick={handleDelete}>Delete</button>

16 </div>

17 </span>

18 </div>

19 );

20}

With this, we get the results from our FaunaDB as our task. Also, the value of the done property is assigned to the checkbox as the default value.

Next, we will create API endpoints for the Create, Update and Delete functionalities. For this, we will create three new folders in the api directory, namely newData, deleteData, and updateData, and create an index.js file in each of these new folders. For our newData endpoint, we have the following code:

1const { Ref } = require("faunadb");

2const faunadb = require("faunadb");

3const secret = "Your app secret";

4const q = faunadb.query;

5const client = new faunadb.Client({ secret });

6module.exports = async (req, res) => {

7 const inputData = req.body.data;

8 try {

9 const dbs = await client.query(

10 q.Create(q.Collection("todos"), {

11 data: {

12 task: inputData.newtodo,

13 done: false,

14 },

15 })

16 );

17 res.status(200).json(dbs.data);

18 } catch (error) {

19 res.status(500).json({ error: error.message });

20 }

21};

This will take the input field’s value from our app and pass a default value of false for the task completion. This value will then be sent to our Fauna database.

Then in Todo.js, we can bind this to the input as follows:

1import newData from "../api/newData";

2

3

4

5const [inputData, setInputData] = useState({});

6const requestParams = {

7 method: "POST",

8 headers: { "Content-Type": "application/json" },

9 body: JSON.stringify({ data: inputData }),

10};

11

12

13

14const handleinput = (e) => {

15 setnewtodo(e.target.value);

16 setInputData({

17 ...inputData,

18 newtodo: e.target.value,

19 });

20};

21

22const HandleSubmit = (e) => {

23 console.log(newtodo);

24 addTodoItem();

25 setnewtodo("");

26};

27

28async function addTodoItem() {

29 await fetch("../api/newData", requestParams)

30 .then(() => newData())

31 .catch((e) => console.log(e));

32}

The handleinput function is used to assign the input field’s value to the new todo property of inputData. The new new todo is added to the previous ones using the spread operator. When the submit button is clicked, it is passed to Fauna via the newData API endpoint.

We can now set up the updateData functionality:

1const { Ref } = require("faunadb");

2const faunadb = require("faunadb");

3const secret = "Your Secret Key";

4const q = faunadb.query;

5const client = new faunadb.Client({ secret });

6

7module.exports = async (req, res) => {

8 const id = req.body.id;

9 const inputData = req.body.data;

10 try {

11 const dbs = await client.query(

12 q.Update(q.Ref(q.Collection("todos"), id), {

13 data: { done: inputData.done },

14 })

15 );

16 res.status(200).json(dbs.data);

17 } catch (error) {

18 res.status(500).json({ error: error.message });

19 }

20};

This references the selected todo by id and updates the chosen task as either completed or not based on the value of the checkbox.

For the deleteData functionality, we have

1const { Ref } = require("faunadb");

2const faunadb = require("faunadb");

3const secret = "Your App Secret";

4const q = faunadb.query;

5const client = new faunadb.Client({ secret });

6

7module.exports = async (req, res) => {

8 const id = req.body.id;

9 try {

10 const dbs = await client.query(

11 q.Delete(q.Ref(q.Collection("todos"), id))

12 );

13 res.status(200).json(dbs.data);

14 } catch (error) {

15 res.status(500).json({ error: error.message });

16 }

17};

Above, the selected task is referenced by its id, and a method is passed to delete it from the Fauna Database.

We can bind the update functionality to the checkbox and the delete functionality to the delete button in the TodoItem component.

1import deleteData from "../api/deleteData";

2

3

4

5const [isChecked, setIsChecked] = useState(false);

6const [done, isDone] = useState(true);

7const [aDelete, isDeleted] = useState("");

8const [inputData, setInputData] = useState({});

9let d = "";

10

11const handlecheck = async () => {

12 isDone(!todo.data.done);

13 let c = !todo.data.done;

14 isDeleted(todo.ref["@ref"].id);

15 d = todo.ref["@ref"].id;

16 let g = {

17 ...inputData,

18 done: c,

19 };

20 await fetch("../api/updateData", {

21 method: "PUT",

22 headers: { "Content-Type": "application/json" },

23

24 body: JSON.stringify({ data: g, id: d }),

25 })

26 .then(() => deleteData())

27 .catch((e) => console.log(e));

28};

29

30const handleDelete = () => {

31 d = todo.ref["@ref"].id;

32 isDeleted(todo.ref["@ref"].id);

33 deleteItem();

34};

35

36async function deleteItem() {

37 await fetch("../api/deleteData", {

38 method: "DELETE",

39 headers: { "Content-Type": "application/json" },

40

41 body: JSON.stringify({ id: d }),

42 })

43 .then(() => deleteData())

44 .catch((e) => console.log(e));

45}

When the value of the checkbox is changed, the opposite value of the previous data is sent to the database to update the task relating to the particular id. This is also applicable to the delete button; when it is clicked, the id is passed to the API endpoint, and the corresponding record is deleted from the database collection.

Conclusion

CRUD functionalities are essential in modern-day applications, and the ability to properly utilize their usage provides better app performance and user experience for your web application. Here we learned how to set up CRUD operations in an application using an example To-do app to demonstrate CRUD functionalities.

Resources

If you are wondering or need a live demonstration of how CRUD operates, here is an example demo using a Todo application built with Next.js on CodeSandbox



Source link

Leave a reply

Please enter your comment!
Please enter your name here