A simple file-based database system
max-db, you can import and use it like this:
javascript
import { connectDatabase } from "cheet-sheet-json-db";
// Example usage
async function initDatabase() {
await connectDatabase();
console.log("Database connected successfully!");
}
initDatabase();
`
After restarting the application =>
⚠️ Database directory 'D:\FullStack_JS\ProjectModels\Db-Json\data' does not exist. Creating...
✅ Database initialized.
Server listening on Port 8000
// import Entity and extend your classes
`javascript
import Entity from "cheet-sheet-json-db/entity.js"; // Adjust this if the file is in a different location
class User extends Entity {
constructor({ name, fullname, isAdmin = false }) {
super();
this.name = name;
this.fullname = fullname;
this.isAdmin = isAdmin;
}
}
export default User;
`
// create new user
// First read all the data in the local DB
`javascript
const users = await User.readData();
`
// logic...
// Check for duplicate user by name
`javascript
if (users.some((user) => user.name === data.name)) {
throw new HttpError("User already exists", 409);
}
//
await User.writeData(users);
``