Question - are there any examples or best practice...
# general
k
Question - are there any examples or best practices out there about how to make good use of Typescript language features? I wrote a program that manages a PostgreSQL instance, users, grants and secrets, and stores the secrets optionally in Kubernetes and AWS Secrets Manager. I want users to be able to define an arbitrary number of database users grants, so I defined types for users and classes for grants and for each kind of secret. These which live in a
utils.ts
file and are called from the main
index.ts
. I'm a relative beginner to TS but for the most part really happy with what I came up with, it's nice and clear compared to a lot of Terraform I've worked on in the past! This file structure is suggested in the docs (here: https://www.pulumi.com/docs/iac/guides/basics/organizing-projects-stacks/), but in terms of how best to use Typescript language features like types, interfaces, classes and class constructors, I haven't seen many examples out there that go into this. I'd love to draw on some community knowledge if there's resources out there.
For example, I spent a while thinking about how to handle the situation where I want users to be able to pass in an object nested and ideally optional keys:
Copy code
extraUsers:
  user1:
    credentials:
      type1: true
      type2: false
    grants:
      [...]
  user2:
    [...]
I came up with something that works for this, but it took me a fair while of tinkering around. I'm sure a more experienced Typescript user knows how to handle these things quite nicely but I bet there's quite a few folks with an ops/infra background who are figuring things out by trial and error and google 🙂
l
Have you looked at the ComponentResource class? It's the base class for your custom Pulumi resources.
2
🙏 1
s
We've got some documentation for that here: https://www.pulumi.com/docs/iac/concepts/components/ 🙂
🙌 1
k
@little-cartoon-10569 - not yet, thanks! That looks very relevant 🙂