Skip to content

Commit 3f540e6

Browse files
committed
upd
1 parent 08ba867 commit 3f540e6

29 files changed

Lines changed: 965 additions & 1019 deletions

.prettierignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# based on https://gist.github.com/thinkricardo/74f37d82b686de371b0853a5d66d559c
2+
3+
# ignore all files
4+
*
5+
6+
# include all folders
7+
!**/
8+
9+
# include files to format
10+
!*.yaml
11+
!*.yml
12+
!*.md
13+
pnpm-lock.yaml
14+
15+
# exclusions
16+
**/*.d.ts
17+
**/cdk.out/**/*
18+
**/*.gomplate.yaml
19+
**/README.md
20+
cloud/lib/docker-image/traefik/plugins/htransformation/**/*
21+
22+
# cdktf
23+
**/.gen/**

cdk-postgresql/README.md

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,56 +13,56 @@
1313
A `Provider` instance is required in order to establish a connection to your Postgresql instance
1414

1515
```typescript
16-
const theMasterSecret: secretsmanager.ISecret;
16+
const theMasterSecret: secretsmanager.ISecret
1717

1818
// you can connect to to a publicly available instance
19-
const provider = new Provider(this, "Provider", {
20-
host: "your.db.host.net",
21-
username: "master",
19+
const provider = new Provider(this, 'Provider', {
20+
host: 'your.db.host.net',
21+
username: 'master',
2222
password: theMasterSecret,
2323
port: 5432,
2424
vpc,
25-
securityGroups: [dbClusterSecurityGroup],
26-
});
25+
securityGroups: [dbClusterSecurityGroup]
26+
})
2727

2828
// or a private instance in your VPC
29-
const provider = new Provider(this, "Provider", {
30-
host: "your.db.host.net",
31-
username: "master",
29+
const provider = new Provider(this, 'Provider', {
30+
host: 'your.db.host.net',
31+
username: 'master',
3232
password: theMasterSecret,
3333
port: 5432,
3434
vpc,
35-
securityGroups: [yourDatabaseSecurityGroup],
36-
});
35+
securityGroups: [yourDatabaseSecurityGroup]
36+
})
3737
```
3838

3939
You can reuse the same `Provider` instance when creating your different `Role` and `Database` instances.
4040

4141
### Database
4242

4343
```typescript
44-
import { Database } from "@botpress/cdk-postgresql";
44+
import { Database } from '@botpress/cdk-postgresql'
4545

46-
const db = new Database(this, "Database", {
46+
const db = new Database(this, 'Database', {
4747
provider,
48-
name: "mynewdb",
49-
owner: "somerole",
50-
removalPolicy: cdk.RemovalPolicy.RETAIN, // default is RETAIN
51-
});
48+
name: 'mynewdb',
49+
owner: 'somerole',
50+
removalPolicy: cdk.RemovalPolicy.RETAIN // default is RETAIN
51+
})
5252
```
5353

5454
### Role
5555

5656
```typescript
57-
import { Role } from "@botpress/cdk-postgresql";
57+
import { Role } from '@botpress/cdk-postgresql'
5858

59-
const rolePassword: secretsmanager.ISecret;
60-
const role = new Role(this, "Role", {
59+
const rolePassword: secretsmanager.ISecret
60+
const role = new Role(this, 'Role', {
6161
provider,
62-
name: "newrole",
62+
name: 'newrole',
6363
password: rolePassword,
64-
removalPolicy: cdk.RemovalPolicy.RETAIN, // Default is DESTROY
65-
});
64+
removalPolicy: cdk.RemovalPolicy.RETAIN // Default is DESTROY
65+
})
6666
```
6767

6868
## Tips
@@ -72,17 +72,17 @@ const role = new Role(this, "Role", {
7272
In many cases, you want to create a `Role` and use that role as the `Database` owner. You can achieve this by adding an [explicit dependency](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib-readme.html#dependencies) between the two instances:
7373

7474
```typescript
75-
const roleName = "newRole";
76-
const role = new Role(this, "Role", {
75+
const roleName = 'newRole'
76+
const role = new Role(this, 'Role', {
7777
provider,
7878
name: roleName,
79-
password: rolePassword,
80-
});
81-
const db = new Database(this, "Database", {
79+
password: rolePassword
80+
})
81+
const db = new Database(this, 'Database', {
8282
provider,
83-
name: "mydb",
84-
owner: roleName,
85-
});
83+
name: 'mydb',
84+
owner: roleName
85+
})
8686

87-
db.node.addDependency(role);
87+
db.node.addDependency(role)
8888
```
Lines changed: 77 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,146 +1,131 @@
1-
import format from "pg-format";
2-
import { getConnectedClient, validateConnection, hashCode } from "./util";
3-
import * as postgres from "./postgres";
1+
import format from 'pg-format'
2+
import { getConnectedClient, validateConnection, hashCode } from './util'
3+
import * as postgres from './postgres'
44

55
import {
66
CloudFormationCustomResourceEvent,
77
CloudFormationCustomResourceCreateEvent,
88
CloudFormationCustomResourceUpdateEvent,
9-
CloudFormationCustomResourceDeleteEvent,
10-
} from "aws-lambda/trigger/cloudformation-custom-resource";
11-
import { Connection } from "./lambda.types";
9+
CloudFormationCustomResourceDeleteEvent
10+
} from 'aws-lambda/trigger/cloudformation-custom-resource'
11+
import { Connection } from './lambda.types'
1212

1313
interface Props {
14-
ServiceToken: string;
15-
Connection: Connection;
16-
Name: string;
17-
Owner: string;
14+
ServiceToken: string
15+
Connection: Connection
16+
Name: string
17+
Owner: string
1818
}
1919

2020
export const handler = async (event: CloudFormationCustomResourceEvent) => {
2121
switch (event.RequestType) {
22-
case "Create":
23-
return handleCreate(event);
24-
case "Update":
25-
return handleUpdate(event);
26-
case "Delete":
27-
return handleDelete(event);
22+
case 'Create':
23+
return handleCreate(event)
24+
case 'Update':
25+
return handleUpdate(event)
26+
case 'Delete':
27+
return handleDelete(event)
2828
}
29-
};
29+
}
3030

3131
const generatePhysicalId = (props: Props): string => {
32-
const { Host, Port } = props.Connection;
33-
const suffix = Math.abs(hashCode(`${Host}-${Port}`));
34-
return `${props.Name}-${suffix}`;
35-
};
32+
const { Host, Port } = props.Connection
33+
const suffix = Math.abs(hashCode(`${Host}-${Port}`))
34+
return `${props.Name}-${suffix}`
35+
}
3636

3737
const handleCreate = async (event: CloudFormationCustomResourceCreateEvent) => {
38-
const props = event.ResourceProperties as Props;
39-
validateProps(props);
38+
const props = event.ResourceProperties as Props
39+
validateProps(props)
4040
await createDatabase({
4141
connection: props.Connection,
4242
name: props.Name,
43-
owner: props.Owner,
44-
});
43+
owner: props.Owner
44+
})
4545
return {
46-
PhysicalResourceId: generatePhysicalId(props),
47-
};
48-
};
46+
PhysicalResourceId: generatePhysicalId(props)
47+
}
48+
}
4949

5050
const handleUpdate = async (event: CloudFormationCustomResourceUpdateEvent) => {
51-
const props = event.ResourceProperties as Props;
52-
validateProps(props);
53-
const oldProps = event.OldResourceProperties as Props;
51+
const props = event.ResourceProperties as Props
52+
validateProps(props)
53+
const oldProps = event.OldResourceProperties as Props
5454

55-
const oldPhysicalResourceId = generatePhysicalId(oldProps);
56-
const physicalResourceId = generatePhysicalId(props);
55+
const oldPhysicalResourceId = generatePhysicalId(oldProps)
56+
const physicalResourceId = generatePhysicalId(props)
5757

5858
if (physicalResourceId != oldPhysicalResourceId) {
5959
await createDatabase({
6060
connection: props.Connection,
6161
name: props.Name,
62-
owner: props.Owner,
63-
});
64-
return { PhysicalResourceId: physicalResourceId };
62+
owner: props.Owner
63+
})
64+
return { PhysicalResourceId: physicalResourceId }
6565
}
6666

6767
if (props.Owner != oldProps.Owner) {
68-
await updateDbOwner(props.Connection, props.Name, props.Owner);
68+
await updateDbOwner(props.Connection, props.Name, props.Owner)
6969
}
7070

71-
return { PhysicalResourceId: physicalResourceId };
72-
};
71+
return { PhysicalResourceId: physicalResourceId }
72+
}
7373

7474
const handleDelete = async (event: CloudFormationCustomResourceDeleteEvent) => {
75-
const props = event.ResourceProperties as Props;
76-
validateProps(props);
77-
await deleteDatabase(props.Connection, props.Name, props.Owner);
78-
return {};
79-
};
75+
const props = event.ResourceProperties as Props
76+
validateProps(props)
77+
await deleteDatabase(props.Connection, props.Name, props.Owner)
78+
return {}
79+
}
8080

8181
const validateProps = (props: Props) => {
82-
if (!("Connection" in props)) {
83-
throw "Connection property is required";
82+
if (!('Connection' in props)) {
83+
throw 'Connection property is required'
8484
}
85-
validateConnection(props.Connection);
85+
validateConnection(props.Connection)
8686

87-
if (!("Name" in props)) {
88-
throw "Name property is required";
87+
if (!('Name' in props)) {
88+
throw 'Name property is required'
8989
}
90-
if (!("Owner" in props)) {
91-
throw "Owner property is required";
90+
if (!('Owner' in props)) {
91+
throw 'Owner property is required'
9292
}
93-
};
94-
95-
export const createDatabase = async (props: {
96-
connection: Connection;
97-
name: string;
98-
owner: string;
99-
}) => {
100-
const { connection, name, owner } = props;
101-
console.log("Creating database", name);
102-
const client = await getConnectedClient(connection);
93+
}
94+
95+
export const createDatabase = async (props: { connection: Connection; name: string; owner: string }) => {
96+
const { connection, name, owner } = props
97+
console.log('Creating database', name)
98+
const client = await getConnectedClient(connection)
10399
try {
104-
await postgres.createDatabase({ client, name, owner });
105-
console.log("Created database");
100+
await postgres.createDatabase({ client, name, owner })
101+
console.log('Created database')
106102
} finally {
107-
await client.end();
103+
await client.end()
108104
}
109-
};
110-
111-
export const deleteDatabase = async (
112-
connection: Connection,
113-
name: string,
114-
owner: string
115-
) => {
116-
console.log("Deleting database", name);
117-
const client = await getConnectedClient(connection);
105+
}
106+
107+
export const deleteDatabase = async (connection: Connection, name: string, owner: string) => {
108+
console.log('Deleting database', name)
109+
const client = await getConnectedClient(connection)
118110
try {
119111
// First, drop all remaining DB connections
120112
// Sometimes, DB connections are still alive even though the ECS service has been deleted
121113
await client.query(
122-
format(
123-
"SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE datname=%L",
124-
name
125-
)
126-
);
114+
format('SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE datname=%L', name)
115+
)
127116
// Then, drop the DB
128-
await client.query(format("DROP DATABASE %I", name));
117+
await client.query(format('DROP DATABASE %I', name))
129118
} finally {
130-
await client.end();
119+
await client.end()
131120
}
132-
};
133-
134-
export const updateDbOwner = async (
135-
connection: Connection,
136-
name: string,
137-
owner: string
138-
) => {
139-
console.log(`Updating DB ${name} owner to ${owner}`);
140-
const client = await getConnectedClient(connection);
121+
}
122+
123+
export const updateDbOwner = async (connection: Connection, name: string, owner: string) => {
124+
console.log(`Updating DB ${name} owner to ${owner}`)
125+
const client = await getConnectedClient(connection)
141126
try {
142-
await client.query(format("ALTER DATABASE %I OWNER TO %I", name, owner));
127+
await client.query(format('ALTER DATABASE %I OWNER TO %I', name, owner))
143128
} finally {
144-
await client.end();
129+
await client.end()
145130
}
146-
};
131+
}

0 commit comments

Comments
 (0)