Skip to content

Instantly share code, notes, and snippets.

@uncletammy
Last active August 29, 2015 13:56
Show Gist options
  • Save uncletammy/8945342 to your computer and use it in GitHub Desktop.
Save uncletammy/8945342 to your computer and use it in GitHub Desktop.

Criteria Object

Purpose

A Criteria Object contains the information used to retrieve your desired query results.

Criteria Objects are passed as parameters in the methods that perform CRUD operations, like .find() and .destroy() .

Structure

Criteria objects are composed of Query Pairs. This is what they look like.

Normal Pair

The key is a model attribute and the value is what you want to search for.

like this

	// `Model Attribute Name` : 'Some Value'
	
	name : 'walter'
Modified Pair

Modified pairs also have model attributes for keys but they also use Query Modifiers to perform additional useful operations.

like this

		/*
		`Model Attribute Name` : {
			' `Query Modifier` ' : 'Some Value'}
		}
		*/

		name : {
			'contains' : 'alt' }

in

These work similarly to mysql 'in queries'. Each element in the array is treated as 'or'.

like this

		// `Model Attribute Name` : ['Some Value', 'Some Value']

		name : ['Walter','Skyler']
'Or' Pair

These can contain any number of normal or modified 'query pair's returning records when any of them are matched.

like this

		// or : [`normalPair`, `modifiedPair', `normalPair`]
		or : ['Skyler',{ name : {
						'contains' : 'alt'} },'Flynn']

Forming Criteria Objects

Rules

  • Criteria Objects can contain any number of 'Normal', 'Modified', or 'In' Query Pairs but only 1 'Or' Query Pair.
  • Each pair must match a record otherwise that record isn't returned. Pairs are treated as 'and' in your query.
  • All attribute values searched for are case INsensitive. You can't find Mike without also getting mikE.

Criteria Object Examples.

I want records where 'name' is 'Walter' and 'age' is 45.

var myCriteria = {
		name: 'walter',
		age: 45
	}

I want records where 'name' contains 'yle' if they have 'age' 40.

var myCriteria = {
		name : { 'contains' : 'yle' },
		age: 40
	}

I want records where 'name' is 'Walter' or 'profession' is 'cook' but only if their 'age' is at least 45.

var myCriteria = {
		or : [{'name':'walter'},{'profession':'cook'}],
		age: {'>=':45}
	}

I want records where 'name' is 'Walter' or 'jessie'. They must be a 'cook' and their 'age' must be either 45 or 28.

var myCriteria = {
		name: ['walter','jessie'],
		profession: 'cook',
		age: {'>=':45}
	}

I want records where either the 'name' is 'walter' or 'jessie', their 'profession' is 'cook', and they are at least 35

or

I want records where the 'name' is 'skyler' and their 'profession' is 'accountant' or 'car wash professional'.

var myCriteria = {
		or: [{
			name:['walter','jessie'],
			profession:'cook',
			age:{'>=':35}
			},{
			name:'skyler',
			profession:['accountant','car wash professional']
			}]
	}

Notes

The query modifiers may not work on the 'id' attribute with certain adapters (like mongo) because of the way they are treated by their respective databases. You cannot use more than one query modifier in a 'modified' Query Pair . This restriction might be lifted in future versions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment