local.dbstore
Fixture store for Angular projects. All data is persisted in localStorage.
Installation
You can install the library using Bower:
bower install local.dbstore
Or using npm:
npm install local.dbstore
What is it?
local.dbstore
is a service that will help you build a fully working UI prototype for your clients, without the need of a real database.
It knows how to manage collections and simple associations like one-to-many.
All the changes are persisted using localStorage
.
NOTE
The store automatically adds an integer id
property to every collection and it autoincrements it as needed.
Getting started
CRUD Example
If we have a User
resource:
angular ;
Then the fixture file would look like:
/** * This code can be abstracted in a service and reused for most models. */ angular
Associations
Let's imagine that we have a Comment
resource which is a nested resource for User
.
angular ;
Now, after we initialise the localDbStore
, we need to specify the association like this:
var store = ;store;
The key, in our case users
, is a name of a different collection which is the parent resource and the value, in our case user_id
, represents the "foreign key".
$httpBackend;
This will load all the users but not the comments.
To include the comments for each user, you need to manually specify it like this:
$httpBackend;
NOTE
You can include any collection when you do a store.findAll
or store.findOne
, not just the ones that have an association defined. This means that instead of having a nested collection of only items that have a set of matching ids, you will get the entire collection nested for each item.
$httpBackend;
Search / Filter
At the moment, the library supports only basic search, meaning that you can specify a field name and it's exact value, there is no support for like
, includes
, greater-than
and so on, but could be added in future versions.
Eralier we saw that we can get to an item by specifying the id
. You can specify any other valid property and it will just work.
$httpBackend;
Tips
If you ever need to clear the collections you saved you can use the browser's console and type:
localStorageclear;
If you want to only delete specific collections, use:
delete localStorageusers
If you use Chrome, you could open the Developer Tools, click on the Resources tab, select Local Storage and manually edit/remove the collections.