mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-18 04:26:01 +00:00
Enhance binary data handling by converting hex strings to base64 in filter parser and updating MongoDB driver for BinData support
This commit is contained in:
@@ -51,6 +51,10 @@ function findArrayResult(resValue) {
|
||||
return null;
|
||||
}
|
||||
|
||||
function BinData(_subType, base64) {
|
||||
return Buffer.from(base64, 'base64');
|
||||
}
|
||||
|
||||
async function getScriptableDb(dbhan) {
|
||||
const db = dbhan.getDatabase();
|
||||
db.getCollection = (name) => db.collection(name);
|
||||
@@ -156,9 +160,9 @@ const driver = {
|
||||
// return printable;
|
||||
// }
|
||||
let func;
|
||||
func = eval(`(db,ObjectId) => ${sql}`);
|
||||
func = eval(`(db,ObjectId,BinData) => ${sql}`);
|
||||
const db = await getScriptableDb(dbhan);
|
||||
const res = func(db, ObjectId.createFromHexString);
|
||||
const res = func(db, ObjectId.createFromHexString, BinData);
|
||||
if (isPromise(res)) await res;
|
||||
},
|
||||
async operation(dbhan, operation, options) {
|
||||
@@ -285,7 +289,7 @@ const driver = {
|
||||
} else {
|
||||
let func;
|
||||
try {
|
||||
func = eval(`(db,ObjectId) => ${sql}`);
|
||||
func = eval(`(db,ObjectId,BinData) => ${sql}`);
|
||||
} catch (err) {
|
||||
options.info({
|
||||
message: 'Error compiling expression: ' + err.message,
|
||||
@@ -299,7 +303,7 @@ const driver = {
|
||||
|
||||
let exprValue;
|
||||
try {
|
||||
exprValue = func(db, ObjectId.createFromHexString);
|
||||
exprValue = func(db, ObjectId.createFromHexString, BinData);
|
||||
} catch (err) {
|
||||
options.info({
|
||||
message: 'Error evaluating expression: ' + err.message,
|
||||
@@ -411,9 +415,9 @@ const driver = {
|
||||
// highWaterMark: 100,
|
||||
// });
|
||||
|
||||
func = eval(`(db,ObjectId) => ${sql}`);
|
||||
func = eval(`(db,ObjectId,BinData) => ${sql}`);
|
||||
const db = await getScriptableDb(dbhan);
|
||||
exprValue = func(db, ObjectId.createFromHexString);
|
||||
exprValue = func(db, ObjectId.createFromHexString, BinData);
|
||||
|
||||
const pass = new stream.PassThrough({
|
||||
objectMode: true,
|
||||
|
||||
@@ -15,7 +15,10 @@ function mongoReplacer(key, value) {
|
||||
function jsonStringifyWithObjectId(obj) {
|
||||
return JSON.stringify(obj, mongoReplacer, 2)
|
||||
.replace(/\{\s*\"\$oid\"\s*\:\s*\"([0-9a-f]+)\"\s*\}/g, (m, id) => `ObjectId("${id}")`)
|
||||
.replace(/\{\s*\"\$bigint\"\s*\:\s*\"([0-9]+)\"\s*\}/g, (m, num) => `${num}n`);
|
||||
.replace(/\{\s*\"\$bigint\"\s*\:\s*\"([0-9]+)\"\s*\}/g, (m, num) => `${num}n`)
|
||||
.replace(/\{\s*"\$binary"\s*:\s*\{\s*"base64"\s*:\s*"([^"]+)"(?:\s*,\s*"subType"\s*:\s*"([0-9a-fA-F]{2})")?\s*\}\s*\}/g, (m, base64, subType) => {
|
||||
return `BinData(${parseInt(subType || "00", 16)}, "${base64}")`;
|
||||
});
|
||||
}
|
||||
|
||||
/** @type {import('dbgate-types').SqlDialect} */
|
||||
@@ -129,7 +132,7 @@ const driver = {
|
||||
|
||||
getCollectionExportQueryScript(collection, condition, sort) {
|
||||
return `db.getCollection('${collection}')
|
||||
.find(${JSON.stringify(convertToMongoCondition(condition) || {})})
|
||||
.find(${jsonStringifyWithObjectId(convertToMongoCondition(condition) || {})})
|
||||
.sort(${JSON.stringify(convertToMongoSort(sort) || {})})`;
|
||||
},
|
||||
getCollectionExportQueryJson(collection, condition, sort) {
|
||||
@@ -148,6 +151,7 @@ const driver = {
|
||||
parseJsonObject: true,
|
||||
parseObjectIdAsDollar: true,
|
||||
parseDateAsDollar: true,
|
||||
parseHexAsBuffer: true,
|
||||
|
||||
explicitDataType: true,
|
||||
supportNumberType: true,
|
||||
|
||||
Reference in New Issue
Block a user