Skip to content

Commit e515d5f

Browse files
committed
check db connection before startup
1 parent d83fc93 commit e515d5f

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

packages/api/app.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
'use strict';
1212

13-
const { db } = require('@sensebox/opensensemap-api-models'),
13+
const { isReady } = require('@sensebox/opensensemap-api-models'),
1414
restify = require('restify'),
1515
{
1616
fullResponse,
@@ -55,9 +55,10 @@ if (config.get('logLevel') === 'debug') {
5555

5656
const run = async function () {
5757
try {
58-
// TODO: Get a client from the Pool and test connection
59-
await db.connect();
58+
// Check if the database is ready
59+
await isReady();
6060

61+
// Load routes
6162
routes(server);
6263

6364
// start the server

packages/models/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ config.util.setModuleDefaults('openSenseMap-API-models', {
1313
user: 'postgres',
1414
userpass: 'postgres',
1515
db: 'opensensemap',
16-
database_url: ''
16+
database_url: '',
17+
ssl: false
1718
},
1819
integrations: {
1920
ca_cert: '',
@@ -56,7 +57,8 @@ const { model: Box } = require('./src/box/box'),
5657
{ model: Claim } = require('./src/box/claim'),
5758
utils = require('./src/utils'),
5859
decoding = require('./src/measurement/decoding'),
59-
db = require('./src/db');
60+
db = require('./src/db'),
61+
isReady = require('./src/drizzle').isReady;
6062

6163
module.exports = {
6264
Box,
@@ -66,5 +68,6 @@ module.exports = {
6668
User,
6769
utils,
6870
decoding,
69-
db
71+
db,
72+
isReady
7073
};

packages/models/src/drizzle.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const {
2929
logEntryRelations,
3030
tokenBlacklistTable
3131
} = require('../schema/schema');
32+
const { sql } = require('drizzle-orm');
3233

3334
const getDBUri = function getDBUri (uri) {
3435
// if available, use user specified db connection uri
@@ -48,11 +49,24 @@ const getDBUri = function getDBUri (uri) {
4849
return `postgresql://${user}:${userpass}@${host}:${port}/${db}`;
4950
};
5051

52+
const isReady = async function isReady () {
53+
try {
54+
await db.execute(sql`select 1`);
55+
} catch (error) {
56+
throw new Error(error);
57+
}
58+
};
59+
5160
const pool = new Pool({
5261
connectionString: getDBUri(),
53-
ssl: false
62+
ssl: config.get('ssl') === 'true' ? true : false
5463
});
5564

65+
// TODO: attach event listener
66+
// pool.on('connect', () => {
67+
// console.log('connected to the db');
68+
// });
69+
5670
const schema = {
5771
accessTokenTable,
5872
refreshTokenTable,
@@ -83,4 +97,9 @@ const db = drizzle(pool, {
8397
schema
8498
});
8599

86-
module.exports.db = db;
100+
module.exports = {
101+
db,
102+
isReady
103+
};
104+
// module.exports.db = db;
105+
// module.exports.isReady = isReady;

0 commit comments

Comments
 (0)