@@ -7,7 +7,6 @@ import com.mongodb.client.result.UpdateResult
7
7
import com.mongodb.mongosh.MongoShellContext
8
8
import com.mongodb.mongosh.result.ArrayResult
9
9
import com.mongodb.mongosh.result.CommandException
10
- import com.mongodb.mongosh.result.DeleteResult
11
10
import com.mongodb.mongosh.result.DocumentResult
12
11
import org.bson.Document
13
12
import org.graalvm.polyglot.HostAccess
@@ -25,9 +24,9 @@ internal class JavaServiceProvider(private val client: MongoClient, private val
25
24
override fun runCommand (database : String , spec : Value ): Value = promise {
26
25
getDatabase(database, null ).map { db ->
27
26
if (spec.isString) {
28
- context.toJs( db.runCommand(Document (spec.asString(), 1 ) ))
27
+ db.runCommand(Document (spec.asString(), 1 ))
29
28
} else {
30
- context.toJs( db.runCommand(toDocument(spec, " spec" ) ))
29
+ db.runCommand(toDocument(spec, " spec" ))
31
30
}
32
31
}
33
32
}
@@ -44,7 +43,7 @@ internal class JavaServiceProvider(private val client: MongoClient, private val
44
43
if (! isOk) {
45
44
throw Exception (res.toJson())
46
45
}
47
- context.toJs( res)
46
+ res
48
47
}
49
48
}
50
49
@@ -54,9 +53,8 @@ internal class JavaServiceProvider(private val client: MongoClient, private val
54
53
val dbOptions = toDocument(dbOptions, " dbOptions" )
55
54
getDatabase(database, dbOptions).map { db ->
56
55
db.getCollection(collection).insertOne(document)
57
- context.toJs(mapOf (
58
- " result" to mapOf (" ok" to true ),
59
- " insertedId" to " UNKNOWN" ))
56
+ mapOf (" result" to mapOf (" ok" to true ),
57
+ " insertedId" to " UNKNOWN" )
60
58
}
61
59
}
62
60
@@ -69,13 +67,11 @@ internal class JavaServiceProvider(private val client: MongoClient, private val
69
67
getDatabase(database, dbOptions).flatMap { db ->
70
68
convert(ReplaceOptions (), replaceOptionsConverters, replaceOptionsDefaultConverters, options).map { options ->
71
69
val res = db.getCollection(collection).replaceOne(filter, replacement, options)
72
- context.toJs(mapOf (
73
- " result" to mapOf (" ok" to res.wasAcknowledged()),
70
+ mapOf (" result" to mapOf (" ok" to res.wasAcknowledged()),
74
71
" matchedCount" to res.matchedCount,
75
72
" modifiedCount" to res.modifiedCount,
76
73
" upsertedCount" to if (res.upsertedId == null ) 0 else 1 ,
77
- " upsertedId" to res.upsertedId
78
- ))
74
+ " upsertedId" to res.upsertedId)
79
75
}
80
76
}
81
77
}
@@ -95,11 +91,11 @@ internal class JavaServiceProvider(private val client: MongoClient, private val
95
91
update.hasMembers() -> Right (db.getCollection(collection).updateMany(filter, toDocument(update, " update" ), updateOptions))
96
92
else -> Left <UpdateResult >(IllegalArgumentException (" updatePipeline must be a list or object" ))
97
93
}.map { res ->
98
- context.toJs( mapOf (" result" to mapOf (" ok" to res.wasAcknowledged()),
94
+ mapOf (" result" to mapOf (" ok" to res.wasAcknowledged()),
99
95
" matchedCount" to res.matchedCount,
100
96
" modifiedCount" to res.modifiedCount,
101
97
" upsertedCount" to if (res.upsertedId == null ) 0 else 1 ,
102
- " upsertedId" to res.upsertedId))
98
+ " upsertedId" to res.upsertedId)
103
99
}
104
100
}
105
101
}
@@ -127,12 +123,11 @@ internal class JavaServiceProvider(private val client: MongoClient, private val
127
123
val pipeline = toList(update, " update" )?.map { it as Document }
128
124
coll.updateOne(filter, pipeline, updateOptions)
129
125
} else coll.updateOne(filter, toDocument(update, " update" ), updateOptions)
130
- context.toJs( mapOf (" result" to mapOf (" ok" to res.wasAcknowledged()),
126
+ mapOf (" result" to mapOf (" ok" to res.wasAcknowledged()),
131
127
" matchedCount" to res.matchedCount,
132
128
" modifiedCount" to res.modifiedCount,
133
129
" upsertedCount" to if (res.upsertedId == null ) 0 else 1 ,
134
- " upsertedId" to res.upsertedId
135
- ))
130
+ " upsertedId" to res.upsertedId)
136
131
}
137
132
}
138
133
}
@@ -164,15 +159,15 @@ internal class JavaServiceProvider(private val client: MongoClient, private val
164
159
val writeModels = requests.map { getWriteModel(it as Document ) }
165
160
unwrap(writeModels).map { requests ->
166
161
val result = db.getCollection(collection).bulkWrite(requests, options)
167
- context.toJs( mapOf (
162
+ mapOf (
168
163
" result" to mapOf (" ok" to result.wasAcknowledged()),
169
164
" insertedCount" to result.insertedCount,
170
165
" insertedIds" to " UNKNOWN" ,
171
166
" matchedCount" to result.matchedCount,
172
167
" modifiedCount" to result.modifiedCount,
173
168
" deletedCount" to result.deletedCount,
174
169
" upsertedCount" to result.upserts.size,
175
- " upsertedIds" to result.upserts.map { it.id }))
170
+ " upsertedIds" to result.upserts.map { it.id })
176
171
}
177
172
}
178
173
}
@@ -218,9 +213,8 @@ internal class JavaServiceProvider(private val client: MongoClient, private val
218
213
getDatabase(database, dbOptions).flatMap { db ->
219
214
convert(DeleteOptions (), deleteConverters, deleteDefaultConverter, options).map { deleteOptions ->
220
215
val result = db.getCollection(collection).deleteMany(filter, deleteOptions)
221
- context.toJs(mapOf (
222
- " result" to mapOf (" ok" to result.wasAcknowledged()),
223
- " deletedCount" to result.deletedCount))
216
+ mapOf (" result" to mapOf (" ok" to result.wasAcknowledged()),
217
+ " deletedCount" to result.deletedCount)
224
218
}
225
219
}
226
220
}
@@ -233,9 +227,8 @@ internal class JavaServiceProvider(private val client: MongoClient, private val
233
227
getDatabase(database, dbOptions).flatMap { db ->
234
228
convert(DeleteOptions (), deleteConverters, deleteDefaultConverter, options).map { deleteOptions ->
235
229
val result = db.getCollection(collection).deleteOne(filter, deleteOptions)
236
- context.toJs(mapOf (
237
- " result" to mapOf (" ok" to result.wasAcknowledged()),
238
- " deletedCount" to result.deletedCount))
230
+ mapOf (" result" to mapOf (" ok" to result.wasAcknowledged()),
231
+ " deletedCount" to result.deletedCount)
239
232
}
240
233
}
241
234
}
@@ -247,7 +240,7 @@ internal class JavaServiceProvider(private val client: MongoClient, private val
247
240
getDatabase(database, null ).flatMap { db ->
248
241
convert(FindOneAndDeleteOptions (), findOneAndDeleteConverters, findOneAndDeleteDefaultConverter, options).map { options ->
249
242
val res = db.getCollection(collection).findOneAndDelete(filter, options)
250
- context.toJs( mapOf (" value" to res) )
243
+ mapOf (" value" to res)
251
244
}
252
245
}
253
246
}
@@ -261,7 +254,7 @@ internal class JavaServiceProvider(private val client: MongoClient, private val
261
254
convert(FindOneAndReplaceOptions (), findOneAndReplaceOptionsConverters, findOneAndReplaceOptionsDefaultConverters, options)
262
255
.map { options ->
263
256
val res = db.getCollection(collection).findOneAndReplace(filter, replacement, options)
264
- context.toJs( mapOf (" value" to res) )
257
+ mapOf (" value" to res)
265
258
}
266
259
}
267
260
}
@@ -274,7 +267,7 @@ internal class JavaServiceProvider(private val client: MongoClient, private val
274
267
getDatabase(database, null ).flatMap { db ->
275
268
convert(FindOneAndUpdateOptions (), findOneAndUpdateConverters, findOneAndUpdateDefaultConverter, options).map { options ->
276
269
val res = db.getCollection(collection).findOneAndUpdate(filter, update, options)
277
- context.toJs( mapOf (" value" to res) )
270
+ mapOf (" value" to res)
278
271
}
279
272
}
280
273
@@ -287,9 +280,8 @@ internal class JavaServiceProvider(private val client: MongoClient, private val
287
280
if (docs == null || docs.any { it !is Document }) return @promise Left (IllegalArgumentException (" docs must be a list of objects" ))
288
281
getDatabase(database, dbOptions).map { db ->
289
282
db.getCollection(collection).insertMany(docs.filterIsInstance<Document >())
290
- context.toJs(mapOf (
291
- " result" to mapOf (" ok" to true ),
292
- " insertedIds" to emptyList<String >()))
283
+ mapOf (" result" to mapOf (" ok" to true ),
284
+ " insertedIds" to emptyList<String >())
293
285
}
294
286
}
295
287
@@ -392,7 +384,7 @@ internal class JavaServiceProvider(private val client: MongoClient, private val
392
384
393
385
@HostAccess.Export
394
386
override fun listDatabases (database : String ): Value = promise {
395
- Right (context.toJs( mapOf (" databases" to client.listDatabases() )))
387
+ Right (mapOf (" databases" to client.listDatabases()))
396
388
}
397
389
398
390
@HostAccess.Export
@@ -407,7 +399,7 @@ internal class JavaServiceProvider(private val client: MongoClient, private val
407
399
@HostAccess.Export
408
400
override fun getIndexes (database : String , collection : String ): Value = promise {
409
401
getDatabase(database, null ).map { db ->
410
- context.toJs( db.getCollection(collection).listIndexes() )
402
+ db.getCollection(collection).listIndexes()
411
403
}
412
404
}
413
405
@@ -417,26 +409,26 @@ internal class JavaServiceProvider(private val client: MongoClient, private val
417
409
getDatabase(database, null ).map { db ->
418
410
val list = db.listCollections()
419
411
if (filter != null ) list.filter(filter)
420
- context.toJs( list)
412
+ list
421
413
}
422
414
}
423
415
424
416
@HostAccess.Export
425
417
override fun stats (database : String , collection : String , options : Value ? ): Value = promise<Any ?> {
426
418
getDatabase(database, null ).map { db ->
427
- context.toJs( db.runCommand(Document (" collStats" , collection) ))
419
+ db.runCommand(Document (" collStats" , collection))
428
420
}
429
421
}
430
422
431
423
@HostAccess.Export
432
- override fun remove (database : String , collection : String , query : Value , options : Value ? , dbOptions : Value ? ): Value {
424
+ override fun remove (database : String , collection : String , query : Value , options : Value ? , dbOptions : Value ? ): Value = promise {
433
425
val query = toDocument(query, " query" )
434
426
val dbOptions = toDocument(dbOptions, " dbOptions" )
435
- val promise = getDatabase(database, dbOptions).map { db ->
427
+ getDatabase(database, dbOptions).map { db ->
436
428
val result = db.getCollection(collection).deleteMany(query)
437
- DeleteResult (result.wasAcknowledged(), result.deletedCount)
429
+ mapOf (" acknowledged" to result.wasAcknowledged(),
430
+ " deletedCount" to result.deletedCount)
438
431
}
439
- return context.toJsPromise(promise)
440
432
}
441
433
442
434
@HostAccess.Export
@@ -454,7 +446,7 @@ internal class JavaServiceProvider(private val client: MongoClient, private val
454
446
}
455
447
val indexes = unwrap(convertedIndexes)
456
448
indexes.map { indexes ->
457
- context.toJs( db.getCollection(collection).createIndexes(indexes) )
449
+ db.getCollection(collection).createIndexes(indexes)
458
450
}
459
451
}
460
452
}
0 commit comments