Skip to content

Commit

Permalink
feat: query with partition field
Browse files Browse the repository at this point in the history
  • Loading branch information
jannyHou committed Nov 20, 2019
1 parent 4ee685e commit b0e1c05
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 23 deletions.
14 changes: 13 additions & 1 deletion lib/cloudant.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,17 @@ Cloudant.prototype.ping = function(cb) {
}
};

Cloudant.prototype._getPartitionKey = function(mo, query, options) {
const pkFromOption = options && options.partitionKey;
debug('_getPartitionKey pkFromOption %s', pkFromOption);
const partitionField = mo.partitionKey;
const pkFromQuery = partitionField &&
query && query.selector && query.selector[partitionField];
debug('_getPartitionKey pkFromQuery %s', pkFromQuery);

return pkFromOption || pkFromQuery;
};

/**
* Apply find queries function.
* This function will perform a partitionedFind if `partitionKey`
Expand All @@ -151,7 +162,8 @@ Cloudant.prototype._findRecursive = function(
) {
const self = this;
const db = this.cloudant.use(self.getDbName(self));
const partitionKey = options && options.partitionKey;

const partitionKey = self._getPartitionKey(mo, query, options);
debug('Cloudant _findRecursive: partitionKey: %s', partitionKey);

const findCb = function(err, rst) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"fs-extra": "^8.1.0",
"lodash": "^4.17.11",
"loopback-connector": "^4.0.0",
"loopback-connector-couchdb2": "^1.5.2",
"loopback-connector-couchdb2": "github:strongloop/loopback-connector-couchdb2#partition/field",
"request": "^2.81.0",
"strong-globalize": "^5.0.0",
"uuid": "^3.3.3"
Expand Down
71 changes: 50 additions & 21 deletions test/partition.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,11 @@ describe('cloudant - partitioned db', () => {
});

context('findAll tests', ()=> {
it('find all records by partition key from option', (done) => {
before(function createModel(done) {
Product = db.define('Product', {
name: {type: String},
tag: {type: String},
city: {type: String, isPartitionKey: true},
}, {
forceId: false,
indexes: {
Expand All @@ -185,32 +186,60 @@ describe('cloudant - partitioned db', () => {
},
},
});
db.automigrate('Product', () => {
Product.create(SEED_DATA, function(err) {
db.automigrate('Product', (err)=> {
if (err) return done(err);
Product.create(SEED_DATA, done);
});
});

it('find all records by partition key from option', (done) => {
Product.find({where: {tag: 'food'}}, {partitionKey: 'toronto'},
(err, results) => {
if (err) return done(err);
Product.find({where: {tag: 'food'}}, {partitionKey: 'toronto'},
(err, results) => {
if (err) return done(err);
should.exist(results);
results.length.should.equal(2);
const resultTaggedFood = _.filter(results,
function(r) {
return r.tag === 'food';
});
resultTaggedFood.length.should.equal(2);
done();
should.exist(results);
results.length.should.equal(2);
const resultTaggedFood = _.filter(results,
function(r) {
return r.tag === 'food' && r.city === 'toronto';
});
resultTaggedFood.length.should.equal(2);
done();
});
});

it('find all records by partition key from property', (done) => {
Product.find({where: {tag: 'food', city: 'toronto'}},
(err, results) => {
if (err) return done(err);
should.exist(results);
results.length.should.equal(2);
const resultTaggedFood = _.filter(results,
function(r) {
return r.tag === 'food' && r.city === 'toronto';
});
resultTaggedFood.length.should.equal(2);
done();
});
});

it('partition key in options overrides property for find all', (done) => {
Product.find({where: {tag: 'food', city: 'toronto'}},
{partitionKey: 'london'}, (err, results) => {
if (err) return done(err);
should.exist(results);
results.length.should.equal(0);
done();
});
});
});
});
});

const SEED_DATA = [
{id: `toronto: ${uuid()}`, name: 'beer', tag: 'drink'},
{id: `toronto: ${uuid()}`, name: 'salad', tag: 'food'},
{id: `toronto: ${uuid()}`, name: 'soup', tag: 'food'},
{id: `london: ${uuid()}`, name: 'beer', tag: 'drink'},
{id: `london: ${uuid()}`, name: 'salad', tag: 'food'},
{id: `london: ${uuid()}`, name: 'salad', tag: 'food'},
{id: `toronto: ${uuid()}`, name: 'beer', tag: 'drink', city: 'toronto'},
{id: `toronto: ${uuid()}`, name: 'salad', tag: 'food', city: 'toronto'},
{id: `toronto: ${uuid()}`, name: 'soup', tag: 'food', city: 'toronto'},
{id: `london: ${uuid()}`, name: 'beer', tag: 'drink', city: 'london'},
{id: `london: ${uuid()}`, name: 'salad', tag: 'food', city: 'london'},
{id: `london: ${uuid()}`, name: 'salad', tag: 'food', city: 'london'},
{id: `unknown: ${uuid()}`, name: 'salad', tag: 'food', city: 'toronto'},
];

0 comments on commit b0e1c05

Please sign in to comment.