This repository has been archived by the owner on Nov 14, 2017. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
032e4ed
commit 645194b
Showing
8 changed files
with
187 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
language: node_js | ||
env: | ||
- NODE_ENV="travis" | ||
node_js: | ||
- "0.8" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
var querystring = require('querystring'); | ||
|
||
|
||
var ChainViewAble = function (Model, view_name) { | ||
var self = this, | ||
view_options = {reduce: false, include_docs: true}, | ||
Model = Model, | ||
view_name = view_name; | ||
|
||
function add_option (name, option) { | ||
view_options[name] = option; | ||
} | ||
|
||
function execute(cb) { | ||
Model.view(view_name, view_options, cb); | ||
} | ||
|
||
function check_cb(cb) { | ||
if (cb) { | ||
return execute(cb); | ||
} | ||
|
||
return self; | ||
} | ||
|
||
this.key = function (inputs, cb) { | ||
add_option('key',inputs); | ||
|
||
return check_cb(cb); | ||
} | ||
|
||
this.startkey = function (inputs, cb) { | ||
add_option('startkey',inputs); | ||
|
||
return check_cb(cb); | ||
} | ||
|
||
this.endkey = function (inputs, cb) { | ||
add_option('endkey',inputs); | ||
return check_cb(cb); | ||
} | ||
|
||
this.limit = function (limit, cb) { | ||
add_option('limit',limit); | ||
return check_cb(cb); | ||
} | ||
|
||
this.skip = function (skip, cb) { | ||
add_option('skip',skip); | ||
return check_cb(cb); | ||
} | ||
|
||
this.descending = function (cb) { | ||
add_option('descending',true); | ||
return check_cb(cb); | ||
} | ||
|
||
this.dont_include_docs = function (cb) { | ||
add_option('include_docs', 'false'); | ||
return check_cb(cb); | ||
} | ||
|
||
|
||
this.stale = function (cb) { | ||
add_option('stale','ok'); | ||
return check_cb(cb); | ||
} | ||
} | ||
|
||
module.exports = ChainViewAble; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
var db = require('./spec_helper').db, | ||
Model = require('../lib/index'); | ||
|
||
describe("Custom views", function () { | ||
|
||
before(function (done) { | ||
var RankingThing = Model.define("RankingThing",{rank: Number, name: String}); | ||
|
||
RankingThing.addView('ByRankAndName',{ | ||
map: function (doc) { | ||
if (doc.model_type === 'RankingThing') { | ||
emit([doc.rank, doc.name],1); | ||
} | ||
} | ||
}); | ||
|
||
RankingThing.addView('ByName',{ | ||
map: function (doc) { | ||
if (doc.model_type === 'RankingThing') { | ||
emit(doc.name,1); | ||
} | ||
} | ||
}); | ||
|
||
|
||
|
||
Model.load(function () { | ||
|
||
RankingThing.create({name: "Thing1", rank: 5}).save(); | ||
RankingThing.create({name: "Thing2", rank: 1}).save(); | ||
RankingThing.create({name: "Thing3", rank: 3}).save(); | ||
RankingThing.create({name: "Thing4", rank: 5}).save(); | ||
RankingThing.create({name: "Thing5", rank: 2}).save(); | ||
RankingThing.create({name: "Thing6", rank: 3}).save(); | ||
RankingThing.create({name: "Thing7", rank: 5}).save(function () { | ||
done(); | ||
|
||
}); | ||
}); | ||
|
||
}); | ||
|
||
it("Should have chainable start and end key", function (done) { | ||
var RankingThing = Model('RankingThing'); | ||
RankingThing.view('ByRankAndName').startkey([2, "Thing"]).endkey([3, {}], function (err, things) { | ||
things.length.should.equal(3); | ||
done(); | ||
}); | ||
}); | ||
|
||
it("Should descend", function (done) { | ||
var RankingThing = Model('RankingThing'); | ||
RankingThing.view('ByName').descending(function (err, things) { | ||
things[0].name.should === "Thing1" | ||
things[3].name.should === "Thing4" | ||
things[6].name.should === "Thing7" | ||
done(); | ||
}); | ||
}); | ||
|
||
it("Should limit", function (done) { | ||
var RankingThing = Model('RankingThing'); | ||
RankingThing.view('ByName').limit(2, function (err, things) { | ||
things.length.should.equal(2); | ||
done(); | ||
}); | ||
}); | ||
|
||
it("Should skip", function (done) { | ||
var RankingThing = Model('RankingThing'); | ||
RankingThing.view('ByName').skip(2, function (err, things) { | ||
things.length.should.equal(5); | ||
things[0].name.should === "Thing3" | ||
done(); | ||
}); | ||
}); | ||
}); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters