Manage easily the data fixtures for your Cozy developments!
Please note that the "load" process do the following:
- removing all the data for the concerned doctypes
- adding the fixtures for the concerned doctypes
The "concerned doctypes" are the ones found in the fixture files or the ones you gaves through parameters.
When you develop an application you might want to feed the Data System with data.
npm install -g cozy-fixturescozy-fixtures load # will load all the fixtures inside ./tests/fixtures/cozy-fixtures load ./fixtures/ # you can specify a foldercozy-fixtures load ./fixtures/my-super-fixtures.json # or a filecozy-fixtures load -d contact # only load the documents for a specified doctypecozy-fixtures load -g -n 25 # uses fixtures/*.json files as model for Mockaroo to auto-generate 25 recordscozy-fixtures load -s # run the script quietlycozy-fixtures -l load # doesn't remove documents before loading
You can use cozy-fixtures programatically to ease automatic testing:
- First, add it to your dev dependencies:
npm install cozy-fixtures --save-dev
- Then use it where you need it
fixtures = require 'cozy-fixtures'fixturesloaddirPath: 'path/to/fixtures' # default is './test/fixtures/'doctypeTarget: 'doctypeName' # default is nullsilent: true # default is falseremoveBeforeLoad: false # default is true. Remove docs for concerned doctypes before loading the datacallback: yourCallback # default is null
You can also do more precise action like:
- deleting all the documents for a specified doctype
fixtures = require 'cozy-fixtures'fixturesremoveDocs "doctypeName"consolelog err if err?
- deleting all the documents from the database
fixtures = require 'cozy-fixtures'fixturesresetDatabase: consolelog err if err?designsToRemove: true # default is false, removes all the design docs in the database (and related) views
If you want to have the same configuration for every call, use the setDefaultValues method:
fixtures = require 'cozy-fixtures'fixturessetDefautValuestarget: './a/custom/target.json'silent: trueremoveBeforeLoad: falsefixturesload : consolelog "fixtures loaded !"
You can still override the defautl values by passing the arguments to the load function.
How to format the fixtures
- You must put the fixtures into files
- Fixtures must be described in valid JSON
- Don't forget the "docType" field in the fixtures
A simple example:
Note that you can add files as attachments. In order to achieve this, add a "_attachments" field to your fixture and put the path relatively to where you run the command.
Mockaroo serviceUse auto-generation through
You can use the Mockaroo service to generate records and inject them as fixtures. You must define your model in your fixtures files, as follow:
Your model declares all fields wanted in each record, and each field must have at least a name and type keys. They relies on the Mockaroo API (see their documentation for more information about types and their parameters). Complex formats such as nested contents are allowed.
Note that an API key is needed to use the service. It can be obtained freely (limit to 200 request per day) on Mockaroo. Just sign up to the service and get your key in my account section. This key must be provided in the
MOCKAROO_API_KEY="my_api_key" cozy-fixtures -g
What is Cozy?
Cozy is a platform that brings all your web services in the same private space. With it, your web apps and your devices can share data easily, providing you with a new experience. You can install Cozy on your own hardware where no one profiles you.
You can reach the Cozy Community by: