@@ -2,18 +2,27 @@ import { type SQSEvent, type EventBridgeEvent } from "aws-lambda";
22import pg , { type QueryResult , type QueryResultRow } from "pg" ;
33import Cursor from "pg-cursor" ;
44import { SendMessageBatchCommand , SQSClient } from "@aws-sdk/client-sqs" ;
5- import { Octokit } from "@octokit/rest" ;
5+ import type { Octokit } from "@octokit/rest" with { "resolution-mode" : "import" } ;
66
77type ScheduledEvent = EventBridgeEvent < "Scheduled Event" , unknown > ;
8- const client = new Octokit ( {
9- auth : process . env . GITHUB_TOKEN ,
10- } ) ;
8+
9+ // This mess is required because @octokit /rest is ESM only and AWS Lambda on docker requires CJS.
10+ let _client : Octokit ;
11+ async function getGithubClient ( ) {
12+ if ( _client ) return _client ;
13+
14+ const { Octokit } = await import ( "@octokit/rest" ) ;
15+ _client = new Octokit ( {
16+ auth : process . env . GITHUB_TOKEN ,
17+ } ) ;
18+ return _client ;
19+ }
1120
1221const connectionString = process . env . DATABASE_URL ;
1322if ( ! connectionString ) {
1423 throw new Error ( "DATABASE_URL env var missing" ) ;
1524}
16- const dbClient = new pg . Client ( { connectionString } ) ;
25+ const dbClient = new pg . Pool ( { connectionString, max : 1 } ) ;
1726
1827export async function query < T extends QueryResultRow > (
1928 text : string ,
@@ -27,17 +36,22 @@ export async function* queryCursor<T extends QueryResultRow>(
2736 params : any , // eslint-disable-line @typescript-eslint/no-explicit-any
2837 batchSize = 1 ,
2938) : AsyncGenerator < T > {
30- const cursor = dbClient . query ( new Cursor < T > ( text , params ) ) ;
39+ const client = await dbClient . connect ( ) ;
40+ try {
41+ const cursor = client . query ( new Cursor < T > ( text , params ) ) ;
3142
32- let size = 0 ;
33- do {
34- const rows = await cursor . read ( batchSize ) ;
35- size = rows . length ;
43+ let size = 0 ;
44+ do {
45+ const rows = await cursor . read ( batchSize ) ;
46+ size = rows . length ;
3647
37- for ( const row of rows ) {
38- yield row ;
39- }
40- } while ( size > 0 ) ;
48+ for ( const row of rows ) {
49+ yield row ;
50+ }
51+ } while ( size > 0 ) ;
52+ } finally {
53+ client . release ( ) ;
54+ }
4155}
4256
4357export async function handler ( event : SQSEvent | ScheduledEvent ) {
@@ -193,6 +207,8 @@ async function createBlobForBook(
193207 languageCode : string ,
194208 book : Book ,
195209) : Promise < TreeItem > {
210+ const client = await getGithubClient ( ) ;
211+
196212 const result = await client . git . createBlob ( {
197213 owner : GH_OWNER ,
198214 repo : GH_REPO ,
@@ -208,6 +224,8 @@ async function createBlobForBook(
208224}
209225
210226async function createTree ( items : TreeItem [ ] ) : Promise < string > {
227+ const client = await getGithubClient ( ) ;
228+
211229 const result = await client . git . getTree ( {
212230 owner : GH_OWNER ,
213231 repo : GH_REPO ,
@@ -224,6 +242,8 @@ async function createTree(items: TreeItem[]): Promise<string> {
224242}
225243
226244async function createCommit ( code : string , treeSha : string ) {
245+ const client = await getGithubClient ( ) ;
246+
227247 const parentResult = await client . git . getRef ( {
228248 owner : GH_OWNER ,
229249 repo : GH_REPO ,
0 commit comments