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

schema-faux

v0.0.1

Published

Mock data generator for mongoose schemas

Downloads

2

Readme

schema-faux

Build

schema-faux is a JS library designed to generate realistic mock data based on Mongoose schemas. This library is essential for testing scenarios where you need accurate simulated data. It caters to Node.js applications employing Mongoose for MongoDB integration, as well as other applications requiring lifelike fake data.

Features

  • Generate Realistic Mock Data: Create mock data that accurately reflects the structure and constraints of your Mongoose schemas.

  • Flexible Configuration Options: Customize the data generation process using a variety of options to align with your specific use cases.

  • Support for Nested Schemas: Efficiently handle nested schemas, including subdocuments, to ensure precise mock data generation, even for intricate data structures.

  • Array Field Handling: Seamlessly generate mock data for fields with array types, catering to both primitive types and subdocuments.

Installation

Install schema-faux using npm:

npm install schema-faux

Usage

Import the generateMock function from the library and use it to generate mock data based on your Mongoose schema.

const { generateMock } = require('schema-faux');
const mongoose = require('mongoose');

// Define your Mongoose schema
const userSchema = new mongoose.Schema({
  username: String,
  email: {
    type: String,
    required: true,
  },
  age: Number,
  posts: [
    {
      title: String,
      content: String,
    },
  ],
});

// Generate mock data based on the schema
const mockUser = generateMock(userSchema);

console.log(mockUser);
// Output: Mock data object with realistic values based on the schema
{
  username: 'Joan Bogan',
  email: '[email protected]',
  age: 64,
  posts: [
    {
      title: 'past',
      content: 'failing',
      _id: 'b64edf4fdafacfb33b73bfe7'
    }
  ],
  _id: '504dda8e8becb7ed78b2e9ef'
}

Supported Mongoose Schema Types

schema-faux supports a wide range of Mongoose schema types, including:

  • String
  • Number
  • Date
  • Buffer
  • Boolean
  • Mixed
  • ObjectId
  • Array
  • Decimal128
  • Schema
  • BigInt

Configuration Options

schema-faux provides options to fine-tune the mock data generation process:

  • requiredOnly: Generate mock data for required fields only. Useful for scenarios where only mandatory data points need to be generated.
const { generateMock } = require('schema-faux');
const mongoose = require('mongoose');

// Define your Mongoose schema
const addressSchema = new mongoose.Schema({
  street: String,
  city: {
    type: String,
    required: true
  },
  zipCode: String
});

const userSchema = new mongoose.Schema({
  username: {
    type: String,
     required: true
  },
  email: {
    type: String,
    required: true,
  },
  age: Number,
  address: {
    type: addressSchema,
    required: true
  },
  posts: [
    {
      title: String,
      content: String,
    },
  ],
});

// Generate mock data based on the schema
const mockUser = generateMock(userSchema, {requiredOnly: true});

console.log(mockUser);

// Output: Mock data object with only required fields

{
  username: 'Mrs. Bernice Berge',
  email: '[email protected]',
  address: { city: 'Prudencehaven' }
}

Handling Common Fields

Commonly used fields such as addresses, countries, and cities can be easily generated with valid data. Example,

const mongoose = require('mongoose');
const { generateMock } = require('schema-faux');

// Define your Mongoose schema
const addressSchema = new mongoose.Schema({
  street: String,
  city: {
    type: String,
    required: true,
  },
  zipCode: String,
});

const oraganizationSchema = new mongoose.Schema({
  name: String,
  address: addressSchema,
  website: String,
  phone: String,

});
// Generate mock data based on the schema
const mockOrganization = generateMock(oraganizationSchema);

console.log(mockOrganization);

// Output: Mock data object with fields with valid values
{
  name: 'Margarita Champlin',
  address: {
    street: 'Flatley Village',
    city: 'Fort Destinview',
    zipCode: '45073',
    _id: '33f28364efb2dee4be91ce46'
  },
  website: 'https://powerful-tadpole.net',
  phone: '(684) 763-2445',
  _id: '47c14bbb478ee9fa197e5ebb'
}

Handling Enum Values

schema-faux handles enum values by utilizing the enum property defined in the Mongoose schema. If your schema defines an enum property for a field, the generated mock data will adhere to the specified enum values.

Handling Mongoose Validations

Min and max values specified in the schema for Number, as well as minLength and maxLength for String, are taken into account during mock data generation. The library ensures that generated data adheres to these constraints while remaining realistic.

Contributing

Contributions to schema-faux are highly appreciated! If you discover a bug, have enhancement ideas, or want to contribute in any capacity, please feel free to open an issue or a pull request on the GitHub repository.

License

This project is licensed under the MIT License - see the LICENSE file for details.