Cache Query Library
JavaScript
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
bin
lib
test
.gitignore
.npmignore
LICENSE
Makefile
README.md
index.js

README.md

Cache Query Library/Language

A simple library for reading and writing data from different web databases. CQL also supports basic reactive programming

Different storage types:

  • Memory - implemented; not persistent; NodeJS, Browser; Async and sync support
  • LocalStorage - not implemented; persistent; Browser; Async and sync support
  • WebSQL - not implemented; persistent; Browser; Async only
  • IndexedDB - not implemented; persistent; Browser; Async only
  • Custom - implemented

Syntax:

# Add a record into 'recipes'. $ is a context specific object
recipes + $

# Add a record into 'recipes' with 'id' as the primary key (faster search, no duplicate keys)
recipes[id] + $

# Add a record into 'recipes' if $.foo equals to 42 and $.bar doesn't exist or $.baz is x
recipes + $ : (foo=42 & !bar) | baz='x'

# Replace data
recipes = $ 

# Delete data
recipes - $

# Delete all data
recipes = 0

# Read all data from 'recipes'
recipes

# Read all data from 'recipes' if $.foo is 42 or $.bar equals to x
recipes : foo=42 | bar=?

# Read with multiple unknown parameters
recipes : foo=? | bar=?
recipes : foo=?0 | bar=?1

# Read the first record where $.id equals to x
$ = recipes : id=?

Supported options:

"ttl" - time to live

Javascript API

var cql = require('cql');

var cache = new cql.Cache(); // Persistent by default. Want a non-persistent store? Use: cql.Cache({ store: "memory" });
var addQ = cache.compile('recipes[id] + $');
var getQ = cache.compile('$ = recipes : id = ?');

// This handler will be called every time an item with title 'Pizza' is added, modified or removed from recipes
cache.on('recipes : title="Pizza"', function(data, op) {
    console.log('Pizza' was added!');
});

addQ([ { id: 1, title: 'Pizza' }, { id: 2, title: 'Salad' } ]);
addQ({ id: 4, title: 'Yogurt' });

// Sync access is not available for every storage.
var a = getQ(2);
var b = cache.query('$ = recipes : id = ?', 2);

// Async access works with any storage
cache.query('recipes', function(err, result) {
    // ...
});

cache.count('recipes', function(err, count) {
    // ...
});
    
// Prints 'true'
console.log((a === b) ? 'true' : 'false');

// Delete 'recipes'
cache.clear('recipes');

// Delete all the tables that start with 'recipes'
cache.clear('recipes*');

// Delete all the data
cache.clear();

An example

Lets say you need to store user settings locally and you don't want to mess with different databases.

var Preferences = function() {
    this._cache = new cql.Cache('Preferences'); // a persistent dedicated DB
    this._get = this._cache.compile('$ = data : key=?');
    this._set = this._cache.compile('data = $ : key=?');
};

Preferences.prototype.get = function(key, fn) {
    this._get(key, (fn) ? function(err, data) { fn(data); } : null);
};

Preferences.prototype.set = function(key, value, fn) {
    this._set(key, { key: key, value: value }, (fn) ? function(err) { fn(data); } : null);
};

Preferences.prototype.on = function(key, fn, fc) {
    this._cache.on('data : key=?', [ key ], fn, fc);
};

var preferences = new Preferences();

preferences.on('key', function() { console.log('key changed!'); });
preferences.set('key', 43);
// etc...