mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-05-01 22:33:59 +00:00
suport mongo ObjectId in data editor
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
<script lang="ts" context="module">
|
<script lang="ts" context="module">
|
||||||
function getEditedValue(value) {
|
function getEditedValue(value) {
|
||||||
if (value?.type == 'Buffer' && _.isArray(value.data)) return arrayToHexString(value.data);
|
if (value?.type == 'Buffer' && _.isArray(value.data)) return arrayToHexString(value.data);
|
||||||
|
if (value?.$oid) return `ObjectId("${value?.$oid}")`;
|
||||||
if (_.isPlainObject(value) || _.isArray(value)) return JSON.stringify(value);
|
if (_.isPlainObject(value) || _.isArray(value)) return JSON.stringify(value);
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
@@ -12,6 +13,13 @@
|
|||||||
data: hexStringToArray(newString),
|
data: hexStringToArray(newString),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
if (_.isString(newString)) {
|
||||||
|
const m = newString.match(/ObjectId\("([0-9a-f]{24})"\)/);
|
||||||
|
if (m) {
|
||||||
|
return { $oid: m[1] };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return newString;
|
return newString;
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
@@ -28,7 +36,7 @@
|
|||||||
export let onSetValue;
|
export let onSetValue;
|
||||||
export let width;
|
export let width;
|
||||||
export let cellValue;
|
export let cellValue;
|
||||||
export let fillParent=false;
|
export let fillParent = false;
|
||||||
|
|
||||||
let domEditor;
|
let domEditor;
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
showModal(EditJsonModal, {
|
showModal(EditJsonModal, {
|
||||||
json: rowData,
|
json: rowData,
|
||||||
onSave: value => {
|
onSave: value => {
|
||||||
if (rowData._id && value._id != rowData._id) {
|
if (grider.getRowStatus(rowIndex).status != 'inserted' && rowData._id && value._id != rowData._id) {
|
||||||
showModal(ErrorMessageModal, { message: '_id attribute cannot be changed' });
|
showModal(ErrorMessageModal, { message: '_id attribute cannot be changed' });
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ function readCursor(cursor, options) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function convertCondition(condition) {
|
function convertObjectId(condition) {
|
||||||
return _.cloneDeepWith(condition, (x) => {
|
return _.cloneDeepWith(condition, (x) => {
|
||||||
if (x && x.$oid) return ObjectId(x.$oid);
|
if (x && x.$oid) return ObjectId(x.$oid);
|
||||||
});
|
});
|
||||||
@@ -205,11 +205,11 @@ const driver = {
|
|||||||
try {
|
try {
|
||||||
const collection = pool.__getDatabase().collection(options.pureName);
|
const collection = pool.__getDatabase().collection(options.pureName);
|
||||||
if (options.countDocuments) {
|
if (options.countDocuments) {
|
||||||
const count = await collection.countDocuments(convertCondition(options.condition) || {});
|
const count = await collection.countDocuments(convertObjectId(options.condition) || {});
|
||||||
return { count };
|
return { count };
|
||||||
} else {
|
} else {
|
||||||
// console.log('options.condition', JSON.stringify(options.condition, undefined, 2));
|
// console.log('options.condition', JSON.stringify(options.condition, undefined, 2));
|
||||||
let cursor = await collection.find(convertCondition(options.condition) || {});
|
let cursor = await collection.find(convertObjectId(options.condition) || {});
|
||||||
if (options.sort) cursor = cursor.sort(options.sort);
|
if (options.sort) cursor = cursor.sort(options.sort);
|
||||||
if (options.skip) cursor = cursor.skip(options.skip);
|
if (options.skip) cursor = cursor.skip(options.skip);
|
||||||
if (options.limit) cursor = cursor.limit(options.limit);
|
if (options.limit) cursor = cursor.limit(options.limit);
|
||||||
@@ -235,7 +235,7 @@ const driver = {
|
|||||||
...insert.document,
|
...insert.document,
|
||||||
...insert.fields,
|
...insert.fields,
|
||||||
};
|
};
|
||||||
const resdoc = await collection.insert(document);
|
const resdoc = await collection.insert(convertObjectId(document));
|
||||||
res.inserted.push(resdoc._id);
|
res.inserted.push(resdoc._id);
|
||||||
}
|
}
|
||||||
for (const update of changeSet.updates) {
|
for (const update of changeSet.updates) {
|
||||||
@@ -245,24 +245,24 @@ const driver = {
|
|||||||
...update.document,
|
...update.document,
|
||||||
...update.fields,
|
...update.fields,
|
||||||
};
|
};
|
||||||
const doc = await collection.findOne(convertCondition(update.condition));
|
const doc = await collection.findOne(convertObjectId(update.condition));
|
||||||
if (doc) {
|
if (doc) {
|
||||||
const resdoc = await collection.replaceOne(convertCondition(update.condition), {
|
const resdoc = await collection.replaceOne(convertObjectId(update.condition), {
|
||||||
...document,
|
...convertObjectId(document),
|
||||||
_id: doc._id,
|
_id: doc._id,
|
||||||
});
|
});
|
||||||
res.replaced.push(resdoc._id);
|
res.replaced.push(resdoc._id);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
const resdoc = await collection.updateOne(convertCondition(update.condition), {
|
const resdoc = await collection.updateOne(convertObjectId(update.condition), {
|
||||||
$set: update.fields,
|
$set: convertObjectId(update.fields),
|
||||||
});
|
});
|
||||||
res.updated.push(resdoc._id);
|
res.updated.push(resdoc._id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (const del of changeSet.deletes) {
|
for (const del of changeSet.deletes) {
|
||||||
const collection = db.collection(del.pureName);
|
const collection = db.collection(del.pureName);
|
||||||
const resdoc = await collection.deleteOne(convertCondition(del.condition));
|
const resdoc = await collection.deleteOne(convertObjectId(del.condition));
|
||||||
res.deleted.push(resdoc._id);
|
res.deleted.push(resdoc._id);
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
|
|||||||
@@ -3,11 +3,11 @@ const { driverBase } = global.DBGATE_TOOLS;
|
|||||||
const Dumper = require('./Dumper');
|
const Dumper = require('./Dumper');
|
||||||
const { mongoSplitterOptions } = require('dbgate-query-splitter/lib/options');
|
const { mongoSplitterOptions } = require('dbgate-query-splitter/lib/options');
|
||||||
|
|
||||||
function getConditionPreview(condition) {
|
function jsonStringifyWithObjectId(obj) {
|
||||||
if (condition && condition._id && condition._id.$oid) {
|
return JSON.stringify(obj, undefined, 2).replace(
|
||||||
return `{ _id: ObjectId('${condition._id.$oid}') }`;
|
/\{\s*\"\$oid\"\s*\:\s*\"([0-9a-f]+)\"\s*\}/g,
|
||||||
}
|
(m, id) => `ObjectId("${id}")`
|
||||||
return JSON.stringify(condition);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @type {import('dbgate-types').SqlDialect} */
|
/** @type {import('dbgate-types').SqlDialect} */
|
||||||
@@ -47,37 +47,29 @@ const driver = {
|
|||||||
getCollectionUpdateScript(changeSet) {
|
getCollectionUpdateScript(changeSet) {
|
||||||
let res = '';
|
let res = '';
|
||||||
for (const insert of changeSet.inserts) {
|
for (const insert of changeSet.inserts) {
|
||||||
res += `db.${insert.pureName}.insert(${JSON.stringify(
|
res += `db.${insert.pureName}.insert(${jsonStringifyWithObjectId({
|
||||||
{
|
...insert.document,
|
||||||
...insert.document,
|
...insert.fields,
|
||||||
...insert.fields,
|
})});\n`;
|
||||||
},
|
|
||||||
undefined,
|
|
||||||
2
|
|
||||||
)});\n`;
|
|
||||||
}
|
}
|
||||||
for (const update of changeSet.updates) {
|
for (const update of changeSet.updates) {
|
||||||
if (update.document) {
|
if (update.document) {
|
||||||
res += `db.${update.pureName}.replaceOne(${getConditionPreview(update.condition)}, ${JSON.stringify(
|
res += `db.${update.pureName}.replaceOne(${jsonStringifyWithObjectId(
|
||||||
{
|
update.condition
|
||||||
...update.document,
|
)}, ${jsonStringifyWithObjectId({
|
||||||
...update.fields,
|
...update.document,
|
||||||
},
|
...update.fields,
|
||||||
undefined,
|
})});\n`;
|
||||||
2
|
|
||||||
)});\n`;
|
|
||||||
} else {
|
} else {
|
||||||
res += `db.${update.pureName}.updateOne(${getConditionPreview(update.condition)}, ${JSON.stringify(
|
res += `db.${update.pureName}.updateOne(${jsonStringifyWithObjectId(
|
||||||
{
|
update.condition
|
||||||
$set: update.fields,
|
)}, ${jsonStringifyWithObjectId({
|
||||||
},
|
$set: update.fields,
|
||||||
undefined,
|
})});\n`;
|
||||||
2
|
|
||||||
)});\n`;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (const del of changeSet.deletes) {
|
for (const del of changeSet.deletes) {
|
||||||
res += `db.${del.pureName}.deleteOne(${getConditionPreview(del.condition)});\n`;
|
res += `db.${del.pureName}.deleteOne(${jsonStringifyWithObjectId(del.condition)});\n`;
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user