0
votes

I've built an app that saves records in Local Storage.

I've just recently realize that by design I'm chocking the CPU by blocking the DOM, as I'm reading lots of records from a single key using ng-repeat.

My idea is to migrate to PouchDB, but I think I'm missing some concepts.

Right now my data structure looks like this:

  • Key(cars) - Value(objectCar1 - objectCar2 - objectCar3 ...)
  • Key(drivers) - Value(objectDriver1 - objectDriver2 - objectDriver3...) -
  • Key(records) - Value(objectRecord1 - objectRecord2 - objectRecord3 ... up to Record70 now, starting to give problems)

I've been reading Pouch and CouchDB documentation, and if I were to switch to PouchDB, then I just have to throw objects into the DB willy-nilly and then iterate through it looking for parameters in order to filter?

Right now a NoSQL database looks like a big bag where to store data without much order.

Or am I missing something?

3

3 Answers

1
votes

PouchDB is not a big bag to store data, but you have to change your mindset a bit in order to store data efficiently.

Long story short, you will want to insert documents that look like this:

{ _id: 'car_<id>', data: ...}
{ _id: 'driver_<id>', data: ...}
{ _id: 'record_<id>', data: ...}

Notice that each document's _id is prefixed by its type.

Then to query the data efficiently, e.g. to find all cars, you would query db.allDocs({startkey: 'car_', endkey: 'car_\uffff', include_docs: true}).

Or if you are tired of writing out these kinds of queries by hand, then relational-pouch will do the exact same thing, but with a nicer API.

1
votes

You're missing a couple of things.

First off, pouchDB's main party trick is replication with couchDB. If you're not using that, you might be better off with some other web database wrapper (e.g ydn)

In terms of querying your data, you could iterate through the whole database. But a more efficient and simple way is to use views or the find plugin

0
votes

If you're already using localstorage you could look at https://github.com/mozilla/localForage