Devin
12/22/2021, 1:49 PMconst customersTable = new sst.Table(this, "Customers", {
fields: {
pk: sst.TableFieldType.STRING,
sk: sst.TableFieldType.STRING,
},
primaryIndex: { partitionKey: "pk", sortKey: "sk" },
});
What I want is to make, createdAt
a secondary primary key so that I can fulfill the access pattern of getCustomerByCreatedAt
It seems like this is the correct way to do this but I”m not sure.
customersTable.addGlobalIndexes({
indexName: "createdAt",
partitionKey: { name: "gs1pk", type: sst.TableFieldType.STRING },
});
thdxr
12/22/2021, 2:00 PMthdxr
12/22/2021, 2:00 PMcreatedAt
Kujtim Hoxha
12/22/2021, 2:11 PMgs1pk
you should probably call the index gs1
Kujtim Hoxha
12/22/2021, 2:14 PM// Organization service table
this.organizationTable = new sst.Table(this, "OrganizationTable", {
dynamodbTable: {
tableName: `${this.stage}-devstride-organizations`,
removalPolicy: process.env.IS_LOCAL
? RemovalPolicy.DESTROY
: RemovalPolicy.RETAIN,
},
fields: {
// Primary index
PK: sst.TableFieldType.STRING,
SK: sst.TableFieldType.STRING,
// GSI1
PK1: sst.TableFieldType.STRING,
SK1: sst.TableFieldType.STRING,
},
primaryIndex: { partitionKey: "PK", sortKey: "SK" },
secondaryIndexes: {
GSI1: {
partitionKey: "PK1",
sortKey: "SK1",
indexProps: {
projectionType: ProjectionType.ALL,
},
},
},
});
In your particular case I imagine that you have some sort of storeId
which the consumer belongs to, in that case your GSI PK
should be the storeId
and your SK
something like ${createdAt}:${customerId}
Kujtim Hoxha
12/22/2021, 2:15 PMDevin
12/22/2021, 2:17 PMcreatedAt
what will happen?Kujtim Hoxha
12/22/2021, 2:26 PMGSI
columns just for the purpose of querying and still keep the columns that are used in the PK
and SK
in the table so in your case you would have
| PK | SK | PK1 | SK1 | storeId | customerId | createdAt |
|------------- |---- |------------ |------------------------- |---------- |------------- |------------ |
| c:customer1 | c: | c:storeId1 | c:2020-01-01:customerId1 | storeId1 | customerId1 | 2020-01-01 |
Kujtim Hoxha
12/22/2021, 2:27 PMDevin
12/22/2021, 2:33 PMKujtim Hoxha
12/22/2021, 2:50 PMKujtim Hoxha
12/22/2021, 2:51 PMDevin
12/22/2021, 9:40 PMDevin
12/22/2021, 9:42 PM