Skip to content

Commit a854a0d

Browse files
committed
feat: Implement 'Like' predicate in entity storage
This acts like the SQL 'LIKE' keyword, allowing partial string matches.
1 parent d733119 commit a854a0d

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

packages/backend/src/om/entitystorage/SQLES.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const { BaseES } = require("./BaseES");
2222
const APIError = require("../../api/APIError");
2323
const { Entity } = require("./Entity");
2424
const { WeakConstructorTrait } = require("../../traits/WeakConstructorTrait");
25-
const { And, Or, Eq, Predicate, Null, PredicateUtil } = require("../query/query");
25+
const { And, Or, Eq, Like, Null, Predicate, PredicateUtil } = require("../query/query");
2626
const { DB_WRITE } = require("../../services/database/consts");
2727

2828
class RawCondition extends AdvancedBase {
@@ -355,6 +355,22 @@ class SQLES extends BaseES {
355355

356356
return new RawCondition({ sql, values });
357357
}
358+
359+
if ( om_query instanceof Like ) {
360+
const key = om_query.key;
361+
let value = om_query.value;
362+
const prop = this.om.properties[key];
363+
364+
value = await prop.sql_reference(value);
365+
366+
const options = prop.descriptor.sql ?? {};
367+
const col_name = options.column_name ?? prop.name;
368+
369+
const sql = `${col_name} LIKE ?`;
370+
const values = [value];
371+
372+
return new RawCondition({ sql, values });
373+
}
358374
}
359375
}
360376
}

packages/backend/src/om/query/query.js

+10
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,15 @@ class Eq extends Predicate {
5050
}
5151
}
5252

53+
class Like extends Predicate {
54+
async check (entity) {
55+
// Convert SQL LIKE pattern to RegExp
56+
// TODO: Support escaping the pattern characters
57+
const regex = new RegExp(this.value.replaceAll('%', '.*').replaceAll('_', '.'), 'i');
58+
return regex.test(await entity.get(this.key));
59+
}
60+
}
61+
5362
Predicate.prototype.and = function (other) {
5463
return new And({ children: [this, other] });
5564
}
@@ -105,4 +114,5 @@ module.exports = {
105114
And,
106115
Or,
107116
Eq,
117+
Like,
108118
};

0 commit comments

Comments
 (0)