Back to Top

parsimonious master

Utilities for Parse Server cloud code and JS SDK

value(parse)

Set the instance of the Parse JS SDK to be used by all methods:

Parameters

Name Type Description
parse object

instance of the Parse JS SDK

Returns

Void

setParse(parse)

Set the instance of the Parse JS SDK to be used by all methods:

Parameters

Name Type Description
parse object

instance of the Parse JS SDK

Returns

Void

getClass(className)

Short-hand for Parse.Object.extend(className) or a special class like Parse.User

Parameters

Name Type Description
className string

Returns

subclass of Parse.Object

getClassInst(className[, attributes, options])

Return instance of Parse.Object class.

Parameters

Name Type Description
className string

Parse.Object subclass name.

attributes object

Properties to set on new object.

Optional
options object

Options to use when creating object.

Optional

Returns

Parse.Object

newQuery(aClass[, constraints])

Returns a new Parse.Query instance from a Parse Object class name.

Parameters

Name Type Description
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.

Optional

Examples

// 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)
// 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)
// 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')

Returns

Parse.Query

constrainQuery(query, constraints)

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.

Parameters

Name Type Description
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

Examples

// 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')

Returns

Parse.Query

getObjById(aClass, id[, opts])

Return a Parse.Object instance from className and id.

Parameters

Name Type Description
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).

Optional

Returns

Void

getUserById(id[, opts])

Return Parse.User instance from user id

Parameters

Name Type Description
id string
opts object

A Backbone-style options object for Parse subclass methods that read/write to database. (See Parse.Query.find).

Optional

Returns

Parse.User

fetchIfNeeded(thing[, opts])

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

Parameters

Name Type Description
thing
opts object

A Backbone-style options object for Parse subclass methods that read/write to database. (See Parse.Query.find).

Optional

Returns

Parse.Promise

getUserRoles(user[, opts])

Return array of names of user's direct roles, or empty array. Requires that the Roles class has appropriate read permissions.

Parameters

Name Type Description
user Parse.User
opts object

A Backbone-style options object for Parse subclass methods that read/write to database. (See Parse.Query.find).

Optional

Returns

Parse.Promise

userHasRole(user, roles[, opts])

Check if a user has a role, or any or all of multiple roles, return a promise resolving to true or false.

Parameters

Name Type Description
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).

Optional

Returns

Parse.Promise

getJoinTableName(from, to)

Return the name of a table used to join two Parse.Object classes in a many-to-many relationship.

Parameters

Name Type Description
from string

First class name

to string

Second class name

Returns

string

_getJoinTableClassVars()

Converts a variable number of arguments into 4 variables used by the joinWithTable, unJoinWithTable, getJoinQuery methods.

Returns

object

joinWithTable(object1, object2[, metadata, opts])

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.)

Parameters

Name Type Description
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.
Optional
opts object

A Backbone-style options object for Parse subclass methods that read/write to database. (See Parse.Query.find).

Optional

Examples

// 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'.
   })

Returns

Parse.Promise

unJoinWithTable(object1, object2[, opts])

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.)

Parameters

Name Type Description
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).

Optional

Returns

Parse.Promise

getJoinQuery(classes[, constraints])

Return a query on a many-to-many join table created by Parsimonious.joinWithTable.

Parameters

Name Type Description
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})

Optional

Examples

// 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.
   })
// 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}.
   })
// 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()

Returns

Parse.Query

getPointer(className, objectId)

Return a pointer to a Parse.Object.

Parameters

Name Type Description
className string
objectId string

Returns

object

isPFObject(thing[, ofClass])

Return true if thing is a Parse.Object, or sub-class of Parse.Object (like Parse.User or Parse.MyCustomClass)

Parameters

Name Type Description
thing
ofClass string

Optionally check if it's of a specific ParseObjectSubclass

Optional

Returns

boolean

isArrayOfPFObjects(thing[, ofClass])

Return true if thing is array of Parse.Object

