This module works as an alternative for localStorage. It has almost the same API, but with more features. It uses the module `client-db` for storage, thus in most environments indexeddb will be used as storagemechanism. Those environments that use localStorage, will have exactly the same features.
With nodejs:
The module client-storage
is a typical browser-module.
In the browser:
For browser-usage, ITSA has a predefined loaderfiles. Once included, a global ITSA
object with default features is available. For customized loaderfiles, read: Customized build.
<script src="/itsabuild-min.js"></script>
ClientStorage is a Class
that creates a store under a specific namespace. Multiple stores can be set up simultaniously. Once set up, you can set
and get
items to and from the store. The keys are unique String
-types, the values can be any type: objects (even Data-objects) retain their structure.
All methods are Promise
-based. This means, you can use them, but need the thenable to inspect the returnvalues.
clear
, size
, each
and some
First, you need to initiate a storage. If the specified storage is already available, you get a reference to it, otherwise you get a reference to a new created store:
var presidentsStorage = new ITSA.ClientStorage('presidents');
var presidentsStorage = new ITSA.ClientStorage('presidents'),
president = {
name: 'John F.',
lastName: 'Kennedy',
'birth': 1917
};
presidentsStorage.set('president1': president);
var presidentsStorage = new ITSA.ClientStorage('presidents'),
president = {
name: 'John F.',
lastName: 'Kennedy',
'birth': 1917
};
presidentsStorage.set('president1': president).then(
function() {
// storage succeeded
},
function(err) {
console.warn(err);
}
);
var presidentsStorage = new ITSA.ClientStorage('presidents');
presidentsStorage.get('president1').then(
function(president) {
// `president` is an object (JFK)
// in case of no record: `president`===`undefined`
},
function(err) {
console.warn(err);
}
);
The promises only rejects on errors. When retrieving a record that isn't available, the promise gets resolved with undefined
as response-value.
All examples are based upon the presidents
-storage above.
presidentsStorage.contains({name: 'John F.', lastName: 'Kennedy', 'birth': 1917}).then(
function(result) {
// result === true
}
);
presidentsStorage.contains({name: 'Bill', lastName: 'Clinton', 'birth': 1946}).then(
function(result) {
// result === false
}
);
presidentsStorage.clear().then(
function() {
presidentsStorage.contains({name: 'John F.', lastName: 'Kennedy', 'birth': 1917}).then(
function(result) {
// result === false
}
);
}
);
presidentsStorage.delete('president1').then(
function() {
presidentsStorage.contains({name: 'John F.', lastName: 'Kennedy', 'birth': 1917}).then(
function(result) {
// result === false
}
);
}
);
presidentsStorage.deleteStorage();
// presidentsStorage should cannot be used anymore
var years = 0;
presidentsStorage.each(function(record) {
years += record.birth;
}).then(
function() {
// years has the total amount of years of all Presidents
}
);
presidentsStorage.get('president1').then(
function(president) {
// `president` is an object (JFK)
}
);
presidentsStorage.has('president1').then(
function(result) {
// result === true
}
);
presidentsStorage.contains('president2').then(
function(result) {
// result === false
}
);
var president = {
name: 'Barack',
lastName: 'Obama',
'birth': 1961
};
presidentsStorage.set('president2': president);
presidentsStorage.size().then(
function(value) {
// value === size of storage
}
);
presidentsStorage.some(function(record) {
return (record.birth===1917);
}).then(
function(president) {
// `president` is an object (JFK)
}
);
presidentsStorage.some(function(record) {
return (record.birth===1999);
}).then(
function(president) {
// `president` is `undefined`
}
);
For convenience, ITSA
already has an universal storage defined: ITSA.localStorage
. This is just a Client-Storage
-instance with a custom-global namespace. You can use this straight ahead:
var president = {
name: 'John F.',
lastName: 'Kennedy',
'birth': 1917
};
ITSA.localStorage.set('president1': president);
ITSA.localStorage.get('president1').then(
function(president) {
// ...
}
);