Skip to content

Instantly share code, notes, and snippets.

@darwinsubramaniam
Created July 18, 2020 11:13
Show Gist options
  • Save darwinsubramaniam/8a615f852f19a7ca8c30f811913ca077 to your computer and use it in GitHub Desktop.
Save darwinsubramaniam/8a615f852f19a7ca8c30f811913ca077 to your computer and use it in GitHub Desktop.
sample provider using config
import { FactoryProvider, Logger } from "@nestjs/common";
import {Configuration, DocumentScope} from 'nano'
import nano = require("nano");
import { ConfigType } from "@nestjs/config";
import databaseConfig from "../database.config";
import { User } from "src/user/user";
export const userDocScopeFactory: FactoryProvider = {
provide: 'UserDocumentScope',
useFactory: async (dbConfig: ConfigType<typeof databaseConfig>):Promise<DocumentScope<User>> => {
const log = new Logger('userDocScopeFactory',true);
const opts: Configuration = {
url: `http://${dbConfig.url}:${dbConfig.port}`,
requestDefaults:{
auth:{
pass:dbConfig.password,
user:dbConfig.user
}
},
log: (id, args) => {
log.debug(`ID : ${JSON.stringify(id)} -> args ${args}`)
},
}
return nano(opts).use('_users');
},
inject:[databaseConfig.KEY]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment