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

@genially/mongoose-mimic

v2.0.1

Published

mongoose-mimic is a small (but powerful) Node.js library to generate test data for Mongoose using only the schema definition

Downloads

1,574

Readme

mongoose-mimic

mongoose-mimic is a simple (but powerful) Node.js library to generate test data for Mongoose using only the schema definition.

@genially/mongoose-mimic

Features

  • Generate random values depending on primitive data types (string, number, boolean, date...)
  • Generate random values that meet constraints (uppercase, lowercase, max...)
  • Generate custom values for specific fields
  • Generate custom values that match non-primitive data types (email, phone, address...)
  • Ignore fields
  • Generate dates as object or string

Installation

npm install @genially/mongoose-mimic

Usage

mimic(model, opts)

Generates a mimetic document from model

  • model: Mongoose schema object
  • opts: Generation options, where the options are in the following format:
        {
          ignore: Array,
          applyFilter: Boolean,
          returnDate: Boolean,
          custom: {
             field: {
               value: Any,
               type: String
             }
          }
        }

| Option | Type | Usage | | :----------------: | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | | ignore | Array | It can contains string paths or RegExp of fields to ignore during generation | | applyFilter | Boolean | Apply lowercase, uppercase, and trim filters on generated object if defined in the path | | returnDate | Boolean | Return dates as Date or String | | custom | Object | Special generator for specific fields | | custom.field.value | Any | Predefined value to the given field | | custom.field.type | String | Data type to generate to the given field, in the format: "type.subtype". Examples: "internet.email" or "address.city". See Faker.js methods to know all supported data types |


Usage Example

const mongoose = require('mongoose');
const mimic = require('@genially/mongoose-mimic');
const ignoredFields = ['_id','created_at', '__v', /detail.*_info/];
const genderValues = ['Male', 'Female']
const schemaDefinition = new mongoose.Schema({
    name: {
        type: String,
        required: true,
        lowercase: true,
        trim: true
    },
    email: {
        type: String,
    },
    phones: {
        type: [String],
    }
    birth_date: {
        type: Date
    },
    gender: {
        type: String,
        enum: genderValues
    },
    data: {
        type: Object,
        default: null
    },
    results: [
        {
            score: Number,
            course: Number
        }
    ],
    is_student: {
        type: Boolean
    },
    parent: {
        type: mongoose.Schema.Types.ObjectId
    },
    detail: {
        main_info: String,
        some_info: String,
        none_match: String
    },
    created_at: {
        type: Date,
        default: Date.now
    }
});
const model = mongoose.model('Student', schemaDefinition);
const randomObject = mimic(model, {
    ignore: ignoredFields,
    returnDate: true,
    custom: {
      name: {
        value: 'Mimic',
      },
      email: {
        type: 'internet.email'
      },
      phones: {
        type: 'phone.phoneNumber'
      },
      birth_date: {
        value: () => new Date('December 25, 1995 23:15:30')
      }
    }
})
console.log(randomObject);

/* Result:
{
    "name": "Mimic",
    "data": {
        "az": {
            "name": "Kayden Oberbrunner",
            "email": "[email protected]",
            "phone": "1-160-514-5977",
            "posts": [{
                "words": "maxime quia sit",
                "sentence": "Fuga vel in architecto ut modi sequi aliquam debitis.",
                "sentences": "Reprehenderit ratione consequuntu.."
            }, {
                "words": "dignissimos qui qui",
                "sentence": "Eveniet est unde quis sit et ab.",
                "sentences": "Sit eos quaerat aut quisquam unde..",
                "paragraph": "Quasi et numquam cumque neque rerum aliquam ullam.."
            }],
            "address": {
                "geo": {
                    "lat": "25.9144",
                    "lng": "6.0991"
                },
                "city": "Amaraville",
                "state": "Indiana",
                "streetA": "O'Conner Prairie",
                "streetB": "5722 Shane Grove",
                "streetC": "8040 Hane Roads Suite 402",
                "streetD": "Apt. 816",
                "country": "Kenya",
                "zipcode": "74052"
            },
            "website": "santina.org",
            "company": {
                "bs": "cross-platform facilitate deliverables",
                "name": "Morissette LLC",
                "catchPhrase": "Self-enabling intangible methodology"
            },
            "username": "Euna44",
            "accountHistory": [{
                "amount": "473.69",
                "date": "2012-02-01T22:00:00.000Z",
                "business": "Lang, Hudson and Heller",
                "name": "Savings Account 3906",
                "type": "invoice",
                "account": "60253551"
            }, {
                "amount": "824.69",
                "date": "2012-02-01T22:00:00.000Z",
                "business": "Rice - Price",
                "name": "Credit Card Account 8924",
                "type": "withdrawal",
                "account": "62599733"
            }]
        }
        }
    },
    "email": "[email protected]",
    "gender": "Male",
    "parent": "59d0ff689b95b02fec446c55",
    "results": [{
        "score": 87467,
        "course": 56396
    }, {
        "score": 728,
        "course": 80047
    }],
    "birth_date": "1995-12-25T22:15:30.000Z",
    "phones": [
      '(857) 375-1663',
      '(601) 926-2014',
      '(705) 324-8873 x9541',
      '(693) 690-3304 x99775',
      '1-088-666-8801 x6452',
      '1-125-206-1792',
      '607.384.4536',
      '228.058.0088 x91535'
    ]
    "is_student": true,
    "detail": {
        "none_match": "Eugenia_Kuvalis"
    }
}*/

Testing

To run the test cases use npm test

Related packages

mongoose-mimic API is inspired by mongoose-dummy, which provides a more limited capability to customize generated values.

Other similar packages to generate test data for Mongoose are:

License

Licensed under MIT