Skip to content

Commit 123e926

Browse files
committed
chore: fix lint
1 parent 52398df commit 123e926

4 files changed

Lines changed: 123 additions & 38 deletions

File tree

examples/cloudrun/knex/tedious/index.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ async function getPasswordConnection() {
7575
// tedious driver-specific connector options
7676
encrypt: true,
7777
trustServerCertificate: true,
78-
}
78+
},
7979
},
8080
// Set min/max pool size, and other pool options
81-
pool: { min: 1, max: 5 }
81+
pool: {min: 1, max: 5},
8282
});
8383
}
8484

@@ -94,10 +94,18 @@ app.get('/', async (req, res) => {
9494
try {
9595
const db = await getConnectionSettings();
9696
const result = await db.raw('SELECT 1');
97-
res.send(`Database connection successful (password authentication), result: ${JSON.stringify(result)}`);
98-
} catch (err: any) {
97+
res.send(
98+
'Database connection successful (password authentication), result: ' +
99+
`${JSON.stringify(result)}`
100+
);
101+
} catch (err: unknown) {
99102
console.error(err);
100-
res.status(500).send(`Error connecting to the database (password authentication): ${err.message}`);
103+
res
104+
.status(500)
105+
.send(
106+
'Error connecting to the database (password authentication): ' +
107+
`${(err as Error).message}`
108+
);
101109
}
102110
});
103111

examples/cloudrun/prisma/postgresql/index.ts

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515
import express from 'express';
1616
import {resolve} from 'node:path';
1717
import fs from 'node:fs';
18-
import {Connector, IpAddressTypes, AuthTypes} from '@google-cloud/cloud-sql-connector';
18+
import {
19+
Connector,
20+
IpAddressTypes,
21+
AuthTypes,
22+
} from '@google-cloud/cloud-sql-connector';
1923
import {PrismaClient} from '@prisma/client';
2024

2125
const app = express();
@@ -58,28 +62,31 @@ async function getIamConnection() {
5862

5963
// Creates a randomly named unix socket path for the local proxy.
6064
const dir = resolve('/tmp', `pg-iam-${Date.now()}`);
61-
fs.mkdirSync(dir, { recursive: true });
65+
fs.mkdirSync(dir, {recursive: true});
6266
const path = resolve(dir, '.s.PGSQL.5432');
6367

6468
// The startLocalProxy method starts a local proxy that listens on the
6569
// specified unix socket path. This allows the application to connect to
6670
// the database using a standard database driver.
67-
await connector.startLocalProxy({
71+
const cleanup = await connector.startLocalProxy({
6872
instanceConnectionName,
6973
ipType: ipType,
7074
authType: AuthTypes.IAM,
7175
listenOptions: {path},
7276
});
7377

7478
// URL encode the user for IAM service accounts
75-
const datasourceUrl = `postgresql://${encodeURIComponent(dbUser)}@localhost/${dbName}?host=${dir}`;
79+
const datasourceUrl = `postgresql://${encodeURIComponent(
80+
dbUser
81+
)}@localhost/${dbName}?host=${dir}`;
7682
const prisma = new PrismaClient({datasourceUrl});
7783

7884
// Returns the prisma client and a cleanup function to close the connection.
7985
return {
8086
prisma,
8187
async close() {
8288
await prisma.$disconnect();
89+
cleanup();
8390
},
8491
};
8592
}
@@ -100,13 +107,13 @@ async function getPasswordConnection() {
100107

101108
// Creates a randomly named unix socket path for the local proxy.
102109
const dir = resolve('/tmp', `pg-pw-${Date.now()}`);
103-
fs.mkdirSync(dir, { recursive: true });
110+
fs.mkdirSync(dir, {recursive: true});
104111
const path = resolve(dir, '.s.PGSQL.5432');
105112

106113
// The startLocalProxy method starts a local proxy that listens on the
107114
// specified unix socket path. This allows the application to connect to
108115
// the database using a standard database driver.
109-
await connector.startLocalProxy({
116+
const cleanup = await connector.startLocalProxy({
110117
instanceConnectionName,
111118
ipType: ipType,
112119
listenOptions: {path},
@@ -122,14 +129,15 @@ async function getPasswordConnection() {
122129
prisma,
123130
async close() {
124131
await prisma.$disconnect();
132+
cleanup();
125133
},
126134
};
127135
}
128136

129137
// Helper to get or create the password pool
130138
async function getConnectionSettings() {
131139
if (!passwordClient) {
132-
const { prisma, close } = await getPasswordConnection();
140+
const {prisma, close} = await getPasswordConnection();
133141
passwordClient = prisma;
134142
passwordCleanup = close;
135143
}
@@ -139,7 +147,7 @@ async function getConnectionSettings() {
139147
// Helper to get or create the IAM pool
140148
async function getIamConnectionSettings() {
141149
if (!iamClient) {
142-
const { prisma, close } = await getIamConnection();
150+
const {prisma, close} = await getIamConnection();
143151
iamClient = prisma;
144152
iamCleanup = close;
145153
}
@@ -151,12 +159,20 @@ app.get('/', async (req, res) => {
151159
const prisma = await getConnectionSettings();
152160
const result = await prisma.$queryRaw`SELECT 1`;
153161
const serialized = JSON.stringify(result, (key, value) =>
154-
typeof value === 'bigint' ? value.toString() : value
162+
typeof value === 'bigint' ? value.toString() : value
155163
);
156-
res.send(`Database connection successful (password authentication), result: ${serialized}`);
157-
} catch (err: any) {
164+
res.send(
165+
'Database connection successful (password authentication), result: ' +
166+
`${serialized}`
167+
);
168+
} catch (err: unknown) {
158169
console.error(err);
159-
res.status(500).send(`Error connecting to the database (password authentication): ${err.message}`);
170+
res
171+
.status(500)
172+
.send(
173+
'Error connecting to the database (password authentication): ' +
174+
`${(err as Error).message}`
175+
);
160176
}
161177
});
162178

@@ -165,12 +181,20 @@ app.get('/iam', async (req, res) => {
165181
const prisma = await getIamConnectionSettings();
166182
const result = await prisma.$queryRaw`SELECT 1`;
167183
const serialized = JSON.stringify(result, (key, value) =>
168-
typeof value === 'bigint' ? value.toString() : value
184+
typeof value === 'bigint' ? value.toString() : value
185+
);
186+
res.send(
187+
'Database connection successful (IAM authentication), result: ' +
188+
`${serialized}`
169189
);
170-
res.send(`Database connection successful (IAM authentication), result: ${serialized}`);
171-
} catch (err: any) {
190+
} catch (err: unknown) {
172191
console.error(err);
173-
res.status(500).send(`Error connecting to the database (IAM authentication): ${err.message}`);
192+
res
193+
.status(500)
194+
.send(
195+
'Error connecting to the database (IAM authentication): ' +
196+
`${(err as Error).message}`
197+
);
174198
}
175199
});
176200

@@ -179,3 +203,15 @@ const port = process.env.PORT ? parseInt(process.env.PORT) : 8080;
179203
app.listen(port, () => {
180204
console.log(`Listening on port ${port}`);
181205
});
206+
207+
process.on('SIGTERM', async () => {
208+
if (passwordCleanup) {
209+
await passwordCleanup();
210+
}
211+
if (iamCleanup) {
212+
await iamCleanup();
213+
}
214+
if (connector) {
215+
connector.close();
216+
}
217+
});

examples/cloudrun/sequelize/mysql2/index.ts

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,27 +129,55 @@ app.get('/', async (req, res) => {
129129
try {
130130
const db = await getConnectionSettings();
131131
await db.authenticate();
132-
const [results, metadata] = await db.query('SELECT 1');
133-
res.send(`Database connection successful (password authentication), result: ${JSON.stringify(results)}`);
134-
} catch (err: any) {
132+
const [results] = await db.query('SELECT 1');
133+
res.send(
134+
'Database connection successful (password authentication), result: ' +
135+
`${JSON.stringify(results)}`
136+
);
137+
} catch (err: unknown) {
135138
console.error(err);
136-
res.status(500).send(`Error connecting to the database (password authentication): ${err.message}`);
139+
res
140+
.status(500)
141+
.send(
142+
'Error connecting to the database (password authentication): ' +
143+
`${(err as Error).message}`
144+
);
137145
}
138146
});
139147

140148
app.get('/iam', async (req, res) => {
141149
try {
142150
const db = await getIamConnectionSettings();
143151
await db.authenticate();
144-
const [results, metadata] = await db.query('SELECT 1');
145-
res.send(`Database connection successful (IAM authentication), result: ${JSON.stringify(results)}`);
146-
} catch (err: any) {
152+
const [results] = await db.query('SELECT 1');
153+
res.send(
154+
'Database connection successful (IAM authentication), result: ' +
155+
`${JSON.stringify(results)}`
156+
);
157+
} catch (err: unknown) {
147158
console.error(err);
148-
res.status(500).send(`Error connecting to the database (IAM authentication): ${err.message}`);
159+
res
160+
.status(500)
161+
.send(
162+
'Error connecting to the database (IAM authentication): ' +
163+
`${(err as Error).message}`
164+
);
149165
}
150166
});
151167

152168
const port = parseInt(process.env.PORT || '8080');
153169
app.listen(port, () => {
154170
console.log(`Server running on port ${port}`);
155171
});
172+
173+
process.on('SIGTERM', async () => {
174+
if (passwordPool) {
175+
await passwordPool.close();
176+
}
177+
if (iamPool) {
178+
await iamPool.close();
179+
}
180+
if (connector) {
181+
connector.close();
182+
}
183+
});

examples/cloudrun/typeorm/tedious/index.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,8 @@
1313
// limitations under the License.
1414

1515
import express from 'express';
16-
import {
17-
AuthTypes,
18-
Connector,
19-
IpAddressTypes,
20-
} from '@google-cloud/cloud-sql-connector';
21-
import { DataSource } from 'typeorm';
16+
import {Connector, IpAddressTypes} from '@google-cloud/cloud-sql-connector';
17+
import {DataSource} from 'typeorm';
2218

2319
const app = express();
2420

@@ -91,14 +87,31 @@ app.get('/', async (req, res) => {
9187
try {
9288
const db = await getConnectionSettings();
9389
const result = await db.query('SELECT 1');
94-
res.send(`Database connection successful (password authentication), result: ${JSON.stringify(result)}`);
95-
} catch (err: any) {
90+
res.send(
91+
'Database connection successful (password authentication), result: ' +
92+
`${JSON.stringify(result)}`
93+
);
94+
} catch (err: unknown) {
9695
console.error(err);
97-
res.status(500).send(`Error connecting to the database (password authentication): ${err.message}`);
96+
res
97+
.status(500)
98+
.send(
99+
'Error connecting to the database (password authentication): ' +
100+
`${(err as Error).message}`
101+
);
98102
}
99103
});
100104

101105
const port = parseInt(process.env.PORT || '8080');
102106
app.listen(port, () => {
103107
console.log(`Server running on port ${port}`);
104108
});
109+
110+
process.on('SIGTERM', async () => {
111+
if (passwordPool) {
112+
await passwordPool.destroy();
113+
}
114+
if (connector) {
115+
connector.close();
116+
}
117+
});

0 commit comments

Comments
 (0)