{"id":15606265,"url":"https://github.com/tb/factory-js","last_synced_at":"2025-04-27T12:46:46.861Z","repository":{"id":15425809,"uuid":"18158230","full_name":"tb/factory-js","owner":"tb","description":"Building JavaScript objects inspired by rosie.js and factory_girl","archived":false,"fork":false,"pushed_at":"2016-11-17T23:00:54.000Z","size":29,"stargazers_count":38,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-27T12:46:39.369Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"CoffeeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tb.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-03-26T23:47:46.000Z","updated_at":"2024-10-03T23:33:38.000Z","dependencies_parsed_at":"2022-08-26T05:11:34.242Z","dependency_job_id":null,"html_url":"https://github.com/tb/factory-js","commit_stats":null,"previous_names":["tb/factory"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tb%2Ffactory-js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tb%2Ffactory-js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tb%2Ffactory-js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tb%2Ffactory-js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tb","download_url":"https://codeload.github.com/tb/factory-js/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251141817,"owners_count":21542428,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-03T04:21:46.843Z","updated_at":"2025-04-27T12:46:46.840Z","avatar_url":"https://github.com/tb.png","language":"CoffeeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Factory [![Build Status](https://travis-ci.org/tb/factory.svg)](https://travis-ci.org/tb/factory)\n\nBuilding JavaScript objects inspired by [rosie](https://github.com/bkeepers/rosie) and\n[factory_girl](https://github.com/thoughtbot/factory_girl).\n\nFactory can integrate with JavaScript framework persistence layer through [Adapters](#adapters).\n\n## Usage\n\n    Factory.define('vote')\n      .sequence('id')\n      .attr('value', 0)\n      .trait('up', function() {\n        return this.attr('value', 1);\n      })\n      .trait('down', function() {\n        return this.attr('value', -1);\n      });\n\n    Factory.define('post')\n      .sequence('id')\n      .sequence('title', function(i) {\n        return \"Post \" + i;\n      })\n      .attr('content', null)\n      .hasMany('votes', 'vote')\n      .after(function() {\n        if (!this.content) {\n          return this.content = \"\" + this.title + \" content\";\n        }\n      });\n\n    Factory.define('category')\n      .sequence('id')\n      .sequence('name', function(i) {\n        return \"Category \" + i;\n      })\n      .ignore('postsCount', 0)\n      .after(function(attributes) {\n        return this.posts = Factory.buildList('post', attributes.postsCount);\n      });\n\n### Build with no attributes\n\n    Factory.build('post')\n\nresult:\n\n    {\n      \"id\":1,\n      \"title\":\"Post 1\",\n      \"content\":\"Post 1 content\"\n    }\n\n### Build with attributes\n\n    Factory.build('post', {content: 'my content'})\n\nresult:\n\n    {\n      \"id\":1,\n      \"title\":\"Post 1\",\n      \"content\":\"my content\"\n    }\n\n### Build with ignored attribute and after() callback\n\n    Factory.build('category',{name: 'First category', postsCount: 2})\n\nresult:\n\n    {\n      \"name\":\"First category\",\n      \"id\":1,\n      \"posts\":[\n        {\"id\":1,\"title\":\"Post 1\",\"content\":\"Post 1 content\"},\n        {\"id\":2,\"title\":\"Post 2\",\"content\":\"Post 2 content\"}\n      ]\n    }\n\n### Build post with votes count\n\n    Factory.build('post', {votes: 2})\n\nresult:\n\n    {\n      \"content\" : \"Post 1 content\",\n      \"id\" : 1,\n      \"title\" : \"Post 1\",\n      \"votes\" : [\n          {\"id\":1,\"value\":1},\n          {\"id\":2,\"value\":1}\n        ]\n    }\n\n### Build post with votes traits or attributes\n\n    Factory.build('post', {votes: ['up', 'down', 'up']})\n\nor\n\n    Factory.build('post', {votes: [{value: 1}, {value: -1}, {value: 1}]})\n\nresult:\n\n    {\n      \"content\" : \"Post 1 content\",\n      \"id\" : 1,\n      \"title\" : \"Post 1\",\n      \"votes\" : [\n          {\"id\":1,\"value\":1},\n          {\"id\":2,\"value\":-1},\n          {\"id\":3,\"value\":1}\n        ]\n    }\n\n## Adapters\n\nBy default factory is building JavaScript objects using default `Factory.Adapter`\n\n    class Factory.Adapter\n      constructor: (factory) -\u003e @factory = factory\n      build: (name, attrs) -\u003e attrs\n      create: (name, attrs) -\u003e attrs\n      push: (name, object) -\u003e @[name].push object\n\nFactory integrates with Ember.js through `Factory.EmberDataAdapter` (used by `Factory.setupForEmber(App)`)\n\n    class Factory.EmberDataAdapter extends Factory.Adapter\n      build: (name, attrs) -\u003e Ember.run =\u003e App.__container__.lookup('store:main').createRecord name, attrs\n      create: (name, attrs) -\u003e @build name, attrs\n      push: (name, object) -\u003e Ember.run =\u003e @get(name).addObject object\n\nYou can set adapter globally\n\n    Factory.adapter = Factory.YourAdapter\n\nor per factory definition\n\n    Factory.define 'yourModel', -\u003e\n      @adapter Factory.YourAdapter\n\n## Setup for Ember.js\n\nCall `Factory.setupForEmber(App)` before factory definitions. See live example at [jsbin](http://emberjs.jsbin.com/serolule/edit)\n\nNOTE: You need to call `Factory.reset()` to reset sequences for each test run.\n\n## Contributing\n\n    git clone git@github.com:tb/factory.git\n    cd factory\n    npm install\n    grunt build\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftb%2Ffactory-js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftb%2Ffactory-js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftb%2Ffactory-js/lists"}