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

db-fabricator

v2.1.0

Published

Conveniently populate your database.

Downloads

3

Readme

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({
  name: 'organization',
  attr: {
    name: 'Fabricator Inc'
  }
});
Fabricator.template({
  name: 'department',
  attr: {
    name: 'IT',
    organizationId: () => Fabricator.fabricate('organization').then(org => org.id)
  }
});
Fabricator.template({
  name: 'user',
  attr: {
    firstName: 'Bob',
    lastName: 'Smith',
    username: (obj) => `${obj.firstName}.${obj.lastName}`,
    departmentId: () => Fabricator.fabricate('department').then(dept => dept.id)
  }
});

Define template from another template

You can define template from another template by specifying from property with another template name.

Fabricator.template({
  name: 'user-teacher',
  from: 'user',
  attr: {
    type: 'teacher'
  }
});
Fabricator.template({
  name: 'user-student',
  from: 'user',
  attr: {
    type: 'student'
  }
});
Fabricator.template({
  name: 'user-student-with-email',
  from: 'user-student',
  attr: {
    email: (obj) => `${obj.username}@student.school.edu`,
  }
});

Populate data from template

Conveniently populate data from the template. You can still override any attribute from the template.

Fabricator.fabricate('user', { firstName: 'Bob' }); // Department and organization will be automatically created for the user
Fabricator.fabricate('user', { firstName: 'Jon' }); // Jon will have different department and organization
Fabricator.fabricate('user-student-with-email', { firstName: 'Dan' });

If you want to use the same department/organization for some users:

let fabId = Fabricator.getId;
let org            = Fabricator.fabricate('organization');
let dept_it        = Fabricator.fabricate('department', { name: 'IT',        organizationId: fabId(org) });
let dept_marketing = Fabricator.fabricate('department', { name: 'Marketing', organizationId: fabId(org) });
let user_bob       = Fabricator.fabricate('user', { firstName: 'Bob',  departmentId: fabId(dept_it) });
let user_jane      = Fabricator.fabricate('user', { firstName: 'Jane', departmentId: fabId(dept_it) });
let user_jon       = Fabricator.fabricate('user', { firstName: 'Jon',  departmentId: fabId(dept_marketing) });
let user_mary      = Fabricator.fabricate('user', { firstName: 'Mary', departmentId: fabId(dept_marketing) });

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({
  name: 'organization',
  attr: {
    name: 'Fabricator Inc'
  },
  afterCreate: (org) => {
    let promises: any[] = [];
    promises.push(Fabricator.fabricate('department', { organizationId: org.id }));
    return Promise.all(promises).then((departments) => {
      org.departments = departments;
      return org;
    });
  }
});

Fabricator.fabricate('organization').then(org => {
  expect(org.departments.length).to.equal(1);
  expect(org.departments[0].organizationId).to.equal(org.id);
});

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.

export interface DataStoreAdaptor {
  createData(tableName: string, finalAttr: Object): Promise<any>;
}

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