Hi Channel! I don’t know if this is the right place as my question is related to AWS dynamodb with ...
a

asavio

over 2 years ago
Hi Channel! I don’t know if this is the right place as my question is related to AWS dynamodb with the V2 enhanced client (of course in Spring Boot + Kotlin). I’m following this blog post. I keep getting this exception from the dynamodb SDK when I try to write to dynamo -
java.lang.IllegalArgumentException: Attempt to execute an operation that requires a primary index without defining any primary key attributes in the table metadata.
. I define partition key in the data class. But it keeps complaining that I don’t have it!. Here’s the entire code:
package com.redacted.backend.monolith.app.controllers

import com.redacted.backend.monolith.config.dynamodb.createDynamoDbClient
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.graphql.data.method.annotation.Argument
import org.springframework.graphql.data.method.annotation.MutationMapping
import org.springframework.stereotype.Controller
import org.springframework.stereotype.Repository
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbEnhancedClient
import software.amazon.awssdk.enhanced.dynamodb.TableSchema
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbBean
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbPartitionKey
import software.amazon.awssdk.enhanced.dynamodb.mapper.annotations.DynamoDbSortKey

@Controller
class DogController(private val repo: DogRepository) {

    @MutationMapping
    fun createDog(@Argument dog: Dog): String {
        return repo.create(dog)
    }

}

@DynamoDbBean
data class Dog(
    @get:DynamoDbPartitionKey
    val dogId: String? = null,
    @get:DynamoDbSortKey
    val rating: Int = 0,
    val name: String? = null
)

@Configuration
class DynamoClient(
    @Value("aws.dynamodb.access-key") private val dynamoDbAccessKey: String,
    @Value("aws.dynamodb.secret-key") private val dynamoDbSecretKey: String,
    @Value("aws.dynamodb.region") private val dynamoDbRegion: String
) {

    @Bean
    fun dynamoDbClient(): DynamoDbEnhancedClient = createDynamoDbClient(
        accessKey = dynamoDbAccessKey,
        secretKey = dynamoDbSecretKey,
        region = dynamoDbRegion
    )

}

@Repository
class DogRepository(private var client: DynamoDbEnhancedClient) {

    private val table = client.table("dog_table", TableSchema.fromBean(Dog::class.java))
    fun create(dog: Dog): String {
        table.putItem(dog)
        println("Written the dog to db successfully!")
        return "Written"
    }
}
Here’s the whole error: Any help would be very much appreciated! Error message is in the 🧵
Hello, I have a little problem with exposed and I don't understand where my mistake comes from, I'm ...
b

Bastien Leveque

almost 3 years ago
Hello, I have a little problem with exposed and I don't understand where my mistake comes from, I'm not supposed to make a transaction here, and I have no idea where to add one to make it work.. . I'm still quite new to using the library, and speaking English quite badly, google is not the most optimal in translating documentation My error :
java.lang.IllegalStateException: No transaction in context.
	at org.jetbrains.exposed.sql.transactions.TransactionManager$Companion.current(TransactionApi.kt:132)
	at org.jetbrains.exposed.sql.AbstractQuery.getTransaction(AbstractQuery.kt:9)
	at org.jetbrains.exposed.sql.AbstractQuery.iterator(AbstractQuery.kt:61)
	at kotlin.collections.CollectionsKt___CollectionsKt.firstOrNull(_Collections.kt:269)
	at org.jetbrains.exposed.dao.Entity.getReadValues(Entity.kt:45)
	at org.jetbrains.exposed.dao.Entity.lookup(Entity.kt:194)
	at org.jetbrains.exposed.dao.Entity.getValue(Entity.kt:174)
	at fr.ftnl.grupo.database.models.tbl.User.getDiscordUsername(Users.kt:25)
	at fr.ftnl.grupo.database.mediator.UsersMediator.getUserByDiscordId(UsersMediator.kt:21)
	at fr.ftnl.grupo.core.commands.list.buttons.JoinEvent.action(JoinEvent.kt:19)
	at fr.ftnl.grupo.core.commands.CommandManager.dispatch(CommandManager.kt:124)
	at fr.ftnl.grupo.core.listeners.Dispatcher.onEvent(Dispatcher.kt:21)
	at dev.minn.jda.ktx.events.CoroutineEventManager.runListener$suspendImpl(CoroutineEventManager.kt:101)
	at dev.minn.jda.ktx.events.CoroutineEventManager.runListener(CoroutineEventManager.kt)
	at dev.minn.jda.ktx.events.CoroutineEventManager$handle$1.invokeSuspend(CoroutineEventManager.kt:91)
	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
	at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
	at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664)
The class it tells me is wrong :
package fr.ftnl.grupo.database.models.tbl

import fr.ftnl.grupo.database.models.tbj.UserGametag
import fr.ftnl.grupo.database.models.tbj.UserGametags
import org.jetbrains.exposed.dao.IntEntity
import org.jetbrains.exposed.dao.IntEntityClass
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.Column
import org.jetbrains.exposed.sql.jodatime.CurrentDateTime
import org.jetbrains.exposed.sql.jodatime.datetime
import org.joda.time.DateTime

object Users : IntIdTable("TBL_USERS_USR") {
    val discordId: Column<Long> = long("discord_id")
    val discordUsername: Column<String> = varchar("discord_username", 40)
    
    val createdAt: Column<DateTime> = datetime("created_at").defaultExpression(CurrentDateTime)
}

class User(id: EntityID<Int>) : IntEntity(id) {
    companion object : IntEntityClass<User>(Users)
    
    var discordId by Users.discordId
    var discordUsername by Users.discordUsername
    
    val gametags by UserGametag referrersOn UserGametags.user
    
    val createdAt by Users.createdAt
}
The whole project is also available on github : https://github.com/OcelusPRO/grupo/tree/dev