Created
July 18, 2020 11:13
-
-
Save darwinsubramaniam/8a615f852f19a7ca8c30f811913ca077 to your computer and use it in GitHub Desktop.
sample provider using config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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