khn-querybuilder
v1.0.0
Published
A Laravel-inspired fluent Query Builder and ORM for Node.js
Maintainers
Readme
🚀 QueryBuilder ORM (ORM Inspired)
A powerful, fluent Query Builder and ORM for Node.js that supports MySQL, PostgreSQL, and MongoDB. Build complex queries and handle relationships with a beautiful ORM syntax.
✨ Features
- Multi-Database Support: Switch between MySQL, Postgres, and MongoDB via
.env. - Fluent Query Builder: Chainable methods like
.where(),.orderBy(),.limit(). - ORM-style Relationships: Support for
hasOne,hasMany, andbelongsTo. - Nested Eager Loading: Load deep relationships using dot notation (e.g.,
.with('a.b.c')). - Full CRUD Support: Built-in
.save(),.update(), and.delete()methods on models. - CLI Tool: Initialize your project structure with one command.
🛠 Installation
npm install khn-querybuilder📂 Quick Setup
Initialize Project:
npx querybuilder initConfigure
.env:DB_CONNECTION=mysql DB_HOST=localhost DB_NAME=your_db DB_USER=root DB_PASSWORD=
📖 Usage Examples
Defining a Model
const { Model } = require("khn-querybuilder");
class User extends Model {
constructor() {
super();
this.table = "users";
}
// Relationships
appointments() {
const Appointment = require("./Appointment");
return this.hasMany(Appointment, "patient_id");
}
}Advanced Querying & Relationships
const users = await User.where("deactivated", "!=", "false")
.with("appointments.medication") // Nested eager loading!
.orderBy("id", "DESC")
.get();CRUD Operations
const user = new User();
user.name = "John Doe";
await user.save(); // Insert
user.name = "Jane Doe";
await user.save(); // Update
await user.delete(); // Delete🔗 Relationships Supported
| Method | Description | Usage |
| :---------- | :--------------- | :-------------------------------- |
| hasOne | One-to-One | this.hasOne(Profile, 'user_id') |
| hasMany | One-to-Many | this.hasMany(Post, 'user_id') |
| belongsTo | Reverse Relation | this.belongsTo(User, 'user_id') |
📄 Pagination
Our pagination returns a consistent array-based format containing the results and the metadata as the last element:
const result = await User.paginate(10, 1);
// [ {user1}, {user2}, ..., { total: 100, current_page: 1 } ]Developed with ❤️ by Fawad Ali
