createTable method

Future<CreateTableOutput> createTable({
  1. required List<AttributeDefinition> attributeDefinitions,
  2. required List<KeySchemaElement> keySchema,
  3. required String tableName,
  4. BillingMode? billingMode,
  5. List<GlobalSecondaryIndex>? globalSecondaryIndexes,
  6. List<LocalSecondaryIndex>? localSecondaryIndexes,
  7. ProvisionedThroughput? provisionedThroughput,
  8. SSESpecification? sSESpecification,
  9. StreamSpecification? streamSpecification,
  10. List<Tag>? tags,
})

The CreateTable operation adds a new table to your account. In an AWS 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 ResourceInUseException. May throw LimitExceededException. May throw InternalServerError.

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

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 List<AttributeDefinition> attributeDefinitions,
  required List<KeySchemaElement> keySchema,
  required String tableName,
  BillingMode? billingMode,
  List<GlobalSecondaryIndex>? globalSecondaryIndexes,
  List<LocalSecondaryIndex>? localSecondaryIndexes,
  ProvisionedThroughput? provisionedThroughput,
  SSESpecification? sSESpecification,
  StreamSpecification? streamSpecification,
  List<Tag>? tags,
}) async {
  ArgumentError.checkNotNull(attributeDefinitions, 'attributeDefinitions');
  ArgumentError.checkNotNull(keySchema, 'keySchema');
  ArgumentError.checkNotNull(tableName, 'tableName');
  _s.validateStringLength(
    'tableName',
    tableName,
    3,
    255,
    isRequired: true,
  );
  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: {
      'AttributeDefinitions': attributeDefinitions,
      'KeySchema': keySchema,
      'TableName': tableName,
      if (billingMode != null) 'BillingMode': billingMode.toValue(),
      if (globalSecondaryIndexes != null)
        'GlobalSecondaryIndexes': globalSecondaryIndexes,
      if (localSecondaryIndexes != null)
        'LocalSecondaryIndexes': localSecondaryIndexes,
      if (provisionedThroughput != null)
        'ProvisionedThroughput': provisionedThroughput,
      if (sSESpecification != null) 'SSESpecification': sSESpecification,
      if (streamSpecification != null)
        'StreamSpecification': streamSpecification,
      if (tags != null) 'Tags': tags,
    },
  );

  return CreateTableOutput.fromJson(jsonResponse.body);
}