diff --git a/packages/java-shell/src/main/kotlin/com/mongodb/mongosh/MongoShellContext.kt b/packages/java-shell/src/main/kotlin/com/mongodb/mongosh/MongoShellContext.kt index c40073aa75..d201b893e7 100644 --- a/packages/java-shell/src/main/kotlin/com/mongodb/mongosh/MongoShellContext.kt +++ b/packages/java-shell/src/main/kotlin/com/mongodb/mongosh/MongoShellContext.kt @@ -35,6 +35,7 @@ internal class MongoShellContext(client: MongoClient) { private var ctx: Context? = Context.create() private val serviceProvider = JavaServiceProvider(client, this) private val shellEvaluator: Value + private val shellInternalState: Value private val bsonTypes: BsonTypes /** Java functions don't have js methods such as apply, bind, call etc. @@ -48,7 +49,7 @@ internal class MongoShellContext(client: MongoClient) { val context = ctx.getBindings("js") val global = context["_global"]!! context.removeMember("_global") - val shellInternalState = global.getMember("ShellInternalState").newInstance(serviceProvider) + shellInternalState = global.getMember("ShellInternalState").newInstance(serviceProvider) shellEvaluator = global.getMember("ShellEvaluator").newInstance(shellInternalState) val jsSymbol = context["Symbol"]!! shellInternalState.invokeMember("setCtx", context) @@ -253,12 +254,23 @@ internal class MongoShellContext(client: MongoClient) { } fun eval(@Language("js") script: String, name: String): Value { + updateDatabase() val originalEval = ProxyExecutable { args -> evalInner(args[0].asString(), name) } return shellEvaluator.invokeMember("customEval", originalEval, script) } + private fun updateDatabase() { + // graaljs does not allow to define property on top context, so we need to update internal state manually + val currentDb = evalInner("db") + val currentDbName = currentDb.invokeMember("getName").asString() + val stateDbName = shellInternalState["currentDb"]?.invokeMember("getName")?.asString() + if (currentDbName != stateDbName) { + shellInternalState.invokeMember("setDbFunc", currentDb) + } + } + fun toJsPromise(promise: Either): Value { return when (promise) { is Right -> evalInner("(v) => new Promise(((resolve) => resolve(v)))", "resolved_promise_script").execute(toJs(promise.value)) diff --git a/packages/java-shell/src/test/resources/db/showCollections.expected.txt b/packages/java-shell/src/test/resources/db/showCollections.expected.txt index 36f00453ea..c7d9c8a5f7 100644 --- a/packages/java-shell/src/test/resources/db/showCollections.expected.txt +++ b/packages/java-shell/src/test/resources/db/showCollections.expected.txt @@ -1 +1,2 @@ -ArrayResult \ No newline at end of file +{ "acknowledged": true, "insertedIds": [ ] } +[ "coll" ] \ No newline at end of file diff --git a/packages/java-shell/src/test/resources/db/showCollections.js b/packages/java-shell/src/test/resources/db/showCollections.js index d9840ccdf9..354161077d 100644 --- a/packages/java-shell/src/test/resources/db/showCollections.js +++ b/packages/java-shell/src/test/resources/db/showCollections.js @@ -1,2 +1,8 @@ -// command dontCheckValue +// before +use test_show_collections +// command +db.coll.insert({}); +// command show collections +// clear +db.coll.drop(); diff --git a/packages/shell-api/src/shell-internal-state.ts b/packages/shell-api/src/shell-internal-state.ts index 26104ef762..7b0e5dfd7a 100644 --- a/packages/shell-api/src/shell-internal-state.ts +++ b/packages/shell-api/src/shell-internal-state.ts @@ -12,7 +12,7 @@ import { } from './index'; import constructShellBson from './shell-bson'; import { EventEmitter } from 'events'; -import { Document, ServiceProvider, DEFAULT_DB } from '@mongosh/service-provider-core'; +import { Document, ServiceProvider, DEFAULT_DB, ReplPlatform } from '@mongosh/service-provider-core'; import { MongoshInvalidInputError } from '@mongosh/errors'; import AsyncWriter from '@mongosh/async-rewriter'; import { toIgnore } from './decorators'; @@ -137,14 +137,14 @@ export default class ShellInternalState { return this.setDbFunc(newDb); }; - try { + if (this.initialServiceProvider.platform === ReplPlatform.JavaShell) { + contextObject.db = this.setDbFunc(this.currentDb); // java shell, can't use getters/setters + } else { Object.defineProperty(contextObject, 'db', { configurable: true, set: setFunc, get: () => (this.currentDb) }); - } catch (e) { // java shell, can't use getters/setters - contextObject.db = this.setDbFunc(this.currentDb); } this.messageBus.emit(