Skip to content

Commit

Permalink
add support for collection.all()
Browse files Browse the repository at this point in the history
  • Loading branch information
scottmessinger committed May 3, 2014
1 parent ef95a2f commit edf0397
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
17 changes: 17 additions & 0 deletions spec/specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,20 @@ asyncTest("it should update a record", function(){
})
.catch(function(e){ console.log(e, e.stack) })
})

asyncTest("it should find all the records for a collection", function(){
expect(1)

KS.db('blog').collection('posts').add({id: 2, title: 'Lamps'})
.then(function(){
return KS.db('blog').collection('posts').add({id: 3, title: 'Turtles'})
})
.then(function(){
return KS.db('blog').collection('posts').all()
})
.then(function(docs){
equal(docs.length, 2, "It should find all the docs")
start()
})
})

35 changes: 28 additions & 7 deletions src/knapsack.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,19 +137,42 @@ KS.Collection.prototype._addToIndex = function(index, doc){

KS.Collection.prototype.find = function(){
var self = this;
if (arguments.length < 3){
if (arguments.length === 0){
// find all
return this._findAll()
} else if (arguments.length == 1 || arguments.length == 2){
// find by id
id = arguments[0]
cb = arguments[1]
return this._simpleFind(id, cb)
return this._findById(id, cb)
} else {
// find by index
index = arguments[0]
value = arguments[1]
cb = arguments[2]
return this._indexFind(index, value, cb)
return this._findByIndex(index, value, cb)
}
}

KS.Collection.prototype._simpleFind = function(id, cb){
KS.Collection.prototype.all = function(){
var self = this;
return new Promise(function(resolve, reject){
localforage.getItem(self.brainName)
.then(function(brain){
return Promise.all(_.map(brain.ids, function(id){
return localforage.getItem(self.docBaseName + id)
}))
}).then(function(docs){
if (cb){ cb(docs) }
resolve(docs)
}).catch(function(error){
if (cb) { cb(null, error) }
reject(error)
})
})
}

KS.Collection.prototype._findById = function(id, cb){
var self = this;
return new Promise(function(resolve, reject){
localforage.getItem(self.docBaseName + id)
Expand All @@ -164,7 +187,7 @@ KS.Collection.prototype._simpleFind = function(id, cb){
})
}

KS.Collection.prototype._indexFind = function(index, value, cb){
KS.Collection.prototype._findByIndex = function(index, value, cb){
var self = this;


Expand Down Expand Up @@ -283,13 +306,11 @@ KS.Collection.prototype.update = function(newDoc){
return indexName
}
}))
console.log(indexNamesToUpdate)
return Promise.all(_.map(self.indexNames, function(indexName){
return localforage.getItem(self.indexBaseName + indexName)
}))
})
.then(function(indexes){
console.log(indexes)
return self._removeFromIndexes(indexes, oldDoc)
})
.then(function(){
Expand Down

0 comments on commit edf0397

Please sign in to comment.