createTable method

Future<CreateTableOutput> createTable({
  1. required String tableName,
  2. List<AttributeDefinition>? attributeDefinitions,
  3. BillingMode? billingMode,
  4. bool? deletionProtectionEnabled,
  5. List<GlobalSecondaryIndex>? globalSecondaryIndexes,
  6. GlobalTableSettingsReplicationMode? globalTableSettingsReplicationMode,
  7. String? globalTableSourceArn,
  8. List<KeySchemaElement>? keySchema,
  9. List<LocalSecondaryIndex>? localSecondaryIndexes,
  10. OnDemandThroughput? onDemandThroughput,
  11. ProvisionedThroughput? provisionedThroughput,
  12. String? resourcePolicy,
  13. SSESpecification? sSESpecification,
  14. StreamSpecification? streamSpecification,
  15. TableClass? tableClass,
  16. List<Tag>? tags,
  17. WarmThroughput? warmThroughput,
})

The CreateTable operation adds a new table to your account. In an Amazon Web Services account, table names must be unique within each Region. That is, you can have two tables with same name if you create the tables in different Regions.

CreateTable is an asynchronous operation. Upon receiving a CreateTable request, DynamoDB immediately returns a response with a TableStatus of CREATING. After the table is created, DynamoDB sets the TableStatus to ACTIVE. You can perform read and write operations only on an ACTIVE table.

You can optionally define secondary indexes on the new table, as part of the CreateTable operation. If you want to create multiple tables with secondary indexes on them, you must create the tables sequentially. Only one table with secondary indexes can be in the CREATING state at any given time.

You can use the DescribeTable action to check the table status.

May throw InternalServerError. May throw InvalidEndpointException. May throw LimitExceededException. May throw ResourceInUseException.

Parameter tableName : The name of the table to create. You can also provide the Amazon Resource Name (ARN) of the table in this parameter.

Parameter attributeDefinitions : An array of attributes that describe the key schema for the table and indexes.

Parameter billingMode : Controls how you are charged for read and write throughput and how you manage capacity. This setting can be changed later.

  • PAY_PER_REQUEST - We recommend using PAY_PER_REQUEST for most DynamoDB workloads. PAY_PER_REQUEST sets the billing mode to On-demand capacity mode.
  • PROVISIONED - We recommend using PROVISIONED for steady workloads with predictable growth where capacity requirements can be reliably forecasted. PROVISIONED sets the billing mode to Provisioned capacity mode.

Parameter deletionProtectionEnabled : Indicates whether deletion protection is to be enabled (true) or disabled (false) on the table.

Parameter globalSecondaryIndexes : One or more global secondary indexes (the maximum is 20) to be created on the table. Each global secondary index in the array includes the following:

  • IndexName - The name of the global secondary index. Must be unique only for this table.
  • KeySchema - Specifies the key schema for the global secondary index. Each global secondary index supports up to 4 partition keys and up to 4 sort keys.
  • Projection - Specifies attributes that are copied (projected) from the table into the index. These are in addition to the primary key attributes and index key attributes, which are automatically projected. Each attribute specification is composed of:
    • ProjectionType - One of the following:
      • KEYS_ONLY - Only the index and primary keys are projected into the index.
      • INCLUDE - Only the specified table attributes are projected into the index. The list of projected attributes is in NonKeyAttributes.
      • ALL - All of the table attributes are projected into the index.
    • NonKeyAttributes - A list of one or more non-key attribute names that are projected into the secondary index. The total count of attributes provided in NonKeyAttributes, summed across all of the secondary indexes, must not exceed 100. If you project the same attribute into two different indexes, this counts as two distinct attributes when determining the total. This limit only applies when you specify the ProjectionType of INCLUDE. You still can specify the ProjectionType of ALL to project all attributes from the source table, even if the table has more than 100 attributes.
  • ProvisionedThroughput - The provisioned throughput settings for the global secondary index, consisting of read and write capacity units.

Parameter globalTableSettingsReplicationMode : Controls the settings synchronization mode for the global table. For multi-account global tables, this parameter is required and the only supported value is ENABLED. For same-account global tables, this parameter is set to ENABLED_WITH_OVERRIDES.

Parameter globalTableSourceArn : The Amazon Resource Name (ARN) of the source table used for the creation of a multi-account global table.

Parameter keySchema : Specifies the attributes that make up the primary key for a table or an index. The attributes in KeySchema must also be defined in the AttributeDefinitions array. For more information, see Data Model in the Amazon DynamoDB Developer Guide.

Each KeySchemaElement in the array is composed of:

  • AttributeName - The name of this key attribute.
  • KeyType - The role that the key attribute will assume:
    • HASH - partition key
    • RANGE - sort key

Implementation

Future<CreateTableOutput> createTable({
  required String tableName,
  List<AttributeDefinition>? attributeDefinitions,
  BillingMode? billingMode,
  bool? deletionProtectionEnabled,
  List<GlobalSecondaryIndex>? globalSecondaryIndexes,
  GlobalTableSettingsReplicationMode? globalTableSettingsReplicationMode,
  String? globalTableSourceArn,
  List<KeySchemaElement>? keySchema,
  List<LocalSecondaryIndex>? localSecondaryIndexes,
  OnDemandThroughput? onDemandThroughput,
  ProvisionedThroughput? provisionedThroughput,
  String? resourcePolicy,
  SSESpecification? sSESpecification,
  StreamSpecification? streamSpecification,
  TableClass? tableClass,
  List<Tag>? tags,
  WarmThroughput? warmThroughput,
}) async {
  final headers = <String, String>{
    'Content-Type': 'application/x-amz-json-1.0',
    'X-Amz-Target': 'DynamoDB_20120810.CreateTable'
  };
  final jsonResponse = await _protocol.send(
    method: 'POST',
    requestUri: '/',
    exceptionFnMap: _exceptionFns,
    // TODO queryParams
    headers: headers,
    payload: {
      'TableName': tableName,
      if (attributeDefinitions != null)
        'AttributeDefinitions': attributeDefinitions,
      if (billingMode != null) 'BillingMode': billingMode.value,
      if (deletionProtectionEnabled != null)
        'DeletionProtectionEnabled': deletionProtectionEnabled,
      if (globalSecondaryIndexes != null)
        'GlobalSecondaryIndexes': globalSecondaryIndexes,
      if (globalTableSettingsReplicationMode != null)
        'GlobalTableSettingsReplicationMode':
            globalTableSettingsReplicationMode.value,
      if (globalTableSourceArn != null)
        'GlobalTableSourceArn': globalTableSourceArn,
      if (keySchema != null) 'KeySchema': keySchema,
      if (localSecondaryIndexes != null)
        'LocalSecondaryIndexes': localSecondaryIndexes,
      if (onDemandThroughput != null)
        'OnDemandThroughput': onDemandThroughput,
      if (provisionedThroughput != null)
        'ProvisionedThroughput': provisionedThroughput,
      if (resourcePolicy != null) 'ResourcePolicy': resourcePolicy,
      if (sSESpecification != null) 'SSESpecification': sSESpecification,
      if (streamSpecification != null)
        'StreamSpecification': streamSpecification,
      if (tableClass != null) 'TableClass': tableClass.value,
      if (tags != null) 'Tags': tags,
      if (warmThroughput != null) 'WarmThroughput': warmThroughput,
    },
  );

  return CreateTableOutput.fromJson(jsonResponse.body);
}