Giter VIP home page Giter VIP logo

ectypes.js's Introduction

build status

ectypes

A way to produce test objects quickly with any persistence layer.

The Name

ectype (plural ectypes) (philosophy) A copy; usually contrasted with the original, or archetype.  

As opposed to prototype, ectype originally meant “wrought in relief” in Greek. Its roots are ec, a variant of “ex,” and týpos, a “figure on a wall.”

Installation

npm install ectypes

Explanation: Contexts, Strategies, Blueprints and Producers.

Ectypes is a DSL for building persistable data types and describing how they should be filled with data. If you've ever used factory_girl or machinist (Ruby testing libraries) the concept will be familiar to you.

Ectypes uses a strategy to identify how to talk to whichever persistence layer you've chosen to use (mongodb, relational db, redis, whatever).

var ectypes = require('ectypes')
  , ctx = ectypes.createContext()
  , SimpleStrategy = require('./simple-strategy');

ctx.load(new SimpleStrategy());

Ectype contexts take blueprints of data types, and make them available.

var projectBlueprint = {
	Project: {
		title: function(){ return Faker.Name.findName() }
	}
};

ctx.add(projectBlueprint);

Blueprints can either be added as objects or an array.

var multiBlueprint = [
  {Person: {
    title: function(){ return Faker.Name.findName() }
  }}
  , {Ancestor: {
    title: function(){ return Faker.Name.findName() }
  }}
];

ctx.load(multiBlueprint);

Every function on the strategy (except for what is listed in an ignores array) is then mapped to the type, as a producer.

ctx.Project.build(function(err, project){ 
  console.log(project.title); //gives 'Elaina Orn', a value produced by the Faker library.
});

In the above example, the simpleStrategy has defined a function named 'build' accepting the arguments '(modelName, values, callback)'. Ectypes builds a producer function mapping to the strategy's function and passing it the appropriate data.

If you wanted to override the value of some test data in some circumstances (to set up relations to other objects, etc.):

ctx.Project.build({title: 'Someone else'}, function(err, project){ 
  console.log(project.title); //gives 'Someone else'
});

In the above, if the first argument to a strategy call is an object, any values it contains will be used to override the generated ones. If it's a function then it's assumed to be the conventional Node.js callback function(err, result).

It is up to you, when writing the strategy, which behaviours you want to support.

Writing Strategies

Ectypes uses a strategy pattern to specify proxied calls to whichever underlying persistence layer(s) you might like to use - they're very simple to write.

Current strategies:

If you write one, please let me know.

Running Tests

npm test 

More Info

Ectypes originally worked for Backbone.js. So it is possible to enable this testing for browser based development. If anyone wants to discuss it, let me know.

ectypes.js's People

Contributors

nicholasf avatar damienwhaley avatar

Watchers

James Cloos avatar  avatar

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.