commonMain.aws.sdk.kotlin.hll.dynamodbmapper.items.ItemSchema.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of dynamodb-mapper-jvm Show documentation
Show all versions of dynamodb-mapper-jvm Show documentation
High level DynamoDbMapper client
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package aws.sdk.kotlin.hll.dynamodbmapper.items
import aws.sdk.kotlin.hll.dynamodbmapper.items.internal.ItemSchemaCompositeKeyImpl
import aws.sdk.kotlin.hll.dynamodbmapper.items.internal.ItemSchemaPartitionKeyImpl
import aws.smithy.kotlin.runtime.ExperimentalApi
/**
* Defines a schema for handling objects of a certain type, including an [ItemConverter] for converting between objects
* items and a [KeySpec] for identifying primary keys.
* @param T The type of objects described by this schema
*/
@ExperimentalApi
public interface ItemSchema {
/**
* The [ItemConverter] used to convert between objects and items
*/
public val converter: ItemConverter
/**
* The name(s) of the attributes which form the primary key of this table
*/
public val keyAttributeNames: Set
/**
* Represents a schema with a primary key consisting of a single partition key
* @param T The type of objects described by this schema
* @param PK The type of the partition key property, either [String], [Number], or [ByteArray]
*/
@ExperimentalApi
public interface PartitionKey : ItemSchema {
/**
* The [KeySpec] for the partition key
*/
public val partitionKey: KeySpec
override val keyAttributeNames: Set
get() = setOf(partitionKey.name)
}
/**
* Represents a schema with a primary key that is a composite of a partition key and a sort key
* @param T The type of objects described by this schema
* @param PK The type of the partition key property, either [String], [Number], or [ByteArray]
* @param SK The type of the sort key property, either [String], [Number], or [ByteArray]
*/
@ExperimentalApi
public interface CompositeKey : PartitionKey {
/**
* The [KeySpec] for the sort key
*/
public val sortKey: KeySpec
override val keyAttributeNames: Set
get() = setOf(partitionKey.name, sortKey.name)
}
}
/**
* Create a new item schema with a primary key consisting of a single partition key.
* @param T The type of objects described by this schema
* @param PK The type of the partition key property, either [String], [Number], or [ByteArray]
* @param converter The [ItemConverter] used to convert between objects and items
* @param partitionKey The [KeySpec] for the partition key
*/
@ExperimentalApi
@Suppress("FunctionName")
public fun ItemSchema(converter: ItemConverter, partitionKey: KeySpec): ItemSchema.PartitionKey =
ItemSchemaPartitionKeyImpl(converter, partitionKey)
/**
* Create a new item schema with a primary key consisting of a single partition key.
* @param T The type of objects described by this schema
* @param PK The type of the partition key property, either [String], [Number], or [ByteArray]
* @param SK The type of the sort key property, either [String], [Number], or [ByteArray]
* @param converter The [ItemConverter] used to convert between objects and items
* @param partitionKey The [KeySpec] for the partition key
* @param sortKey The [KeySpec] for the sort key
*/
@ExperimentalApi
@Suppress("FunctionName")
public fun ItemSchema(
converter: ItemConverter,
partitionKey: KeySpec,
sortKey: KeySpec,
): ItemSchema.CompositeKey = ItemSchemaCompositeKeyImpl(converter, partitionKey, sortKey)
/**
* Associate this [ItemConverter] with a [KeySpec] for a partition key to form a complete [ItemSchema]
* @param T The type of objects described by this schema
* @param PK The type of the partition key property, either [String], [Number], or [ByteArray]
* @param partitionKey The [KeySpec] that describes the partition key
*/
@ExperimentalApi
public fun ItemConverter.withKeySpec(partitionKey: KeySpec): ItemSchema.PartitionKey =
ItemSchema(this, partitionKey)
/**
* Associate this [ItemConverter] with [KeySpec] instances for a composite key to form a complete [ItemSchema]
* @param T The type of objects described by this schema
* @param PK The type of the partition key property, either [String], [Number], or [ByteArray]
* @param SK The type of the sort key property, either [String], [Number], or [ByteArray]
* @param partitionKey The [KeySpec] that describes the partition key
* @param sortKey The [KeySpec] that describes the sort key
*/
@ExperimentalApi
public fun ItemConverter.withKeySpec(
partitionKey: KeySpec,
sortKey: KeySpec,
): ItemSchema.CompositeKey = ItemSchema(this, partitionKey, sortKey)