@scaffold.js/scaffold

0.1.0 • Public • Published

Scaffold.js

Creating test fixtures has never been so easy

Installation

npm install @scaffold.js/scaffold

Usage

const scaffold = require('@scaffold.js/scaffold');
const pure = {
    functions: {
        email: function (firstName, lastName) {
            return firstName + '_' + lastName + '@gmail.com'
        }
    }
}
const schema = {
    types: {
        Employee: {
            id: 'id',
            name: 'string',
            contact: 'string',
            department: 'string',
            manager: 'Employee',
            email: {
                _func: [pure.functions.email, 'scaffold', 'awesome'],
            },
        },
    },
};
const {employeeGenerator} = scaffold({
    hasManyMin: 1,
    schema,
});
const employee = employeeGenerator();
const manager = employee.manager;
console.log(employee);
// {
//     id: 1,
//     name: 'non',
//     contact: 'sapien',
//     department: 'nec',
//     manager: [Getter],
//     email: 'scaffold_awesome@gmail.com'
// }
console.log(manager);
// {
//     id: 2,
//     name: 'nisi',
//     contact: 'adipiscing',
//     department: 'sapien',
//     manager: [Getter],
//     email: 'scaffold_awesome@gmail.com'
// }

Supports multiple in-built primitive types

[
    boolean,
    date,
    float,
    id, // generates unique id
    number, // generates a random integer
    string, // generates a random word
    text, // generates a sentence
]

If the in built primitives are not sufficient you can always pass a pure function for a field like this

const pure = {
    functions: {
        email: function (firstName, lastName) {
            return firstName + '_' + lastName + '@gmail.com'
        }
    }
}

{
    email: {
        _func: [pure.functions.email, 'scaffold', 'awesome']
    }
}

where the first element is a pure function where-as the rest of the elements are arguments to this function

Relations in schema

A fixture in general will have one-many relation or one-one relation scaffold.js makes it easy to manage such relations without any state dependency between them

One-Many

one-many relationship is made possible through _hasMany field within a type

{
    User: {
        _hasMany: {
            friends: ['User']
        }
    }
}
// this will add a friends getter in the generated user fixture
{
    Student: {
        _hasMany: ['Course', 'Project']
    }
}
// this will pluralize 'Course', 'Project' to courses and projects and add them as getters to the generated Student fixture

One-One

one-one relationship doesn't require a special field within a type

{
    Employee: {
        manager: 'Employee'
    }
}
// scaffold.js will check if the 'Employee' type exist and attaches a manager getter to the generated employee fixture

check out an elaborate example here

Package Sidebar

Install

npm i @scaffold.js/scaffold

Weekly Downloads

2

Version

0.1.0

License

MIT

Unpacked Size

682 kB

Total Files

23

Last publish

Collaborators

  • scaffold.js