Hey I want to generate some code based on my sst s...
# sst
t
Hey I want to generate some code based on my sst stack that can be used in a lambda. Any way to do this with SST?
t
tell me more
t
Iā€™m thinking about working on a construct that is a high level abstraction over a dynamodb table
that generates a typed js client at build time (a bit like prisma) and makes it available in the lambdas
t
I actually don't suggest defining too much of your schema in SST, it only really needs to know the primary + sort keys and any index columns
t
Does it not bother you that you configure your table in the stack and in your code? šŸ˜„
t
My table rarely changes in the stack, it looks pretty generic like this:
Copy code
const table = new Table(ctx.stack, "table", {
    fields: {
      pk: "string",
      sk: "string",
      gsi1pk: "string",
      gsi1sk: "string",
      gsi2pk: "string",
      gsi2sk: "string",
    },
    primaryIndex: {
      partitionKey: "pk",
      sortKey: "sk",
    },
    globalIndexes: {
      gsi1: {
        partitionKey: "gsi1pk",
        sortKey: "gsi1sk",
      },
      gsi2: {
        partitionKey: "gsi2pk",
        sortKey: "gsi2sk",
      },
    },
  })
Would be nice to get just this config in code but any application specific things like "user_email" I don't define here
t
It was going to look like
Copy code
const table = new MagicTable(ctx.stack, "table", {
    entities: [
       { name: 'users', values: { name: 'key', email: 'string' }}
       { name: 'posts', values: { slug: 'key', title: 'string', ..., user: MagicTable.relates_to('users') }
    ]
})
For some single table patterns you can kinda get away some predefined logic.
Copy code
posts.find({ where: { user: 'thdxr' } }).then(results => {....})
not for any serious production reasons but just to see if its actually possible
t
Yeah I generally don't specify specific columns like
name
email
etc in stacks code
since it doesn't really do anything with them
but anyway to answer your original question, sst doesn't help with this but we are getting more into codegen. Nothing fancy here I usually will track everything in the construct and provide a
codgen()
function which I call at the end
t
it could also be more generally useful for service discovery
t
If you want to support multiple tables and generate them all together I'd usually create static functions on the construct to maintain things
t
I built an experiment a year ago to generate a typed s3 client for your buckets https://github.com/Ankcorn/arc-buckets-plugin
Wanted to build the next evolution of it over easter weekend šŸ˜„
g
Do you have any other recommendations for libraries that make it easy to work with the single table design? I have been trying out https://github.com/jeremydaly/dynamodb-toolbox
t
ElectroDB!
just use it!