-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseed.js
executable file
·106 lines (97 loc) · 2.37 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
102
103
104
105
106
#!/usr/bin/env node
'use strict';
const mongoose = require('mongoose');
const async = require('async');
mongoose.connect(process.env.MONGO_URL || 'mongodb://localhost/penguin');
const IDs = [mongoose.Types.ObjectId(), mongoose.Types.ObjectId()];
const Seeds = {
Node: [
{
type: 'p',
published: true,
user: IDs[0],
date: new Date(),
title: 'About',
content: 'We are simple the best, and the most humble, there is...'
}, {
type: 'p',
published: false,
user: IDs[1],
date: new Date(),
title: 'History',
content: 'Since the big bang...'
}, {
type: 'a',
published: true,
user: IDs[0],
date: new Date(),
title: 'Welcome',
content: 'Welcome to our new home...'
}
],
User: [
{
_id: IDs[0],
username: 'Master',
email: 'master@example.org',
password: 'plaintextpass',
isAdmin: true,
data: {
alternateEmail: 'master2@example.org'
},
meta: {
slug: 'admin-master',
settings: {
hasLoggedOn: false,
lastLoginDate: Date.now()
},
deepMeta: {
deepSlug: 'deep-admin-master'
}
},
tags: ['admin', 'master', 'keyMaster']
}, {
_id: IDs[1],
username: 'Peon',
email: 'peon@example.org',
password: 'usebcrypttostorepasswords!',
isAdmin: false
}
]
};
const now = Date.now();
let c, i;
for (c = i = 1; i <= 100; c = ++i) {
Seeds.Node.push({
type: 'a',
user: Math.random() > 0.5 ? IDs[0] : IDs[1],
title: "Article " + c,
content: "Content for article " + c,
date: new Date(now + 1000 * c),
published: Math.random() > 0.5
});
}
const createSeeder = function(modelName) {
return function(done) {
var model;
model = mongoose.models[modelName];
return model.remove(function() {
return model.create(Seeds[modelName], function(err) {
console.log('Inserted', arguments.length - 1, modelName, 'documents for testing purposes.');
return done(err);
});
});
};
};
require('coffee-script/register');
const tasks = [];
for (const modelName in Seeds) {
require("./models/" + (modelName.toLowerCase()));
tasks.push(createSeeder(modelName));
}
async.parallel(tasks, function(err) {
if (err) {
console.log('Error', err);
}
return mongoose.disconnect();
});