Parameters

Name Type Description
thing
ofClass string

Optionally check if it's of a specific ParseObjectSubclass

Optional

Returns

boolean

isPointer(thing[, ofClass])

Return true of thing is a valid pointer to a Parse.Object, regardless of whether the Parse.Object exists.

Parameters

Name Type Description
thing
ofClass string

Optionally check if it's of a specific ParseObjectSubclass

Optional

Returns

boolean

isPFObjectOrPointer(thing[, ofClass])

Return true if thing is a Parse.Object or pointer

Parameters

Name Type Description
thing
ofClass string

Optionally check if it's of a specific ParseObjectSubclass

Optional

Returns

boolean

isUser(thing)

Return true if thing is an instance of Parse.User.

Parameters

Name Type Description
thing

Returns

boolean

pfObjectMatch(thing1, thing2)

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.

Parameters

Name Type Description
thing1 Parse.Object object
thing2 Parse.Object object

Returns

boolean

getPFObject([thing, className, opts])

Resolves thing to a Parse.Object, or attempts to retrieve from db if a pointer. Resolves as undefined otherwise.

Parameters

Name Type Description
thing Parse.Object object string Optional
className string

If set, and first param is a Parse.Object, resolves to the Parse.Object only if it is of this class.

Optional
opts object

A Backbone-style options object for Parse subclass methods that read/write to database. (See Parse.Query.find).

Optional

Returns

Parse.Promise

toJsn(thing[, deep])

Return a json representation of a Parse.Object, or of plain object that may contain Parse.Object instances, optionally recursively.

Parameters

Name Type Description
thing

Value to create json from.

deep boolean

If true, recursively converts all Parse.Objects and sub-classes of Parse.Objects contained in any plain objects found or created during recursion.

Optional

Returns

getId(thing)

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.

Parameters

Name Type Description
thing

Returns

string

objPick(parseObj, keys)

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.

Parameters

Name Type Description
parseObj Parse.Object
keys string Array.<string>

Examples

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
     }
   }
 }

Returns

object

objGetDeep(parseObj, path)

Get the value of a key from a Parse object and return the value of a nested key within it.

Parameters

Name Type Description
parseObj Parse.Object
path string

Dot-notation path whose first segment is the column name.

Examples

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"

Returns

objSetMulti(parseObj, dataObj[, doMerge])

Set some columns on a Parse object. Mutates the Parse object.

Parameters

Name Type Description
parseObj Parse.Object
dataObj object
doMerge boolean

If true, each column value is shallow-merged with existing value

Optional

Returns

Void

sortPFObjectsByKey([objs], key)

Sort an array of Parse objects by key (column name) Mutates array.

Parameters

Name Type Description
objs Array.<object> Optional
key string

Returns

Void

copyPFObjectAttributes(from, to, attributeNames)

Copy a set of attributes from one instance of Parse.Object to another. Mutates target Parse.Object.

Parameters

Name Type Description
from Parse.Object
to Parse.Object

Is mutated.

attributeNames string Array.<string>

Returns

Void

keysAreDirty(parseObj, keys)

Return true if any of the passed keys are dirty in parseObj

Parameters

Name Type Description
parseObj Parse.Object
keys string Array.<string>

Array of string or comma-separated string list of keys.

Returns

boolean

getPFObjectClassName(thing)

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.

Parameters

Name Type Description
thing object string

Returns

string

classStringOrSpecialClass(thing)

Returns the corresponding special Parse class (like 'User') if passed the name of one; otherwise, returns the value unchanged.

Parameters

Name Type Description
thing string

Returns

classNameToParseClassName(className)

If className represents one of the special classes like 'User,' return prefixed with an underscore.

Parameters

Name Type Description
className

Returns

Void

_toArray(thing[, type])

Return thing if array, string[] if string, otherwise array with thing as only item, even if undefined.

Parameters

Name Type Description
thing
type string

If set, only include values of this type in resulting array.

Optional

Returns

array