Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Oct 21, 2024. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 51 additions & 2 deletions platform/src/components/aws/dynamo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,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 @@ -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);

Expand Down Expand Up @@ -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-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