server-ts: Port services/search/expressions/property_comparison

This commit is contained in:
Elian Doran 2024-02-18 01:50:16 +02:00
parent 533a597a5c
commit 29b3fb3646
No known key found for this signature in database
2 changed files with 21 additions and 11 deletions

View File

@ -1,14 +1,14 @@
"use strict"; "use strict";
const Expression = require('./expression'); import Expression = require('./expression');
const NoteSet = require('../note_set'); import NoteSet = require('../note_set');
const buildComparator = require('../services/build_comparator'); import buildComparator = require('../services/build_comparator');
/** /**
* Search string is lower cased for case-insensitive comparison. But when retrieving properties, * Search string is lower cased for case-insensitive comparison. But when retrieving properties,
* we need the case-sensitive form, so we have this translation object. * we need the case-sensitive form, so we have this translation object.
*/ */
const PROP_MAPPING = { const PROP_MAPPING: Record<string, string> = {
"noteid": "noteId", "noteid": "noteId",
"title": "title", "title": "title",
"type": "type", "type": "type",
@ -36,12 +36,22 @@ const PROP_MAPPING = {
"revisioncount": "revisionCount" "revisioncount": "revisionCount"
}; };
interface SearchContext {
dbLoadNeeded?: boolean;
}
class PropertyComparisonExp extends Expression { class PropertyComparisonExp extends Expression {
static isProperty(name) {
private propertyName: string;
private operator: string;
private comparedValue: string;
private comparator;
static isProperty(name: string) {
return name in PROP_MAPPING; return name in PROP_MAPPING;
} }
constructor(searchContext, propertyName, operator, comparedValue) { constructor(searchContext: SearchContext, propertyName: string, operator: string, comparedValue: string) {
super(); super();
this.propertyName = PROP_MAPPING[propertyName]; this.propertyName = PROP_MAPPING[propertyName];
@ -54,11 +64,11 @@ class PropertyComparisonExp extends Expression {
} }
} }
execute(inputNoteSet, executionContext, searchContext) { execute(inputNoteSet: NoteSet, executionContext: {}, searchContext: SearchContext) {
const resNoteSet = new NoteSet(); const resNoteSet = new NoteSet();
for (const note of inputNoteSet.notes) { for (const note of inputNoteSet.notes) {
let value = note[this.propertyName]; let value = (note as any)[this.propertyName];
if (value !== undefined && value !== null && typeof value !== 'string') { if (value !== undefined && value !== null && typeof value !== 'string') {
value = value.toString(); value = value.toString();
@ -68,7 +78,7 @@ class PropertyComparisonExp extends Expression {
value = value.toLowerCase(); value = value.toLowerCase();
} }
if (this.comparator(value)) { if (this.comparator && this.comparator(value)) {
resNoteSet.add(note); resNoteSet.add(note);
} }
} }
@ -77,4 +87,4 @@ class PropertyComparisonExp extends Expression {
} }
} }
module.exports = PropertyComparisonExp; export = PropertyComparisonExp;

View File

@ -8,7 +8,7 @@ const ChildOfExp = require('../expressions/child_of');
const DescendantOfExp = require('../expressions/descendant_of'); const DescendantOfExp = require('../expressions/descendant_of');
const ParentOfExp = require('../expressions/parent_of'); const ParentOfExp = require('../expressions/parent_of');
const RelationWhereExp = require('../expressions/relation_where'); const RelationWhereExp = require('../expressions/relation_where');
const PropertyComparisonExp = require('../expressions/property_comparison.js'); const PropertyComparisonExp = require('../expressions/property_comparison');
const AttributeExistsExp = require('../expressions/attribute_exists'); const AttributeExistsExp = require('../expressions/attribute_exists');
const LabelComparisonExp = require('../expressions/label_comparison'); const LabelComparisonExp = require('../expressions/label_comparison');
const NoteFlatTextExp = require('../expressions/note_flat_text'); const NoteFlatTextExp = require('../expressions/note_flat_text');