Unlock the Power of Universal Database Layering with nanoSQL
What is nanoSQL?
nanoSQL is a standardized query language that allows you to interact with multiple databases, including MySQL, MongoDB, indexedDB, and Redis, to name a few. Its identical API makes it easy to migrate to other databases without having to rewrite existing queries. Whether you’re working with relational database management systems (RDBMSs) or NoSQL databases, nanoSQL has got you covered.
Building a Simple To-Do Backend App
To demonstrate the power of nanoSQL, we’ll build a simple to-do backend app using MongoDB as our database layer. Don’t worry if you’re new to MongoDB; we’ll guide you through the process.
Setting Up the Project
First, let’s set up our project structure and install the necessary dependencies. We’ll create a new project folder and initialize it using Node.js. Next, we’ll install the required dependencies, including nanoSQL.
mkdir todo-app
cd todo-app
npm init -y
npm install nanosql
Creating the Backend API
Our backend API will consist of five routes:
- /: returns a list of to-dos
- /:id: returns a single to-do
- /del/:id: deletes a single to-do
- /update/:id: updates a single to-do
- /delete: deletes all to-dos
Building Database Handlers
Our database handlers will be responsible for carrying out CRUD (Create, Read, Update, Delete) operations. We’ll create a database.js
file that will contain our database object, which will include methods for each CRUD operation.
// database.js
const nanoSQL = require('nanosql');
const db = nanoSQL('mongodb://localhost:27017/todo-app');
db.createCollection('todos', ['title', 'description']);
const retrieve = async (id) => {
const result = await db.query('SELECT * FROM todos WHERE id =?', [id]);
return result.rows[0];
};
const retrieveAll = async () => {
const result = await db.query('SELECT * FROM todos');
return result.rows;
};
const insert = async (title, description) => {
await db.query('INSERT INTO todos (title, description) VALUES (?,?)', [title, description]);
};
const update = async (id, title, description) => {
await db.query('UPDATE todos SET title =?, description =? WHERE id =?', [title, description, id]);
};
const del = async (id) => {
await db.query('DELETE FROM todos WHERE id =?', [id]);
};
module.exports = { retrieve, retrieveAll, insert, update, del };
Querying with nanoSQL
nanoSQL uses a standardized query language that makes it easy to perform data operations. We’ve defined the following methods:
- retrieve: returns a single to-do item
- retrieveAll: returns a list of all to-dos
- insert: adds a new to-do item
- update: updates a single to-do item
- delete: deletes a single to-do item
Connecting the Database to API Routes
Now that we have our database handlers in place, let’s connect them to our API routes. We’ll update our app.js
file to include the necessary logic for each route.
// app.js
const express = require('express');
const app = express();
const { retrieve, retrieveAll, insert, update, del } = require('./database');
app.get('/', async (req, res) => {
const todos = await retrieveAll();
res.json(todos);
});
app.get('/:id', async (req, res) => {
const id = req.params.id;
const todo = await retrieve(id);
res.json(todo);
});
app.post('/', async (req, res) => {
const { title, description } = req.body;
await insert(title, description);
res.json({ message: 'To-do created successfully' });
});
app.put('/:id', async (req, res) => {
const id = req.params.id;
const { title, description } = req.body;
await update(id, title, description);
res.json({ message: 'To-do updated successfully' });
});
app.delete('/:id', async (req, res) => {
const id = req.params.id;
await del(id);
res.json({ message: 'To-do deleted successfully' });
});
app.listen(3000, () => {
console.log('Server listening on port 3000');
});
Testing the API with Postman
It’s time to put our API to the test! We’ll use Postman to verify that our API is working as expected. We’ll start by retrieving the list of to-dos, then add a few new to-dos, update an existing to-do, and finally, delete a to-do item.
The Power of nanoSQL
With nanoSQL, you can switch between different databases without having to learn new query languages. Its standardized query language makes it easy to migrate to other databases, reducing the time and effort required to develop and maintain your application.
Ready to unlock the power of nanoSQL? Check out the code used in this tutorial on GitHub and start building your own universal database layer app today!