Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

can-query-logic

Package Overview
Dependencies
Maintainers
3
Versions
46
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

can-query-logic

query data

  • 1.1.4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
3
Weekly downloads
 
Created
Source

can-query-logic

Build Status

Purpose

can-query-logic is used to give CanJS an understanding of what the parameters used to retrieve a list of data represent. This awareness helps other libraries like [can-connect] and [can-fixture] provide real-time, caching and other behaviors.

The parameters used to retrieve a list of data?

In many applications, you request a list of data by making a fetch or XMLHTTPRequest to a url like:

/api/todos?filter[complete]=true&sort=name

The values after the ? are used to control the data that comes back. Those values are deserialized into a query object look like this:

{
    filter: {complete: true},
    sort: "name"
}

This object represents a Query. This specific query is for requesting completed todos and have the todos sorted by their name.

A QueryLogic instance understands what a Query represents. For example, it can filter records that match a particular query:

var todos = [
  { id: 1, name: "learn CanJS",   complete: true  },
  { id: 2, name: "wash the car",  complete: false },
  { id: 3, name: "do the dishes", complete: true  }
]

var queryLogic = new QueryLogic();

var result = queryLogic.filterMembers({
  filter: {complete: true}
}, todos);

result //-> [
//  { id: 3, name: "do the dishes", complete: true  },
//  { id: 1, name: "learn CanJS",   complete: true  }
//]

The filterMembers method allows QueryLogic to be used similar to a database. QueryLogic instances methods help solve other problems too:

  • real-time - .isMember returns if a particular item belongs to a query and .index returns the location where that item belongs.
  • caching - .isSubset can tell you if you've already loaded data you are looking for. .difference can tell you what data you need to load that already isn't in your cache.

In fact, can-query-logic's most unique ability is to be able to directly compare queries that represent sets of data instead of having to compare the data itself. For example, if you already loaded all completed todos, can-query-logic can tell you how to get all remaining todos:

var completedTodosQuery = {filter: {complete: false}};
var allTodosQuery = {};
var remainingTodosQuery = queryLogic.difference(allTodosQuery, completedTodosQuery);

remainingTodosQuery //-> {filter: {complete: {$ne: false}}}

Matching the default query structure

By default, can-query-logic assumes your service layer will match a default query structure that looks like:

{
    // Selects only the todos that match.
    filter: {
        complete: {$in: [false, null]}
    },
    // Sort the results of the selection
    sort: "-name",
    // Selects a range of the sorted result
    page: {start: 0, end: 19}
}

This structures follows the Fetching Data JSONAPI specification.

There's:

  • a filter property for filtering records,
  • a sort property for specifying the order to sort records, and
  • a page property that selects a range of the sorted result. The range indexes are inclusive.

If you control the service layer, we encourage you to make it match the default query structure. The default query structure also supports the following comparison operators: $eq, $gt, $gte, $in, $lt, $lte, $ne, $nin.

Keywords

FAQs

Package last updated on 29 Oct 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc