interface MyTableSchema {
partitionKey: string;
clusteringKey: number;
otherField: Map<string, string>;
}
type MyTablePrimaryKey = Pick<MyTableSchema, 'partitionKey' | 'clusteringKey'>;
const table = db.table<MyTableSchema, MyTablePrimaryKey>('my_table');
However, if you are constructing a table with Db.createTable, you may use the InferTablePrimaryKey utility type to infer the TS-equivalent type of the primary key from the CreateTableDefinition.
const MyTableSchema = Table.schema({
columns: {
partitionKey: 'text',
clusteringKey: 'int',
otherField: 'map<text, text>',
},
primaryKey: {
partitionBy: ['partitionKey'],
partitionSort: { clusteringKey: 1 },
},
});
type MyTableSchema = typeof MyTableSchema;
type MyTablePrimaryKey = InferTablePrimaryKey<typeof MyTableSchema>;
const table = db.createTable<MyTableSchema, MyTablePrimaryKey>('my_table', {
definition: MyTableSchema,
});
This type is used as the second type parameter of the Table class.
If this is not provided, then the table's primary key type will default to Partial<Schema>
where Schema
is the schema of the table.
Overview
Represents some primary key in a table. This is a generic type that represents some (any) table primary key with any number & types of columns. All it asks for is that the primary key be an object with string keys and any values.
Keep in mind the logical constraints, however:
Constructing this type
Often, you may want to construct this type using the Pick utility type, to select only the fields of the schema which are in the primary key.