
Product
Introducing Webhook Events for Alert Changes
Add real-time Socket webhook events to your workflows to automatically receive software supply chain alert changes in real time.
nextpay-querystring
Advanced tools
Thư viện QueryString của NextPay - Chuyển đổi QueryString thành điều kiện select cho MongoDB và MySQL với kiểm soát bảo mật
Thư viện QueryString của NextPay - Chuyển đổi QueryString thành điều kiện select cho MongoDB và MySQL với kiểm soát bảo mật và tránh SQL injection.
==, !=, >, >=, <, <=, IN, NIN, BETWEEN, LIKEnpm install nextpay-querystring
// ✅ sử dụng cho mongodb
import { toMongoCriteria, FieldDescription, DataType } from 'nextpay-querystring';
// ✅ sử dụng cho Mysql
import { toMySqlCriteria, FieldDescription, DataType } from 'nextpay-querystring';
const fieldDescriptions: FieldDescription[] = [
{
field: '_id',
required: true,
dataType: DataType.STRING,
maxOccurrence: 5,
mapTo: '_id',
},
{
field: 'title',
required: true,
dataType: DataType.STRING,
maxOccurrence: 5,
mapTo: 'title',
},
{
field: 'status',
required: true,
dataType: DataType.STRING,
maxOccurrence: 5,
mapTo: 'status',
},
{
field: 'createdAt',
required: true,
dataType: DataType.DATE,
maxOccurrence: 5,
mapTo: 'createdAt',
},
{
field: 'isActive',
required: true,
dataType: DataType.BOOLEAN,
maxOccurrence: 5,
mapTo: 'isActive',
},
];
const queryString = "{title} == '''MACQ''' AND {status} IN ['''OPEN''', '''ON-HOLD'''] AND {isActive} == true";
const mongoCriteria = toMongoCriteria(queryString, fieldDescriptions);
const whereCondition = JSON.parse(mongoCriteria);
// Kết quả: {title: "MACQ", status: {$in: ["OPEN", "ON-HOLD"]}, isActive: true}
const result = await collection.find(whereCondition);
const queryString = "{title} == '''MACQ''' AND {status} IN ['''OPEN''', '''ON-HOLD'''] AND {isActive} == true";
const mysqlCriteria = toMySqlCriteria(queryString, fieldDescriptions);
// Kết quả: title = 'MACQ' AND status IN ('OPEN', 'ON-HOLD') AND isActive = true
const result = await connection.query(`SELECT * FROM table WHERE ${mysqlCriteria}`);
import { toMongoCriteria, toMySqlCriteria, FieldDescription, DataType } from 'nextpay-querystring';
const fieldDescriptions: FieldDescription[] = [
{
field: 'status',
required: true,
dataType: DataType.STRING,
maxOccurrence: 5,
mapTo: 'status',
},
{
field: 'id',
required: true,
dataType: DataType.INTEGER,
maxOccurrence: 5,
mapTo: 'id',
},
{
field: 'title',
required: true,
dataType: DataType.STRING,
maxOccurrence: 5,
mapTo: 'title',
},
{
field: 'category',
required: false,
dataType: DataType.STRING,
maxOccurrence: 5,
mapTo: 'category',
},
];
// Query với grouping
const queryString = "{status} == '''ACTIVE''' AND ({id} == 1 OR {title} LIKE '''admin''') AND ({priority} >= 3 OR {category} == '''VIP''')";
// MongoDB
const mongoResult = toMongoCriteria(queryString, fieldDescriptions);
const mongoCriteria = JSON.parse(mongoResult);
console.log('MongoDB:', mongoCriteria);
// Output: {
// $and: [
// { status: 'ACTIVE' },
// { $or: [{ id: 1 }, { title: { $regex: 'admin', $options: 'i' } }] },
// { $or: [{ priority: { $gte: 3 } }, { category: 'VIP' }] }
// ]
// }
// MySQL
const mysqlResult = toMySqlCriteria(queryString, fieldDescriptions);
console.log('MySQL:', mysqlResult);
// Output: (status = "ACTIVE") AND ((id = 1) OR (title LIKE %"admin"%)) AND ((priority >= 3) OR (category = "VIP"))
{<tên-field>} <operator> <value>
| Kiểu dữ liệu | Cú pháp | Ví dụ |
|---|---|---|
| String | '''value''' | {title} == '''MACQ''' |
| Number | value | {id} == 1 |
| Boolean | true/false | {isActive} == true |
| Date | #value# | {createdAt} == #2025-01-15# |
== - Equals!= - Not equals=== - Triple equals> - Greater than>= - Greater than or equal< - Less than<= - Less than or equalIN - In arrayNIN - Not in arrayBETWEEN - Between rangeNBETWEEN - Not between rangelike% - Starts withlike - Contains%like - Ends withnlike - Not containsnlike% - Not starts with%nlike - Not ends withis_null - Is nullis_not_null - Is not nullAND - Logical ANDOR - Logical ORNOT - Logical NOTconst complexQuery = `
{title} == '''MACQ''' AND
{id} == 1 AND
{status} IN ['''OPEN''', '''ON-HOLD'''] AND
{isPrivate} == true AND
{dueDate} == #2025-08-25# AND
{description} like '''important'''
`;
// String negative operators
const negativeStringQuery = `
{title} nlike '''admin''' AND
{description} nlike% '''draft''' AND
{filename} %nlike '''.tmp'''
`;
// Range negative operator
const negativeRangeQuery = `
{priority} nbetween [1, 5] AND
{createdAt} nbetween [#2024-01-01#, #2024-12-31#]
`;
// Complex negative query
const complexNegativeQuery = `
{title} nlike '''admin''' AND
{status} nbetween ['''DELETED''', '''ARCHIVED'''] AND
({priority} >= 3 OR {category} == '''VIP''')
`;
// NULL operators
const nullQuery = `
{title} is_not_null AND
{description} is_null AND
{priority} >= 3
`;
// Complex query with NULL operators
const complexNullQuery = `
({title} is_null OR {description} is_not_null) AND
{status} == '''ACTIVE''' AND
{priority} is_not_null
`;
// BETWEEN operator examples
const betweenQuery = `
{priority} between [1, 5] AND
{createdAt} between [#2024-01-01#, #2024-12-31#] AND
{status} between ['''ACTIVE''', '''PENDING''']
`;
// ✅ Simple OR group
const orGroup = "({id} == 1 OR {id} == 2)";
// ✅ AND với OR group
const andWithOr = "{status} == '''ACTIVE''' AND ({id} == 1 OR {title} LIKE '''tan''')";
// ✅ Nested groups
const nestedGroups = "({status} == '''ACTIVE''' AND ({id} == 1 OR {id} == 2)) OR ({category} == '''VIP''')";
// ✅ Multiple groups
const multipleGroups = "({id} == 1 OR {id} == 2) AND ({status} == '''ACTIVE''' OR {status} == '''PENDING''')";
// ✅ Group với IN operator
const groupWithIn = "({id} == 1 OR {id} == 2) AND {status} IN ['''ACTIVE''', '''PENDING''']";
// ✅ Complex nested groups
const complexNested = "({status} == '''ACTIVE''' AND ({id} == 1 OR {id} == 2)) AND ({priority} >= 3 OR ({category} == '''VIP''' AND {isActive} == true))";
enum DataType {
STRING, // String values
INTEGER, // Integer numbers
DOUBLE, // Decimal numbers
BOOLEAN, // true/false
DATE, // Date values
}
# Chạy tests
npm test
# Chạy tests với watch mode
npm run test:watch
# Chạy tests với coverage
npm test -- --coverage
toMongoCriteria(queryString: string, fieldDescriptions: FieldDescription[]): string - Chuyển đổi QueryString thành MongoDB criteriatoMySqlCriteria(queryString: string, fieldDescriptions: FieldDescription[]): string - Chuyển đổi QueryString thành MySQL WHERE clauseFieldDescription - Interface cho field descriptionFieldDescriptionContainer - Class container cho field descriptionsDataType - Enum cho các kiểu dữ liệu (STRING, INTEGER, DOUBLE, BOOLEAN, DATE)FieldDescriptionType - Alias cho FieldDescriptiontoMongoCriteria(queryString: string, fieldDescriptions: FieldDescription[]): stringChuyển đổi QueryString thành MongoDB criteria dạng JSON string.
toMySqlCriteria(queryString: string, fieldDescriptions: FieldDescription[]): stringChuyển đổi QueryString thành MySQL WHERE clause.
FieldDescriptioninterface FieldDescription {
readonly field: string; // Tên field trong QueryString
readonly required: boolean; // Field có bắt buộc không
readonly dataType: DataType; // Kiểu dữ liệu
readonly maxOccurrence: number; // Số lần xuất hiện tối đa
readonly mapTo: string; // Tên field trong database
}
git checkout -b feature/AmazingFeature)git commit -m 'Add some AmazingFeature')git push origin feature/AmazingFeature)MIT License - xem file LICENSE để biết thêm chi tiết.
Nếu gặp vấn đề, vui lòng tạo issue tại GitHub Issues.
FAQs
Thư viện QueryString của NextPay - Chuyển đổi QueryString thành điều kiện select cho MongoDB và MySQL với kiểm soát bảo mật
The npm package nextpay-querystring receives a total of 25 weekly downloads. As such, nextpay-querystring popularity was classified as not popular.
We found that nextpay-querystring demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Add real-time Socket webhook events to your workflows to automatically receive software supply chain alert changes in real time.

Security News
ENISA has become a CVE Program Root, giving the EU a central authority for coordinating vulnerability reporting, disclosure, and cross-border response.

Product
Socket now scans OpenVSX extensions, giving teams early detection of risky behaviors, hidden capabilities, and supply chain threats in developer tools.