Code Monkey home page Code Monkey logo

meteor-factory's Introduction

Meteor Factory

A package for creating test data or for generating fixtures.

Installation

$ meteor add dburles:factory

Table of Contents

Examples

Defining factories

Authors = new Meteor.Collection('authors');
Books = new Meteor.Collection('books');

Factory.define('author', Authors, {
  name: 'John Smith'
}).after(author => {
  // Do something smart
});

Factory.define('book', Books, {
  authorId: Factory.get('author'),
  name: 'A book',
  year() { return _.random(1900, 2014); }
});

// We can also extend from an existing factory
Factory.define('anotherBook', Books, Factory.extend('book', {
  // ...
}));

Creating documents

// Ex. 1: Inserts a new book into the books collection
const book = Factory.create('book');

// Ex. 2: New fields can be added or overwritten
const book = Factory.create('book', { name: 'A better book' });

API

Note: When calling Factory.create('book') both the Book and an Author are created. The newly created Author _id will then be automatically assigned to that field. In the case of calling Factory.build('book') as no insert operations are run, the _id will be faked.

define

Factory.define('name', Collection, doc).after(doc => { ... })

  • name
    • A name for this factory
  • Collection
    • A meteor collection
  • doc
    • Document object
  • .after hook (Optional)
    • Returns the newly inserted document after calling Factory.create

get

Factory.get('name')

Returns the instance of name. Typical usage is to specify a relationship between collections as seen in the Book example above.

build

Factory.build('name', doc)

Builds the data structure for this factory

  • name
    • The name defined for this factory
  • doc (Optional)
    • Document object

tree

Factory.tree('name', doc)

Builds an object tree without _id fields. Useful for generating data for templates or passing to mutator methods.

  • name
    • The name define for this factory
  • doc (Optional)
    • Document object

Example:

  Factory.define('author', Authors, {
    name: "John Smith"
  });

  Factory.define('book', Books, {
    name: "A book",
    author: Factory.get('author')
  });

  const book = Factory.tree('book');

book then equals:

{ name: 'A book', author: { name: 'John Smith' }}

create

Factory.create('name', doc)

Creates (inserts) this factory into mongodb

  • name
    • The name defined for this factory
  • doc (Optional)
    • Document object

extend

Factory.extend('name', doc)

Extend from an existing factory

  • name
    • The name defined for this factory
  • doc (Optional)
    • Document object

Other

Fake makes a great companion package. See https://atmospherejs.com/anti/fake

License

MIT. (c) Percolate Studio

factory was developed as part of the Verso project.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.