DEV Community

Build an Application Without SQL Server Database (Avoiding RPrometheusedis, MongoDB, and )

In the world of software development, databases are one of the fundamental pillars for storing and managing information. However, there are cases where applications do not require a conventional database such as SQL Server or NoSQL solutions like Redis, MongoDB, or Prometheus. This can happen when the application is small, does not need persistent data, or when a simplified and lightweight solution is sought.

In this article, we will explore how to build a functional application without using databases. We will use flat files (JSON) as temporary storage, which is useful for small projects or applications with no large scalability requirements.

1. Reasons to Avoid Traditional Databases

Although SQL Server, Redis, and MongoDB are popular choices, they can be unnecessary in the following scenarios:

  • Small applications or prototypes.
  • Temporary storage requirements.
  • Avoiding costs associated with cloud services.
  • Need for a simple and lightweight implementation.
  • Lack of infrastructure or access to servers. A common alternative is to use flat files, like JSON, to store data locally on the server.

2. Technologies to Use
For this example, we will build a simple application using:

  • Language: Node.js (JavaScript)
  • Storage: Local JSON files
  • Framework: Express.js to create a simple HTTP server
  • Development Tools: Visual Studio Code and npm We will not use any database management system or NoSQL solutions.

3. Project Structure
The file structure will be straightforward:
my-app/
│-- server.js
│-- data/
│ └── data.json
└── package.json

  • server.js: Main file where the Express server is implemented.
  • data.json: Local storage file.
  • package.json: Dependency configuration file.

4. Code Implementation
4.1 Install Dependencies
Initialize a Node.js project and install Express.js:
mkdir my-app
cd my-app
npm init -y
npm install express

4.2 Create the JSON Data File

In the data folder, create the data.json file:
[
{
"id": 1,
"name": "Juan",
"age": 25
},
{
"id": 2,
"name": "Pedro",
"age": 30
}
]

4.3 Implement the Express Server

In server.js, implement a server that reads and writes data to the JSON file:
`const express = require('express');
const fs = require('fs');
const app = express();
const PORT = 3000;

app.use(express.json());

const filePath = './data/data.json';

// Get all data
app.get('/users', (req, res) => {
fs.readFile(filePath, 'utf8', (err, data) => {
if (err) return res.status(500).send('Error reading data');
res.json(JSON.parse(data));
});
});

// Add a new user
app.post('/users', (req, res) => {
const newUser = req.body;

fs.readFile(filePath, 'utf8', (err, data) => {
    if (err) return res.status(500).send('Error reading data');

    const users = JSON.parse(data);
    users.push({ id: users.length + 1, ...newUser });

    fs.writeFile(filePath, JSON.stringify(users, null, 2), (err) => {
        if (err) return res.status(500).send('Error saving data');
        res.status(201).send('User added successfully');
    });
});
Enter fullscreen mode Exit fullscreen mode

});

app.listen(PORT, () => {
console.log(Server running at http://localhost:${PORT});
});`

5. How It Works

  1. Data Reading: The GET /users endpoint reads the JSON file and returns its content as a response.
  2. Data Writing: The POST /users endpoint adds a new user to the JSON file and saves the changes.

6. Testing the Application

  1. Start the server: node server.js
  2. Use Postman or your browser to test the endpoints:
  3. GET http://localhost:3000/users returns the list of users.
  4. POST http://localhost:3000/users with a JSON body like: { "name": "Luis", "age": 28 } adds a new user. 7. Source Code on GitHub You can find the complete code in the following GitHub repository: GitHub Repository: https://github.com/Angelica-R/App-without-SQL-Server-Database.git

8. Conclusion
Building an application without databases like SQL Server, Redis, or MongoDB is a valid option for small and simple applications. Using JSON files provides a lightweight and easy-to-implement solution, allowing data management in a temporary or local form. This technique is ideal for prototypes or environments where a complex database is not needed.

Furthermore, this approach allows for future scalability, as it is always possible to migrate to a traditional database when the project requires it.

Top comments (0)