forked from typicode/jsonplaceholder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seed.js
101 lines (86 loc) · 2.52 KB
/
seed.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Run this to generate data.json
var fs = require('fs')
var _ = require('underscore')
var Factory = require('rosie').Factory
var Faker = require('Faker')
var db = {}
// Credit http://www.paulirish.com/2009/random-hex-color-code-snippets/
function hex() {
return Math.floor(Math.random()*16777215).toString(16)
}
// Tables
db.posts = []
db.comments = []
db.albums = []
db.photos = []
db.users = []
db.todos = []
// Factories
Factory.define('post')
.sequence('id')
.attr('title', function() {return Faker.Lorem.sentence()})
.attr('body', function() {return Faker.Lorem.sentences(4)})
Factory.define('comment')
.sequence('id')
.attr('name', function() {return Faker.Lorem.sentence()})
.attr('email', function() {return Faker.Internet.email()})
.attr('body', function() {return Faker.Lorem.sentences(4)})
Factory.define('album')
.sequence('id')
.attr('title', function() {return Faker.Lorem.sentence()})
Factory.define('photo')
.sequence('id')
.attr('title', function() {return Faker.Lorem.sentence()})
.option('color', hex())
.attr('url', [ 'color' ], function(color) {
return 'http://placehold.it/600/' + color
})
.attr('thumbnailUrl', [ 'color' ], function(color) {
return 'http://placehold.it/150/' + color
})
Factory.define('todo')
.sequence('id')
.attr('title', function() {return Faker.Lorem.sentence()})
.attr('completed', function() { return _.random(1) ? true : false})
Factory.define('user')
.sequence('id')
.after(function(user) {
var card = Faker.Helpers.userCard()
_.each(card, function(value, key) {
user[key] = value
})
})
// Has many relationships
// Users
_(10).times(function () {
var user = Factory.build('user')
db.users.push(user)
// Posts
_(10).times(function() {
// userId not set in create so that it appears as the last
// attribute
var post = Factory.build('post', {userId: user.id})
db.posts.push(post)
// Comments
_(5).times(function () {
var comment = Factory.build('comment', {postId: post.id})
db.comments.push(comment)
})
})
// Albums
_(10).times(function() {
var album = Factory.build('album', {userId: user.id})
db.albums.push(album)
// Photos
_(50).times(function() {
var photo = Factory.build('photo', {albumId: album.id})
db.photos.push(photo)
})
})
// Todos
_(20).times(function() {
var todo = Factory.build('todo', {userId: user.id})
db.todos.push(todo)
})
})
fs.writeFileSync('db.json', JSON.stringify(db, null, 2));