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: extra database connection properties - take 2 #35

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,24 @@ const provider = new Provider(this, "Provider", {
})
```

### Additional connection options

Additional options can be passed to the provider.
The available options can be found [here](https://node-postgres.com/apis/client).

> Keep in mind that certain properties (e.g., username, password, host) are forcibly by this resource. If you provide them in `connectionProps`, they will be ignored.
```ts
const provider = new Provider(this, "Provider", {
vpc: vpc,
instance: instance,
secret: cluster.secret!,
connectionProps: {
ssl: true
}
})
```


## Roles

Create a postgres role (user) for a cluster as follows:
Expand Down
72 changes: 2 additions & 70 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export class Database extends CustomResource implements IDatabase {
ResourceId: props.databaseName,
SecretArn: props.provider.secret.secretArn,
Owner: props.owner?.roleName,
ConnectionProps: props.provider.connectionProps,
},
})
this.node.addDependency(props.provider)
Expand Down
6 changes: 5 additions & 1 deletion src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ export const handler = async (
const resource: RdsSqlResource = event.ResourceProperties.Resource
const resourceId = event.ResourceProperties.ResourceId
const databaseName = event.ResourceProperties.DatabaseName
const connectionProps = event.ResourceProperties.ConnectionProps || {}

if (!Object.keys(jumpTable).includes(event.ResourceProperties.Resource)) {
throw `Resource type '${resource}' not recognised.`
Expand Down Expand Up @@ -310,14 +311,17 @@ export const handler = async (
} else {
database = databaseName ?? secretValues.dbname // connect to given database if possible, else to database mentioned in secret
}
const params = {
const baseParams = {
host: secretValues.host,
port: secretValues.port,
user: secretValues.username,
password: secretValues.password,
database: database,
connectionTimeoutMillis: 30000, // return an error if a connection could not be established within 30 seconds
}
// merge the base params with the new params provided
// directly to the resource
const params = { ...connectionProps, ...baseParams }
log(
`Connecting to host ${params.host}: ${params.port}, database ${params.database} as ${params.user}`
)
Expand Down
10 changes: 10 additions & 0 deletions src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,28 @@ export interface RdsSqlProps {
* @default - empty
*/
readonly functionProps?: NodejsFunctionProps

/**
* Additional properties when establishing the database sql
* connection.
*
* @default - empty object
*/
readonly connectionProps?: any
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, not keen on this.

}

export class Provider extends Construct {
public readonly serviceToken: string
public readonly secret: ISecret
public readonly handler: IFunction
public readonly cluster: IServerlessCluster | IDatabaseCluster | IDatabaseInstance
public readonly connectionProps?: any

constructor(scope: Construct, id: string, props: RdsSqlProps) {
super(scope, id)
this.secret = props.secret
this.cluster = props.cluster
this.connectionProps = props.connectionProps

const functionName = "RdsSql" + slugify("28b9e791-af60-4a33-bca8-ffb6f30ef8c5")
this.handler =
Expand Down
1 change: 1 addition & 0 deletions src/role.custom-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export class Role extends CustomResource {
SecretArn: props.provider.secret.secretArn,
PasswordArn: props.passwordArn,
DatabaseName: props.database ? props.database.databaseName : props.databaseName,
ConnectionProps: props.provider.connectionProps,
},
})
this.node.addDependency(props.provider)
Expand Down
1 change: 1 addition & 0 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class Schema extends CustomResource {
ResourceId: props.schemaName,
SecretArn: props.provider.secret.secretArn,
DatabaseName: props.database ? props.database.databaseName : undefined,
ConnectionProps: props.provider.connectionProps,
},
})
this.node.addDependency(props.provider)
Expand Down
1 change: 1 addition & 0 deletions src/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export class Sql extends CustomResource {
DatabaseName: props.database ? props.database.databaseName : undefined,
Statement: props.statement,
Rollback: props.rollback,
ConnectionProps: props.provider.connectionProps,
},
})
this.node.addDependency(props.provider)
Expand Down