diff --git a/postssystem.core.exposed/src/main/kotlin/com/insanusmokrassar/postssystem/core/exposed/ExposedContentRepo.kt b/postssystem.core.exposed/src/main/kotlin/com/insanusmokrassar/postssystem/core/exposed/ExposedContentRepo.kt index b6a3b873..fd036447 100644 --- a/postssystem.core.exposed/src/main/kotlin/com/insanusmokrassar/postssystem/core/exposed/ExposedContentRepo.kt +++ b/postssystem.core.exposed/src/main/kotlin/com/insanusmokrassar/postssystem/core/exposed/ExposedContentRepo.kt @@ -3,8 +3,8 @@ package com.insanusmokrassar.postssystem.core.exposed import com.insanusmokrassar.postssystem.core.content.* import com.insanusmokrassar.postssystem.core.content.api.ContentRepo import com.insanusmokrassar.postssystem.core.exposed.content.* -import com.insanusmokrassar.postssystem.core.utils.generateContentId -import com.insanusmokrassar.postssystem.core.utils.pagination.* +import com.insanusmokrassar.postssystem.core.generateContentId +import com.insanusmokrassar.postssystem.utils.repos.pagination.* import com.insanusmokrassar.postssystem.exposed.commons.paginate import kotlinx.coroutines.channels.BroadcastChannel import kotlinx.coroutines.channels.Channel diff --git a/postssystem.core.exposed/src/main/kotlin/com/insanusmokrassar/postssystem/core/exposed/ExposedPostsRepo.kt b/postssystem.core.exposed/src/main/kotlin/com/insanusmokrassar/postssystem/core/exposed/ExposedPostsRepo.kt index 68f717d8..4b9f8957 100644 --- a/postssystem.core.exposed/src/main/kotlin/com/insanusmokrassar/postssystem/core/exposed/ExposedPostsRepo.kt +++ b/postssystem.core.exposed/src/main/kotlin/com/insanusmokrassar/postssystem/core/exposed/ExposedPostsRepo.kt @@ -1,10 +1,10 @@ package com.insanusmokrassar.postssystem.core.exposed import com.insanusmokrassar.postssystem.core.content.ContentId +import com.insanusmokrassar.postssystem.core.generatePostId import com.insanusmokrassar.postssystem.core.post.* import com.insanusmokrassar.postssystem.core.post.repo.PostsRepo -import com.insanusmokrassar.postssystem.core.utils.generatePostId -import com.insanusmokrassar.postssystem.core.utils.pagination.* +import com.insanusmokrassar.postssystem.utils.repos.pagination.* import com.insanusmokrassar.postssystem.exposed.commons.paginate import com.soywiz.klock.* import kotlinx.coroutines.channels.BroadcastChannel diff --git a/postssystem.core.publishing.exposed/src/main/kotlin/com/insanusmokrassar/postssystem/core/publishing/exposed/ExposedPublishingKeysRepo.kt b/postssystem.core.publishing.exposed/src/main/kotlin/com/insanusmokrassar/postssystem/core/publishing/exposed/ExposedPublishingKeysRepo.kt index 3865ed32..8c9fdc98 100644 --- a/postssystem.core.publishing.exposed/src/main/kotlin/com/insanusmokrassar/postssystem/core/publishing/exposed/ExposedPublishingKeysRepo.kt +++ b/postssystem.core.publishing.exposed/src/main/kotlin/com/insanusmokrassar/postssystem/core/publishing/exposed/ExposedPublishingKeysRepo.kt @@ -5,6 +5,7 @@ import com.insanusmokrassar.postssystem.core.publishing.TriggerControlKey import com.insanusmokrassar.postssystem.core.publishing.repos.PublishingKeysRepo import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction +import org.jetbrains.exposed.sql.transactions.transaction class ExposedPublishingKeysRepo( private val database: Database @@ -14,9 +15,9 @@ class ExposedPublishingKeysRepo( override val primaryKey: PrimaryKey = PrimaryKey(postIdColumn, triggerControlKeyColumn) init { - newSuspendedTransaction( - db = database - ) { + transaction( + db = database + ) { SchemaUtils.createMissingTablesAndColumns(this@ExposedPublishingKeysRepo) } } diff --git a/postssystem.core/build.gradle b/postssystem.core/build.gradle index 6f8071c1..9b26e026 100644 --- a/postssystem.core/build.gradle +++ b/postssystem.core/build.gradle @@ -42,6 +42,12 @@ kotlin { api "com.soywiz.korlibs.klock:klock:$klockVersion" api "com.benasher44:uuid:$uuidVersion" + + if ((project.hasProperty('RELEASE_MODE') && project.property('RELEASE_MODE') == "true") || System.getenv('RELEASE_MODE') == "true") { + api "com.insanusmokrassar:postssystem.utils.repos:$core_version" + } else { + api project(":postssystem.utils.repos") + } } } commonTest { diff --git a/postssystem.core/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/utils/IdUtils.kt b/postssystem.core/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/IdUtils.kt similarity index 63% rename from postssystem.core/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/utils/IdUtils.kt rename to postssystem.core/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/IdUtils.kt index 304b87aa..83409218 100644 --- a/postssystem.core/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/utils/IdUtils.kt +++ b/postssystem.core/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/IdUtils.kt @@ -1,10 +1,8 @@ -package com.insanusmokrassar.postssystem.core.utils +package com.insanusmokrassar.postssystem.core -import com.benasher44.uuid.uuid4 import com.insanusmokrassar.postssystem.core.content.ContentId import com.insanusmokrassar.postssystem.core.post.PostId - -fun generateId() = uuid4().toString() +import com.insanusmokrassar.postssystem.utils.repos.generateId fun generatePostId(): PostId = generateId() fun generateContentId(): ContentId = generateId() diff --git a/postssystem.core/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/content/api/ReadContentRepo.kt b/postssystem.core/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/content/api/ReadContentRepo.kt index 199ba2dc..77a0cd83 100644 --- a/postssystem.core/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/content/api/ReadContentRepo.kt +++ b/postssystem.core/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/content/api/ReadContentRepo.kt @@ -2,8 +2,8 @@ package com.insanusmokrassar.postssystem.core.content.api import com.insanusmokrassar.postssystem.core.content.ContentId import com.insanusmokrassar.postssystem.core.content.RegisteredContent -import com.insanusmokrassar.postssystem.core.utils.pagination.Pagination -import com.insanusmokrassar.postssystem.core.utils.pagination.PaginationResult +import com.insanusmokrassar.postssystem.utils.repos.pagination.Pagination +import com.insanusmokrassar.postssystem.utils.repos.pagination.PaginationResult /** * Simple read API by different properties of [com.insanusmokrassar.postssystem.core.content.Content]. diff --git a/postssystem.core/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/post/repo/ReadPostsRepo.kt b/postssystem.core/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/post/repo/ReadPostsRepo.kt index b9989cd3..f189e419 100644 --- a/postssystem.core/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/post/repo/ReadPostsRepo.kt +++ b/postssystem.core/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/post/repo/ReadPostsRepo.kt @@ -5,8 +5,8 @@ import com.insanusmokrassar.postssystem.core.post.PostId import com.insanusmokrassar.postssystem.core.post.RegisteredPost import com.insanusmokrassar.postssystem.core.utils.MAX_DATE import com.insanusmokrassar.postssystem.core.utils.MIN_DATE -import com.insanusmokrassar.postssystem.core.utils.pagination.Pagination -import com.insanusmokrassar.postssystem.core.utils.pagination.PaginationResult +import com.insanusmokrassar.postssystem.utils.repos.pagination.Pagination +import com.insanusmokrassar.postssystem.utils.repos.pagination.PaginationResult import com.soywiz.klock.DateTime /** diff --git a/postssystem.core/src/commonTest/kotlin/com/insanusmokrassar/postssystem/core/api/ContentSerialization.kt b/postssystem.core/src/commonTest/kotlin/com/insanusmokrassar/postssystem/core/api/ContentSerialization.kt index 4798c0a9..96c98751 100644 --- a/postssystem.core/src/commonTest/kotlin/com/insanusmokrassar/postssystem/core/api/ContentSerialization.kt +++ b/postssystem.core/src/commonTest/kotlin/com/insanusmokrassar/postssystem/core/api/ContentSerialization.kt @@ -1,7 +1,7 @@ package com.insanusmokrassar.postssystem.core.api import com.insanusmokrassar.postssystem.core.content.* -import com.insanusmokrassar.postssystem.core.utils.generateContentId +import com.insanusmokrassar.postssystem.core.generateContentId import kotlinx.serialization.json.Json import kotlin.test.Test import kotlin.test.assertEquals diff --git a/postssystem.exposed.commons/src/main/kotlin/com/insanusmokrassar/postssystem/exposed/commons/QueryExtensions.kt b/postssystem.exposed.commons/src/main/kotlin/com/insanusmokrassar/postssystem/exposed/commons/QueryExtensions.kt index 2524305e..03b1caf7 100644 --- a/postssystem.exposed.commons/src/main/kotlin/com/insanusmokrassar/postssystem/exposed/commons/QueryExtensions.kt +++ b/postssystem.exposed.commons/src/main/kotlin/com/insanusmokrassar/postssystem/exposed/commons/QueryExtensions.kt @@ -1,7 +1,22 @@ package com.insanusmokrassar.postssystem.exposed.commons -import com.insanusmokrassar.postssystem.core.utils.pagination.Pagination -import com.insanusmokrassar.postssystem.core.utils.pagination.firstIndex -import org.jetbrains.exposed.sql.Query +import com.insanusmokrassar.postssystem.utils.repos.pagination.* +import org.jetbrains.exposed.sql.* -fun Query.paginate(pagination: Pagination) = limit(pagination.size, pagination.firstIndex.toLong()) +fun Query.paginate(with: Pagination, orderBy: Pair, SortOrder>? = null) = limit( + with.size, + (if (orderBy ?.second == SortOrder.DESC) { + with.lastIndex + } else { + with.firstIndex + }).toLong() +).let { + if (orderBy != null) { + it.orderBy( + orderBy.first, + orderBy.second + ) + } else { + it + } +} diff --git a/postssystem.utils.repos.exposed/build.gradle b/postssystem.utils.repos.exposed/build.gradle new file mode 100644 index 00000000..d6ec3967 --- /dev/null +++ b/postssystem.utils.repos.exposed/build.gradle @@ -0,0 +1,49 @@ +buildscript { + repositories { + mavenLocal() + jcenter() + mavenCentral() + } + + dependencies { + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version" + classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:$gradle_bintray_plugin_version" + } +} + +plugins { + id "org.jetbrains.kotlin.plugin.serialization" version "$kotlin_version" +} + +project.version = "$core_version" +project.group = "com.insanusmokrassar" + +apply plugin: "java-library" +apply plugin: "kotlin" +apply from: "./publish.gradle" + +repositories { + mavenLocal() + jcenter() + mavenCentral() + maven { url "https://kotlin.bintray.com/kotlinx" } +} + +dependencies { + api "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" + api "org.jetbrains.exposed:exposed-core:$exposed_version" + api "org.jetbrains.exposed:exposed-jdbc:$exposed_version" + + if ((project.hasProperty('RELEASE_MODE') && project.property('RELEASE_MODE') == "true") || System.getenv('RELEASE_MODE') == "true") { + api "com.insanusmokrassar:postssystem.utils.repos:$core_version" + api "com.insanusmokrassar:postssystem.exposed.commons:$core_version" + } else { + api project(":postssystem.utils.repos") + api project(":postssystem.exposed.commons") + } + + testImplementation "org.xerial:sqlite-jdbc:$test_sqlite_version" + testImplementation "org.jetbrains.kotlin:kotlin-test" + testImplementation "org.jetbrains.kotlin:kotlin-test-junit" +} diff --git a/postssystem.utils.repos.exposed/gradle.properties b/postssystem.utils.repos.exposed/gradle.properties new file mode 100644 index 00000000..1576438d --- /dev/null +++ b/postssystem.utils.repos.exposed/gradle.properties @@ -0,0 +1,2 @@ +exposed_version=0.23.1 +test_sqlite_version=3.28.0 diff --git a/postssystem.utils.repos.exposed/maven.publish.gradle b/postssystem.utils.repos.exposed/maven.publish.gradle new file mode 100644 index 00000000..50ee129b --- /dev/null +++ b/postssystem.utils.repos.exposed/maven.publish.gradle @@ -0,0 +1,53 @@ +apply plugin: 'maven-publish' + +task javadocsJar(type: Jar) { + classifier = 'javadoc' +} + +afterEvaluate { + project.publishing.publications.all { + // rename artifacts + groupId "${project.group}" + if (it.name.contains('kotlinMultiplatform')) { + artifactId = "${project.name}" + } else { + artifactId = "${project.name}-$name" + } + } +} + +publishing { + publications.all { + artifact javadocsJar + + pom { + description = "Common utils for all exposed modules" + name = "PostsSystem Exposed commons" + url = "https://git.insanusmokrassar.com/PostsSystem/Core/" + + scm { + developerConnection = "scm:git:[fetch=]https://git.insanusmokrassar.com/PostsSystem/Core/.git[push=]https://git.insanusmokrassar.com/PostsSystem/Core/.git" + url = "https://git.insanusmokrassar.com/PostsSystem/Core/.git" + } + + developers { + + developer { + id = "InsanusMokrassar" + name = "Ovsiannikov Aleksei" + email = "ovsyannikov.alexey95@gmail.com" + } + + } + + licenses { + + license { + name = "Apache Software License 2.0" + url = "https://git.insanusmokrassar.com/PostsSystem/Core/src/master/LICENSE" + } + + } + } + } +} \ No newline at end of file diff --git a/postssystem.utils.repos.exposed/publish.gradle b/postssystem.utils.repos.exposed/publish.gradle new file mode 100644 index 00000000..b8cf4528 --- /dev/null +++ b/postssystem.utils.repos.exposed/publish.gradle @@ -0,0 +1,55 @@ +apply plugin: 'com.jfrog.bintray' + +apply from: "maven.publish.gradle" + +bintray { + user = project.hasProperty('BINTRAY_USER') ? project.property('BINTRAY_USER') : System.getenv('BINTRAY_USER') + key = project.hasProperty('BINTRAY_KEY') ? project.property('BINTRAY_KEY') : System.getenv('BINTRAY_KEY') + filesSpec { + from "${buildDir}/publications/" + eachFile { + String directorySubname = it.getFile().parentFile.name + if (it.getName() == "module.json") { + if (directorySubname == "kotlinMultiplatform") { + it.setPath("${project.name}/${project.version}/${project.name}-${project.version}.module") + } else { + it.setPath("${project.name}-${directorySubname}/${project.version}/${project.name}-${directorySubname}-${project.version}.module") + } + } else { + if (directorySubname == "kotlinMultiplatform" && it.getName() == "pom-default.xml") { + it.setPath("${project.name}/${project.version}/${project.name}-${project.version}.pom") + } else { + it.exclude() + } + } + } + into "${project.group}".replace(".", "/") + } + pkg { + repo = "InsanusMokrassar" + name = "${project.name}" + vcsUrl = "https://github.com/PostsSystem/PostsSystemCore" + licenses = ["Apache-2.0"] + version { + name = "${project.version}" + released = new Date() + vcsTag = "${project.version}" + gpg { + sign = true + passphrase = project.hasProperty('signing.gnupg.passphrase') ? project.property('signing.gnupg.passphrase') : System.getenv('signing.gnupg.passphrase') + } + } + } +} + +bintrayUpload.doFirst { + publications = publishing.publications.collect { + if (it.name.contains('kotlinMultiplatform')) { + null + } else { + it.name + } + } - null +} + +bintrayUpload.dependsOn publishToMavenLocal \ No newline at end of file diff --git a/postssystem.utils.repos.exposed/publish_config.kpsb b/postssystem.utils.repos.exposed/publish_config.kpsb new file mode 100644 index 00000000..2bd43e2b --- /dev/null +++ b/postssystem.utils.repos.exposed/publish_config.kpsb @@ -0,0 +1 @@ +{"bintrayConfig":{"repo":"InsanusMokrassar","packageName":"${project.name}","packageVcs":"https://github.com/PostsSystem/PostsSystemCore"},"licenses":[{"id":"Apache-2.0","title":"Apache Software License 2.0","url":"https://git.insanusmokrassar.com/PostsSystem/Core/src/master/LICENSE"}],"mavenConfig":{"name":"PostsSystem Exposed commons","description":"Common utils for all exposed modules","url":"https://git.insanusmokrassar.com/PostsSystem/Core/","vcsUrl":"https://git.insanusmokrassar.com/PostsSystem/Core/.git","developers":[{"id":"InsanusMokrassar","name":"Ovsiannikov Aleksei","eMail":"ovsyannikov.alexey95@gmail.com"}]},"type":"Multiplatform"} \ No newline at end of file diff --git a/postssystem.utils.repos.exposed/src/main/kotlin/com/insanusmokrassar/postssystem/utils/repos/exposed/keyvalue/AbstractExposedKeyValueRepo.kt b/postssystem.utils.repos.exposed/src/main/kotlin/com/insanusmokrassar/postssystem/utils/repos/exposed/keyvalue/AbstractExposedKeyValueRepo.kt new file mode 100644 index 00000000..b43a23da --- /dev/null +++ b/postssystem.utils.repos.exposed/src/main/kotlin/com/insanusmokrassar/postssystem/utils/repos/exposed/keyvalue/AbstractExposedKeyValueRepo.kt @@ -0,0 +1,48 @@ +package com.insanusmokrassar.postssystem.utils.repos.exposed.keyvalue + +import com.insanusmokrassar.postssystem.utils.repos.StandardKeyValueRepo +import kotlinx.coroutines.channels.BroadcastChannel +import kotlinx.coroutines.channels.Channel +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.asFlow +import org.jetbrains.exposed.sql.* +import org.jetbrains.exposed.sql.transactions.transaction + +abstract class AbstractExposedKeyValueRepo( + database: Database, + keyColumn: Column, + valueColumn: Column +) : StandardKeyValueRepo, AbstractExposedReadKeyValueRepo( + database, + keyColumn, + valueColumn +) { + private val onNewValueChannel = BroadcastChannel>(Channel.BUFFERED) + private val onValueRemovedChannel = BroadcastChannel(Channel.BUFFERED) + + override val onNewValue: Flow> = onNewValueChannel.asFlow() + override val onValueRemoved: Flow = onValueRemovedChannel.asFlow() + + override suspend fun set(k: Key, v: Value) { + transaction(database) { + if (select { keyColumn.eq(k) }.limit(1).any()) { + update({ keyColumn.eq(k) }) { + it[valueColumn] = v + } + } else { + insert { + it[keyColumn] = k + it[valueColumn] = v + } + } + } + onNewValueChannel.send(k to v) + } + + override suspend fun unset(k: Key) { + transaction(database) { + deleteWhere { keyColumn.eq(k) } + } + onValueRemovedChannel.send(k) + } +} diff --git a/postssystem.utils.repos.exposed/src/main/kotlin/com/insanusmokrassar/postssystem/utils/repos/exposed/keyvalue/AbstractExposedReadKeyValueRepo.kt b/postssystem.utils.repos.exposed/src/main/kotlin/com/insanusmokrassar/postssystem/utils/repos/exposed/keyvalue/AbstractExposedReadKeyValueRepo.kt new file mode 100644 index 00000000..bf33901c --- /dev/null +++ b/postssystem.utils.repos.exposed/src/main/kotlin/com/insanusmokrassar/postssystem/utils/repos/exposed/keyvalue/AbstractExposedReadKeyValueRepo.kt @@ -0,0 +1,37 @@ +package com.insanusmokrassar.postssystem.utils.repos.exposed.keyvalue + +import com.insanusmokrassar.postssystem.exposed.commons.paginate +import com.insanusmokrassar.postssystem.utils.repos.StandardReadKeyValueRepo +import com.insanusmokrassar.postssystem.utils.repos.pagination.* +import org.jetbrains.exposed.sql.* +import org.jetbrains.exposed.sql.transactions.transaction + +abstract class AbstractExposedReadKeyValueRepo( + protected val database: Database, + protected val keyColumn: Column, + protected val valueColumn: Column +) : StandardReadKeyValueRepo, Table() { + override val primaryKey: PrimaryKey = PrimaryKey(keyColumn, valueColumn) + + override suspend fun get(k: Key): Value? = transaction(database) { + select { keyColumn.eq(k) }.limit(1).firstOrNull() ?.getOrNull(valueColumn) + } + + override suspend fun contains(key: Key): Boolean = transaction(database) { + select { keyColumn.eq(key) }.limit(1).any() + } + + override suspend fun count(): Long = transaction(database) { selectAll().count() } + + override suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult = transaction(database) { + selectAll().paginate(pagination, keyColumn to if (reversed) SortOrder.DESC else SortOrder.ASC).map { + it[keyColumn] + } + }.createPaginationResult(pagination, count()) + + override suspend fun values(pagination: Pagination, reversed: Boolean): PaginationResult = transaction(database) { + selectAll().paginate(pagination, keyColumn to if (reversed) SortOrder.DESC else SortOrder.ASC).map { + it[valueColumn] + } + }.createPaginationResult(pagination, count()) +} diff --git a/postssystem.utils.repos/build.gradle b/postssystem.utils.repos/build.gradle new file mode 100644 index 00000000..6f8071c1 --- /dev/null +++ b/postssystem.utils.repos/build.gradle @@ -0,0 +1,79 @@ +buildscript { + repositories { + mavenLocal() + jcenter() + mavenCentral() + } + + dependencies { + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version" + classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:$gradle_bintray_plugin_version" + } +} + +plugins { + id "org.jetbrains.kotlin.multiplatform" version "$kotlin_version" + id "org.jetbrains.kotlin.plugin.serialization" version "$kotlin_version" +} + +project.version = "$core_version" +project.group = "com.insanusmokrassar" + +apply from: "publish.gradle" + +repositories { + mavenLocal() + jcenter() + mavenCentral() + maven { url "https://kotlin.bintray.com/kotlinx" } +} + +kotlin { + jvm() + js() + + sourceSets { + commonMain { + dependencies { + implementation kotlin('stdlib') + api "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$kotlin_coroutines_version" + api "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$kotlin_serialisation_runtime_version" + + api "com.soywiz.korlibs.klock:klock:$klockVersion" + api "com.benasher44:uuid:$uuidVersion" + } + } + commonTest { + dependencies { + implementation kotlin('test-common') + implementation kotlin('test-annotations-common') + } + } + jvmMain { + dependencies { + implementation kotlin('stdlib-jdk8') + api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlin_coroutines_version" + api "org.jetbrains.kotlinx:kotlinx-serialization-runtime:$kotlin_serialisation_runtime_version" + } + } + jvmTest { + dependencies { + implementation kotlin('test-junit') + } + } + jsMain { + dependencies { + implementation kotlin('stdlib-js') + api "org.jetbrains.kotlinx:kotlinx-coroutines-core-js:$kotlin_coroutines_version" + api "org.jetbrains.kotlinx:kotlinx-serialization-runtime-js:$kotlin_serialisation_runtime_version" + } + } + jsTest { + dependencies { + implementation kotlin('test-js') + implementation kotlin('test-junit') + } + } + } +} diff --git a/postssystem.utils.repos/maven.publish.gradle b/postssystem.utils.repos/maven.publish.gradle new file mode 100644 index 00000000..c871a756 --- /dev/null +++ b/postssystem.utils.repos/maven.publish.gradle @@ -0,0 +1,60 @@ +apply plugin: 'maven-publish' + +task javadocsJar(type: Jar) { + classifier = 'javadoc' +} + +afterEvaluate { + project.publishing.publications.all { + // rename artifacts + groupId "${project.group}" + if (it.name.contains('kotlinMultiplatform')) { + artifactId = "${project.name}" + } else { + artifactId = "${project.name}-$name" + } + } +} + +publishing { + publications.all { + artifact javadocsJar + + pom { + description = "PostsSystem Core Repos utility subproject" + name = "PostsSystem Core Repos" + url = "https://git.insanusmokrassar.com/PostsSystem/PostsSystemCore" + + scm { + developerConnection = "scm:git:[fetch=]https://git.insanusmokrassar.com/PostsSystem/PostsSystemCore.git[push=]https://git.insanusmokrassar.com/PostsSystem/PostsSystemCore.git" + url = "https://git.insanusmokrassar.com/PostsSystem/PostsSystemCore.git" + } + + developers { + + developer { + id = "InsanusMokrassar" + name = "Ovsiannikov Aleksei" + email = "ovsyannikov.alexey95@gmail.com" + } + + + developer { + id = "mi-ast" + name = "Michail Astafiev" + email = "astaf65@gmail.com" + } + + } + + licenses { + + license { + name = "Apache Software License 2.0" + url = "https://git.insanusmokrassar.com/PostsSystem/PostsSystemCore/src/master/LICENSE" + } + + } + } + } +} \ No newline at end of file diff --git a/postssystem.utils.repos/publish.gradle b/postssystem.utils.repos/publish.gradle new file mode 100644 index 00000000..b8cf4528 --- /dev/null +++ b/postssystem.utils.repos/publish.gradle @@ -0,0 +1,55 @@ +apply plugin: 'com.jfrog.bintray' + +apply from: "maven.publish.gradle" + +bintray { + user = project.hasProperty('BINTRAY_USER') ? project.property('BINTRAY_USER') : System.getenv('BINTRAY_USER') + key = project.hasProperty('BINTRAY_KEY') ? project.property('BINTRAY_KEY') : System.getenv('BINTRAY_KEY') + filesSpec { + from "${buildDir}/publications/" + eachFile { + String directorySubname = it.getFile().parentFile.name + if (it.getName() == "module.json") { + if (directorySubname == "kotlinMultiplatform") { + it.setPath("${project.name}/${project.version}/${project.name}-${project.version}.module") + } else { + it.setPath("${project.name}-${directorySubname}/${project.version}/${project.name}-${directorySubname}-${project.version}.module") + } + } else { + if (directorySubname == "kotlinMultiplatform" && it.getName() == "pom-default.xml") { + it.setPath("${project.name}/${project.version}/${project.name}-${project.version}.pom") + } else { + it.exclude() + } + } + } + into "${project.group}".replace(".", "/") + } + pkg { + repo = "InsanusMokrassar" + name = "${project.name}" + vcsUrl = "https://github.com/PostsSystem/PostsSystemCore" + licenses = ["Apache-2.0"] + version { + name = "${project.version}" + released = new Date() + vcsTag = "${project.version}" + gpg { + sign = true + passphrase = project.hasProperty('signing.gnupg.passphrase') ? project.property('signing.gnupg.passphrase') : System.getenv('signing.gnupg.passphrase') + } + } + } +} + +bintrayUpload.doFirst { + publications = publishing.publications.collect { + if (it.name.contains('kotlinMultiplatform')) { + null + } else { + it.name + } + } - null +} + +bintrayUpload.dependsOn publishToMavenLocal \ No newline at end of file diff --git a/postssystem.utils.repos/publish_config.kpsb b/postssystem.utils.repos/publish_config.kpsb new file mode 100644 index 00000000..2004655f --- /dev/null +++ b/postssystem.utils.repos/publish_config.kpsb @@ -0,0 +1 @@ +{"bintrayConfig":{"repo":"InsanusMokrassar","packageName":"${project.name}","packageVcs":"https://github.com/PostsSystem/PostsSystemCore"},"licenses":[{"id":"Apache-2.0","title":"Apache Software License 2.0","url":"https://git.insanusmokrassar.com/PostsSystem/PostsSystemCore/src/master/LICENSE"}],"mavenConfig":{"name":"PostsSystem Core Repos","description":"PostsSystem Core Repos utility subproject","url":"https://git.insanusmokrassar.com/PostsSystem/PostsSystemCore","vcsUrl":"https://git.insanusmokrassar.com/PostsSystem/PostsSystemCore.git","developers":[{"id":"InsanusMokrassar","name":"Ovsiannikov Aleksei","eMail":"ovsyannikov.alexey95@gmail.com"},{"id":"mi-ast","name":"Michail Astafiev","eMail":"astaf65@gmail.com"}]},"type":"Multiplatform"} \ No newline at end of file diff --git a/postssystem.utils.repos/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/IdUtils.kt b/postssystem.utils.repos/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/IdUtils.kt new file mode 100644 index 00000000..9e15140a --- /dev/null +++ b/postssystem.utils.repos/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/IdUtils.kt @@ -0,0 +1,5 @@ +package com.insanusmokrassar.postssystem.utils.repos + +import com.benasher44.uuid.uuid4 + +fun generateId() = uuid4().toString() diff --git a/postssystem.utils.repos/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/OneToManyKeyValueRepo.kt b/postssystem.utils.repos/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/OneToManyKeyValueRepo.kt new file mode 100644 index 00000000..37883226 --- /dev/null +++ b/postssystem.utils.repos/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/OneToManyKeyValueRepo.kt @@ -0,0 +1,23 @@ +package com.insanusmokrassar.postssystem.utils.repos + +import com.insanusmokrassar.postssystem.utils.repos.pagination.Pagination +import com.insanusmokrassar.postssystem.utils.repos.pagination.PaginationResult + +interface OneToManyReadKeyValueRepo : Repo { + suspend fun get(k: Key, pagination: Pagination, reversed: Boolean = false): PaginationResult + suspend fun keys(pagination: Pagination, reversed: Boolean = false): PaginationResult + suspend fun contains(k: Key): Boolean + suspend fun contains(k: Key, v: Value): Boolean + suspend fun count(k: Key): Long + suspend fun count(): Long +} + +interface OneToManyWriteKeyValueRepo : + Repo { + suspend fun add(k: Key, v: Value) + suspend fun remove(k: Key, v: Value) + suspend fun clear(k: Key) +} + +interface OneToManyKeyValueRepo : OneToManyReadKeyValueRepo, + OneToManyWriteKeyValueRepo \ No newline at end of file diff --git a/postssystem.utils.repos/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/Repo.kt b/postssystem.utils.repos/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/Repo.kt new file mode 100644 index 00000000..5023cad5 --- /dev/null +++ b/postssystem.utils.repos/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/Repo.kt @@ -0,0 +1,4 @@ +package com.insanusmokrassar.postssystem.utils.repos + +interface Repo { +} \ No newline at end of file diff --git a/postssystem.utils.repos/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/StandardCRUDRepo.kt b/postssystem.utils.repos/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/StandardCRUDRepo.kt new file mode 100644 index 00000000..1a657078 --- /dev/null +++ b/postssystem.utils.repos/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/StandardCRUDRepo.kt @@ -0,0 +1,34 @@ +package com.insanusmokrassar.postssystem.utils.repos + +import com.insanusmokrassar.postssystem.utils.repos.pagination.Pagination +import com.insanusmokrassar.postssystem.utils.repos.pagination.PaginationResult +import kotlinx.coroutines.flow.Flow + +interface ReadStandardCRUDRepo : + Repo { + suspend fun getByPagination(pagination: Pagination): PaginationResult + suspend fun getById(id: IdType): ObjectType? + suspend fun contains(id: IdType): Boolean +} + +typealias UpdatedValuePair = Pair +val UpdatedValuePair.id + get() = first +val UpdatedValuePair<*, ValueType>.value + get() = second + +interface WriteStandardCRUDRepo : + Repo { + val newObjectsFlow: Flow + val updatedObjectsFlow: Flow + val deletedObjectsIdsFlow: Flow + + suspend fun create(vararg values: InputValueType): List + suspend fun update(id: IdType, value: InputValueType): ObjectType? + suspend fun update(vararg values: UpdatedValuePair): List + suspend fun deleteById(vararg ids: IdType) +} + +interface StandardCRUDRepo + : ReadStandardCRUDRepo, + WriteStandardCRUDRepo diff --git a/postssystem.utils.repos/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/StandardKeyValueRepo.kt b/postssystem.utils.repos/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/StandardKeyValueRepo.kt new file mode 100644 index 00000000..0dddf7e2 --- /dev/null +++ b/postssystem.utils.repos/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/StandardKeyValueRepo.kt @@ -0,0 +1,24 @@ +package com.insanusmokrassar.postssystem.utils.repos + +import com.insanusmokrassar.postssystem.utils.repos.pagination.Pagination +import com.insanusmokrassar.postssystem.utils.repos.pagination.PaginationResult +import kotlinx.coroutines.flow.Flow + +interface StandardReadKeyValueRepo : Repo { + suspend fun get(k: Key): Value? + suspend fun values(pagination: Pagination, reversed: Boolean): PaginationResult + suspend fun keys(pagination: Pagination, reversed: Boolean): PaginationResult + suspend fun contains(key: Key): Boolean + suspend fun count(): Long +} + +interface StandardWriteKeyValueRepo : Repo { + val onNewValue: Flow> + val onValueRemoved: Flow + + suspend fun set(k: Key, v: Value) + suspend fun unset(k: Key) +} + +interface StandardKeyValueRepo : StandardReadKeyValueRepo, + StandardWriteKeyValueRepo \ No newline at end of file diff --git a/postssystem.core/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/utils/pagination/Pagination.kt b/postssystem.utils.repos/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/pagination/Pagination.kt similarity index 90% rename from postssystem.core/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/utils/pagination/Pagination.kt rename to postssystem.utils.repos/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/pagination/Pagination.kt index d4810f13..1045624c 100644 --- a/postssystem.core/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/utils/pagination/Pagination.kt +++ b/postssystem.utils.repos/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/pagination/Pagination.kt @@ -1,4 +1,4 @@ -package com.insanusmokrassar.postssystem.core.utils.pagination +package com.insanusmokrassar.postssystem.utils.repos.pagination import kotlin.math.ceil @@ -45,4 +45,8 @@ fun calculatePagesNumber(datasetSize: Long, pageSize: Int): Int { /** * Calculates pages count for given [datasetSize] */ -fun calculatePagesNumber(datasetSize: Int, pageSize: Int): Int = calculatePagesNumber(datasetSize.toLong(), pageSize) +fun calculatePagesNumber(datasetSize: Int, pageSize: Int): Int = + calculatePagesNumber( + datasetSize.toLong(), + pageSize + ) diff --git a/postssystem.core/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/utils/pagination/PaginationResult.kt b/postssystem.utils.repos/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/pagination/PaginationResult.kt similarity index 87% rename from postssystem.core/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/utils/pagination/PaginationResult.kt rename to postssystem.utils.repos/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/pagination/PaginationResult.kt index dfd3c490..03be6ca7 100644 --- a/postssystem.core/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/utils/pagination/PaginationResult.kt +++ b/postssystem.utils.repos/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/pagination/PaginationResult.kt @@ -1,4 +1,4 @@ -package com.insanusmokrassar.postssystem.core.utils.pagination +package com.insanusmokrassar.postssystem.utils.repos.pagination import kotlinx.serialization.Serializable diff --git a/postssystem.core/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/utils/pagination/SimplePagination.kt b/postssystem.utils.repos/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/pagination/SimplePagination.kt similarity index 66% rename from postssystem.core/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/utils/pagination/SimplePagination.kt rename to postssystem.utils.repos/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/pagination/SimplePagination.kt index b967aeaa..116b6ff3 100644 --- a/postssystem.core/src/commonMain/kotlin/com/insanusmokrassar/postssystem/core/utils/pagination/SimplePagination.kt +++ b/postssystem.utils.repos/src/commonMain/kotlin/com/insanusmokrassar/postssystem/utils/repos/pagination/SimplePagination.kt @@ -1,4 +1,4 @@ -package com.insanusmokrassar.postssystem.core.utils.pagination +package com.insanusmokrassar.postssystem.utils.repos.pagination import kotlinx.serialization.Serializable @@ -8,16 +8,18 @@ const val defaultLargePageSize = 10 const val defaultExtraLargePageSize = 15 @Suppress("NOTHING_TO_INLINE", "FunctionName") -inline fun FirstPagePagination(size: Int = defaultMediumPageSize) = SimplePagination( - page = 0, - size = size -) +inline fun FirstPagePagination(size: Int = defaultMediumPageSize) = + SimplePagination( + page = 0, + size = size + ) @Suppress("NOTHING_TO_INLINE") -inline fun Pagination.nextPage() = SimplePagination( - page + 1, - size -) +inline fun Pagination.nextPage() = + SimplePagination( + page + 1, + size + ) @Serializable data class SimplePagination( diff --git a/settings.gradle b/settings.gradle index 4725db6e..96a4d38c 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,5 +1,7 @@ rootProject.name='postssystem' +include ':postssystem.utils.repos' include ':postssystem.exposed.commons' +include ':postssystem.utils.repos.exposed' include ':postssystem.core' include ':postssystem.core.exposed' include ':postssystem.core.publishing'