Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement get for dynamoDB #1178

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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: Input<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-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<string>) {
return new Dynamo(name, {
ref: true,
table: dynamodb.Table.get(`${name}Table`, tableName),
} as unknown as DynamoArgs);
}

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