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

@xpresser/xpress-mongo

v2.2.1

Published

Xpress Mongo plugin for xpresser.

Downloads

21

Readme

Xpresser XpressMongo Plugin

Xpress mongo is a light nodejs library for mongodb.

Installation

Install xpress-mongo && @xpresser/xpress-mongo plugin

npm i xpress-mongo @xpresser/xpress-mongo
# OR
yarn add xpress-mongo @xpresser/xpress-mongo

Setup

Add npm://@xpresser/xpress-mongo to plugins array in your plugins.json file.

Note: if you don't have plugins.json file in your project create one in your backend folder. backend/plugins.json

{
 "npm://@xpresser/xpress-mongo": true
}

Config

Add to your configuration using key mongodb, Example:

const config = {
 /**
  * If Enabled, xjs make:model will generate Models
  * that requires you to define all data types.
  */
 useStrictTypescriptModels: true, // >=v1.0.0
 
 // Connection Config
 mongodb: {
   url: 'mongodb://127.0.0.1:27017',
   database: 'test',
   options: {
     useNewUrlParser: true,
     useUnifiedTopology: true
   }
 }
};

Commands

npx xjs make:model <ModelName>

NOTE: This plugin modifies xpresser's default factory template for models.

Migration

To activate migration commands, add npm://@xpresser/xpress-mongo to the extensions array in your use-xjs-cli.json file.

Like so:

{
  "extensions": [
    "npm://@xpresser/xpress-mongo"
  ]
}

This will add the following commands to xjs cli.

# Make migration file
npx xjs make:migration <filename>

# Do migration
npx xjs migrate

# Undo migration
npx xjs migrate undo

Make migration file

To make a migration file, run the following command:

npx xjs make:migration <filename>

# Example
npx xjs make:migration add-users

This will create a migration file in your jobs/migrations folder. The file name will be prefixed with a timestamp. E.g 1610000000000__add-users, the timestamp is in milliseconds, it is used to order migrations.

The migration file should look like this:

import {defineMigrationJob} from "@xpresser/xpress-mongo";

export = defineMigrationJob((m) => {
    /**
     * Do Migration
     */
    m.do(async () => {
        // do migration code
    });

    /**
     * Undo Migration
     */
    m.undo(async () => {
        // undo migration code
    });
});

Do migration

To run the do function in your migration file, run the following command:

npx xjs migrate

This will run all migrations do functions that have not been run yet and save the migration to the database as a batch.

Undo migration

To run the undo function in your migration file, run the following command:

npx xjs migrate undo

This will run all migrations undo functions in the last batch and remove the batch from the database.

Do or Undo if condition is met

There is a doIf and undoIf method that can be used to run the do or undo function if a condition is met.

if doIf returns false the do function will be skipped same goes for undoIf and undo function.

Note: if there is no doIf or undoIf function, the do or undo function will be called.

import {defineMigrationJob} from "@xpresser/xpress-mongo";

export = defineMigrationJob((m) => {

    m.doIf(async () => {
        // return true to run `do` function
        // return false to skip `do` function
        return true;
    });


    m.undoIf(async () => {
        // return true to run `undo` function
        // return false to skip `undo` function
        return true;
    });

    /**
     * Do Migration
     */
    m.do(async () => {
        // do migration code
    });

    /**
     * Undo Migration
     */
    m.undo(async () => {
        // undo migration code
    });
});

Do or Undo a migration file.

In some cases you may want to run a migration file without relying on the database to keep track of migrations, you can call the do or undo function directly.

Since migration files are jobs, you can call them directly using the xjs cli.

# Do migration
npx xjs run migrations/<filename> do

# Undo migration
npx xjs run migrations/<filename> undo