Simplify Your Database Management with nanoSQL
The Problem with Traditional Databases
Traditional databases like MySQL are slow and can’t keep up with the “instant” experience modern users expect. To connect to your database, you need a database client library, which means learning two different sets of database client libraries and writing the correct query to achieve what you want to do.
Introducing nanoSQL
nanoSQL is a universal JavaScript client library that allows you to connect to multiple databases, both in-memory and permanent, using a standardized query language. This means you can use the same API on different database systems, reducing development time and increasing efficiency.
Using nanoSQL in Action
Let’s see how nanoSQL can help us operate local storage and MySQL database using the same query language and API. We’ll create a small form component using React that saves temporary user inputs in local storage. When the user clicks submit, the app will save the data in a table in MySQL through a REST API built with Express.
nanoSQL on the Backend
// Initialize Express server
const express = require('express');
const app = express();
app.use(express.json());
// Import required modules
const { createDatabase } = require('@nano-sql/core');
const mysqlAdapter = require('@nano-sql/adapter-mysql');
// Create an instance of Express server
const server = app.listen(3000, () => {
console.log('Server started on port 3000');
});
Connecting to MySQL with nanoSQL
// Connect to MySQL using nanoSQL
const mysqlConfig = {
id: 'ydb',
mode: 'MySQL',
tables: [
{
name: 'users',
model: [
{ name: 'id', type: 'int' },
{ name: 'name', type: 'tring' },
],
},
],
MySQL: {
host: 'localhost',
username: 'root',
password: 'password',
db: 'ydb',
},
};
const db = createDatabase(mysqlConfig);
Creating a React Form on the Frontend
import React, { useState, useEffect } from 'eact';
function App() {
const [userData, setUserData] = useState({});
useEffect(() => {
fetchData();
}, []);
const fetchData = async () => {
const response = await fetch('/get');
const data = await response.json();
setUserData(data);
};
return (
);
}
Saving Temporary User Input in Local Storage
const localStorageConfig = {
id: 'localstorage',
mode: 'LOCAL STORAGE',
tables: [
{
name: 'temp_data',
model: [
{ name: 'id', type: 'int' },
{ name: 'data', type: 'tring' },
{ name: 'form', type: 'tring' },
],
},
],
};
const localStorageDB = createDatabase(localStorageConfig);
const saveTemporaryData = async (formValue) => {
const existingData = await localStorageDB.query('SELECT * FROM temp_data WHERE form =?', [formValue]);
if (existingData.length > 0) {
await localStorageDB.query('UPDATE temp_data SET data =? WHERE form =?', [JSON.stringify(userData), formValue]);
} else {
await localStorageDB.query('INSERT INTO temp_data (data, form) VALUES (?,?)', [JSON.stringify(userData), formValue]);
}
};
The Benefits of nanoSQL
nanoSQL helps you reduce development time by enabling a standardized query language so that you can use the same API on different database systems. Even though it has “SQL” in its name, nanoSQL also supports noSQL databases, such as MongoDB and ScyllaDB. You don’t need to learn a new query language when you add any of these databases to your architecture. Jumping between different databases is a trivial matter.
Try nanoSQL Today
If you’re developing software with multidatabase architecture, be sure to check out nanoSQL documentation to see if it’s the right choice for you. With nanoSQL, you can simplify your database management and create a seamless experience for your users.