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

knex-seeder

v0.1.1

Published

Knex tool for seeding massive amount of fake data into a database

Downloads

11

Readme

knex-seeder

GitHub top language license GitHub (pre-)release

Knex tool for seeding massive amount of fake data into a database.

Table of topics

  1. Get started
  2. Installation
  3. Basic configuration
  4. Examples
  5. API
  6. Database connections
  7. Be a contributor
  8. Get updated
  9. Licence
  10. Further help

About

This tool incorporates Faker.js for generating massive amounts of fake data. As knex-seeder is built in top of Knex.js module,it is possible to use the functions for creating and seeding tables.

Get Started

Installation

For development

git clone https://github.com/2rhop/knex-seeder.git ks
cd ks
npm install

For production

npm install knex-seeder --save

Configuration

Its important to know that knex-seeder loads all database configurations from a knexfile.js file located in your relative path, you can get more info about this file here or just copy this code snippet:

module.exports = {
    development: {
        client: 'database_client',
        connection: {
            host: '127.0.0.1',
            user: 'your_database_user',
            password: 'your_database_password',
            database: 'your_database_name',
            port: 'database_client_port'
        }
    }
    production: {
        client: 'pg',
        connection: process.env.DATABASE_URL || { user: 'me', database: 'my_app' }
    }
}

Note: To switch between environments you need to change the value of the NODE_ENV variable:

process.env.NODE_ENV = 'development' | 'production'

Examples

Note: All the example scripts are located in /examples/. . . folder.

  • First we are going to see how to create a FieldModel: (e.g here)
const ks = require('knex-seeder')

var myFieldModel = new ks.FieldModelBuilder({
    
    fieldName : [1, 2, 5, 6, 7],//chooses an element in the array
    
    fieldName : new ks.SeedFaker('name.findName','en')//calls the faker.js function for [name.findName] with lang [english]
    
    fieldName : new ks.SeedFaker(ks.FAKER.NAME,ks.LANG.ENGLISH)//or call the faker.js functions with some predefine constants by kenex-seeder
    
    fieldName : new ks.SeedRange(2, 60),//chooses a random value in the range [2, 60]
    
    fieldName : 7,//chooses only this element for the seeding
    
    fieldName : 'Ingrid',//chooses only this element for the seeding

    fieldName : [true, false],//seeds randomly with TRUE or FALSE

    fieldName : new Date(),//seeds the table with the actual date
    
    fieldName : ()=>{//uses this fn for the seeding
      var gender = ['M', 'F'];
      var index = Math.floor(Math.random() * c.length);
      return gender[index];
    }
    
    //you can add more fields here
    ...
    
}).build;

...

And also how to create a TableModel:

...

var tableName = 'myTable'
var myTableModel = new ks.TableModelBuilder(tableName, myFieldModel).build;

Note: Notice the use of Builder Pattern to create the TableModel and FieldModel, reason why before pass it as an argument you must call .build getter to construct the Object.

  • You can use the Knex functions inside knex-seeder to create a new table and also chain multiple tables creations: ( e.g. here )
const ks=require('knex-seeder')

ks.createTable(myTableModel, (table) => {
//here you can especify the fields for the first table
       table.increments();
       table.string('name');
       table.integer('role_id');
       table.timestamps(true,true);

       ...

    }).then(() => {//do chainning...
           ks.createTableAndClose(myTableModel2, (table) => {
           //here you can especify the fields for the second table
                table.increments();
                table.string('name');

                ...

            }).then(() => {
            //do something after here...
    });
});

Or you can omit the use of the knex.js function to create a new table and let knex-seeder to do that for you:

...

ks.createTable(myTableModel).then(() => {//do chainning...
           ks.createTableAndClose(myTableModel2).then(() => {
            //do something after here...
    });
});

Note: The function createTable() returns a Promise after creation is done and the function createTableAndClose() also closes the connection. The last one is useful for calling it at the end of a chainning.

  • Also with knex-seeder you can (of course) seed tables in a database: ( e.g. here )
const ks = require('knex-seeder')

const queries = 10;

ks.Seeder.seedAndClose(myTableModel, queries).then(() => {
    //do something after...
})
  • We have seen how to create a table and how to seed it, but how about do both at the same time, well here is a piece of code to show you how to do that: ( e.g. here )
const ks = require('knex-seeder')

//creating and seeding process
ks.createAndSeed(myTableModel, 10, (table) => {
    table.increments(),
        table.integer('age'),
        table.string('name'),
        table.string('country'),
        ...
        table.string('gender'),
        table.timestamps(true, true)
}).then(() => {
    ks.createAndSeed_close(myTableModel2, 10, (table) => {//closes the connection after process
        table.increments(),
            table.string('name'),
            table.string('category'),
            ...
            table.timestamps(true, true)
    })
}).then(() => {
    //do something after...
})

or omitting the knex.js function to create automatically the table

...

//creating and seeding process
ks.createAndSeed(myTableModel, 10).then(() => {
    ks.createAndSeed_close(myTableModel2, 10).then(() => {
    //do something after...
})

Note: If you forget how to create a Model, see the first example from above.

API

Note: If you wanna use the knex.js api, just import knex variable from knex-seeder, just like this:

const { knex } = require('knex-seeder')

or do this instead:

const ks = require('knex-seeder')

const knex = ks.knex;

Connections

To integrate this tool with a database connection you need to install the appropriate package:

  • npm install pg
  • npm install mysql
  • npm install mysql2
  • npm install mariasql
  • npm install strong-oracle
  • npm install oracle
  • npm install mssql
  • npm install sqlite3

Support

Contributing

Build Status

All the unit tests are written with Jasmine. Feel free to add more functionalities and bug fixes but also add a test for each of them. Just type npm test, but before that you need to install Jasmine globally.

npm install -g jasmine

Also remember to update the CHANGELOG.md file with your modifications on this tool.

Changelog

GitHub (Pre-)Release Date

Get update with all new versions here

Licence

Copyright (c) 2018 Rene Ricardo. Licensed under the MIT license.

Help

Send me an email or a tweet if you have some doubt or just add an issue