-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: draft how accessing mongodb will work
- Loading branch information
Showing
17 changed files
with
265 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 0 additions & 24 deletions
24
.../jetbrains-plugin/src/main/kotlin/com/mongodb/jbplugin/dataaccess/ServerInfoRepository.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
dependencies { | ||
|
||
implementation(libs.owasp.encoder) | ||
implementation(libs.mongodb.driver) | ||
} |
22 changes: 22 additions & 0 deletions
22
packages/mongodb-access-adapter/datagrip-access-adapter/build.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
repositories { | ||
maven("https://www.jetbrains.com/intellij-repository/releases/") | ||
} | ||
dependencies { | ||
implementation(libs.gson) | ||
implementation(libs.mongodb.driver) | ||
implementation(project(":packages:mongodb-access-adapter")) | ||
|
||
compileOnly(libs.testing.intellij.ideImpl) | ||
compileOnly(libs.testing.intellij.coreUi) | ||
compileOnly("com.jetbrains.intellij.database:database-sql:241.15989.150") | ||
compileOnly("com.jetbrains.intellij.database:database-connectivity:241.15989.150") | ||
compileOnly("com.jetbrains.intellij.database:database-core-impl:241.15989.150") { | ||
exclude("com.jetbrains.fus.reporting", "ap-validation") | ||
} | ||
compileOnly("com.jetbrains.intellij.database:database-jdbc-console:241.15989.150") | ||
compileOnly("com.jetbrains.intellij.database:database:241.15989.150") | ||
compileOnly("com.jetbrains.intellij.platform:images:241.15989.157") | ||
compileOnly("com.jetbrains.intellij.grid:grid-impl:241.15989.150") | ||
compileOnly("com.jetbrains.intellij.grid:grid:241.15989.150") | ||
compileOnly("com.jetbrains.intellij.grid:grid-core-impl:241.15989.150") | ||
} |
40 changes: 40 additions & 0 deletions
40
...main/kotlin/com/mongodb/jbplugin/accessadapter/datagrip/DataGripBasedReadModelProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.mongodb.jbplugin.accessadapter.datagrip | ||
|
||
import com.intellij.database.dataSource.LocalDataSource | ||
import com.intellij.openapi.components.Service | ||
import com.intellij.openapi.project.Project | ||
import com.intellij.psi.util.CachedValue | ||
import com.intellij.psi.util.CachedValueProvider | ||
import com.intellij.psi.util.CachedValuesManager | ||
import com.mongodb.jbplugin.accessadapter.MongoDBReadModelProvider | ||
import com.mongodb.jbplugin.accessadapter.Slice | ||
import com.mongodb.jbplugin.accessadapter.datagrip.adapter.DataGripMongoDBDriver | ||
import com.mongodb.jbplugin.accessadapter.slice.BuildInfoSlice | ||
import kotlinx.coroutines.runBlocking | ||
|
||
@Service(Service.Level.PROJECT) | ||
class DataGripBasedReadModelProvider( | ||
private val project: Project, | ||
) : MongoDBReadModelProvider<LocalDataSource> { | ||
private val cachedValues: MutableMap<String, CachedValue<*>> = mutableMapOf() | ||
|
||
override fun <T : Any> slice(dataSource: LocalDataSource, slice: Slice<T>): T { | ||
return cachedValues | ||
.computeIfAbsent(slice.javaClass.canonicalName, fromSlice(dataSource, BuildInfoSlice)) | ||
.value as T | ||
} | ||
|
||
private inline fun <reified T : Any> fromSlice(dataSource: LocalDataSource, slice: Slice<T>): (String) -> CachedValue<T> { | ||
val cacheManager = CachedValuesManager.getManager(project) | ||
return { | ||
cacheManager.createCachedValue { | ||
runBlocking { | ||
val driver = DataGripMongoDBDriver(project, dataSource) | ||
val sliceData = slice.queryUsingDriver(driver) | ||
|
||
CachedValueProvider.Result.create(sliceData, dataSource) | ||
} | ||
} | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
.../main/kotlin/com/mongodb/jbplugin/accessadapter/datagrip/adapter/DataGripMongoDBDriver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.mongodb.jbplugin.accessadapter.datagrip.adapter | ||
|
||
import com.intellij.database.dataSource.LocalDataSource | ||
import com.intellij.openapi.project.Project | ||
import com.mongodb.jbplugin.accessadapter.MongoDBDriver | ||
import com.mongodb.jbplugin.accessadapter.Namespace | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import org.bson.Document | ||
import kotlin.reflect.KClass | ||
import kotlin.time.Duration | ||
|
||
class DataGripMongoDBDriver( | ||
val project: Project, | ||
val dataSource: LocalDataSource | ||
) : MongoDBDriver { | ||
override suspend fun <T : Any> runCommand(command: Document, result: KClass<T>, timeout: Duration): T = withContext(Dispatchers.IO) { | ||
DataSourceQuery(project, dataSource, result).runQuery( | ||
"""db.runCommand(${command.toJson()})""", | ||
timeout | ||
)[0] | ||
} | ||
|
||
override suspend fun <T : Any> findOne( | ||
namespace: Namespace, | ||
query: Document, | ||
options: Document, | ||
result: KClass<T>, | ||
timeout: Duration | ||
): T? = withContext(Dispatchers.IO) { | ||
DataSourceQuery(project, dataSource, result).runQuery( | ||
"""db.getSiblingDB("${namespace.database}") | ||
.getCollection("${namespace.collection}") | ||
.findOne(${query.toJson()}, ${options.toJson()}) """.trimMargin(), | ||
timeout | ||
).getOrNull(0) | ||
} | ||
|
||
override suspend fun <T : Any> findAll( | ||
namespace: Namespace, | ||
query: Document, | ||
result: KClass<T>, | ||
limit: Int, | ||
timeout: Duration | ||
) = withContext(Dispatchers.IO) { | ||
DataSourceQuery(project, dataSource, result).runQuery( | ||
"""db.getSiblingDB("${namespace.database}") | ||
.getCollection("${namespace.collection}") | ||
.find(${query.toJson()}).limit(${limit}) """.trimMargin(), | ||
timeout | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...ongodb-access-adapter/src/main/kotlin/com/mongodb/jbplugin/accessadapter/MongoDBDriver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.mongodb.jbplugin.accessadapter | ||
|
||
import org.bson.Document | ||
import org.owasp.encoder.Encode | ||
import kotlin.reflect.KClass | ||
import kotlin.time.Duration | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
data class Namespace(val database: String, val collection: String) { | ||
override fun toString(): String { | ||
return "${database}.${collection}" | ||
} | ||
} | ||
|
||
fun String.toNS(): Namespace { | ||
val (db, coll) = trim().split(".", limit = 2) | ||
return Namespace( | ||
Encode.forJavaScript(db), | ||
Encode.forJavaScript(coll) | ||
) | ||
} | ||
|
||
interface MongoDBDriver { | ||
suspend fun <T: Any> runCommand(command: Document, result: KClass<T>, timeout: Duration = 1.seconds): T | ||
suspend fun <T: Any> findOne(namespace: Namespace, query: Document, options: Document, result: KClass<T>, timeout: Duration = 1.seconds): T? | ||
suspend fun <T: Any> findAll(namespace: Namespace, query: Document, result: KClass<T>, limit: Int = 10, timeout: Duration = 1.seconds): List<T> | ||
} |
9 changes: 9 additions & 0 deletions
9
...ss-adapter/src/main/kotlin/com/mongodb/jbplugin/accessadapter/MongoDBReadModelProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.mongodb.jbplugin.accessadapter | ||
|
||
interface Slice<State: Any> { | ||
suspend fun queryUsingDriver(from: MongoDBDriver): State | ||
} | ||
|
||
interface MongoDBReadModelProvider<DataSource> { | ||
fun <T: Any> slice(dataSource: DataSource, slice: Slice<T>): T | ||
} |
20 changes: 20 additions & 0 deletions
20
...access-adapter/src/main/kotlin/com/mongodb/jbplugin/accessadapter/slice/BuildInfoSlice.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.mongodb.jbplugin.accessadapter.slice | ||
|
||
import com.mongodb.jbplugin.accessadapter.MongoDBDriver | ||
import com.mongodb.jbplugin.accessadapter.Slice | ||
import org.bson.Document | ||
|
||
data class BuildInfo( | ||
val version: String, | ||
val gitVersion: String | ||
) | ||
data object BuildInfoSlice : Slice<BuildInfo> { | ||
override suspend fun queryUsingDriver(from: MongoDBDriver): BuildInfo { | ||
return from.runCommand( | ||
Document(mapOf( | ||
"buildInfo" to 1, | ||
)), | ||
BuildInfo::class | ||
) | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...db-access-adapter/src/test/kotlin/com/mongodb/jbplugin/accessadapter/MongoDBDriverTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.mongodb.jbplugin.accessadapter | ||
|
||
import org.junit.jupiter.api.Assertions.assertEquals | ||
import org.junit.jupiter.api.Test | ||
|
||
class MongoDBDriverTest { | ||
@Test | ||
fun `parses a namespace`() { | ||
val namespace = "mydb.mycoll".toNS() | ||
assertEquals("mydb", namespace.database) | ||
assertEquals("mycoll", namespace.collection) | ||
} | ||
|
||
@Test | ||
fun `parses a namespace where collections have dots in a name`() { | ||
val namespace = "mydb.myco.ll".toNS() | ||
assertEquals("mydb", namespace.database) | ||
assertEquals("myco.ll", namespace.collection) | ||
} | ||
|
||
@Test | ||
fun `escapes characters that can be dangerous in javascript`() { | ||
val namespace = """mydb.myco"ll""".toNS() | ||
assertEquals("mydb", namespace.database) | ||
assertEquals("myco\\x22ll", namespace.collection) | ||
} | ||
|
||
@Test | ||
fun `removes trailing spaces`() { | ||
val namespace = """ mydb.myco"ll """.toNS() | ||
assertEquals("mydb", namespace.database) | ||
assertEquals("myco\\x22ll", namespace.collection) | ||
} | ||
|
||
@Test | ||
fun `serialises to a valid namespace string`() { | ||
val namespace = Namespace("mydb", "my.cool.col") | ||
assertEquals("mydb.my.cool.col", namespace.toString()) | ||
} | ||
|
||
@Test | ||
fun `can parse back a serialised namespace`() { | ||
val namespace = Namespace("mydb", "my.cool.col") | ||
val deserialized = namespace.toString().toNS() | ||
|
||
assertEquals(namespace, deserialized) | ||
} | ||
} |
Oops, something went wrong.