mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-03-18 18:40:58 +01:00
@@ -26,3 +26,19 @@ helpers.mergeBatch = function (batchData, start, stop, sort) {
|
||||
} while (item && (result.length < (stop - start + 1) || stop === -1));
|
||||
return result;
|
||||
};
|
||||
|
||||
helpers.globToRegex = function (match) {
|
||||
if (!match) {
|
||||
return '^.*$';
|
||||
}
|
||||
let _match = match.replace(/[.+^${}()|[\]\\]/g, '\\$&');
|
||||
_match = _match.replace(/\*/g, '.*').replace(/\?/g, '.');
|
||||
|
||||
if (!match.startsWith('*')) {
|
||||
_match = '^' + _match;
|
||||
}
|
||||
if (!match.endsWith('*')) {
|
||||
_match = _match + '$';
|
||||
}
|
||||
return _match;
|
||||
};
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
const helpers = module.exports;
|
||||
const utils = require('../../utils');
|
||||
|
||||
helpers.noop = function () {};
|
||||
|
||||
@@ -50,20 +49,3 @@ helpers.valueToString = function (value) {
|
||||
return String(value);
|
||||
};
|
||||
|
||||
helpers.buildMatchQuery = function (match) {
|
||||
let _match = match;
|
||||
if (match.startsWith('*')) {
|
||||
_match = _match.substring(1);
|
||||
}
|
||||
if (match.endsWith('*')) {
|
||||
_match = _match.substring(0, _match.length - 1);
|
||||
}
|
||||
_match = utils.escapeRegexChars(_match);
|
||||
if (!match.startsWith('*')) {
|
||||
_match = `^${_match}`;
|
||||
}
|
||||
if (!match.endsWith('*')) {
|
||||
_match += '$';
|
||||
}
|
||||
return _match;
|
||||
};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
module.exports = function (module) {
|
||||
const helpers = require('./helpers');
|
||||
const dbHelpers = require('../helpers');
|
||||
module.flushdb = async function () {
|
||||
await module.client.dropDatabase();
|
||||
};
|
||||
@@ -39,7 +39,7 @@ module.exports = function (module) {
|
||||
};
|
||||
|
||||
module.scan = async function (params) {
|
||||
const match = helpers.buildMatchQuery(params.match);
|
||||
const match = dbHelpers.globToRegex(params.match);
|
||||
return await module.client.collection('objects').distinct(
|
||||
'_key', { _key: { $regex: new RegExp(match) } }
|
||||
);
|
||||
|
||||
@@ -543,7 +543,7 @@ module.exports = function (module) {
|
||||
project.score = 1;
|
||||
}
|
||||
|
||||
const match = helpers.buildMatchQuery(params.match);
|
||||
const match = dbHelpers.globToRegex(params.match);
|
||||
let regex;
|
||||
try {
|
||||
regex = new RegExp(match);
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
module.exports = function (module) {
|
||||
const helpers = require('./helpers');
|
||||
const dbHelpers = require('../helpers');
|
||||
|
||||
module.flushdb = async function () {
|
||||
await module.pool.query(`DROP SCHEMA "public" CASCADE`);
|
||||
@@ -84,20 +85,14 @@ module.exports = function (module) {
|
||||
};
|
||||
|
||||
module.scan = async function (params) {
|
||||
let { match } = params;
|
||||
if (match.startsWith('*')) {
|
||||
match = `%${match.substring(1)}`;
|
||||
}
|
||||
if (match.endsWith('*')) {
|
||||
match = `${match.substring(0, match.length - 1)}%`;
|
||||
}
|
||||
const regex = dbHelpers.globToRegex(params.match);
|
||||
|
||||
const res = await module.pool.query({
|
||||
text: `
|
||||
SELECT o."_key"
|
||||
FROM "legacy_object_live" o
|
||||
WHERE o."_key" LIKE $1`,
|
||||
values: [match],
|
||||
WHERE o."_key" ~ $1`,
|
||||
values: [regex],
|
||||
});
|
||||
|
||||
return res.rows.map(r => r._key);
|
||||
|
||||
Reference in New Issue
Block a user