https://serverless-stack.com/ logo
#sst
Title
# sst
t

Thomas Ankcorn

04/14/2022, 8:46 PM
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

thdxr

04/14/2022, 8:47 PM
tell me more
t

Thomas Ankcorn

04/14/2022, 8:47 PM
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

thdxr

04/14/2022, 8:49 PM
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

Thomas Ankcorn

04/14/2022, 8:50 PM
Does it not bother you that you configure your table in the stack and in your code? šŸ˜„
t

thdxr

04/14/2022, 8:50 PM
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

Thomas Ankcorn

04/14/2022, 9:04 PM
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

thdxr

04/14/2022, 9:04 PM
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

Thomas Ankcorn

04/14/2022, 9:05 PM
it could also be more generally useful for service discovery
t

thdxr

04/14/2022, 9:06 PM
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

Thomas Ankcorn

04/14/2022, 9:06 PM
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

Geoffrey Dagley

05/19/2022, 7:38 PM
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

thdxr

05/19/2022, 7:39 PM
ElectroDB!
just use it!