aboutsummaryrefslogtreecommitdiff
path: root/src/server/database.ts
diff options
context:
space:
mode:
authorSam Wilkins <35748010+samwilkins333@users.noreply.github.com>2019-10-16 18:38:31 -0400
committerSam Wilkins <35748010+samwilkins333@users.noreply.github.com>2019-10-16 18:38:31 -0400
commit7b43e349d31c911ab43763a4ff7179b3778a2d96 (patch)
treef9d143e4840f286d897e65a3d4cd76ff6d0290b9 /src/server/database.ts
parent91e4ac65e0b8d1ff5c17ea0e80666038281ec5a6 (diff)
database separation and preliminary functions
Diffstat (limited to 'src/server/database.ts')
-rw-r--r--src/server/database.ts27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/server/database.ts b/src/server/database.ts
index 25e1e67e0..4f93d1ee6 100644
--- a/src/server/database.ts
+++ b/src/server/database.ts
@@ -5,6 +5,7 @@ import { Utils, emptyFunction } from '../Utils';
import { DashUploadUtils } from './DashUploadUtils';
import { Credentials } from 'google-auth-library';
import { GoogleApiServerUtils } from './apis/google/GoogleApiServerUtils';
+import mongoose, { ConnectionStates } from 'mongoose';
export namespace Database {
@@ -12,6 +13,32 @@ export namespace Database {
const port = 27017;
export const url = `mongodb://localhost:${port}/${schema}`;
+ export async function tryInitializeConnection() {
+ try {
+ const { connection } = mongoose;
+ process.on('SIGINT', () => {
+ connection.close(() => {
+ console.log('Mongoose default connection disconnected through app termination');
+ process.exit(0);
+ });
+ });
+ if (connection.readyState === ConnectionStates.disconnected) {
+ await new Promise<void>((resolve, reject) => {
+ connection.on('error', reject);
+ connection.on('connected', () => {
+ console.log(`Mongoose established default connection at ${url}`);
+ resolve();
+ });
+ });
+ }
+ } catch (e) {
+ console.error(`Mongoose FAILED to establish default connection at ${url} with the following error:`);
+ console.error(e);
+ console.log('Since a valid database connection is required to use Dash, the server process will now exit.\nPlease try again later.');
+ process.exit(1);
+ }
+ }
+
class Database {
public static DocumentsCollection = 'documents';
private MongoClient = mongodb.MongoClient;