const parsm = require('parsimonious')
// Create an instance of 'Course' with a name attribute:
// Also creates the class if needed.
const course = await parsm.getClassInst('Course', {
name: 'Sociology 201'
}).save()
// Create another with two attributes:
const student = await parsm.getClassInst('Student', {
name: 'Maria',
class: 2020
}).save()
// Create a many-to-many relationship between students and courses,
// and record the fact that a student completed a course,
// with date of completion and grade earned:
const meta = {completed: new Date(2017, 11, 17), grade: 3.2}
const opts = {sessionToken: 'r:cbd9ac93162d0ba1287970fb85d8d168'}
const joinObj = await parsm.joinWithTable(student, course, meta, opts)
// joinObj is now an instance of the class 'Student2Course',
// which was created if it didn't exist.
// The Student2Course class has pointer columns 'student' and 'course',
// plus a date column named 'completed' and a numeric column named 'grade'.
// Find the top 10 students who have taken a particular course
// and earned a grade of at least 3:
const classes = {
Student: null,
Course: course
}
const criteria = {
descending: 'grade',
greaterThanOrEqualTo: ['grade', 3],
limit: 10,
include: 'student'
}
const joinObjs = await parsm.getJoinQuery(classes, criteria).find()
// joinObjs is now an array of instances of the class 'Student2Course'
// with details of students in the 'student' column.
// Initialize Parse JS SDK first:
Parse.initialize('myAppId')
Parse.masterKey = 'myMasterKey'
// Initialize parsimonious with the initialized Parse instance:
const parsm = require('parsimonious')
parsm.setParse(Parse)
Kind: global class
-
- .setParse(parse)
- .getClass(className) ⇒
- .getClassInst(className, [attributes], [options]) ⇒
Parse.Object
- .newQuery(aClass, [constraints]) ⇒
Parse.Query
- .constrainQuery(query, constraints) ⇒
Parse.Query
- .getObjById(aClass, id, [opts])
- .getUserById(id, [opts]) ⇒
Parse.User
- .fetchIfNeeded(thing, [opts]) ⇒
Parse.Promise
- .getUserRoles(user, [opts]) ⇒
Parse.Promise
- .userHasRole(user, roles, [opts]) ⇒
Parse.Promise
- .getJoinTableName(from, to) ⇒
string
- ._getJoinTableClassVars() ⇒
object
- .joinWithTable(object1, object2, [metadata], [opts]) ⇒
Parse.Promise
- .unJoinWithTable(object1, object2, [opts]) ⇒
Parse.Promise
- .getJoinQuery(classes, [constraints]) ⇒
Parse.Query
- .getPointer(className, objectId) ⇒
object
- .isPFObject(thing, [ofClass]) ⇒
boolean
- .isArrayOfPFObjects(thing, [ofClass]) ⇒
boolean
- .isPointer(thing, [ofClass]) ⇒
boolean
- .isPFObjectOrPointer(thing, [ofClass]) ⇒
boolean
- .isUser(thing) ⇒
boolean
- .pfObjectMatch(thing1, thing2) ⇒
boolean
- .getPFObject([thing], [className], [opts]) ⇒
Parse.Promise
- .toJsn(thing, [deep]) ⇒
*
- .getId(thing) ⇒
string
|undefined
- .objPick(parseObj, keys) ⇒
object
- .objGetDeep(parseObj, path) ⇒
*
- .objSetMulti(parseObj, dataObj, [doMerge])
- .sortPFObjectsByKey([objs], key)
- .copyPFObjectAttributes(from, to, attributeNames)
- .keysAreDirty(parseObj, keys) ⇒
boolean
- .getPFObjectClassName(thing) ⇒
string
- .classStringOrSpecialClass(thing) ⇒
*
- .classNameToParseClassName(className)
- ._toArray(thing, [type]) ⇒
array
Utilities for Parse Server cloud code and JS SDK.
Set the instance of the Parse JS SDK to be used by all methods:
Kind: static method of Parsimonious
Params
- parse
object
- instance of the Parse JS SDK
Short-hand for Parse.Object.extend(className) or a special class like Parse.User
Kind: static method of Parsimonious
Returns: subclass of Parse.Object
Params
- className
string
Return instance of Parse.Object class.
Kind: static method of Parsimonious
Params
- className
string
- Parse.Object subclass name. - [attributes]
object
- Properties to set on new object. - [options]
object
- Options to use when creating object.
Returns a new Parse.Query instance from a Parse Object class name.
Kind: static method of Parsimonious
Params
- aClass
Parse.Object
|string
- Parse class instance or name - [constraints]
object
- Plain object whose keys may be any Parse.Query constraint methods and whose values are arrays of arguments for those methods.
Example
// Generate a new Parse.Query on the User class,
const query = Parsimonious.newQuery('User')
// which is equivalent to:
const query = new Parse.Query(Parse.User)
Example
// Generate a new Parse.Query on a custom class,
const query = Parsimonious.newQuery('Company')
// which is equivalent to:
const Company = Parse.Object.extend('Company')
const query = new Parse.Query(Company)
Example
// Generate a new Parse.Query on the User class, adding constraints 'startsWith,' 'limit,' and 'select.' (See Parsimonious.constrainQuery for constraints parameter details.)
const query = Parsimonious.newQuery('Company', {
startsWith: ['name', 'tar'],
limit: 10, // If there is only one argument, does not need to be in an array
select: [ ['name', 'address', 'url'] ] // If any argument for a constraint is an array, it must be passed to constrainQuery within another array to indicate that its array items are not individual arguments.
})
// which is equivalent to:
const Company = Parse.Object.extend('Company')
const query = new Parse.Query(Company)
query.startsWith('name', 'tar')
query.limit(10)
query.select('name')
query.select('address')
query.select('url')
Calls one or more query constraint methods on a query with arbitrary number of arguments for each method. This method is useful when, for example, building a complex query configuration to pass to another function that may modify the configuration further and then generate the actual query. Mutates the 'query' parameter because it calls constraint methods on it. Returns the query, so you can chain this call.
Kind: static method of Parsimonious
Params
- query
Parse.Query
- The query on which to call the constraint methods - constraints
object
- Plain object containing query constraint methods as keys and arguments as values
Example
// Modify a query with startsWith, limit, select, equalTo, and notEqualTo constraints:
const query = Parsimonious.newQuery('User')
const constraints = {
startsWith: ['name', 'Sal'],
limit: 10, // If there is only one argument, does not need to be in an array
select: [ ['name', 'email', 'birthDate'] ], // If a single constraint argument is an array, it must be within another array to indicate that its items are not individual arguments.
equalTo: [ ['gender', 'f'], ['country', 'US'] ], // An array of 2 or more arrays indicates that the constraint method should be called once with each inner array as its arguments.
notEqualTo: ['company', 'IBM'] // There is just one set of parameters, so there is no need to enclose in another array.
}
Parsimonious.constrainQuery(query, constraints)
// which is equivalent to:
const query = new Parse.Query(Parse.User)
query.startsWith('name', 'Sal')
query.limit(10)
query.select('name')
query.select('email')
query.select('birthDate')
Return a Parse.Object instance from className and id.
Kind: static method of Parsimonious
Params
- aClass
string
|object
- class name or constructor - id
string
- [opts]
object
- A Backbone-style options object for Parse subclass methods that read/write to database. (See Parse.Query.find).
Return Parse.User instance from user id
Kind: static method of Parsimonious
Params
- id
string
- [opts]
object
- A Backbone-style options object for Parse subclass methods that read/write to database. (See Parse.Query.find).
Given a value thing, return a promise that resolves to
- thing if thing is a clean Parse.Object,
- fetched Parse.Object if thing is a dirty Parse.Object,
- fetched Parse.Object if thing is a pointer;
- thing if otherwise
Kind: static method of Parsimonious
Params
- thing
*
- [opts]
object
- A Backbone-style options object for Parse subclass methods that read/write to database. (See Parse.Query.find).
Return array of names of user's direct roles, or empty array. Requires that the Roles class has appropriate read permissions.
Kind: static method of Parsimonious
Params
- user
Parse.User
- [opts]
object
- A Backbone-style options object for Parse subclass methods that read/write to database. (See Parse.Query.find).
Check if a user has a role, or any or all of multiple roles, return a promise resolving to true or false.
Kind: static method of Parsimonious
Params
- user
Parse.User
- roles
string
|object
- Can be single role name string, or object containing 'names' key whose value is an array of role names and 'op' key with value 'and' or 'or' - [opts]
object
- A Backbone-style options object for Parse subclass methods that read/write to database. (See Parse.Query.find).
Return the name of a table used to join two Parse.Object classes in a many-to-many relationship.
Kind: static method of Parsimonious
Params
- from
string
- First class name - to
string
- Second class name
Converts a variable number of arguments into 4 variables used by the joinWithTable, unJoinWithTable, getJoinQuery methods.
Kind: static method of Parsimonious
Join two parse objects in a many-to-many relationship by adding a document to a third join table. Like Parse.Relation.add except that it allows you to add metadata to describe the relationship. Creates join tables which are named with the class names separated with the numeral 2; e.g.: Student2Course. (For backwards-compatibility with v4.1.0, this method may still be called with the 3 parameters 'classes', 'metadata', and 'opts', where 'classes' is a plain object whose two keys are the classes to join, and whose values are the Parse.Object instances.)
Kind: static method of Parsimonious
Params
- object1
Parse.Object
- Parse object or pointer - object2
Parse.Object
- Parse object or pointer - [metadata]
object
- optional key/value pairs to set on the new document to describe relationship. - [opts]
object
- A Backbone-style options object for Parse subclass methods that read/write to database. (See Parse.Query.find).
Example
// Record the fact that a student completed a course, with date of completion and grade earned:
const student = <instance of Parse.Student subclass>
const course = <instance of Parse.Course subclass>
const meta = {completed: new Date(2017, 11, 17), grade: 3.2}
const opts = {sessionToken: 'r:cbd9ac93162d0ba1287970fb85d8d168'}
Parsimonious.joinWithTable(student, course, meta, opts)
.then(joinObj => {
// joinObj is now an instance of the class 'Student2Course', which was created if it didn't exist.
// The Student2Course class has pointer columns 'student' and 'course',
// plus a date column named 'completed' and a numeric column named 'grade'.
})
Unjoin two parse objects previously joined by Parsimonious.joinWithTable If can't unjoin objects, returned promise resolves to undefined. (For backwards-compatibility with v4.1.0, this method may still be called with the 2 parameters 'classes' and 'opts', where 'classes' is a plain object whose two keys are the classes to join, and whose values are the Parse.Object instances.)
Kind: static method of Parsimonious
Params
- object1
Parse.Object
- Parse object or pointer - object2
Parse.Object
- Parse object or pointer - [opts]
object
- A Backbone-style options object for Parse subclass methods that read/write to database. (See Parse.Query.find).
Return a query on a many-to-many join table created by Parsimonious.joinWithTable.
Kind: static method of Parsimonious
Params
- classes
object
- Must contain two keys corresponding to existing classes. At least one key's value must be a valid parse object. If the other key's value is not a valid parse object, the query retrieves all objects of the 2nd key's class that are joined to the object of the 1st class. Same for vice-versa. If both values are valid parse objects, then the query should return zero or one row from the join table. - [constraints]
object
- (Options for Parsimonious.newQuery})
Example
// Find the join table record linking a particular student and course together:
const classes = {
Student: <instance of Student class>,
Course: <instance of Course class>
}
Parsimonious.getJoinQuery(classes)
.first()
.then(joinObj => {
// joinObj is the instance of the class 'Student2Course'
// that was created by Parsimonious.joinWithTable}
// to link that particular student and course together,
// along with any metadata describing the relationship.
})
Example
// Find all courses taken by a particular student:
const classes = {
Student: <instance of Student class>,
Course: null
}
Parsimonious.getJoinQuery(classes)
.find()
.then(joinObjs => {
// joinObj is an array of instances of the class 'Student2Course'
// that were created by Parsimonious.joinWithTable}.
})
Example
// Find the top 10 students who have taken a particular course and received a grade of at least 3:
const classes = {
Student: null,
Course: <instance of Course class>
}
Parsimonious.getJoinQuery(classes, {
descending: 'grade',
greaterThanOrEqualTo: ['grade', 3],
limit: 10
}).find()
Return a pointer to a Parse.Object.
Kind: static method of Parsimonious
Params
- className
string
- objectId
string
Return true if thing is a Parse.Object, or sub-class of Parse.Object (like Parse.User or Parse.MyCustomClass)
Kind: static method of Parsimonious
Params
- thing
*
- [ofClass]
string
- Optionally check if it's of a specific ParseObjectSubclass
Return true if thing is array of Parse.Object
Kind: static method of Parsimonious
Params
- thing
*
- [ofClass]
string
- Optionally check if it's of a specific ParseObjectSubclass
Return true of thing is a valid pointer to a Parse.Object, regardless of whether the Parse.Object exists.
Kind: static method of Parsimonious
Params
- thing
*
- [ofClass]
string
- Optionally check if it's of a specific ParseObjectSubclass
Return true if thing is a Parse.Object or pointer
Kind: static method of Parsimonious
Params
- thing
*
- [ofClass]
string
- Optionally check if it's of a specific ParseObjectSubclass
Return true if thing is an instance of Parse.User.
Kind: static method of Parsimonious
Params
- thing
*
Return true if values both represent the same Parse.Object instance (same class and id) even if one is a pointer and the other is a Parse.Object instance.
Kind: static method of Parsimonious
Params
- thing1
Parse.Object
|object
- thing2
Parse.Object
|object
Resolves thing to a Parse.Object, or attempts to retrieve from db if a pointer. Resolves as undefined otherwise.
Kind: static method of Parsimonious
Params
- [thing]
Parse.Object
|object
|string
- [className]
string
- If set, and first param is a Parse.Object, resolves to the Parse.Object only if it is of this class. - [opts]
object
- A Backbone-style options object for Parse subclass methods that read/write to database. (See Parse.Query.find).
Return a json representation of a Parse.Object, or of plain object that may contain Parse.Object instances, optionally recursively.
Kind: static method of Parsimonious
Params
- thing
*
- Value to create json from. - [deep]
boolean
= false
- If true, recursively converts all Parse.Objects and sub-classes of Parse.Objects contained in any plain objects found or created during recursion.
Attempt to return the ID of thing if it's a Parse.Object or pointer. If thing is a string, just return it. Otherwise, return undefined.
Kind: static method of Parsimonious
Params
- thing
*
Get some columns from a Parse object and return them in a plain object. If keys is not an array or comma-separated string, return undefined.
Kind: static method of Parsimonious
Params
- parseObj
Parse.Object
- keys
string
|Array.<string>
Example
const car = new Parse.Object.extend('Car')
car.set('type', 'SUV')
car.set('interior', {
seats:5,
leather: {
color: 'tan',
seats: true,
doors: false
}
})
car.set('specs', {
length: 8,
height: 4,
performance: {
speed: 120,
zeroTo60: 6
}
})
Parsimonious.objPick(car, 'type,interior.leather,specs.performance.speed')
// returns
{
type: 'SUV',
interior: {
leather: {
color: 'tan',
seats: true,
doors: false
},
specs: {
performance: {
speed: 120
}
}
}
Get the value of a key from a Parse object and return the value of a nested key within it.
Kind: static method of Parsimonious
Params
- parseObj
Parse.Object
- path
string
- Dot-notation path whose first segment is the column name.
Example
const car = new Parse.Object.extend('Car')
car.set('type', 'SUV')
car.set('interior', {
seats:5,
leather: {
color: 'tan',
seats: true,
doors: false
}
})
Parsimonious.objGetDeep(car, 'interior.leather.color')
// returns "tan"
Set some columns on a Parse object. Mutates the Parse object.
Kind: static method of Parsimonious
Params
- parseObj
Parse.Object
- dataObj
object
- [doMerge]
boolean
= false
- If true, each column value is shallow-merged with existing value
Sort an array of Parse objects by key (column name) Mutates array.
Kind: static method of Parsimonious
Params
- [objs]
Array.<object>
- key
string
Copy a set of attributes from one instance of Parse.Object to another. Mutates target Parse.Object.
Kind: static method of Parsimonious
Params
- from
Parse.Object
- to
Parse.Object
- Is mutated. - attributeNames
string
|Array.<string>
Return true if any of the passed keys are dirty in parseObj
Kind: static method of Parsimonious
Params
- parseObj
Parse.Object
- keys
string
|Array.<string>
- Array of string or comma-separated string list of keys.
Returns valid class name when passed either a subclass of Parse.Object or any string. Removes the underscore if it is one of the special classes with a leading underscore. Returns undefined if anything else.
Kind: static method of Parsimonious
Params
- thing
object
|string
Returns the corresponding special Parse class (like 'User') if passed the name of one; otherwise, returns the value unchanged.
Kind: static method of Parsimonious
Params
- thing
string
If className represents one of the special classes like 'User,' return prefixed with an underscore.
Kind: static method of Parsimonious
Params
- className
Return thing if array, string[] if string, otherwise array with thing as only item, even if undefined.
Kind: static method of Parsimonious
Params
- thing
*
- [type]
string
- If set, only include values of this type in resulting array.
- Updated dependencies
- keysAreDirty method
- minor documentation additions
- minor documentation errors
- minor documentation errors
- copyPFObjectAttributes method
- pfObjectMatch method
- getId method
- isPFObjectOrPointer method
- getPFObject method
- sortPFObjectsByKey method
- isArrayOfPFObjects method
- isPFObject method now checks for ParseObjectSubclass constructor as well as instanceOf Parse.Object
- constrainQuery and getJoinQuery methods now accept multiple constraints of the same type, such as three equalTo's.
- getJoinQuery can now be passed pointers.
- TypeError is now thrown when invalid parameters are passed to getJoinQuery, joinWithTable, unJoinWithTable methods.
- Switched testing platform from Mocha/Chai to Jest.
- Switched testing version of mongodb from parse-mockdb to mongodb-memory-server.
- Remove links from change log as I can't get jsdoc-to-markdown to generate them correctly.
- README
- constrainQuery method, which newQuery now uses for optional constraints.
- newQuery method now supports all query constraints (instead of just limit, skip, select and include).
- README
- Support for calling joinWithTable with two Parse.Object instances, while still supporting the original parameter format.
- Examples for join-table methods.
- Improved some other documentation.
- getPointer method was generating pseudo pointers that did not have a fetch function.
- Addressed deprecation notice for the mocha "compilers" option
- Added optional 'ofClass' param to isPointer method to check if it is a pointer of a specific class.
- 'include' option to newQuery method
- Added support for getting nested values from pointer columns to objGetDeep method.
- getClass method now creates constructors for special classes 'User,' 'Role,' and 'Session' with 'Parse[],' but still creates constructors for custom classes with 'Parse.Object.extend().'
- getClass method now throws a TypeError if not passed a string, which it should have done anyway.
- The above changes to getClass method affect getClassInst method because it always uses getClass method.
- The above changes to getClass method affect newQuery method because it uses getClass method when passed a string.
- Added back 'use strict'
- getUserById was passing the Parse.User constructor rather than an instance of Parse.User to getObjById, which was then trying to use it in a Parse.Query, but Parse.Query requires class instances or names.
- getPointer method
- sample code for objGetDeep
- Minor README changes, but published in order to get NPM to show the last few updates to README.
- Minor jsdoc fixes.
- Uses 'browser' field in package.json to hint to webpack, browserify to substitute 'parse' for 'parse/node'
- Minor README changes
- Exports a class with static methods, rather than a frozen singleton instance. It is still used the same way; it's just no longer a frozen instance of the class.
- setParse method, which allows you to override the instance of the Parse JS SDK used by the module. (By default, the module will try to use the Parse instance from the global scope. If none exists, it will use the node or browser based on which environment is detected by the 'detect-is-node' module.) If you wish to use the setParse method, do it after you initialize your Parse instance and set the masterKey. (See "Usage" section at the top of this file.)
- getClass method.
- objGetDeep method.
- classStringOrSpecialClass method.
- userHasRole method rejects when passed invalid params
- Switched test framework from jest to mocha+chai because of issue between jest and parse-mockdb module.
- The way isPointer recognizes of one of the 3 different types of pointer objects it checks for.
- isPointer method' recognizes 3 different types of pointer objects.
- More thorough tests for isPFObject method, including invalidating pointers.
- More thorough tests for isUser method.
- isPointer method was restricting to plain objects.
- Minor jsdoc fixes.
- fetchIfNeeded method.
- isPointer method.
- Refactored into two files -- one for node environments and one for browsers. Reason: Runtime environment detection is too unreliable, even using "detect-node" module, because of problems running in webpack-dev-server.
- a "browser" option in package.json as a hint to babel, browserify, etc. to use the browser version.
- isUser method
- getUserRoles method returns array of names of user's direct roles.
- userHasRole method can check if a user has any or all of an array of roles.
- Improved documentation of newQuery method
- getJoinQuery signature has changed: The 'select' parameter has been removed. Instead, set a 'select' key in the 2nd options param object for use by newQuery method.
- newQuery method accepts a 'select' key in its 2nd parameter to select fields to restrict results to.
- Improved documentation.
- Improved ci config.
- Moved Change Log to bottom of README.
- Removed commitizen.
- Removed semantic-release for now.
- Reconfigured ci.
- codecov reporting and badge.
- Reduced minimum required node to 4.
- 100% test coverage with jest.
- Use different branch of parse-shim to account for parse already being loaded in cloud code.
- Use different branch of parse-shim to correctly detect when running in browser or node to import correct parse version.
- userHasRole method
- all methods that access the database now accept optional sessionToken
- isPFObject now accepts an optional class name param
- can pass array of field names, in addition to comma-separated list, to getJoinQuery
- If unJoinWithTable can't unjoin objects, it returns a promise that resolves to undefined instead of null.