parse-query-builder v1.0.14
Parse Query Builder
Parse Query Builder is a Node.js module that simplifies the process of building database queries for the Parse platform. It provides an intuitive and flexible way to construct queries with support for various conditions and operators while also supporting all existing methods of the Parse query.
Installation
To use this package, you can install it via npm:
npm install parse-query-builder
Usage
To get started with the Parse Query Builder, follow these steps:
- Import the package into your project.
const { DB } = require('parse-query-builder');
- Create a query for a specific Parse class.
const query = DB.table(Parse.Object.extend("YourClassName"));
Use the where method to add conditions to your query. You can specify conditions as arguments or use nested arrays for complex queries.
query.where('fieldName', '=', 'value');
query.where([
['field1', '=', 'value1'],
['field2', '>', 'value2'],
]);
Supported Operators
The Parse Query Builder supports the following operators for building conditions:
= (equalTo)
!= (notEqualTo)
> (greaterThan)
< (lessThan)
>= (greaterThanOrEqual)
<= (lessThanOrEqual)
Example
Here's an example of how to use the Parse Query Builder:
const { DB } = require('parse-query-builder');
const query = DB.table(Parse.Object.extend("YourClassName"));
query.where('fieldName', '=', 'value');
const results = await query.find();
console.log(results);
Other Supported Methods
Query Execution
Get
Executes the query and get record by id.
query.get(id);
Find
Executes the query and retrieves all matching results.
query.find();
First
Executes the query and retrieves the first matching result.
query.first();
Distinct
Queries can find unique values for a specified field.
query.distinct("fieldName")
Select
Select particular fields from the query
query.select("fieldName1", "fieldName2");
Comparison Operators
equalTo
Adds a condition to the query where the specified field is equal to the given value.
query.where("playerName", "Dan Stemkoski");
// query.where("playerName","=", "Dan Stemkoski");
// query.equalTo("playerName","Dan Stemkoski");
notEqualTo
Adds a condition to the query where the specified field is not equal to the given value.
query.where("playerName","!=" ,"Dan Stemkoski");
// query.notEqualTo("playerName","Dan Stemkoski");
greaterThan
Adds a condition to the query where the specified field is greater than the given value.
query.where("wins",">",50);
// query.greaterThan("wins",50);
lessThan
Adds a condition to the query where the specified field is less than the given value.
query.where("wins",50);
// query.lessThan("wins","<",50);
greaterThanOrEqualTo
Adds a condition to the query where the specified field is greater than or equal to the given value.
query.where("wins",">=",50);
// query.greaterThanOrEqualTo("wins",50);
lessThanOrEqualTo
Adds a condition to the query where the specified field is less than or equal to the given value.
query.where("wins","<=",50);
//query.lessThanOrEqualTo("wins",50);
limit
Limiting and Skipping Results
query.limit(10); // limit to at most 10 results
skip
Skips the specified number of results.
query.skip(10); // skip the first 10 results
Additional Query Options
withCount
Includes the count of the total number of results in the query result.
query.withCount(); // to include count
count
get only the count of the total number of results in the query result.
query.count();
Sorting Results
ascending
Sorts the results in ascending order based on the specified field.
query.ascending(); // Sorts the results in ascending order by the score field
descending
Sorts the results in descending order based on the specified field.
query.descending(); // Sorts the results in descending order by the score field
Array and Value Checks
containsAll
Adds a condition to the query where the array in the specified field contains all of the specified elements.
// Find objects where the array in arrayKey contains all of the elements 2, 3, and 4.
query.containsAll("arrayKey", [2, 3, 4]);
containedIn
Adds a condition to the query where the specified field contains any of the specified values.
// Finds scores from any of Jonathan, Dario, or Shawn
query.containedIn("playerName",["Jonathan Walsh", "Dario Wunsch", "Shawn Simon"]);
notContainedIn
Adds a condition to the query where the specified field does not contain any of the specified values.
// Finds scores from any of Jonathan, Dario, or Shawn
// Finds scores from anyone who is neither Jonathan, Dario, nor Shawn
query.notContainedIn("playerName",["Jonathan Walsh", "Dario Wunsch", "Shawn Simon"]);
Existence Checks
exists
Adds a condition to the query to find objects where the specified field is set.
// Finds objects that have the score set
query.exists("score");
doesNotExist
Adds a condition to the query to find objects where the specified field is not set.
// Finds objects that don't have the score set
query.doesNotExist("score");
String Matching
startsWith
Adds a condition to the query to find objects where the specified field starts with the given value.
query.startsWith("name", "Big Daddy's");
Other Methods
toJSON
Returns a JSON representation of this query.
query.toJSON();
aggregate
Queries can be made using aggregate, similar to mongodb aggregate method.
const pipeline = [
{ $group: { _id: '$name' } }
];
const query = new Parse.Query("User");
query.aggregate(pipeline)
.then(function(results) {
// results contains unique name values
})
.catch(function(error) {
// There was an error.
});
License
This package is open-source and available under the MIT License.
Issues and Contributions
If you encounter any issues or have suggestions for improvements, please feel free to open an issue on the GitHub repository: Link to GitHub Repository
Contributions and pull requests are welcome!