DB Fabricator
Convenient way to populate your database, mainly for setting up e2e/integration testing data.
Install
$ npm install db-fabricator
Usage
Setup
import { Fabricator, MySQLAdaptor } from 'db-fabricator';
import * as mysql from 'mysql';
let conn = mysql.createConnection({
host: 'localhost',
user: 'dev',
password: 'pass',
database: 'dbname'
});
Fabricator.setAdaptor(new MySQLAdaptor({conn: conn}));
Defining Template
Define template for each of the data type (table if you are using relational DB). The base template name has to be the table name.
Fabricator.template;Fabricator.template;Fabricator.template;
Define template from another template
You can define template from another template by specifying from
property
with another template name.
Fabricator.template;Fabricator.template;Fabricator.template;
Populate data from template
Conveniently populate data from the template. You can still override any attribute from the template.
Fabricator.fabricate'user', ; // Department and organization will be automatically created for the userFabricator.fabricate'user', ; // Jon will have different department and organizationFabricator.fabricate'user-student-with-email', ;
If you want to use the same department/organization for some users:
;;;;;;;;
Fabricator.getId
is a helper function which will return the id of the resolved promise. It can be passed a promise or
just the object from which you want to get the id.
// In the above example
fabId(org)
// is the same as
org.then(o => o.id)
After Create hook
Fabricator.template; Fabricator.fabricate'organization'.then;
Extensible
Currently db-fabricator only supports MySQL data store, but you can create an adaptor for any database.
Just implement a class that implements the DataStoreAdaptor
interface. For an example, see the
MySQLAdaptor
implementation.
Contributing Guide
Build
$ tsc
Running Test
Install ts-node to run the test without compiling to js first.
$ npm install -g ts-node
Run all tests
$ mocha --compilers ts:ts-node/register test/*
Run the main test
$ mocha --compilers ts:ts-node/register test/fabricator.spec.ts
Run the mysql-adaptor test
$ mocha --compilers ts:ts-node/register test/fabricator-mysql.spec.ts
You have to have a mysql instance running and a user that can create/drop database.
License
MIT