V1.0.1
Version 1.0.1: Initial Release 🚀
This release introduces utility functions for building dynamic MySQL queries with strong type definitions. The package focuses on flexibility, reusability, and developer-friendly documentation.
Key Features
DELETE Query Builder (
deleteQuery
)
Generate flexible SQLDELETE
queries with support for:WHERE
conditionsJOIN
clauses- Sorting (
ORDER BY
) - Pagination (
LIMIT
)
INSERT Query Builder (
insertQuery
)
Dynamically constructINSERT
queries with features like:- Single or bulk inserts
- Handling timestamp fields (e.g.,
created_at
,updated_at
) - Conflict resolution (
INSERT IGNORE
,ON DUPLICATE KEY UPDATE
)
SELECT Query Builder (
selectQuery
)
Create advancedSELECT
queries with support for:- Distinct selection
- Aggregates (
COUNT
,SUM
,AVG
, etc.) - Grouping (
GROUP BY
) and filtering (HAVING
) - Joins, recursive CTEs, subqueries, and more
UPDATE Query Builder (
updateQuery
)
FlexibleUPDATE
query builder supporting:- Conditional updates (
CASE WHEN
) - Joins
- Null/default value management
- Inline calculations (
SET
)
- Conditional updates (
Utility Function (
mysql_datetime
)
Format dates into MySQL-compatibleDATETIME
strings.
Type Definitions
JoinsType
andSortType
These define the structure forJOIN
andORDER BY
clauses.DeleteQueryParams<Tables>
Parameters for buildingDELETE
queries:table: string
where: string
joins?: JoinsType<Tables>
sort?: SortType<Tables>
limit?: string | number
insertQueryParams
Parameters for buildingINSERT
queries:table: string
insertData: Record<string, any> | Record<string, any>[]
dateFields?: string[]
uniqueColumn?: string | null
onDuplicateUpdateFields?: string[]
SelectQueryParams<Tables>
Parameters for buildingSELECT
queries:distinct?: boolean
table?: string
columns?: string | string[] | Record<string, string[]>
where?: string
joins?: JoinsType<Tables>
aggregates?: Array<Record<string, string>>
groupBy?: string | string[]
having?: string
limitSkip?: { limit?: number; skip?: number }
updateQueryParams<Tables>
Parameters for buildingUPDATE
queries:table: string
updateData?: Record<string, any>
sort?: SortType<Tables>
where: string
limit?: string | number
joins?: JoinsType<Tables>
setCalculations?: Record<string, string>
Example Usage
DELETE Query:
const query = deleteQuery({
table: 'users',
where: 'age > 30',
sort: { users: { id: -1 } },
limit: 10,
});
console.log(query);
// Output: DELETE users FROM users WHERE age > 30 ORDER BY id DESC LIMIT 10;
INSERT Query:
const query = insertQuery({
table: 'products',
insertData: { name: 'Laptop', price: 1500 },
dateFields: ['created_at'],
});
console.log(query);
// Output: INSERT INTO products (name, price, created_at) VALUES ('Laptop', 1500, CURRENT_TIMESTAMP);
SELECT Query:
const query = selectQuery({
table: 'orders',
columns: { orders: ['order_id', 'status'] },
where: 'status = "shipped"',
joins: [
{ type: 'INNER JOIN', table: 'customers', on: 'orders.customer_id = customers.id' }
],
sort: { orders: { created_at: -1 } },
limitSkip: { limit: 5 },
});
console.log(query);
// Output: SELECT orders.order_id, orders.status FROM orders
// INNER JOIN customers ON orders.customer_id = customers.id
// WHERE status = "shipped" ORDER BY created_at DESC LIMIT 5;
UPDATE Query:
const query = updateQuery({
table: 'users',
updateData: {
name: 'Updated Name',
last_login: {
case: [{ when: 'status = "active"', then: 'CURRENT_TIMESTAMP' }],
default: 'NULL',
},
},
where: 'id = 1',
});
console.log(query);
// Output: UPDATE users SET name = 'Updated Name',
// last_login = CASE WHEN status = "active" THEN CURRENT_TIMESTAMP ELSE NULL END WHERE id = 1;
Utility (mysql_datetime
):
const formattedDate = mysql_datetime(new Date());
console.log(formattedDate);
// Output: 2024-11-24 14:32:00
Get Started
Install agile-query
via npm:
npm install agile-query
This is just the beginning! Stay tuned for future updates as we expand the feature set of agile-query
. 🚀