qeasy
Version:
Query Easy: Simple SQL helper like Mongoose for MySQL/MariaDB
165 lines (134 loc) β’ 3.56 kB
Markdown
# π QEasy (Query Easy)
[](https://www.npmjs.com/package/qeasy)
[](https://www.npmjs.com/package/qeasy)
[](./LICENSE)
## π QEasy (Query Easy)
A lightweight SQL helper for Node.js
Easily perform CRUD operations, joins, where clauses, ordering, limits, and offsets without writing raw SQL.
## π¦ Installation
npm install qeasy
## β‘ Quick Start
```js
const qeasy = require("qeasy");
// 1οΈβ£ Connect to Database
qeasy.connectDB({
host: "localhost",
user: "root",
password: "password",
database: "mydb",
port: 3306,
});
// 2οΈβ£ Check Connection
qeasy.checkConnection()
.then(() => console.log("β
Database connected"))
.catch(err => console.error("β Connection error:", err));
```
## π Basic CRUD Examples
## Insert Data (Table name , dataObj)
```js
await qeasy.insert("users", {
name: "John Doe",
email: "john@example.com",
age: 25,
});
```
## Find All (Table name)
```js
const users = await qeasy.findAll("users");
console.log(users);
```
## Find By ID (Table name , Field name , id )
```js
const user = await qeasy.findById("users", "id", 1);
console.log(user);
```
## Update By ID (Table name , Field name , id , dataObj)
```js
await qeasy.findByIdAndUpdate("users", "id", 1, { age: 26 });
```
## Delete By ID (Table name , Field name , id)
```js
await qeasy.findByIdAndDelete("users", "id", 1);
```
## π Joins Made Easy
```js
const results = await qeasy.findWithJoins({
table: "orders",
as: "o",
selectedColumns: ["o.id", "o.total", "u.name"],
joins: [
{
table: "users",
as: "u",
type: "INNER",
on: { "u.id": "o.user_id" },
},
],
where: [
{ column: "u.age", operator: ">", value: 18 },
],
orderBy: [
{ column: "o.total", direction: "DESC" },
],
limit: 10,
offset: 0,
});
console.log(results);
```
## π― Options Explained
```js
## Joins
interface JoinOption {
table: string; // table name
as?: string; // alias
type?: "INNER" | "LEFT" | "RIGHT";
on: { [key: string]: string }; // join condition
}
```
## Where Condition
```js
interface WhereCondition {
column: string; // e.g., "u.id"
operator?: string; // '=', '>', '<', 'LIKE'
value: any;
}
```
## Order By
```js
interface OrderByOption {
column: string; // e.g., "u.name"
direction?: "ASC" | "DESC";
}
```
## π§βπ» Example Project
```js
const qeasy = require("qeasy");
async function run() {
qeasy.connectDB({
host: "localhost",
user: "root",
password: "password",
database: "shopdb",
});
// Insert a product
await qeasy.insert("products", { name: "Laptop", price: 50000 });
// Get all products
const products = await qeasy.findAll("products");
console.log(products);
// Join example: Orders + Users
const orders = await qeasy.findWithJoins({
table: "orders",
as: "o",
selectedColumns: ["o.id", "u.name", "o.total"],
joins: [{ table: "users", as: "u", on: { "u.id": "o.user_id" } }],
});
console.log(orders);
}
run();
```
## π Why QEasy?
β
Simple, mongoose-like syntax
β
No need to write raw SQL queries
β
Supports CRUD + complex joins
β
Beginner-friendly
β¨ Thatβs it! With just npm i qeasy, you can start querying your SQL DB easily.