How to use ConfigService in NestJS in application bootstrap

You might want to access the configuration, for example to set the microservice configuration based on the values from the configuration service. One way to do this is to create an application context from the AppModule. const appContext = await NestFactory.createApplicationContext(BootstrapConfigModule) const configService = appContext.get(ConfigService) const SERVICE_PORT = configService.get('SERVICE_PORT') appContext.close() But a better idea is to use a “temporary” module to avoid double instantiation of the whole app. See a full example below. import { Module } from '@nestjs/common' import { ConfigService } from '@nestjs/config' import { NestFactory } from '@nestjs/core' import { MicroserviceOptions, Transport } from '@nestjs/microservices' import { AppModule } from './app.module' // Bootstrap configuration module is needed to avoid // doing the DI resolution twice @Module({ providers: [ConfigService], exports: [ConfigService], }) class BootstrapConfigModule {} async function bootstrap() { const appContext = await NestFactory.createApplicationContext(BootstrapConfigModule) const configService = appContext.get(ConfigService) const SERVICE_PORT = configService.get('SERVICE_PORT') appContext.close() const app = await NestFactory.createMicroservice<MicroserviceOptions>(AppModule, { transport: Transport.TCP, options: { port: SERVICE_PORT, }, }) await app.listen() } bootstrap() Why would you need to use the configuration service instead of process.env you might ask. Well, while that may work, it doesn’t offer any type safety or support for fallbacks. ...

How to merge a nested list in Mongo document with another found in adjacent collection

Let’s consider a situation where you have two collections, houses and people. Each house has a collection of key holders, which link to the persons collection with their IDs. Key holders list also holds information when the key was given for the identified person. In bson, the situation in the database looks like this: { "houses": [ { "_id": ObjectId("5fba17c1c4566e57fafdcd7e"), "address": "Main street 1", "keyHolders": [ { "keyDelivered": "2022-02-02T02:02:02", "personId": "5fbb5ab778045a985690b5fc" }, { "keyDelivered": "2021-01-01T01:01:01", "personId": "5fbb5ab778045a985690b5fd" } ] }, { "_id": ObjectId("5fba17c1c4566e57fafdcd7f"), "address": "Broadway 3", "keyHolders": [ { "keyDelivered": "1993-03-03T03:03:03", "personId": "5fbb5ab778045a985690b5fc" } ] } ], "persons": [ { "_id": ObjectId("5fbb5ab778045a985690b5fc"), "name": "Jack Bauer", }, { "_id": ObjectId("5fbb5ab778045a985690b5fd"), "name": "James Bond", } ] } You can also do some mapping for the source list, for instance convert foreign keys from strings to ObjectIds. ...

How to Copy a Collection of Documents to Another Database with mongosh

If you need to copy a collection of documents from one Mongo collection to another, you can use the mongodump and mongorestore. But if the source database is in the same instance as the target, then you may be able to take a shortcut with mongosh. Here is the script to use: db.<source collection>.find().forEach(function(d){ db.getSiblingDB('<dest db>')['<dest collection>'].insert(d); }); For example, if you are copying all documents from current databases cars collection to a database called another-db’s collection vehicles: ...

How to set up a blog with Hugo and GitHub Pages in 10 minutes

So you want to set up a blog. Here is how to do it in 10 minutes or less. For me, it took a little more time, just to make sure for you it would not. And actually, even for me, most of the time was used to write this post. So here we go, you have 40 for each step. Create GitHub account if you do not have one yet Create new GitHub repository named <your GitHub username>.github.io On your local development environment, create a folder to work in, run npm install hugo-bin --save-dev to install Hugo CLI Clone your GitHub repository under the same directory git clone https://github.com/<your username>/<your username>.github.io.git Run npx hugo new site <your username>.github.io Navigate to the repository root directory cd <your username>.github.io Add a theme as Git Submodule: git submodule add --depth=1 https://github.com/adityatelange/hugo-PaperMod.git themes/PaperMod Update config.toml by adding the theme = 'PaperMod' option (and add a name for the site while at it) Also update config.toml with your own baseUrl, e.g. baseURL = 'https://<your GitHub username>.github.io/' Run npx hugo new posts/initial-post.md Edit the initial post and when ready to publish, remove the draft tag Add the following into .github\workflows\gh-pages.yml file in the repository name: github pages on: push: branches: - main # Set a branch that will trigger a deployment pull_request: jobs: deploy: runs-on: ubuntu-22.04 steps: - uses: actions/checkout@v3 with: submodules: true # Fetch Hugo themes (true OR recursive) fetch-depth: 0 # Fetch all history for .GitInfo and .Lastmod - name: Setup Hugo uses: peaceiris/actions-hugo@v2 with: hugo-version: 'latest' # extended: true - name: Build run: hugo --minify - name: Deploy uses: peaceiris/actions-gh-pages@v3 if: github.ref == 'refs/heads/main' with: github_token: ${{ secrets.GITHUB_TOKEN }} publish_dir: ./public Create a branch named gh-pages in GitHub Check which branch is used for GitHub pages publishing in your GitHub repository, set it to gh-pages Set in GitHub Settings > Actions > General: Workflow permissions to Read and write permissions Push to Git remote using the main branch Make sure the deployment went smoothly, and enjoy the result in https://<your GitHub username>.github.io