npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nest-mongo-migrate

v0.0.6

Published

A nestjs module to matain mongodb incremental scripts, and run scripts to mongodb

Downloads

4

Readme

Installation

npm install --save nest-mongo-migrate

Quick start

Import MongoMigrateModule into the some module SomeModule and use the register() method to configure it.

import { Module } from '@nestjs/common';
import { MongoMigrateModule } from 'nest-mongo-migrate';
import { resolve } from 'path';

@Module({
  imports: [
    MongoMigrateModule.register({
      dbUrl: configService.dbUrl,
      dbName: configService.dbName,
      scriptsDir: resolve(__dirname, 'db/scripts'),
      collectionName: 'ScriptLog',
    }),
  ],
})
export class SomeModule {}

Async Configuration

import {
  ConfigurationModule,
  ConfigurationService,
} from 'xxxx';
import { Module } from '@nestjs/common';
import { MongoMigrateModule } from 'nest-mongo-migrate';
import { resolve } from 'path';

@Module({
  imports: [
    MongoMigrateModule.registerAsync({
      useFactory: async (configService: ConfigurationService) => ({
        dbUrl: configService.dbUrl, // mongodb://localhost:27017
        dbName: configService.dbName, // test
        scriptsDir: resolve(__dirname, 'db/scripts'), // the scripts dir
        collectionName: 'ScriptLog', // the collection name to store the script log
      }),
      imports: [ConfigurationModule],
      inject: [ConfigurationService],
    }),
  ],
})
export class SomeModule {}

then you can use the MongoMigrateService to run the migration scripts.

import { Body, Controller, Get, Logger, Post } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { MongoMigrateService } from 'nest-mongo-migrate';

@ApiTags('Some')
@Controller('some')
export class SomeController {
  private readonly logger = new Logger(SomeController.name);

  constructor(private readonly _mongoMigrateService: MongoMigrateService) {}

  @Get('up')
  async up() {
    // run all the scripts
    return this._mongoMigrateService.up();
  }

  @Get('down')
  async down() {
    // rollback latest one script
    return this._mongoMigrateService.down();
  }

  @Get('status')
  async status() {
    // get the scripts status in db
    return await this._mongoMigrateService.status();
  }

  @Post('create')
  async create(@Body() body: { name: string }) {
    // create a new migration script, the name is the file name
    // a file named `<timestamp>_<name>.js` will be created in the scripts dir
    return this._mongoMigrateService.create(body.name);
  }
}

Create a migration script

naming convention

<timestamp>_<name>.js, for example: 20230403123456_init-user.js

script template

/**
 * description: xxxxxxxxxx
 */

module.exports = {
  /**
   * upgrade script
   * @param {mongo.Db} db 
   */
  async up(db) {
    // TODO write your migration here.
    // Example:
    // await db.collection('albums').updateOne({artist: 'The Beatles'}, {$set: {blacklisted: true}});
  },

  /**
   * downgrade script
   * @param {mongo.Db} db 
   */
  async down(db) {
    // TODO write the statements to rollback your migration (if possible)
    // Example:
    // await db.collection('albums').updateOne({artist: 'The Beatles'}, {$set: {blacklisted: false}});
  }
};

TODO

  • Add transaction supoort if db in replica set mode
  • Add cli command supoort