Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
feat: implement get for dynamoDB (#1178)
Browse files Browse the repository at this point in the history
* feat: implement `get` for dynamoDB

closes #1165

* sync

---------

Co-authored-by: Frank <[email protected]>
  • Loading branch information
natac13 and fwang authored Oct 16, 2024
1 parent e3e055e commit b85fa80
Showing 1 changed file with 51 additions and 2 deletions.
53 changes: 51 additions & 2 deletions platform/src/components/aws/dynamo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,11 @@ export interface DynamoSubscriberArgs {
};
}

interface DynamoRef {
ref: boolean;
table: dynamodb.Table;
}

/**
* The `Dynamo` component lets you add an [Amazon DynamoDB](https://aws.amazon.com/dynamodb/) table to your app.
*
Expand Down Expand Up @@ -415,13 +420,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);

Expand Down Expand Up @@ -696,6 +707,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-mytable")
* : new sst.aws.Dynamo("MyTable");
* ```
*
* Here `app-dev-mytable` is the name of the DynamoDB 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<string>) {
return new Dynamo(name, {
ref: true,
table: dynamodb.Table.get(`${name}Table`, tableName),
} satisfies DynamoRef as unknown as DynamoArgs);
}

/** @internal */
public getSSTLink() {
return {
Expand Down

0 comments on commit b85fa80

Please sign in to comment.