Collection.js is small but yet powerfull local database build on top of the localstorage. Collection.js works with all modern browsers:
-
=FF 4.0
-
=Chrome 5
-
=IE9
-
=Opera 11.6
-
=Safari 5.1
If you need IE8 support use es5 pollyfill
If you are looking for simple solution to store you data locally and localStorage is not enough try collection.js. Collection.js gives you powerfull API, which allows you to save/update/remove/sort/filter data.
- No configuration
- No dependencies
- Simple API
- MIT license
- only 1 KB minified and gzipped
var myCollection = new Collection('myCollection');
myCollection.save({test: 'test'});
myCollection.remove(1);
myCollection.find(function(item) {
if (item.description.substring('test') >= 0) {
return true;
}
});
myCollection.sort(function(a,b){
return a.order - b.order;
});
myCollection.id(1);
myCollection.group('key');
myCollection.destroy();
for (var i = 0; i < myCollection.length; i++) {
console.log(myCollection[i]);
}
Creates new collection object
###Properties
name
collection's name points to localStorage's key namereader
(optional) called when collection is loaded from localStorage, allows you to cast data, or extend entitieswriter
(optional) called right before the entity is saved
Reloads data from localstorage
###Properties
reader
(optional) called when collection is loaded from localStorage on each entity object
Saves or updates entity in the collection, returns the entitie's id
###Properties
entity
object stored in the collection (after storing each object automatically gets immutable_id
key)
Removes existing entity
###Properties
entity
can be either entity or entities's id
Returns entity with given id
###Properties
id
entitie's id
Filters entities matching the query
function. If none passed all entities will be available in collection
###Properties
query
(optional) filter functionsort
(optional) sorting function
Sorts the collection
###Properties
sort
sorting function
Groups collection by key's value and returns object with grouped elements.
###Properties
key
entities' property name which value should be used to group entities.
Clears collection object and destroys collection's data stored in local storage