1.1.1 • Published 1 year ago
faster.database v1.1.1
faster.database 🚀
Basically mongoose without an MongoDB Database
faster.database is a lightweight Node.js library for managing local databases. It allows you to define schemas, validate documents, and perform CRUD operations on JSON files.
Usage ❓
Importing the library
import { Schema, model, Document } from 'faster.database';
(Alternative you can use)
const { Schema, model } = require('faster.database');
Defining a Schema
A schema defines the structure of documents in a collection. You can specify field types, required fields, default values, and custom validation functions.
const userSchema = new Schema({
name: { type: 'string', required: true },
email: { type: 'string', required: true, validate: (value) => /\S+@\S+\.\S+/.test(value) },
age: { type: 'number', default: 18 },
role: { type: 'string', enum: ['user', 'admin'], default: 'user' }
}, { timestamps: true });
Creating a Model
A model is a class that provides methods for interacting with a collection of documents.
const UserModel = model('User', userSchema);
// module.exports = model('User', userSchema);
// export const userModel = model('User', userSchema);
Adding Custom Methods
You can add custom instance methods and static methods to your models.
userSchema.addMethod('greet', function() {
return `Hello, my name is ${this.name}`;
});
userSchema.addStatic('findByEmail', async function(email) {
return this.findOne({ email });
});
const user = await UserModel.insert({ name: 'Jane Doe', email: 'jane@example.com' });
console.log(user.greet());
const jane = await UserModel.findByEmail('jane@example.com');
console.log(jane);
Schema Hooks
You can add middleware functions that will be executed before and after certain actions, like inserting a document.
userSchema.pre('insert', async (document) => {
document.name = document.name.toUpperCase();
});
userSchema.post('insert', async (document) => {
console.log(`New user inserted: ${document.name}`);
});
const newUser = await UserModel.insert({ name: 'Jake', email: 'jake@example.com' });
console.log(newUser);
Full Example 📚
Here's a full example demonstrating the use of faster.database:
const { Schema, model } = require( 'simple-json-db');
// Define schema
const userSchema = new Schema({
name: { type: 'string', required: true },
email: { type: 'string', required: true, validate: (value) => /\S+@\S+\.\S+/.test(value) },
age: { type: 'number', default: 18 },
role: { type: 'string', enum: ['user', 'admin'], default: 'user' }
}, { timestamps: true });
// Add custom method
userSchema.addMethod('greet', function() {
return `Hello, my name is ${this.name}`;
});
// Add static method
userSchema.addStatic('findByEmail', async function(email) {
return this.findOne({ email });
});
// Pre and post hooks
userSchema.pre('insert', async (document) => {
document.name = document.name.toUpperCase();
});
userSchema.post('insert', async (document) => {
console.log(`New user inserted: ${document.name}`);
});
// Create model
const UserModel = model('User', userSchema);
// Insert a document
const newUser = await UserModel.insert({ name: 'John Doe', email: 'john@example.com' });
console.log(newUser.greet());
// Find documents
const users = await UserModel.find({ role: 'user' });
console.log(users);
// Update documents
const updatedUsers = await UserModel.update({ role: 'user' }, { age: 21 });
console.log(updatedUsers);
// Delete a document
const result = await UserModel.delete({ email: 'john@example.com' });
console.log(result);