-
Notifications
You must be signed in to change notification settings - Fork 2
/
DataModel.js
73 lines (65 loc) · 2.43 KB
/
DataModel.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
// postShuffle -- web forum software for node.js
// Copyright (c) 2012 Mooneer Salem
var AppConfig = require('./AppConfig');
var Sequelize = require('sequelize');
var db = new Sequelize(AppConfig.Database.name, AppConfig.Database.username, AppConfig.Database.password, {
'dialect': AppConfig.Database.type,
'storage': AppConfig.Database.name + ".db"
});
// Tables.
exports.Users = db.define('User', {
'username': Sequelize.STRING,
'password': Sequelize.STRING,
'is_moderator': Sequelize.BOOLEAN,
'is_admin': Sequelize.BOOLEAN,
'title': Sequelize.STRING,
'confirmation_code': Sequelize.STRING,
'email': Sequelize.STRING
});
exports.Posts = db.define('Post', {
'title': Sequelize.STRING,
'body': Sequelize.TEXT
});
exports.Tags = db.define('Tag', {
'tag': Sequelize.STRING
});
exports.Comments = db.define('Comment', {
'body': Sequelize.TEXT
});
// Associations.
exports.Users.hasMany(exports.Comments);
exports.Users.hasMany(exports.Posts);
exports.Posts.hasMany(exports.Tags);
exports.Posts.hasMany(exports.Comments);
exports.Posts.belongsTo(exports.Users);
//exports.Tags.hasMany(exports.Posts);
exports.Comments.hasOne(exports.Comments, {'as': 'Parent'});
exports.Comments.belongsTo(exports.Users);
var Passwords = require(__dirname + "/Utility/Passwords.js");
var generator = require(__dirname + "/Utility/RandomGenerators.js");
db.sync().then(function() {
exports.Users.findAll({limit: 1}).then(function(result) {
if (!result || result.length === 0)
{
// first run.
var newpw = generator.randomString();
exports.Users.create({
username: "admin",
password: Passwords.hash(newpw),
is_moderator: true,
is_admin: true,
title: AppConfig.defaultTitle,
email: AppConfig.admin_email
}).then(function(u) {
console.log("============= FIRST RUN =============");
console.log("Your admin user is as follows:");
console.log(" Username: admin");
console.log(" Password: " + newpw);
console.log("KEEP THIS INFO IN A SAFE PLACE OR YOU WILL BE UNABLE TO ADMINISTER YOUR FORUM.");
console.log("============= /FIRST RUN =============");
}).catch(function(err) {
console.log("Cannot access database: " + err);
});
}
});
});