StorkJS - 瀏覽器的 K/V 存儲引擎
StorkJS 是一個在瀏覽器上實現的建議 Key-Value 存儲引擎。
API
-
then(callback): Start a chain of asynchronous calls -
get(key, [success], [failure]): Return a single value if it exists -
getMany(keys, [success], [failure]): Return several values at once -
save(record, [success], [failure]): Save a record, generating a key if one doesn't exist -
batch(records, [success], [failure]): Save multuple records at once -
put(key, value, [success], [failure]): Save a key-value pair -
remove(key, [success], [failure]): Removes a value/record based on their key -
removeMany(keys, [success], [failure]): Removes several values/records at once -
destroy([success], [failure]): Remove all key-values/records -
reload([success], [failure]): Load all records into cache -
each(callback, [failure]): Invokes the callback for all key-value pairs -
all([success], [failure]): Returns all keys and values to the callback -
size([success], [failure]): Returns the number of key-value pairs to the callback
可通過插件添加的功能
-
where(condition, [success], [failure]): Returns a subset of all key-value pairs that match some condition -
select(columns, [success], [failure]): Returns an array of specified properties -
sort(comparator, [success], [failure]): Sorts keys and values and returns the sorted value -
aggregate(property, accumulate, getResult, [success], [failure]): Performs an aggregation on a property -
count(property, [success], [failure]): Returns the number of objects with a property -
sum(property, [success], [failure]): Returns the sum of a property on object values -
avg(property, [success], [failure]): Returns the average of a property on object values -
min(property, [success], [failure]): Returns the minimum value of a property on object values -
max(property, [success], [failure]): Returns the maximum value of a property on object values
示例代碼:
var db = new Stork( {name: 'settings'} );
db.put( 'meow', 'string key' );
db.put( 23, 'number key' );
db.put( true, 'boolean key' );
db.put( {y:5}, 'object key' );
db.put( [1,3], 'array key' );
// Remove one
db.remove( [1,3] );
// Retrieve one key-value pair
db.get( {y:5}, function(value, key)
{
// value == 'object key'
});
// Retrieve all key-value pairs
db.all(function(values, keys)
{
// values = array of all values, keys = array of corresponding keys
});