dbdummy

0.0.10 • Public • Published

README

This README would normally document whatever steps are necessary to get your application up and running.

What is this repository for?

  • This module was developped to get possibility of fast generating db structure and add dummy data to created tables according json object send to the module's create() function.
  • 0.0.3

How do I get set up?

Installing:

Install dbdummy module:

#!npm

npm install dbdummy

one of DB driver:

#!npm

npm install mysql
npm install pg
npm install sqlite3

Configuration

See example in the module's folder (see node_modules/example/app.js).

Configuration parameters send as json object to dbdummy.create(json, callback) function:

#!json

{
  "debug": false,  // enable/disable showing debug massages at the console
  "recreate": true,  // If set to true then tables will be deleted and created again else data will be added to the tables
  "driver": "mysql",  // db driver
  "host": "192.168.3.158", // db server address
  "filename": "", // filename for sqlite db
  "user": "root", // db user name
  "password": "", // db user password
  "database": "dbdummy_test", // database where tables will be created/updated
// Creation parameters section
  "tables": {
    "user": { // Table name
      "length": 200, // Quantity of generated records
      "fields": {
        "id": { // field name
          "type": "increments" // Field type (see Field types and values section)
        },
        "name": {
          "type": "string",
          "value": "function(){ return chance.name();}" // Field value (see Field types and values section)
        },
        "type": {
          "type": "string",
          "value": "function(){ return chance.string();}"
        }
      }
    },
    "file": {
      "length": 1000,
      "fields": {
        "id": {
          "type": "increments"
        },
        "filename": {
          "type": "string",
          "value": "function(){ return chance.string();}"
        },
        "user_id": {
          "type": "foreign_key",
          "table": "user",
          "field": "id"
        },
        "file_type": {
          "type": "reference",
          "table": "file_type",
          "field": "type"
        }
      }
    },
    "file_type": {
      "length": 10,
      "fields": {
        "id": {
          "type": "increments"
        },
        "type": {
          "type": "string",
          "value": "function(){ return chance.string();}"
        }
      }
    },
    "file_type": {
      "length": 10,
      "fields": {
        "id": {
          "type": "increments"
        },
        "type": {
          "type": "string",
          "value": "function(){ return chance.string();}"
        }
      }
    }
  } 
}

Usage

  1. Create your application
  2. Install dbdummy module (see Installing section)
  3. Install one of db drivers modules (see Installing section)
  4. Include dbdummy module:
#!javascript

dbdummy = require('dbdummy');
  1. Create new database
  2. Call dbdummy.create function to create tables:
#!javascript

dbdummy.create(config, function(err, data) {
 ....
});

config - json object with db configuration data (see Configuration section). closure - callback function where result is sent

There is an application code example in the module's folder (see node_modules/example/app.js).

Dependencies

knex.js, chance.js

Database configuration

This module based on knex.js module and can work with mysql, postgreesql and sqlite databases. Database configuration should be set in config json that is sent as the first parameter to the dbdummy.create() function (see Configuration section).

Field types and values

Field types:

This module accepts all field types that knex.js module accepts (with only one obligatory parameter):

incrementstable.increments(name) Adds an auto incrementing column, in PostgreSQL this is a serial. This will be used as the primary key for the column. Also available is a bigIncrements if you wish to add a bigint incrementing number (in PostgreSQL bigserial).

integertable.integer(name) Adds an integer column.

bigIntegertable.bigInteger(name) In MySQL or PostgreSQL, adds a bigint column, otherwise adds a normal integer.

texttable.text(name, [textType]) Adds a text column, with optional textType for MySql text datatype preference. textType may be mediumtext or longtext, otherwise defaults to text.

** stringtable.string(name, [length])** Adds a string column, with optional length defaulting to 255.

floattable.float(column, [precision], [scale]) Adds a float column, with optional precision and scale.

decimaltable.decimal(column, [precision], [scale]) Adds a decimal column, with optional precision and scale.

booleantable.boolean(name) Adds a boolean column.

datetable.date(name) Adds a date column.

dateTimetable.dateTime(name) Adds a dateTime column.

timetable.time(name) Adds a time column.

timestamptable.timestamp(name, [standard]) Adds a timestamp column, defaults to timestamptz in PostgreSQL, unless true is passed as the second argument. Note that the method for defaulting to the current datetime varies from one database to another. For example: PostreSQL requires .defaultTo(knex.raw('now()')), but SQLite3 requires .defaultTo(knex.raw("date('now')")).

timestampstable.timestamps() Adds a created_at and updated_at column on the database, setting these each to dateTime types.

binarytable.binary(name) Adds a binary column.

jsontable.json(name, [jsonb]) Adds a json column, using the built-in json type in postgresql, defaulting to a text column in older versions of postgresql or in unsupported databases. jsonb can be used by passing true as the second argument.

uuidtable.uuid(name) Adds a uuid column - this uses the built-in uuid type in postgresql, and falling back to a char(36) in other databases.

And 2 special types were added:

reference

Add column that consists of column's values from other table

foreign_key

Add column that consists of column's values from other table and create foreign key. This column should point to field that is primary key (now it can be field with increments type).

Field with such types must contain two obligatory parameters:

table //Referenced table

field //Referenced field

Field values:

To set field values you should add value parameter. There are 3 possible way of setting value:

  1. Set value as String (for ex.: value: 'test'). In this case every value of this string will be equal and set to string provided ('test');

  2. Set value as Array (for ex.: value: ['asd', 'qwe']). In this case field values will be chosen randomly from values of provided array;

  3. Set value as String with format: 'function(){return ....}' (for ex.: value: 'function(){return chance.name()}'). In this case field values will be set according to value returned by this function (this module out-of-the-box supports chance.js functions (installs as dependency));

Who do I talk to?

Package Sidebar

Install

npm i dbdummy

Weekly Downloads

1

Version

0.0.10

License

MIT

Last publish

Collaborators

  • nklimkovich