NodeJS Datastore-agnostic Factory pattern
Factory Worker is a node.js library that creates the Factory pattern for object stores. It assumes the objects are building respond to new() and save(). Other than that, it does not care.
Factory-worker is available on npm:
npm install factory-worker
To begin, we need to define our Factory. We do this with the Factory.define method:
var Factory = ;Factory;
In the example above, we've created the "profile" Factory for the class Profile with our default attributes.
created_at attribute will be filled dynamically by evaluating the function at object build time. This is useful to dynamically fill attributes with a name generator, e.g. https://github.com/marak/Faker.js/
build(factory, [attributes], [callback])
Builds an instance of the factory, overriding the factory with the attributes hash if passed.
If a callback is passed, it is possible to use callback functions for building attributes. The callback is detected by checking if the attribute function will accept arguments. Here is an example that will create BlogEntry for a Comment object and set the foreign key accordingly.
var Factory = ;Factory;Factory;
create(factory, [attributes], callback)
build function followed by calling
save(callback) on the created object.
examples directory for more information.
FactoryWorker uses jessie for tests. The test
suite can be invoked with
Contributions are welcome, submit a pull request and I'll happily take a look. I only ask that you don't alter the version of the library in your pull request.
This library was inspired by factory_girl because I wanted something similar while working in NodeJS.
It is by no means as fully functional as
factory_girl, but it's a start.