For quickly adding data to a node project. Ideal for mock or testing data. Fixtures serve up json files as a data source.
Requires Nodejs http://nodejs.org
From the Command Line
npm install git+https://github.com/bdunford/fixtures.git
package.json as a dependency
add "fixtures": "git+https://github.com/bdunford/fixtures.git" to dependencies
{
"name": "your-app",
"version": "0.0.0",
"private": true,
"description": "just another node app",
"dependencies": {
"helpers": "git+https://github.com/bdunford/fixtures.git"
}
}
Raw
git clone https://github.com/bdunford/fixtures
Be sure to run npm install from with in the fixtures directory
Require fixtures in your javascript
var fixtures = require('fixtures');store returns an instance of Store for the matching JSON file found with in the fixtures directory If you had the following JSON file in your directory fixtures/cusomters.json
fixtures.store('customers'); //will return
{
file: 'fixtures/customers.json',
all: [Function],
find: [Function]
}stores returns an object containing all of the stores (JSON files) found with in the fixtures folder in you project. If you had the following JSON files in your directory fixtures/cusomters.json and fixtures/orders.json
fixtures.stores(); //will return
{
customers: {
file: 'fixtures/customers.json',
all: [Function],
find: [Function] },
orders: {
file: 'spec/fixtures/orders.json',
all: [Function],
find: [Function] }
}
}Store.all returns the contents of the JSON file parsed to an object or array of objects for that store. If you had the following JSON file in your directory fixtures/cusomters.json
fixtures.store('customers').all();
//will return the content of "fixtures/cusomters.json" parsed to an array of objectsStore.find returns the contents of the JSON file matching the filter object passed to in you had the following JSON file in your directory fixtures/cusomters.json
fixtures.store("customers").find({lastName: "smith"});
//will return the content of "fixtures/cusomters.json" parsed to an array where the customers had a lastName of smith.location can be set on fixtures to control the location of the JSON files. The default location is fixtures/
fixtures.location = "./db/fixtures";
//would make fixtures look for JSON files in db/fixtures/ instead of fixtures/