Notes/src/services/search/expressions/property_comparison.js

74 lines
2.1 KiB
JavaScript
Raw Normal View History

"use strict";
const Expression = require('./expression');
const NoteSet = require('../note_set');
const buildComparator = require("../services/build_comparator.js");
2020-05-23 20:52:55 +02:00
/**
* Search string is lower cased for case insensitive comparison. But when retrieving properties
* we need case sensitive form so we have this translation object.
*/
const PROP_MAPPING = {
"noteid": "noteId",
"title": "title",
"type": "type",
"mime": "mime",
"isprotected": "isProtected",
"isarchived": "isArchived",
2020-05-23 20:52:55 +02:00
"datecreated": "dateCreated",
"datemodified": "dateModified",
"utcdatecreated": "utcDateCreated",
"utcdatemodified": "utcDateModified",
"parentcount": "parentCount",
"childrencount": "childrenCount",
"attributecount": "attributeCount",
"labelcount": "labelCount",
"relationcount": "relationCount",
"contentsize": "contentSize",
"notesize": "noteSize",
"revisioncount": "revisionCount"
2020-05-23 20:52:55 +02:00
};
class PropertyComparisonExp extends Expression {
2020-05-23 20:52:55 +02:00
static isProperty(name) {
return name in PROP_MAPPING;
}
constructor(searchContext, propertyName, operator, comparedValue) {
super();
2020-05-23 20:52:55 +02:00
this.propertyName = PROP_MAPPING[propertyName];
this.operator = operator; // for DEBUG mode
this.comparedValue = comparedValue; // for DEBUG mode
this.comparator = buildComparator(operator, comparedValue);
if (['contentsize', 'notesize', 'revisioncount'].includes(this.propertyName)) {
searchContext.dbLoadNeeded = true;
}
}
execute(inputNoteSet, executionContext) {
const resNoteSet = new NoteSet();
for (const note of inputNoteSet.notes) {
2020-05-23 20:52:55 +02:00
let value = note[this.propertyName];
2020-05-23 20:52:55 +02:00
if (value !== undefined && value !== null && typeof value !== 'string') {
value = value.toString();
}
if (value) {
value = value.toLowerCase();
}
2020-07-19 23:19:45 +02:00
if (this.comparator(value)) {
resNoteSet.add(note);
}
}
return resNoteSet;
}
}
module.exports = PropertyComparisonExp;