@ttoss/lambda-postgres-query 0.2.0 → 0.2.2

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.
@@ -0,0 +1,18 @@
1
+ import { CloudFormationTemplate } from '@ttoss/cloudformation';
2
+ export { CloudFormationTemplate } from '@ttoss/cloudformation';
3
+ import { Handler } from 'aws-lambda';
4
+ import { QueryParams } from '../index.cjs';
5
+ import 'pg';
6
+
7
+ declare const HANDLER_DEFAULT = "handler.handler";
8
+ declare const MEMORY_SIZE_DEFAULT = 128;
9
+ declare const TIMEOUT_DEFAULT = 30;
10
+ declare const createLambdaQueryTemplate: ({ handler, memorySize, timeout, }?: {
11
+ handler?: string;
12
+ memorySize?: number;
13
+ timeout?: number;
14
+ }) => CloudFormationTemplate;
15
+
16
+ declare const handler: Handler<QueryParams>;
17
+
18
+ export { HANDLER_DEFAULT, MEMORY_SIZE_DEFAULT, TIMEOUT_DEFAULT, createLambdaQueryTemplate, handler };
@@ -0,0 +1,18 @@
1
+ import { CloudFormationTemplate } from '@ttoss/cloudformation';
2
+ export { CloudFormationTemplate } from '@ttoss/cloudformation';
3
+ import { Handler } from 'aws-lambda';
4
+ import { QueryParams } from '../index.js';
5
+ import 'pg';
6
+
7
+ declare const HANDLER_DEFAULT = "handler.handler";
8
+ declare const MEMORY_SIZE_DEFAULT = 128;
9
+ declare const TIMEOUT_DEFAULT = 30;
10
+ declare const createLambdaQueryTemplate: ({ handler, memorySize, timeout, }?: {
11
+ handler?: string;
12
+ memorySize?: number;
13
+ timeout?: number;
14
+ }) => CloudFormationTemplate;
15
+
16
+ declare const handler: Handler<QueryParams>;
17
+
18
+ export { HANDLER_DEFAULT, MEMORY_SIZE_DEFAULT, TIMEOUT_DEFAULT, createLambdaQueryTemplate, handler };
@@ -0,0 +1,210 @@
1
+ /** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
2
+ "use strict";
3
+
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all) __defProp(target, name, {
10
+ get: all[name],
11
+ enumerable: true
12
+ });
13
+ };
14
+ var __copyProps = (to, from, except, desc) => {
15
+ if (from && typeof from === "object" || typeof from === "function") {
16
+ for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
17
+ get: () => from[key],
18
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
19
+ });
20
+ }
21
+ return to;
22
+ };
23
+ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
24
+ value: true
25
+ }), mod);
26
+
27
+ // src/cloudformation/index.ts
28
+ var cloudformation_exports = {};
29
+ __export(cloudformation_exports, {
30
+ HANDLER_DEFAULT: () => HANDLER_DEFAULT,
31
+ MEMORY_SIZE_DEFAULT: () => MEMORY_SIZE_DEFAULT,
32
+ TIMEOUT_DEFAULT: () => TIMEOUT_DEFAULT,
33
+ createLambdaQueryTemplate: () => createLambdaQueryTemplate,
34
+ handler: () => handler
35
+ });
36
+ module.exports = __toCommonJS(cloudformation_exports);
37
+
38
+ // src/cloudformation/createLambdaQueryTemplate.ts
39
+ var HANDLER_DEFAULT = "handler.handler";
40
+ var MEMORY_SIZE_DEFAULT = 128;
41
+ var TIMEOUT_DEFAULT = 30;
42
+ var createLambdaQueryTemplate = ({
43
+ handler: handler2 = HANDLER_DEFAULT,
44
+ memorySize = 128,
45
+ timeout = 30
46
+ } = {}) => {
47
+ return {
48
+ AWSTemplateFormatVersion: "2010-09-09",
49
+ Description: "A Lambda function to query PostgreSQL.",
50
+ Parameters: {
51
+ DatabaseHost: {
52
+ Type: "String",
53
+ Description: "Database host."
54
+ },
55
+ DatabaseHostReadOnly: {
56
+ Type: "String",
57
+ Description: "Database host read only."
58
+ },
59
+ DatabaseName: {
60
+ Type: "String",
61
+ Description: "Database name."
62
+ },
63
+ DatabaseUsername: {
64
+ Type: "String",
65
+ Description: "Database username."
66
+ },
67
+ DatabasePassword: {
68
+ Type: "String",
69
+ Description: "Database password."
70
+ },
71
+ DatabasePort: {
72
+ Type: "String",
73
+ Default: "5432",
74
+ Description: "Database port."
75
+ },
76
+ LambdaS3Bucket: {
77
+ Type: "String",
78
+ Description: "The S3 bucket where the Lambda code is stored."
79
+ },
80
+ LambdaS3Key: {
81
+ Type: "String",
82
+ Description: "The S3 key where the Lambda code is stored."
83
+ },
84
+ LambdaS3ObjectVersion: {
85
+ Type: "String",
86
+ Description: "The S3 object version of the Lambda code."
87
+ },
88
+ SecurityGroupIds: {
89
+ Description: "Security Group IDs",
90
+ Type: "List<AWS::EC2::SecurityGroup::Id>"
91
+ },
92
+ SubnetIds: {
93
+ Description: "Subnet IDs",
94
+ Type: "List<AWS::EC2::Subnet::Id>"
95
+ }
96
+ },
97
+ Resources: {
98
+ LambdaQueryExecutionRole: {
99
+ Type: "AWS::IAM::Role",
100
+ Properties: {
101
+ AssumeRolePolicyDocument: {
102
+ Version: "2012-10-17",
103
+ Statement: [{
104
+ Effect: "Allow",
105
+ Principal: {
106
+ Service: "lambda.amazonaws.com"
107
+ },
108
+ Action: "sts:AssumeRole"
109
+ }]
110
+ },
111
+ ManagedPolicyArns: ["arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole", "arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole"]
112
+ }
113
+ },
114
+ LambdaQueryFunction: {
115
+ Type: "AWS::Lambda::Function",
116
+ Properties: {
117
+ Code: {
118
+ S3Bucket: {
119
+ Ref: "LambdaS3Bucket"
120
+ },
121
+ S3Key: {
122
+ Ref: "LambdaS3Key"
123
+ },
124
+ S3ObjectVersion: {
125
+ Ref: "LambdaS3ObjectVersion"
126
+ }
127
+ },
128
+ MemorySize: memorySize,
129
+ Timeout: timeout,
130
+ Handler: handler2,
131
+ Role: {
132
+ "Fn::GetAtt": ["LambdaQueryExecutionRole", "Arn"]
133
+ },
134
+ Runtime: "nodejs20.x",
135
+ Environment: {
136
+ Variables: {
137
+ DATABASE_HOST: {
138
+ Ref: "DatabaseHost"
139
+ },
140
+ DATABASE_HOST_READ_ONLY: {
141
+ Ref: "DatabaseHostReadOnly"
142
+ },
143
+ DATABASE_NAME: {
144
+ Ref: "DatabaseName"
145
+ },
146
+ DATABASE_USERNAME: {
147
+ Ref: "DatabaseUsername"
148
+ },
149
+ DATABASE_PASSWORD: {
150
+ Ref: "DatabasePassword"
151
+ },
152
+ DATABASE_PORT: {
153
+ Ref: "DatabasePort"
154
+ }
155
+ }
156
+ },
157
+ VpcConfig: {
158
+ SecurityGroupIds: {
159
+ Ref: "SecurityGroupIds"
160
+ },
161
+ SubnetIds: {
162
+ Ref: "SubnetIds"
163
+ }
164
+ }
165
+ }
166
+ }
167
+ }
168
+ };
169
+ };
170
+
171
+ // src/cloudformation/lambdaQueryHandler.ts
172
+ var import_pg = require("pg");
173
+ var database = process.env.DATABASE_NAME;
174
+ var username = process.env.DATABASE_USERNAME;
175
+ var password = process.env.DATABASE_PASSWORD;
176
+ var host = process.env.DATABASE_HOST;
177
+ var hostReadOnly = process.env.DATABASE_HOST_READ_ONLY;
178
+ var port = process.env.DATABASE_PORT;
179
+ var handler = async event => {
180
+ try {
181
+ const client = new import_pg.Client({
182
+ database,
183
+ user: username,
184
+ password,
185
+ host: event.readOnly && hostReadOnly ? hostReadOnly : host,
186
+ port: Number(port)
187
+ });
188
+ await client.connect();
189
+ try {
190
+ const res = await client.query(event);
191
+ return res;
192
+ } finally {
193
+ await client.end();
194
+ }
195
+ } catch (error) {
196
+ console.error("Error running query", {
197
+ error,
198
+ event
199
+ });
200
+ throw error;
201
+ }
202
+ };
203
+ // Annotate the CommonJS export names for ESM import in node:
204
+ 0 && (module.exports = {
205
+ HANDLER_DEFAULT,
206
+ MEMORY_SIZE_DEFAULT,
207
+ TIMEOUT_DEFAULT,
208
+ createLambdaQueryTemplate,
209
+ handler
210
+ });
@@ -0,0 +1,168 @@
1
+ /** Powered by @ttoss/config. https://ttoss.dev/docs/modules/packages/config/ */
2
+
3
+ // src/cloudformation/createLambdaQueryTemplate.ts
4
+ var HANDLER_DEFAULT = "handler.handler";
5
+ var MEMORY_SIZE_DEFAULT = 128;
6
+ var TIMEOUT_DEFAULT = 30;
7
+ var createLambdaQueryTemplate = ({
8
+ handler: handler2 = HANDLER_DEFAULT,
9
+ memorySize = 128,
10
+ timeout = 30
11
+ } = {}) => {
12
+ return {
13
+ AWSTemplateFormatVersion: "2010-09-09",
14
+ Description: "A Lambda function to query PostgreSQL.",
15
+ Parameters: {
16
+ DatabaseHost: {
17
+ Type: "String",
18
+ Description: "Database host."
19
+ },
20
+ DatabaseHostReadOnly: {
21
+ Type: "String",
22
+ Description: "Database host read only."
23
+ },
24
+ DatabaseName: {
25
+ Type: "String",
26
+ Description: "Database name."
27
+ },
28
+ DatabaseUsername: {
29
+ Type: "String",
30
+ Description: "Database username."
31
+ },
32
+ DatabasePassword: {
33
+ Type: "String",
34
+ Description: "Database password."
35
+ },
36
+ DatabasePort: {
37
+ Type: "String",
38
+ Default: "5432",
39
+ Description: "Database port."
40
+ },
41
+ LambdaS3Bucket: {
42
+ Type: "String",
43
+ Description: "The S3 bucket where the Lambda code is stored."
44
+ },
45
+ LambdaS3Key: {
46
+ Type: "String",
47
+ Description: "The S3 key where the Lambda code is stored."
48
+ },
49
+ LambdaS3ObjectVersion: {
50
+ Type: "String",
51
+ Description: "The S3 object version of the Lambda code."
52
+ },
53
+ SecurityGroupIds: {
54
+ Description: "Security Group IDs",
55
+ Type: "List<AWS::EC2::SecurityGroup::Id>"
56
+ },
57
+ SubnetIds: {
58
+ Description: "Subnet IDs",
59
+ Type: "List<AWS::EC2::Subnet::Id>"
60
+ }
61
+ },
62
+ Resources: {
63
+ LambdaQueryExecutionRole: {
64
+ Type: "AWS::IAM::Role",
65
+ Properties: {
66
+ AssumeRolePolicyDocument: {
67
+ Version: "2012-10-17",
68
+ Statement: [{
69
+ Effect: "Allow",
70
+ Principal: {
71
+ Service: "lambda.amazonaws.com"
72
+ },
73
+ Action: "sts:AssumeRole"
74
+ }]
75
+ },
76
+ ManagedPolicyArns: ["arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole", "arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole"]
77
+ }
78
+ },
79
+ LambdaQueryFunction: {
80
+ Type: "AWS::Lambda::Function",
81
+ Properties: {
82
+ Code: {
83
+ S3Bucket: {
84
+ Ref: "LambdaS3Bucket"
85
+ },
86
+ S3Key: {
87
+ Ref: "LambdaS3Key"
88
+ },
89
+ S3ObjectVersion: {
90
+ Ref: "LambdaS3ObjectVersion"
91
+ }
92
+ },
93
+ MemorySize: memorySize,
94
+ Timeout: timeout,
95
+ Handler: handler2,
96
+ Role: {
97
+ "Fn::GetAtt": ["LambdaQueryExecutionRole", "Arn"]
98
+ },
99
+ Runtime: "nodejs20.x",
100
+ Environment: {
101
+ Variables: {
102
+ DATABASE_HOST: {
103
+ Ref: "DatabaseHost"
104
+ },
105
+ DATABASE_HOST_READ_ONLY: {
106
+ Ref: "DatabaseHostReadOnly"
107
+ },
108
+ DATABASE_NAME: {
109
+ Ref: "DatabaseName"
110
+ },
111
+ DATABASE_USERNAME: {
112
+ Ref: "DatabaseUsername"
113
+ },
114
+ DATABASE_PASSWORD: {
115
+ Ref: "DatabasePassword"
116
+ },
117
+ DATABASE_PORT: {
118
+ Ref: "DatabasePort"
119
+ }
120
+ }
121
+ },
122
+ VpcConfig: {
123
+ SecurityGroupIds: {
124
+ Ref: "SecurityGroupIds"
125
+ },
126
+ SubnetIds: {
127
+ Ref: "SubnetIds"
128
+ }
129
+ }
130
+ }
131
+ }
132
+ }
133
+ };
134
+ };
135
+
136
+ // src/cloudformation/lambdaQueryHandler.ts
137
+ import { Client } from "pg";
138
+ var database = process.env.DATABASE_NAME;
139
+ var username = process.env.DATABASE_USERNAME;
140
+ var password = process.env.DATABASE_PASSWORD;
141
+ var host = process.env.DATABASE_HOST;
142
+ var hostReadOnly = process.env.DATABASE_HOST_READ_ONLY;
143
+ var port = process.env.DATABASE_PORT;
144
+ var handler = async event => {
145
+ try {
146
+ const client = new Client({
147
+ database,
148
+ user: username,
149
+ password,
150
+ host: event.readOnly && hostReadOnly ? hostReadOnly : host,
151
+ port: Number(port)
152
+ });
153
+ await client.connect();
154
+ try {
155
+ const res = await client.query(event);
156
+ return res;
157
+ } finally {
158
+ await client.end();
159
+ }
160
+ } catch (error) {
161
+ console.error("Error running query", {
162
+ error,
163
+ event
164
+ });
165
+ throw error;
166
+ }
167
+ };
168
+ export { HANDLER_DEFAULT, MEMORY_SIZE_DEFAULT, TIMEOUT_DEFAULT, createLambdaQueryTemplate, handler };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ttoss/lambda-postgres-query",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "Create a Lambda function that queries a PostgreSQL database.",
5
5
  "author": "ttoss",
6
6
  "contributors": [
@@ -21,7 +21,7 @@
21
21
  "./cloudformation": {
22
22
  "import": "./dist/esm/cloudformation.js",
23
23
  "require": "./dist/cloudformation.js",
24
- "types": "./dist/cloudformation.d.ts"
24
+ "types": "./dist/cloudformation/index.d.ts"
25
25
  }
26
26
  },
27
27
  "files": [
@@ -1,5 +1,7 @@
1
1
  import type { CloudFormationTemplate } from '@ttoss/cloudformation';
2
2
 
3
+ export type { CloudFormationTemplate };
4
+
3
5
  export const HANDLER_DEFAULT = 'handler.handler';
4
6
 
5
7
  export const MEMORY_SIZE_DEFAULT = 128;