From bb306b9ecbb27ce7f1a0ab5d5036a0eb026763d4 Mon Sep 17 00:00:00 2001 From: Sean Campbell Date: Mon, 30 Sep 2024 08:41:08 -0400 Subject: [PATCH] feat: implement `get` for dynamoDB closes #1165 --- platform/src/components/aws/dynamo.ts | 53 ++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 2 deletions(-) diff --git a/platform/src/components/aws/dynamo.ts b/platform/src/components/aws/dynamo.ts index 9f8bb891b..180ed2710 100644 --- a/platform/src/components/aws/dynamo.ts +++ b/platform/src/components/aws/dynamo.ts @@ -290,6 +290,11 @@ export interface DynamoSubscriberArgs { }; } +interface DynamoRef { + ref: boolean; + table: Input; +} + /** * The `Dynamo` component lets you add an [Amazon DynamoDB](https://aws.amazon.com/dynamodb/) table to your app. * @@ -404,13 +409,19 @@ export class Dynamo extends Component implements Link.Linkable { opts: ComponentResourceOptions = {}, ) { super(__pulumiType, name, args, opts); + this.constructorName = name; + this.constructorOpts = opts; + + if (args && "ref" in args) { + const ref = args as unknown as DynamoRef; + this.table = output(ref.table); + return; + } const parent = this; const table = createTable(); - this.constructorName = name; - this.constructorOpts = opts; this.table = table; this.isStreamEnabled = Boolean(args.stream); @@ -676,6 +687,44 @@ export class Dynamo extends Component implements Link.Linkable { }); } + /** + * Reference an existing DynamoDB Table with the given table name. This is useful when you + * create a table in one stage and want to share it in another stage. It avoid having to + * create a new table in the other stage. + * + * :::tip + * You can use the `static get` method to share a table across stages. + * ::: + * + * @param name The name of the component. + * @param tableName The name of the DynamoDB Table. + * + * @example + * Imagine you create a table in the `dev` stage. And in your personal stage `frank`, + * instead of creating a new table, you want to share the table from `dev`. + * + * ```ts title=sst.config.ts" + * const table = $app.stage === "frank" + * ? sst.aws.Dynamo.get("MyTable", "app-dev-table-12345678") + * : new sst.aws.Dynamo("MyTable"); + * ``` + * + * Here `app-dev-table-12345678` is the auto-generated table name for the table created + * in the `dev` stage. You can find this by outputting the table name in the `dev` stage. + * + * ```ts title="sst.config.ts" + * return { + * table: table.name + * }; + * ``` + */ + public static get(name: string, tableName: Input) { + return new Dynamo(name, { + ref: true, + table: dynamodb.Table.get(`${name}Table`, tableName), + } as unknown as DynamoArgs); + } + /** @internal */ public getSSTLink() { return {