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

mongodb-builder

v0.6.1

Published

MongoDB migration framework.

Downloads

89

Readme

Build Status NPM Version Dependency Status

mongodb-builder

MongoDB migration framework.

This is a light weight open source package for NodeJS written with TypeScript. It's actively maintained, well tested and already used in production environments. The source code is available on GitHub where you can also find our issue tracker.

Installation

Run the command below to install the package.

$ npm install --save mongodb-builder

This package uses promises thus you need to use Promise polyfill when promises are not supported.

Usage

The package provides two core classes. The Migrator class is used for running MongoDB migrations and the Seeder is used for performing seed operations.

Migrations

Migrations are performed in a sequence based on the index parameter. If you pass a context object into Migrator class the object will be passed to each migration method. Methods upgrade and downgrade runs migration recipes and stores the last successfully performed migration index value in the database.

import { MongoClient } from 'mongodb';
import { Migrator } from 'mongodb-builder';

// connecting to the database
const mongo = await MongoClient.connect('mongodb://localhost:27017');

// initialize migration class
const migrator = new Migrator({
  collection: mongo.db('test').collection('migrations'), // required
  context: { foo: "foo" }, // optional
});

// register migrations (you could move this object into a separate file)
migrator.add({
  upgrade: async (context) => { /* do something */ },
  downgrade: async (context) => { /* do something */ },
});

// run `upgrade` migrations
await migrator.upgrade();

Migrator({ db, context })

Performs MongoDB migration operations.

| Option | Type | Required | Default | Description |--------|------|----------|---------|------------ | collection | Collection | Yes | - | Instance of MongoDB collection. | context | Object | No | - | Object which is passed into each up and down method.

migrator.add({ upgrade, downgrade })

Registeres new migration recipe.

| Option | Type | Required | Default | Description |--------|------|----------|---------|------------ | upgrade | Function, Promise | Yes | - | Logic for upgrading the database. | downgrade | Function, Promise | Yes | - | Logic for downgrading the database.

migrator.addDir(path)

Registeres new migration recipes by loading files at path.

| Option | Type | Required | Default | Description |--------|------|----------|---------|------------ | path | String | Yes | - | Path to the directory with migration files.

export async function upgrade(context) {}
export async function downgrade(context) {}

migrator.remove(index)

Unregisters migration recipe at index.

| Option | Type | Required | Default | Description |--------|------|----------|---------|------------ | index | Integer | Yes | - | Migration recipe index.

migrator.upgrade(index)

Runs a sequence of registered upgrade methods.

| Option | Type | Required | Default | Description |--------|------|----------|---------|------------ | index | Integer | No | - | Sequential number of the last performable method from the beginning.

migrator.downgrade(index)

Runs a sequence of registered downgrade methods.

| Option | Type | Required | Default | Description |--------|------|----------|---------|------------ | index | Integer | No | - | Sequential number of the last performable method from the beginning.

migrator.lastIndex()

Runs the last migration index that has already been performed.

Seeding

Seed operations are similar to migrations. The difference is only that they can be performed multiple times.

import { MongoClient } from 'mongodb';
import { Seeder } from 'mongodb-builder';

// connecting to the database
const mongo = await MongoClient.connect('mongodb://localhost:27017');

// initialize migration class
const seeder = new Seeder(
  collection: mongo.db('test').collection('migrations'), // required
  context: { foo: "foo" }, // optional
);

// register migrations (you could move this object into a separate file)
seeder.add({
  perform: async (context) => { /* do something */ },
});

// run `perform` methods
await seeder.perform();

Seeder({ db, context })

Performs MongoDB seed operations.

| Option | Type | Required | Default | Description |--------|------|----------|---------|------------ | collection | Collection | Yes | - | Instance of MongoDB collection. | context | Object | No | - | Object which is passed into each perform method.

seeder.add({ seed })

Registeres new seed recipe.

| Option | Type | Required | Default | Description |--------|------|----------|---------|------------ | perform | Function, Promise | Yes | - | Logic for seeding the database.

seeder.addDir(path)

Registeres new seed recipes by loading files at path.

| Option | Type | Required | Default | Description |--------|------|----------|---------|------------ | path | String | Yes | - | Path to the directory with seed files.

export async function perform(context) {}

seeder.remove(index)

Unregisters seed recipe at index.

| Option | Type | Required | Default | Description |--------|------|----------|---------|------------ | index | Integer | Yes | - | Seed recipe index.

seeder.perform(index)

Runs a sequence of registered upgrade methods.

| Option | Type | Required | Default | Description |--------|------|----------|---------|------------ | index | Integer | No | - | Sequential number of the last performable method from the beginning.

License (MIT)

Copyright (c) 2017+ Kristijan Sedlak <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated modelation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.