Skip to content

Commit

Permalink
Vector: static get method (#898)
Browse files Browse the repository at this point in the history
* typo

* vector static get method

* sync

---------

Co-authored-by: Frank <[email protected]>
  • Loading branch information
jacob-burgess and fwang committed Aug 28, 2024
1 parent 2d81aaf commit b16b138
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
2 changes: 1 addition & 1 deletion platform/src/components/aws/postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ export class Postgres extends Component implements Link.Linkable {
}

/**
* Reference an existing Postgrest cluster with the given cluster name. This is useful when you
* Reference an existing Postgres cluster with the given cluster name. This is useful when you
* create a Postgres cluster in one stage and want to share it in another. It avoids having to
* create a new Postgres cluster in the other stage.
*
Expand Down
70 changes: 68 additions & 2 deletions platform/src/components/aws/vector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ export interface VectorArgs {
};
}

interface VectorRef {
ref: boolean;
postgres: Postgres;
}

/**
* The `Vector` component lets you store and retrieve vector data in your app.
*
Expand Down Expand Up @@ -88,8 +93,15 @@ export class Vector extends Component implements Link.Linkable {
const parent = this;
const tableName = normalizeTableName();

const postgres = createDB();
createDBTable();
let postgres: Postgres;
if (args && "ref" in args) {
const ref = args as unknown as VectorRef;
postgres = ref.postgres;
} else {
postgres = createDB();
createDBTable();
}

const queryHandler = createQueryHandler();
const putHandler = createPutHandler();
const removeHandler = createRemoveHandler();
Expand Down Expand Up @@ -200,6 +212,60 @@ export class Vector extends Component implements Link.Linkable {
}
}

/**
* Reference an existing Vector database with the given name. This is useful when you
* create a Vector database in one stage and want to share it in another. It avoids having to
* create a new Vector database in the other stage.
*
* :::tip
* You can use the `static get` method to share Vector databases across stages.
* :::
*
* @param name The name of the component.
* @param clusterID The RDS cluster id of the existing Vector database.
*
* @example
* Imagine you create a vector databse in the `dev` stage. And in your personal stage `frank`,
* instead of creating a new database, you want to share the same database from `dev`.
*
* ```ts title="sst.config.ts"
* const vector = $app.stage === "frank"
* ? sst.aws.Vector.get("MyVectorDB", "app-dev-myvectordb")
* : new sst.aws.Vector("MyVectorDB", {
* dimension: 1536
* });
* ```
*
* Here `app-dev-myvectordb` is the ID of the underlying Postgres cluster created in the `dev` stage.
* You can find this by outputting the cluster ID in the `dev` stage.
*
* ```ts title="sst.config.ts"
* return {
* cluster: vector.clusterID
* };
* ```
*
* :::note
* The Vector component creates a Postgres cluster and lambda functions for interfacing with the VectorDB.
* The `static get` method only shares the underlying Postgres cluster. Each stage will have its own
* lambda functions.
* :::
*/
public static get(name: string, clusterID: Input<string>) {
const postgres = Postgres.get(`${name}Database`, clusterID);
return new Vector(name, {
ref: true,
postgres,
} as unknown as VectorArgs);
}

/**
* The ID of the RDS Postgres Cluster.
*/
public get clusterID() {
return this.postgres.nodes.cluster.id;
}

/**
* The underlying [resources](/docs/components/#nodes) this component creates.
*/
Expand Down

0 comments on commit b16b138

Please sign in to comment.