@fy-stack/database-construct 0.0.125 → 0.0.127
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +16 -4
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/lib/database-construct.d.ts +11 -6
- package/dist/lib/database-construct.d.ts.map +1 -1
- package/dist/lib/database-construct.js +37 -21
- package/dist/lib/database-user-construct.d.ts +18 -0
- package/dist/lib/database-user-construct.d.ts.map +1 -0
- package/dist/lib/database-user-construct.js +27 -0
- package/dist/lib/types.d.ts +23 -0
- package/dist/lib/types.d.ts.map +1 -0
- package/dist/lib/types.js +2 -0
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,7 +1,19 @@
|
|
|
1
|
-
#
|
|
1
|
+
# Database Construct Documentation
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
## `DatabaseConstruct`
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Represents a database construct that provisions an RDS database instance along with associated secrets. This class implements both `Attachable` and `Grantable` interfaces.
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
- **Properties**
|
|
8
|
+
- `dbSecrets: ISecret`
|
|
9
|
+
- Stores the secrets related to the database instance.
|
|
10
|
+
- `db: DatabaseInstance`
|
|
11
|
+
- The provisioned RDS database instance.
|
|
12
|
+
- `dbName: string`
|
|
13
|
+
- The name assigned to the database instance.
|
|
14
|
+
|
|
15
|
+
- **Constructor**
|
|
16
|
+
- `constructor(scope: Construct, id: string, props: DatabaseConstructProps)`
|
|
17
|
+
- Initializes the database construct with a unique identifier and configuration options defined by `DatabaseConstructProps`.
|
|
18
|
+
|
|
19
|
+
[//]: # ( - For further details, see the project’s documentation [here](https://github.com/festusyuma/fy-stack/blob/main/packages/types/README.md).)
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAC7D,OAAO,EAAE,qBAAqB,EAAE,MAAM,+BAA+B,CAAC;AACtE,YAAY,EAAE,sBAAsB,EAAE,MAAM,aAAa,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DatabaseConstruct = void 0;
|
|
3
|
+
exports.DatabaseUserConstruct = exports.DatabaseConstruct = void 0;
|
|
4
4
|
var database_construct_1 = require("./lib/database-construct");
|
|
5
5
|
Object.defineProperty(exports, "DatabaseConstruct", { enumerable: true, get: function () { return database_construct_1.DatabaseConstruct; } });
|
|
6
|
+
var database_user_construct_1 = require("./lib/database-user-construct");
|
|
7
|
+
Object.defineProperty(exports, "DatabaseUserConstruct", { enumerable: true, get: function () { return database_user_construct_1.DatabaseUserConstruct; } });
|
|
@@ -1,19 +1,24 @@
|
|
|
1
|
-
import { Attachable, Grantable } from '@fy-stack/types';
|
|
1
|
+
import type { Attachable, Grantable } from '@fy-stack/types';
|
|
2
2
|
import { IGrantable } from 'aws-cdk-lib/aws-iam';
|
|
3
3
|
import * as rds from 'aws-cdk-lib/aws-rds';
|
|
4
4
|
import * as secretsManager from 'aws-cdk-lib/aws-secretsmanager';
|
|
5
5
|
import { Construct } from 'constructs';
|
|
6
|
+
import { DatabaseUserConstruct } from './database-user-construct';
|
|
7
|
+
import { DatabaseConstructProps } from './types';
|
|
8
|
+
/**
|
|
9
|
+
* Represents a database construct that provisions an RDS database instance along with associated secrets.
|
|
10
|
+
* It implements both {@link Attachable `Attachable`} and {@link Grantable `Grantable`} interfaces.
|
|
11
|
+
*/
|
|
6
12
|
export declare class DatabaseConstruct extends Construct implements Attachable, Grantable {
|
|
7
|
-
|
|
8
|
-
db: rds.
|
|
9
|
-
|
|
10
|
-
constructor(scope: Construct, id: string);
|
|
13
|
+
secrets: secretsManager.ISecret;
|
|
14
|
+
db: rds.DatabaseInstance;
|
|
15
|
+
constructor(scope: Construct, id: string, props: DatabaseConstructProps);
|
|
11
16
|
grantable(grant: IGrantable): void;
|
|
12
17
|
attachable(): {
|
|
13
18
|
arn: string;
|
|
14
|
-
name: string;
|
|
15
19
|
secretsArn: string;
|
|
16
20
|
secretsName: string;
|
|
17
21
|
};
|
|
22
|
+
createDatabase(username: string, dbName: string): DatabaseUserConstruct;
|
|
18
23
|
}
|
|
19
24
|
//# sourceMappingURL=database-construct.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"database-construct.d.ts","sourceRoot":"","sources":["../../src/lib/database-construct.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"database-construct.d.ts","sourceRoot":"","sources":["../../src/lib/database-construct.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAG7D,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACjD,OAAO,KAAK,GAAG,MAAM,qBAAqB,CAAC;AAE3C,OAAO,KAAK,cAAc,MAAM,gCAAgC,CAAC;AACjE,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEvC,OAAO,EAAE,qBAAqB,EAAE,MAAM,2BAA2B,CAAC;AAClE,OAAO,EAAE,sBAAsB,EAAE,MAAM,SAAS,CAAC;AAEjD;;;GAGG;AACH,qBAAa,iBACX,SAAQ,SACR,YAAW,UAAU,EAAE,SAAS;IAEzB,OAAO,EAAE,cAAc,CAAC,OAAO,CAAC;IAChC,EAAE,EAAE,GAAG,CAAC,gBAAgB,CAAC;gBAEpB,KAAK,EAAE,SAAS,EAAE,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,sBAAsB;IAgCvE,SAAS,CAAC,KAAK,EAAE,UAAU;IAY3B,UAAU;;;;;IAQV,cAAc,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM;CAQhD"}
|
|
@@ -2,44 +2,60 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.DatabaseConstruct = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
|
+
const ec2 = tslib_1.__importStar(require("aws-cdk-lib/aws-ec2"));
|
|
5
6
|
const iam = tslib_1.__importStar(require("aws-cdk-lib/aws-iam"));
|
|
6
7
|
const rds = tslib_1.__importStar(require("aws-cdk-lib/aws-rds"));
|
|
8
|
+
const aws_rds_1 = require("aws-cdk-lib/aws-rds");
|
|
7
9
|
const constructs_1 = require("constructs");
|
|
10
|
+
const database_user_construct_1 = require("./database-user-construct");
|
|
11
|
+
/**
|
|
12
|
+
* Represents a database construct that provisions an RDS database instance along with associated secrets.
|
|
13
|
+
* It implements both {@link Attachable `Attachable`} and {@link Grantable `Grantable`} interfaces.
|
|
14
|
+
*/
|
|
8
15
|
class DatabaseConstruct extends constructs_1.Construct {
|
|
9
|
-
|
|
16
|
+
secrets;
|
|
10
17
|
db;
|
|
11
|
-
|
|
12
|
-
constructor(scope, id) {
|
|
18
|
+
constructor(scope, id, props) {
|
|
13
19
|
super(scope, id);
|
|
14
|
-
const
|
|
15
|
-
this.
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
20
|
+
const vpc = ec2.Vpc.fromLookup(this, 'VPC', props?.vpcId ? { vpcId: props.vpcId } : { isDefault: true });
|
|
21
|
+
this.db = new rds.DatabaseInstance(this, 'DB', {
|
|
22
|
+
vpc,
|
|
23
|
+
engine: props?.engine ?? aws_rds_1.DatabaseInstanceEngine.POSTGRES,
|
|
24
|
+
instanceType: ec2.InstanceType.of(props?.instance?.class ?? ec2.InstanceClass.T4G, props?.instance?.size ?? ec2.InstanceSize.MICRO),
|
|
25
|
+
publiclyAccessible: props?.public,
|
|
26
|
+
vpcSubnets: {
|
|
27
|
+
subnetType: props?.public
|
|
28
|
+
? ec2.SubnetType.PUBLIC
|
|
29
|
+
: ec2.SubnetType.PRIVATE_ISOLATED,
|
|
30
|
+
},
|
|
19
31
|
});
|
|
20
|
-
|
|
21
|
-
this.db
|
|
22
|
-
|
|
32
|
+
this.db.addRotationSingleUser();
|
|
33
|
+
if (!this.db.secret)
|
|
34
|
+
throw new Error('Could not create database credentials secret');
|
|
35
|
+
this.secrets = this.db.secret;
|
|
23
36
|
}
|
|
24
37
|
grantable(grant) {
|
|
25
|
-
|
|
26
|
-
effect: iam.Effect.ALLOW,
|
|
27
|
-
actions: ['rds-data:*'],
|
|
28
|
-
resources: [this.db.clusterArn],
|
|
29
|
-
}));
|
|
38
|
+
this.db.grantConnect(grant);
|
|
30
39
|
grant.grantPrincipal.addToPrincipalPolicy(new iam.PolicyStatement({
|
|
31
40
|
effect: iam.Effect.ALLOW,
|
|
32
41
|
actions: ['secretsmanager:GetSecretValue'],
|
|
33
|
-
resources: [this.
|
|
42
|
+
resources: [this.secrets.secretArn],
|
|
34
43
|
}));
|
|
35
44
|
}
|
|
36
45
|
attachable() {
|
|
37
46
|
return {
|
|
38
|
-
arn: this.db.
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
secretsName: this.dbSecrets.secretName,
|
|
47
|
+
arn: this.db.instanceArn,
|
|
48
|
+
secretsArn: this.secrets.secretArn,
|
|
49
|
+
secretsName: this.secrets.secretName,
|
|
42
50
|
};
|
|
43
51
|
}
|
|
52
|
+
createDatabase(username, dbName) {
|
|
53
|
+
return new database_user_construct_1.DatabaseUserConstruct(this, username + 'DatabaseUserStack', {
|
|
54
|
+
username,
|
|
55
|
+
dbName,
|
|
56
|
+
db: this.db,
|
|
57
|
+
masterSecret: this.secrets,
|
|
58
|
+
});
|
|
59
|
+
}
|
|
44
60
|
}
|
|
45
61
|
exports.DatabaseConstruct = DatabaseConstruct;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Attachable } from '@fy-stack/types';
|
|
2
|
+
import { DatabaseInstance } from 'aws-cdk-lib/aws-rds';
|
|
3
|
+
import { ISecret } from 'aws-cdk-lib/aws-secretsmanager';
|
|
4
|
+
import { Construct } from 'constructs';
|
|
5
|
+
type Props = {
|
|
6
|
+
db: DatabaseInstance;
|
|
7
|
+
masterSecret: ISecret;
|
|
8
|
+
username: string;
|
|
9
|
+
dbName: string;
|
|
10
|
+
};
|
|
11
|
+
export declare class DatabaseUserConstruct extends Construct implements Attachable {
|
|
12
|
+
secrets: ISecret;
|
|
13
|
+
dbName: string;
|
|
14
|
+
constructor(scope: Construct, id: string, props: Props);
|
|
15
|
+
attachable(): Record<string, string>;
|
|
16
|
+
}
|
|
17
|
+
export {};
|
|
18
|
+
//# sourceMappingURL=database-user-construct.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"database-user-construct.d.ts","sourceRoot":"","sources":["../../src/lib/database-user-construct.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,gBAAgB,EAAkB,MAAM,qBAAqB,CAAC;AACvE,OAAO,EAAE,OAAO,EAAE,MAAM,gCAAgC,CAAC;AACzD,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEvC,KAAK,KAAK,GAAG;IACX,EAAE,EAAE,gBAAgB,CAAC;IACrB,YAAY,EAAE,OAAO,CAAC;IACtB,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,qBAAa,qBAAsB,SAAQ,SAAU,YAAW,UAAU;IACjE,OAAO,EAAE,OAAO,CAAC;IACjB,MAAM,EAAE,MAAM,CAAC;gBAEV,KAAK,EAAE,SAAS,EAAE,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,KAAK;IActD,UAAU,IAAI,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;CAOrC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DatabaseUserConstruct = void 0;
|
|
4
|
+
const aws_rds_1 = require("aws-cdk-lib/aws-rds");
|
|
5
|
+
const constructs_1 = require("constructs");
|
|
6
|
+
class DatabaseUserConstruct extends constructs_1.Construct {
|
|
7
|
+
secrets;
|
|
8
|
+
dbName;
|
|
9
|
+
constructor(scope, id, props) {
|
|
10
|
+
super(scope, id);
|
|
11
|
+
this.dbName = props.dbName;
|
|
12
|
+
this.secrets = new aws_rds_1.DatabaseSecret(this, 'Secret', {
|
|
13
|
+
username: props.username,
|
|
14
|
+
masterSecret: props.masterSecret,
|
|
15
|
+
});
|
|
16
|
+
this.secrets.attach(props.db);
|
|
17
|
+
// todo create rds user with create db permissions
|
|
18
|
+
}
|
|
19
|
+
attachable() {
|
|
20
|
+
return {
|
|
21
|
+
dbName: this.dbName,
|
|
22
|
+
secretArn: this.secrets.secretArn,
|
|
23
|
+
secretName: this.secrets.secretName,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.DatabaseUserConstruct = DatabaseUserConstruct;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { InstanceClass, InstanceSize } from 'aws-cdk-lib/aws-ec2';
|
|
2
|
+
import type { DatabaseInstanceProps, IInstanceEngine } from 'aws-cdk-lib/aws-rds';
|
|
3
|
+
/**
|
|
4
|
+
* Properties required for setting up a database construct.
|
|
5
|
+
*/
|
|
6
|
+
export type DatabaseConstructProps = {
|
|
7
|
+
/** Optionally pass in existing VPC id */
|
|
8
|
+
vpcId?: string;
|
|
9
|
+
/**
|
|
10
|
+
* Define specific RDS instance {@link IInstanceEngine engine}
|
|
11
|
+
*/
|
|
12
|
+
engine?: IInstanceEngine;
|
|
13
|
+
/** Define specific RDS instance {@link InstanceClass class} and {@link InstanceSize size} to use. */
|
|
14
|
+
instance?: {
|
|
15
|
+
class: InstanceClass;
|
|
16
|
+
size: InstanceSize;
|
|
17
|
+
};
|
|
18
|
+
/** Make database public */
|
|
19
|
+
public?: boolean;
|
|
20
|
+
/** Other RDS properties {@link DatabaseInstanceProps} */
|
|
21
|
+
additionalData?: Omit<DatabaseInstanceProps, 'instanceType' | 'databaseName' | 'publiclyAccessible' | 'engine'>;
|
|
22
|
+
};
|
|
23
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/lib/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACvE,OAAO,KAAK,EACV,qBAAqB,EACrB,eAAe,EAChB,MAAM,qBAAqB,CAAC;AAE7B;;GAEG;AACH,MAAM,MAAM,sBAAsB,GAAG;IACnC,yCAAyC;IACzC,KAAK,CAAC,EAAE,MAAM,CAAA;IACd;;OAEG;IACH,MAAM,CAAC,EAAE,eAAe,CAAC;IACzB,qGAAqG;IACrG,QAAQ,CAAC,EAAE;QAAE,KAAK,EAAE,aAAa,CAAC;QAAC,IAAI,EAAE,YAAY,CAAA;KAAE,CAAC;IACxD,2BAA2B;IAC3B,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,yDAAyD;IACzD,cAAc,CAAC,EAAE,IAAI,CACnB,qBAAqB,EACrB,cAAc,GAAG,cAAc,GAAG,oBAAoB,GAAG,QAAQ,CAClE,CAAC;CACH,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fy-stack/database-construct",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.127",
|
|
4
4
|
"dependencies": {
|
|
5
5
|
"tslib": "^2.3.0",
|
|
6
|
-
"@fy-stack/types": "0.0.
|
|
6
|
+
"@fy-stack/types": "0.0.127"
|
|
7
7
|
},
|
|
8
8
|
"peerDependencies": {
|
|
9
|
-
"aws-cdk-lib": "2.
|
|
9
|
+
"aws-cdk-lib": "2.166.0",
|
|
10
10
|
"constructs": "10.4.2"
|
|
11
11
|
},
|
|
12
12
|
"type": "commonjs",
|