Skip to main content
Version: v1

Connecting to a Database

It is recommended to initialize a database connection in the subscription.service file in your PUBSUB_ROOT_DIR. Insert your database connection logic in the init method.

/pubsub/subscription.service.ts
import * as PubSub from '@honestfoodcompany/pubsub';
import mongoose from 'mongoose';
import { SubscriberOptions } from '@honestfoodcompany/pubsub';
export default class SubscriptionService extends PubSub.SubscriptionService {
/**
* This function is called when the subscription server starts.
*/
static async init(): Promise<void> {
/**
* This is a good place to initialize a database connection
*/
await mongoose.connect();
}
}