#166 filter by mongo ObjectID

This commit is contained in:
Jan Prochazka 2021-09-16 12:43:51 +02:00
parent 90262a0318
commit 81fbed7a7f
3 changed files with 36 additions and 10 deletions

View File

@ -28,6 +28,20 @@ const numberTestCondition = () => value => ({
],
});
const objectIdTestCondition = () => value => ({
$or: [
{
__placeholder__: {
$regex: `.*${value}.*`,
$options: 'i',
},
},
{
__placeholder__: { $oid: value },
},
],
});
const testCondition = (operator, value) => () => ({
__placeholder__: {
[operator]: value,
@ -64,9 +78,12 @@ const createParser = () => {
.map(Number)
.desc('number'),
objectid: () => token(P.regexp(/[0-9a-f]{24}/)).desc('ObjectId'),
noQuotedString: () => P.regexp(/[^\s^,^'^"]+/).desc('string unquoted'),
value: r => P.alt(r.string1, r.string2, r.number, r.noQuotedString),
value: r => P.alt(r.objectid, r.string1, r.string2, r.number, r.noQuotedString),
valueTestObjectId: r => r.objectid.map(objectIdTestCondition()),
valueTestNum: r => r.number.map(numberTestCondition()),
valueTest: r => r.value.map(regexCondition('.*#VALUE#.*')),
@ -108,6 +125,7 @@ const createParser = () => {
r.startsWithNot,
r.endsWithNot,
r.containsNot,
r.valueTestObjectId,
r.valueTestNum,
r.valueTest
).trim(whitespace),

View File

@ -26,6 +26,7 @@
if (!filters[uniqueName]) continue;
try {
const ast = parseFilter(filters[uniqueName], 'mongo');
// console.log('AST', ast);
const cond = _.cloneDeepWith(ast, expr => {
if (expr.__placeholder__) {
return {
@ -112,7 +113,6 @@
return response.data.count;
}
</script>
<script lang="ts">
@ -229,7 +229,6 @@
{ command: 'collectionDataGrid.export', tag: 'export' }
);
</script>
<LoadingDataGridCore

View File

@ -18,7 +18,7 @@ function readCursor(cursor, options) {
}
const mongoIdRegex = /^[0-9a-f]{24}$/;
function convertCondition(condition) {
function convertConditionInternal(condition) {
if (condition && _.isString(condition._id) && condition._id.match(mongoIdRegex)) {
return {
_id: ObjectId(condition._id),
@ -27,6 +27,12 @@ function convertCondition(condition) {
return condition;
}
function convertConditionUser(condition) {
return _.cloneDeepWith(condition, (x) => {
if (x && x.$oid) return ObjectId(x.$oid);
});
}
function findArrayResult(resValue) {
if (!_.isPlainObject(resValue)) return null;
const arrays = _.values(resValue).filter((x) => _.isArray(x));
@ -191,10 +197,11 @@ const driver = {
try {
const collection = pool.__getDatabase().collection(options.pureName);
if (options.countDocuments) {
const count = await collection.countDocuments(options.condition || {});
const count = await collection.countDocuments(convertConditionUser(options.condition) || {});
return { count };
} else {
let cursor = await collection.find(options.condition || {});
// console.log('options.condition', JSON.stringify(options.condition, undefined, 2));
let cursor = await collection.find(convertConditionUser(options.condition) || {});
if (options.sort) cursor = cursor.sort(options.sort);
if (options.skip) cursor = cursor.skip(options.skip);
if (options.limit) cursor = cursor.limit(options.limit);
@ -230,22 +237,24 @@ const driver = {
...update.document,
...update.fields,
};
const doc = await collection.findOne(convertCondition(update.condition));
const doc = await collection.findOne(convertConditionInternal(update.condition));
if (doc) {
const resdoc = await collection.replaceOne(convertCondition(update.condition), {
const resdoc = await collection.replaceOne(convertConditionInternal(update.condition), {
...document,
_id: doc._id,
});
res.replaced.push(resdoc._id);
}
} else {
const resdoc = await collection.updateOne(convertCondition(update.condition), { $set: update.fields });
const resdoc = await collection.updateOne(convertConditionInternal(update.condition), {
$set: update.fields,
});
res.updated.push(resdoc._id);
}
}
for (const del of changeSet.deletes) {
const collection = db.collection(del.pureName);
const resdoc = await collection.deleteOne(convertCondition(del.condition));
const resdoc = await collection.deleteOne(convertConditionInternal(del.condition));
res.deleted.push(resdoc._id);
}
return res;