Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ MONGO_ACCOUNTS_DATABASE_URI=mongodb://localhost:27017/hawk
# MongoDB URL for connecting to events database
MONGO_EVENTS_DATABASE_URI=mongodb://localhost:27017/hawk_events

# MongoDB appName for per-worker load attribution (e.g. hawk-worker-limiter)
MONGO_APP_NAME=

# JWT secret key for projects tokens
JWT_SECRET=qwerty

Expand Down
10 changes: 9 additions & 1 deletion lib/db/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export class DatabaseController {
*/
private readonly connectionUri: string;

/**
* Sent to MongoDB on handshake; overrides any `appName` query param in the URI
*/
private readonly appName?: string;

/**
* GridFSBucket object
* Used to store files in GridFS
Expand All @@ -30,12 +35,14 @@ export class DatabaseController {
* Creates controller instance
*
* @param connectionUri - mongo URI for connection
* @param appName - MongoDB appName, defaults to `process.env.MONGO_APP_NAME`
*/
constructor(connectionUri: string) {
constructor(connectionUri: string, appName?: string) {
if (!connectionUri) {
throw new DatabaseConnectionError('Connection URI is not specified. Check .env');
}
this.connectionUri = connectionUri;
this.appName = appName ?? process.env.MONGO_APP_NAME;
}

/**
Expand All @@ -53,6 +60,7 @@ export class DatabaseController {
this.connection = await connect(this.connectionUri, {
useNewUrlParser: true,
useUnifiedTopology: true,
...(this.appName ? { appName: this.appName } : {}),
});
this.db = await this.connection.db();

Expand Down
Loading