Skip to content

Commit

Permalink
feat: add support for setting mongoose connection options
Browse files Browse the repository at this point in the history
  • Loading branch information
DanRibbens committed Feb 4, 2021
1 parent 3facbf9 commit 82c4898
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
4 changes: 4 additions & 0 deletions docs/getting-started/installation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ This is a fully qualified MongoDB connection string that points to your Mongo da

In contrast to running Mongo locally, a popular option is to sign up for a free [MongoDB Atlas account](https://www.mongodb.com/cloud/atlas), which is a fully hosted and cloud-based installation of Mongo that you don't need to ever worry about.

##### `mongoOptions`

An optional object to customize connection options. Payload will connect to your MongoDB database using default options which you can override and extend to include all the [options](https://mongoosejs.com/docs/connections.html#options) available to mongoose.

##### `express`

This is your Express app as shown above. Payload will tie into your existing `app` and scope all of its functionalities to sub-routers. By default, Payload will add an `/admin` router and an `/api` router, but you can customize these paths.
Expand Down
2 changes: 2 additions & 0 deletions src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Transporter } from 'nodemailer';
import { Configuration } from 'webpack';
import SMTPConnection from 'nodemailer/lib/smtp-connection';
import GraphQL from 'graphql';
import { ConnectionOptions } from 'mongoose';
import { Payload } from '..';
import { AfterErrorHook, PayloadCollectionConfig, CollectionConfig } from '../collections/config/types';
import { PayloadGlobalConfig, GlobalConfig } from '../globals/config/types';
Expand Down Expand Up @@ -48,6 +49,7 @@ export function hasTransportOptions(emailConfig: EmailOptions): emailConfig is E
export type InitOptions = {
express?: Express;
mongoURL: string;
mongoOptions?: ConnectionOptions;
secret: string;
license?: string;
email?: EmailOptions;
Expand Down
5 changes: 2 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import express, { Express, Router } from 'express';
import crypto from 'crypto';
import { TestAccount } from 'nodemailer';
import {
Config,
EmailOptions,
Expand Down Expand Up @@ -32,7 +31,7 @@ import performFieldOperations from './fields/performFieldOperations';
import localOperations from './collections/operations/local';
import localGlobalOperations from './globals/operations/local';
import { encrypt, decrypt } from './auth/crypto';
import { MockEmailHandler, BuildEmailResult, Message } from './email/types';
import { BuildEmailResult, Message } from './email/types';
import { PayloadRequest } from './express/types';
import sendEmail from './email/sendEmail';

Expand Down Expand Up @@ -143,7 +142,7 @@ export class Payload {
initGlobals(this);

// Connect to database
connectMongoose(this.mongoURL);
connectMongoose(this.mongoURL, options.mongoOptions);

// If not initializing locally, set up HTTP routing
if (!this.local) {
Expand Down
18 changes: 10 additions & 8 deletions src/mongoose/connect.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
/* eslint-disable no-console */
import mongoose from 'mongoose';
import mongoose, { ConnectionOptions } from 'mongoose';
import Logger from '../utilities/logger';

const logger = Logger();

const connectMongoose = async (url: string): Promise<void> => {
const connectMongoose = async (url: string, options: ConnectionOptions): Promise<void> => {
let urlToConnect = url;
let successfulConnectionMessage = 'Connected to Mongo server successfully!';
const connectionOptions = {
...options,
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
autoIndex: false,
};

if (process.env.NODE_ENV === 'test') {
// eslint-disable-next-line global-require
Expand All @@ -22,12 +29,7 @@ const connectMongoose = async (url: string): Promise<void> => {
}

try {
await mongoose.connect(urlToConnect, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
autoIndex: false,
});
await mongoose.connect(urlToConnect, connectionOptions);
logger.info(successfulConnectionMessage);
} catch (err) {
logger.error('Error: cannot connect to MongoDB. Details: ', err);
Expand Down

0 comments on commit 82c4898

Please sign in to comment.