Namespace: query

query

Selection query as defined by JSData's Query Syntax.

Details
Since Source Tutorials
3.0.0 Query.js, line 329
Properties:
Name Type Argument Description
limit Number <optional>

See query.limit.

offset Number <optional>

See query.offset.

orderBy String | Array.<Array> <optional>

See query.orderBy.

skip Number <optional>

Alias for query.offset.

sort String | Array.<Array> <optional>

Alias for query.orderBy.

where Object <optional>

See query.where.

Examples

const store = new JSData.DataStore()
store.defineMapper('post')
store.findAll('post').then((posts) => {
  console.log(posts) // [...]
})

const store = new JSData.DataStore()
store.defineMapper('post')
const posts = store.filter('post')
console.log(posts) // [...]

const PAGE_SIZE = 2
let currentPage = 3

const store = new JSData.DataStore()
store.defineMapper('post')
const posts = [
  { author: 'John', age: 30, status: 'published', id: 1 },
  { author: 'Sally', age: 31, status: 'published', id: 2 },
  { author: 'Mike', age: 32, status: 'draft', id: 3 },
  { author: 'Adam', age: 33, status: 'deleted', id: 4 },
  { author: 'Adam', age: 33, status: 'published', id: 5 }
  { author: 'Peter', age: 25, status: 'deleted', id: 6 },
  { author: 'Sally', age: 21, status: 'draft', id: 7 },
  { author: 'Jim', age: 27, status: 'draft', id: 8 },
  { author: 'Jim', age: 27, status: 'published', id: 9 },
  { author: 'Jason', age: 55, status: 'published', id: 10 }
]
store.add('post', posts)
// Retrieve a filtered page of blog posts
// Would typically replace filter with findAll
store.filter('post', {
  where: {
    status: {
      // WHERE status = 'published'
      '==': 'published'
    },
    author: {
      // AND author IN ('bob', 'alice')
      'in': ['bob', 'alice'],
      // OR author IN ('karen')
      '|in': ['karen']
    }
  },
  orderBy: [
    // ORDER BY date_published DESC,
    ['date_published', 'DESC'],
    // ORDER BY title ASC
    ['title', 'ASC']
  ],
  // LIMIT 2
  limit: PAGE_SIZE,
  // SKIP 4
  offset: PAGE_SIZE * (currentPage - 1)
})

Members


<static> limit

Maximum number of records to retrieve.

Examples

const PAGE_SIZE = 10
let currentPage = 1
PostMapper.findAll({
  offset: PAGE_SIZE * (currentPage 1)
  limit: PAGE_SIZE
})

const PAGE_SIZE = 5
let currentPage = 2
const store = new JSData.DataStore()
store.defineMapper('post')
const posts = [
  { author: 'John', age: 30, id: 1 },
  { author: 'Sally', age: 31, id: 2 },
  { author: 'Mike', age: 32, id: 3 },
  { author: 'Adam', age: 33, id: 4 },
  { author: 'Adam', age: 33, id: 5 },
  { author: 'Peter', age: 25, id: 6 },
  { author: 'Sally', age: 21, id: 7 },
  { author: 'Jim', age: 27, id: 8 },
  { author: 'Jim', age: 27, id: 9 },
  { author: 'Jason', age: 55, id: 10 }
]
store.add('post', posts)
store.filter('post', {
  offset: PAGE_SIZE * (currentPage 1)
  limit: PAGE_SIZE
})

console.log(results)

<static> offset

Number of records to skip.

Examples

const PAGE_SIZE = 10
let currentPage = 1
PostMapper.findAll({
  offset: PAGE_SIZE * (currentPage 1)
  limit: PAGE_SIZE
})

const PAGE_SIZE = 5
let currentPage = 2
const store = new JSData.DataStore()
store.defineMapper('post')
const posts = [
  { author: 'John', age: 30, id: 1 },
  { author: 'Sally', age: 31, id: 2 },
  { author: 'Mike', age: 32, id: 3 },
  { author: 'Adam', age: 33, id: 4 },
  { author: 'Adam', age: 33, id: 5 },
  { author: 'Peter', age: 25, id: 6 },
  { author: 'Sally', age: 21, id: 7 },
  { author: 'Jim', age: 27, id: 8 },
  { author: 'Jim', age: 27, id: 9 },
  { author: 'Jason', age: 55, id: 10 }
]
store.add('post', posts)
store.filter('post', {
  offset: PAGE_SIZE * (currentPage 1)
  limit: PAGE_SIZE
})

console.log(results)

<static> orderBy

Determines how records should be ordered in the result.

Details
Type Since Source See
String | Array.<Array> 3.0.0 Query.js, line 472
Example

const store = new JSData.DataStore()
store.defineMapper('post')
const posts = [
  { author: 'John', age: 30, id: 5 },
  { author: 'Sally', age: 31, id: 6 },
  { author: 'Mike', age: 32, id: 7 },
  { author: 'Adam', age: 33, id: 8 },
  { author: 'Adam', age: 33, id: 9 }
]
store.add('post', posts)
store.filter('post', {
    orderBy:[['author','ASC'],['id','DESC']]
})
console.log(results)

<static> where

Filtering criteria. Records that do not meet this criteria will be exluded from the result.

Example

const store = new JSData.DataStore()
store.defineMapper('post')
const posts = [
  { author: 'John', age: 30, id: 5 },
  { author: 'Sally', age: 31, id: 6 },
  { author: 'Mike', age: 32, id: 7 },
  { author: 'Adam', age: 33, id: 8 },
  { author: 'Adam', age: 33, id: 9 }
]
store.add('post', posts)
store.filter('post', {
  where: {
    age: {
      '>=': 30
    }
  }
})
console.log(results)