Permalink
Browse files

feat(project): handle predefined values

  • Loading branch information...
jeremyvergnas committed Jul 22, 2016
1 parent f1b3823 commit 710b1694e1225278a751484eca33e5f4bbad64c8
Showing with 83 additions and 7 deletions.
  1. +5 −2 src/criteriaBuilder.js
  2. +78 −5 src/filter.js
@@ -25,12 +25,15 @@ export class CriteriaBuilder {

parseOperator(filter) {
switch(filter.operator) {
case 'equals':
return filter.value;
case 'between':
return this.parseBetween(filter);
case 'in':
return this.toArray(filter.value);
case 'not-in':
filter.operator = 'not';
case '!':
// not-in
filter.operator = '!';
filter.value = this.toArray(filter.value);
default:
return {[filter.operator]: filter.value};
@@ -27,7 +27,7 @@ export class Filter extends CriteriaBuilder {
{name: 'equals', value: 'equals'},
{name: 'not equals', value: 'not'},
{name: 'in', value: 'in'},
{name: 'not in', value: 'not-in'},
{name: 'not in', value: '!'},
{name: 'contains', value: 'contains'},
{name: 'begins with', value: 'startsWith'},
{name: 'ends with', value: 'endsWith'},
@@ -60,15 +60,83 @@ export class Filter extends CriteriaBuilder {
this.fieldElement.options = this.columns;
this.valueElement.type = this.columns[0].type || 'string'; // set the initial valueElement `type`

this.create();
// Do we need to set pre-defined values for the filter?
if (Object.keys(this.criteria.where).length) {
this.parseCriteria(this.criteria.where);
}
else {
this.create();
}
}

parseCriteria(criteriaWhere) {
let data = {};

if (criteriaWhere.or) {
criteriaWhere.or.forEach((block, i) => {
Object.keys(block).forEach((field) => {
data = Object.assign(this.buildFieldData(block[field]), {field: field});
if (!this.filters[i]) {
// create a new block
this.create(undefined, data);
}
else {
// Add AND condition to the current block
this.create(i, data);
}
});
});
}
else {
Object.keys(criteriaWhere).forEach((field, i) => {
data = Object.assign(this.buildFieldData(criteriaWhere[field]), {field: field});
if (i === 0) {
// create the first block
this.create(undefined, data);
}
else {
// Add AND condition to the first block
this.create(0, data);
}
});
}
}

create(blockIndex) {
buildFieldData(field) {
if (typeof field === 'string') {
return {operator: 'equals', value: field};
}
else if (Array.isArray(field)) {
return {operator: 'in', value: field.join()};
}
else {
if (Object.keys(field).length > 1) {
return {operator: 'between', value: field['>='], between: field['<=']};
}
else {
let name = Object.keys(field)[0];

if (Array.isArray(field[name])) {
// not-in
return {operator: '!', value: field[name].join()};
}
else {
let operator = Object.keys(field)[0];

return {operator: operator, value: field[operator]};
}
}
}

return {operator: null, value: null};
}

create(blockIndex, data) {
let filter = {
field : this.fieldElement,
operator: this.operatorElement,
value : Object.create(this.valueElement),
data : {}
data : data || {}
};

// create filter
@@ -136,7 +204,12 @@ export class Filter extends CriteriaBuilder {
let repositories = this.entity.getRepository().entityManager.repositories;
for (let association in metaData.associations) {
let entityName = metaData.associations[association].entity;
let repoData = repositories[entityName].getMeta().metadata.types; // no `asObject` method available

if (!repositories[entityName]) {
return;
}

let repoData = repositories[entityName].getMeta().metadata.types; // no `asObject` method available

if (!repoData) {
continue;

0 comments on commit 710b169

Please sign in to comment.