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

faquel

v0.2.91

Published

Generate fake data directly from sequelize schema

Downloads

64

Readme

Faquel

Generate fake data for your sequelize model directly from your sequelize schema.

Installation

Install from npm npm i faquel

Usage

This library exports two function generateEntryFromModel, generateEntryFromSchema. Both of them take 2 arguments.

First argument for generateEntryFromModel is a sequelize model instance which will be used as reference to generate fake data. First argument for generateEntryFromSchema on the other hand, is a raw schema definition in data object format.

Second argument is an optional object config that you can use to overwrite the default generated fake data with custom faker methods. Follow the examples below to get a better understanding.

Example 1

Let's start with a model that looks like:

const PersonSchema = {
    name: {
        type: DataTypes.CHAR(20),
    },
    annualIncome: {
        type: DataTypes.DOUBLE(11, 2),
    },
    dob: {
        type: DataTypes.DATEONLY
    },
    option: {
        type: DataTypes.ENUM("1", "2", "3", "4"),
        allowNull: true,
    },
};

class Person extends Model {
    static init(sequelize: any) {
        const inst = super.init(PersonSchema, {
            modelName: 'Person',
            timestamps: true,
            sequelize,
        });

        return inst;
    };
};

Assuming that you have followed sequelize guide and setup your initialized models in a container named models.

import { generateEntryFromModel } from 'faquel';

// other code.... i.e: setup for sequelize 
...

const fakePersonFromModel = generateEntryFromModel(models.Person);
/*
    fakePerson = { 
        id: 26651,
        name: 'Cupiditate nulla del',
        annualIncome: 80886196623.68,
        monthlyIncome: 20656,
        dob: 2018-08-09T13:37:39.846Z,
        option: '3',
        createdAt: 2019-01-03T00:12:40.279Z,
        updatedAt: 2018-08-29T13:52:40.877Z 
    };
*/

const fakePersonFromSchema = generateEntryFromSchema(PersonSchema);
/*
    fakePersonFromSchema = { 
        name: 'Cupiditate nulla del',
        annualIncome: 80886196623.68,
        monthlyIncome: 20656,
        dob: 2018-08-09T13:37:39.846Z,
        option: '3',
    };
*/

Notice how in fakePersonFromSchema the id, createdAt, updatedAt etc fields are missing? that's because those columns are autogenerated by sequelize model when they are initiated and since the generateEntryFromSchema function generates the data from data object schema definition, it only outputs field that are present in the object.

Example 2

That's great and all but the name doesnt look like a real name here, does it? This library uses faker.js under the hood and faker has a lot of nifty methods that can generate "realistic" data. Fortunately, you can use those too.

const fakePerson = generateEntryFromModel(models.Person, {
    name: 'name.findName'
});
/*
    fakePerson = { 
        ... other columns
        name: 'John Doe',
        ... other columns
    };
*/

Notice how the name looks more like a real name in the example above? that's because of the 2nd object parameter we are passing to the generateEntryFromModel function specifies that the name column should use the faker.name.findName() method to generate it's value.

You can pass any faker method name as a string like the example above.

Also, faker may not be good enough for generating the desired data so as a supreme overrider, you can pass a function mapped to column name through fakerMap and the function will be invoked to generate your data.

const fakePerson = generateEntryFromModel(models.Person, {
    name: () => {
        // do a lot of computation here
        return 'the answer to life is 42';
    }
});
/*
    fakePerson = { 
        ... other columns
        name: 'the answer to life is 42',
        ... other columns
    };
*/

ToDos

  • mysql SET columns are currently handled but not tested since we are using sqlite for testing. may be a test suite across dbs would help?

Contribute

PRs are more than welcome since right now, the library only supports a few of the column types from sequelize.

To run the lib locally, you can clone the repo then run the following commands:

  • npm i to install the dependencies
  • npm run build:watch to compile typescript code and run the compiled code through nodemon so that you can develop without restarting the builder over and over again.
  • npm test to run the only couple of tests we have right now.