1 | # loopback-component-fixturing
|
2 |
|
3 | Expressive fixtures generator for Loopback
|
4 |
|
5 | [![build status](https://secure.travis-ci.org/sghribi/loopback-fixtures.svg)](http://travis-ci.org/sghribi/loopback-fixtures)
|
6 | [![dependency status](https://david-dm.org/sghribi/loopback-fixtures.svg)](https://david-dm.org/sghribi/loopback-fixtures)
|
7 |
|
8 | ## Installation
|
9 |
|
10 | ### Basic usage
|
11 |
|
12 | ```
|
13 | npm install --save loopback-fixtures
|
14 | ```
|
15 |
|
16 | Then, in your `server/component-config.json`, add :
|
17 |
|
18 | ``` json
|
19 | {
|
20 | "loopback-fixtures": {
|
21 | "fixturePath": "/fixtures/data/",
|
22 | "append": false,
|
23 | "autoLoad": false
|
24 | }
|
25 | }
|
26 | ```
|
27 |
|
28 | Write your YML fixture file `/fixture/data/data.yml` (adapt according your model) :
|
29 |
|
30 |
|
31 | ``` yaml
|
32 | Group:
|
33 | group{1..10}:
|
34 | name: "Groupe {@} depuis les fixtures"
|
35 |
|
36 | User:
|
37 | user{1..10}:
|
38 | name: "User {@} : {{name.lastName}}"
|
39 | groupId: @group{@}
|
40 | email: "{{internet.email}}"
|
41 | birthDate: "2016-01-01"
|
42 | favoriteNumber: "(function() { return Math.round(Math.random()*1000);})()"
|
43 | ```
|
44 |
|
45 | ### How to load fixtures ?
|
46 |
|
47 | - If `autoLoad` is set to `true`, fixtures will be loaded when you start your application
|
48 |
|
49 | - With the server:
|
50 |
|
51 | `app.loadFixtures()` (return a promise)
|
52 |
|
53 | e.g:
|
54 |
|
55 | ``` js
|
56 | app.loadFixtures()
|
57 | .then(function() {
|
58 | console.log('Done!');
|
59 | })
|
60 | .catch(function(err) {
|
61 | console.log('Errors:', err);
|
62 | });
|
63 | ```
|
64 |
|
65 | - With a node command:
|
66 |
|
67 | ```
|
68 | ./node_modules/loopback-fixtures/lib/load-fixtures.js
|
69 | ```
|
70 |
|
71 | ### Configuration options
|
72 |
|
73 | - `fixturePath` (default value `'/fixtures/data'`)
|
74 |
|
75 | The directory to load data fixtures from
|
76 |
|
77 | - `append` (default value `false`)
|
78 |
|
79 | If set to `true`, data fixtures will be append instead of deleting all data from the database first.
|
80 | **WARNING** `false` will erase your database
|
81 |
|
82 | - `autoLoad` (default value `false`)
|
83 |
|
84 |
|
85 | ### Features
|
86 |
|
87 | - Load data according your model
|
88 |
|
89 | - Multiple generators :
|
90 |
|
91 | ``` yaml
|
92 | User:
|
93 | user{1..45}:
|
94 | name: "User number {@}"
|
95 | ```
|
96 |
|
97 | `{@}` represents the current identifier for the generator
|
98 |
|
99 | - References :
|
100 |
|
101 | ``` yaml
|
102 | Group:
|
103 | group{1..3}:
|
104 | name: "Groupe number {@}"
|
105 |
|
106 | User:
|
107 | user{1..9}:
|
108 | name: "User number {@}"
|
109 | group: @group1 # Reference to group1
|
110 |
|
111 | user{10..19}:
|
112 | name: "User number {@}"
|
113 | group: @group.* # Reference to any matching group
|
114 | ```
|
115 |
|
116 | `@group1` represents the reference for the group1 and can be used in other fixtures
|
117 | `@group.*` represents the reference for a **random** matching group
|
118 |
|
119 | - References Array :
|
120 |
|
121 | ``` yaml
|
122 | User:
|
123 | user1:
|
124 | name: "Steve"
|
125 | user2:
|
126 | name: "Tom"
|
127 |
|
128 | Car:
|
129 | car1:
|
130 | name: "Porsche"
|
131 | car2:
|
132 | name: "Volvo"
|
133 |
|
134 | Ownership:
|
135 | steve:
|
136 | ownership: "[@user1, @car1]"
|
137 | tom:
|
138 | ownership: "[@user2, @car2]"
|
139 | ```
|
140 |
|
141 | - Fakers :
|
142 |
|
143 | ``` yaml
|
144 | User:
|
145 | user{1..10}:
|
146 | name: "User n°{@} : {{name.lastName}} {{name.firstName}}"
|
147 | email: "{{internet.email}}"
|
148 | ```
|
149 |
|
150 | You can use [Faker.js](https://github.com/marak/faker.js) API to provide fake data
|
151 |
|
152 | - Custom function :
|
153 |
|
154 | ``` yaml
|
155 | User:
|
156 | user{1..10}:
|
157 | favoriteNumber: "(function() { return Math.round(Math.random()*1000);})()"
|
158 | ```
|
159 |
|
160 | You can use custom functions too
|
161 |
|
162 |
|
163 |
|
164 | ## Credits
|
165 | - [Samy Ghribi](https://github.com/sghribi)
|
166 | - [Louie Bao](https://github.com/louie007)
|
167 |
|
168 | ## License
|
169 |
|
170 | ISC
|