Skip to content

Commit

Permalink
feat: added multiple and/or conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
dkrantsberg committed Jul 9, 2020
1 parent b5434b4 commit e43aa48
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
8 changes: 7 additions & 1 deletion lib/buildWhere.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
39 changes: 27 additions & 12 deletions test/palantir-connector.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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);
});
Expand All @@ -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;
});
});

0 comments on commit e43aa48

Please sign in to comment.