File tree Expand file tree Collapse file tree 3 files changed +31
-8
lines changed
Expand file tree Collapse file tree 3 files changed +31
-8
lines changed Original file line number Diff line number Diff line change 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
5656const 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
Original file line number Diff line number Diff 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
6163module . exports = {
6264 Box,
@@ -66,5 +68,6 @@ module.exports = {
6668 User,
6769 utils,
6870 decoding,
69- db
71+ db,
72+ isReady
7073} ;
Original file line number Diff line number Diff line change @@ -29,6 +29,7 @@ const {
2929 logEntryRelations,
3030 tokenBlacklistTable
3131} = require ( '../schema/schema' ) ;
32+ const { sql } = require ( 'drizzle-orm' ) ;
3233
3334const 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+
5160const 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+
5670const 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;
You can’t perform that action at this time.
0 commit comments