Skip to content

Commit 6f99520

Browse files
committed
Add fake data when starting when INITDB = true
1 parent dfcbd4b commit 6f99520

7 files changed

Lines changed: 99 additions & 17 deletions

File tree

common/models/event.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
3+
module.exports = function (Event) {
4+
5+
Event.createFakeData = function (faker) {
6+
return Event.create({
7+
name: faker.lorem.sentence(),
8+
description: faker.lorem.paragraph(),
9+
startDate: faker.date.future(),
10+
endDate: faker.date.future(),
11+
image: faker.image.imageUrl()
12+
});
13+
}
14+
15+
};

common/models/event.json

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@
1010
"description": {
1111
"type": "string"
1212
},
13-
"start_time": {
14-
"type": "date"
13+
"startDate": {
14+
"type": "date",
15+
"required": true
1516
},
16-
"end_time": {
17+
"endDate": {
1718
"type": "date"
1819
},
19-
"timezone": {
20-
"type": "String"
21-
},
2220
"location": {
2321
"type": "String"
2422
},
@@ -27,14 +25,6 @@
2725
},
2826
"image": {
2927
"type": "String"
30-
},
31-
"tickets": {
32-
"type": "String"
33-
},
34-
"tags": {
35-
"type": [
36-
"string"
37-
]
3828
}
3929
},
4030
"validations": [],

common/models/note.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
module.exports = function (Note) {
4+
5+
Note.createFakeData = function (faker) {
6+
return Note.create({
7+
title: faker.lorem.sentence(),
8+
body: faker.lorem.paragraph()
9+
});
10+
}
11+
12+
};

common/models/page.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ var Showdown = require('showdown');
44

55
module.exports = function(Page) {
66

7+
Page.createFakeData = function (faker) {
8+
return Page.create({
9+
name: faker.lorem.sentence(),
10+
content: ' > ' + faker.lorem.paragraph()
11+
});
12+
};
13+
714
var converter = new Showdown.converter();
815

916
Page.html = function(id, cb) {

common/models/post.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
module.exports = function (Post) {
4+
5+
Post.createFakeData = function (faker) {
6+
return Post.create({
7+
title: faker.lorem.sentence(),
8+
content: faker.lorem.paragraph(),
9+
image: faker.image.imageUrl()
10+
});
11+
};
12+
13+
};

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"errorhandler": "1.3.3",
3535
"eventemitter3": "0.1.6",
3636
"express-session": "1.10.2",
37+
"faker": "3.0.1",
3738
"get-env": "0.4.0",
3839
"grunt": "0.4.5",
3940
"grunt-angular-gettext": "2.0.1",
@@ -63,7 +64,7 @@
6364
"grunt-svgmin": "2.0.0",
6465
"grunt-usemin": "3.0.0",
6566
"grunt-wiredep": "2.0.0",
66-
"gulp-nodemon": "^2.0.3",
67+
"gulp-nodemon": "2.0.3",
6768
"jshint": "2.6.0",
6869
"jshint-stylish": "1.0.0",
6970
"load-grunt-tasks": "3.1.0",
@@ -77,10 +78,10 @@
7778
"nodemon": "1.3.7",
7879
"passport": "0.2.1",
7980
"passport-facebook": "1.0.3",
80-
"passport-google-oauth": "~0.1.5",
81+
"passport-google-oauth": "0.1.5",
8182
"passport-local": "1.0.0",
8283
"passport-oauth2": "1.1.2",
83-
"passport-twitter": "~1.0.2",
84+
"passport-twitter": "1.0.2",
8485
"serve-favicon": "2.2.0",
8586
"showdown": "0.3.1",
8687
"time-grunt": "1.0.0"

server/boot/99-fake-data.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
'use strict';
2+
3+
var log = require('debug')('boot:99-fake.data');
4+
var faker = require('faker');
5+
var Promise = require('bluebird');
6+
7+
module.exports = function (app) {
8+
9+
if (app.dataSources.db.name !== 'Memory' && !process.env.INITDB) {
10+
return;
11+
}
12+
13+
var promises = [];
14+
15+
var structure = {
16+
Post: {
17+
count: 15
18+
},
19+
Event: {
20+
count: 15
21+
},
22+
Note: {
23+
count: 15
24+
},
25+
Page: {
26+
count: 15
27+
}
28+
};
29+
30+
for (var model in structure) {
31+
var options = structure[model];
32+
log('Creating %s items for model %s', options.count, model);
33+
for (var i = 0; i < options.count; i++) {
34+
promises.push(app.models[model].createFakeData(faker));
35+
}
36+
}
37+
38+
log('Creating fake data!');
39+
40+
Promise.all(promises).then(function () {
41+
log('Creating fake data done!');
42+
}).catch();
43+
44+
};

0 commit comments

Comments
 (0)