Anyone have any examples of adding a GSI1PK exampl...
# help
d
Anyone have any examples of adding a GSI1PK example in SST. I’m trying to do the single table design thing. My table is created like
Copy code
const 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.
Copy code
customersTable.addGlobalIndexes({
      indexName: "createdAt",
      partitionKey: { name: "gs1pk", type: sst.TableFieldType.STRING },
    });
t
The index name should probably be generic
instead of specific to
createdAt
k
since you use
gs1pk
you should probably call the index
gs1
Here is an example of how I have set this up
Copy code
// 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}
^ this is assuming that you want the customers sorted by the createAt date
d
Yeah that’s very close to it. So for existing items that have a
createdAt
what will happen?
k
So if there is data already in the system you will have to: • Create the index • Run a migration script that adds the new PK/SK for all records What I usually do is that I keep the
GSI
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
Copy code
| PK          	| SK 	| PK1        	| SK1                     	| storeId  	| customerId  	| createdAt  	|
|-------------	|----	|------------	|-------------------------	|----------	|-------------	|------------	|
| c:customer1 	| c: 	| c:storeId1 	| c:2020-01-01:customerId1 	| storeId1 	| customerId1 	| 2020-01-01 	|
^ you probably need to expand the thread panel to see the table correctly 😂
d
Thanks kujtim! I need to learn how to do this anyway so I guess now's the time
k
I have started using https://github.com/sensedeep/dynamodb-onetable for my personal project if you are using JS or TS this is a pretty good library
It has some small issues here and there but overall I am happy with it
d
I have Dynobase
so I can use that should I need to. I’m not exactly sure if at this point I know enough about DynamoDB to use an abstraction. IME it’s better to fumble your way through and learn the why first. I’ll absolutely look at dynamo-onetable as this goes along