namedFrom

inline fun <T> namedFrom(parameterBlock: T): QueryParameters

Sets query parameters by using the query's JSON serializer to serialize the given object. The resulting JSON Object is used as the named parameter map.

For example, if you have a data class like this:

    // Annotate as @Serializable if using kotlinx.serialization
data class MyParams(val name: String, val number: Int)

then

    parameters = QueryParameters.namedFrom(MyParams("Fido", 3))

is equivalent to

    parameters = QueryParameters.named {
param("name", "Fido")
param("number", 3)
}

Parameters

parameterBlock

The object to serialize to get named parameters.

Samples

import com.couchbase.client.kotlin.Cluster
import com.couchbase.client.kotlin.query.QueryMetadata
import com.couchbase.client.kotlin.query.QueryParameters
import com.couchbase.client.kotlin.query.QueryResult
import com.couchbase.client.kotlin.query.execute
import kotlinx.serialization.Serializable

fun main() { 
   //sampleStart 
   // Query with named parameters from a parameter block
@Serializable // (or whatever annotation your JsonSerializer requires)
data class MyParameters(val type: String, val limit: Int)

val result: QueryResult = cluster
    .query(
        "select * from `travel-sample` where type = @type limit @limit",
        parameters = QueryParameters.namedFrom(
            MyParameters(type = "airline", limit = 3)
        )
    )
    .execute()

result.rows.forEach { println(it) } 
   //sampleEnd
}