diff --git a/lib/buildWhere.js b/lib/buildWhere.js index 82ff116..d1624cf 100644 --- a/lib/buildWhere.js +++ b/lib/buildWhere.js @@ -7,11 +7,17 @@ function buildWhere(modelName, where) { const model = self._models[modelName]; const filter = {}; const keys = Object.keys(where); - // Simple condition with only 1 property if (keys.length === 1 && model.properties.hasOwnProperty(keys[0])) { + // Simple condition with only 1 property filter.type = 'terms'; const palantirField = self.getPalantirPropertyName(modelName, keys[0]); filter.terms = {terms: [where[keys[0]]], field: `${palantirField}.raw`}; + } else if (keys.length === 1 && (keys[0] === 'and' || keys[0] === 'or')) { + // a logical condition with collection AND or OR + const logicalOperator = keys[0]; + filter.type = logicalOperator; + const innerFilters = _.map(where[logicalOperator], lbCondition => buildWhere.call(self, modelName, lbCondition)); + filter[logicalOperator] = innerFilters; } return filter; } diff --git a/test/palantir-connector.test.js b/test/palantir-connector.test.js index 033066c..4c5df3a 100644 --- a/test/palantir-connector.test.js +++ b/test/palantir-connector.test.js @@ -4,17 +4,17 @@ describe('Palantir connector tests', () => { const ds = global.getDataSource(); const Project = ds.define('Project', { - id: {type: String, id: true, palantir: {primaryKey: true, propertyName: 'project_uid'}}, - title: {type: String, palantir: {unique: true, propertyName: 'project'}}, - objectTypeId: {type: String}, - team: {type: String}, - projectId: {type: Number, palantir: {propertyName: 'project_id'}} - }, - { - palantir: { - objectTypeId: process.env.PALANTIR_OBJECT_TYPE - } - }); + id: {type: String, id: true, palantir: {primaryKey: true, propertyName: 'project_uid'}}, + title: {type: String, palantir: {unique: true, propertyName: 'project'}}, + objectTypeId: {type: String}, + team: {type: String}, + projectId: {type: Number, palantir: {propertyName: 'project_id'}} + }, + { + palantir: { + objectTypeId: process.env.PALANTIR_OBJECT_TYPE + } + }); const testProjects = [{ title: 'Test-Project-10', @@ -32,7 +32,10 @@ describe('Palantir connector tests', () => { }); it('should get object back', async () => { - const expectedResult = Object.assign({}, testProjects[0], {id: projectId, objectTypeId: process.env.PALANTIR_OBJECT_TYPE}); + const expectedResult = Object.assign({}, testProjects[0], { + id: projectId, + objectTypeId: process.env.PALANTIR_OBJECT_TYPE + }); const result = await Project.findById(projectId); expect(result.__data).to.eql(expectedResult); }); @@ -47,4 +50,16 @@ describe('Palantir connector tests', () => { const findObjectResult = await Project.find({where: {team: 'Bioprinting'}}); expect(findObjectResult).not.to.be.empty; }); + + it.only('should get objects by 2 column where criteria', async () => { + const findObjectResult = await Project.find({ + where: { + and: [ + {team: 'Bioprinting'}, + {title: '3D-HEAL_Cortical_Profiling-Stemonix'} + ] + } + }); + expect(findObjectResult).not.to.be.empty; + }); });