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.
To use this package, you can install it via npm:
npm install parse-query-builder
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'],
]);
The Parse Query Builder supports the following operators for building conditions:
= (equalTo)
!= (notEqualTo)
> (greaterThan)
< (lessThan)
>= (greaterThanOrEqual)
<= (lessThanOrEqual)
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);
Executes the query and get record by id.
query.get(id);
Executes the query and retrieves all matching results.
query.find();
Executes the query and retrieves the first matching result.
query.first();
Queries can find unique values for a specified field.
query.distinct("fieldName")
Select particular fields from the query
query.select("fieldName1", "fieldName2");
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");
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");
Adds a condition to the query where the specified field is greater than the given value.
query.where("wins",">",50);
// query.greaterThan("wins",50);
Adds a condition to the query where the specified field is less than the given value.
query.where("wins",50);
// query.lessThan("wins","<",50);
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);
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);
Limiting and Skipping Results
query.limit(10); // limit to at most 10 results
Skips the specified number of results.
query.skip(10); // skip the first 10 results
Includes the count of the total number of results in the query result.
query.withCount(); // to include count
get only the count of the total number of results in the query result.
query.count();
Sorts the results in ascending order based on the specified field.
query.ascending(); // Sorts the results in ascending order by the score field
Sorts the results in descending order based on the specified field.
query.descending(); // Sorts the results in descending order by the score field
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]);
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"]);
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"]);
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");
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");
Adds a condition to the query to find objects where the specified field starts with the given value.
query.startsWith("name", "Big Daddy's");
Returns a JSON representation of this query.
query.toJSON();
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.
});
This package is open-source and available under the MIT License.
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!