-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.js
167 lines (155 loc) · 3.54 KB
/
model.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
var Store = require("ringo-sqlstore").Store;
var log = require('ringo/logging').getLogger(module.id);
var config = require('./config');
/**
* Create mysql store
*/
var store = exports.store = new Store({
"url": "jdbc:mysql://localhost/" + config.store.database,
"driver": "com.mysql.jdbc.Driver",
"username": config.store.username,
"password": config.store.password
}, {
maxConnections: 100,
cacheSize: 100
});
/**
* Page entity definition
*/
var Page = exports.Page = store.defineEntity('Page', {
properties: {
title: 'string',
path: 'string',
text: 'text',
created: 'timestamp',
updated: 'timestamp',
template: 'string',
comments: {
type: 'collection',
entity: 'Comment',
foreignProperty: 'page',
orderBy: 'created desc'
}
}
});
/**
* Serialize page into json-able object
* @returns {Object}
*/
Page.prototype.serialize = function() {
var comments = [];
// NOTE no map() on collections...
for each (var c in this.comments) {
comments.push(c.serialize());
};
return {
title: this.title,
text: this.text,
created: this.created.getTime(),
updated: this.updated.getTime(),
path: this.path,
comments: comments
};
};
/**
* Like serialize() but with minimal set of properties
* @see serialize()
* @returns {Object}
*/
Page.prototype.serializeMini = function() {
return {
title: this.title,
path: this.path,
created: this.created.getTime()
};
};
/**
* @returns {Object} page matching path or null
*/
Page.getByPath = function(path) {
var pages = Page.query().equals('path', path).limit(1).select();
return pages.length ? pages[0] : null;
};
/**
* Updates a page if it exists.
* @returns {Boolean} true if page was updated, false otherweise
*/
Page.update = function(data) {
var page = Page.getByPath(data.path);
if (page) {
for (var key in data) {
page[key] = data[key];
}
page.save();
log.info('updated page @', data.path);
return true;
}
return false;
};
/**
* Create a page it it does not exist.
* @returns {Boolean} true if page was created, false otherwise
*/
Page.create = function(data) {
var page = Page.getByPath(data.path);
if (!page) {
data.created = data.updated;
page = new Page(data);
page.save();
log.info('created page @', data.path);
return true;
}
return false;
};
/**
* Comment database entity
*/
var Comment = exports.Comment = store.defineEntity('Comment', {
properties: {
author: 'string',
text: 'text',
created: 'timestamp',
page: {
type: 'object',
entity: 'Page',
}
}
});
/**
* Serialize into json-able object
* @returns {Object}
*/
Comment.prototype.serialize = function() {
return {
_id: this._id,
text: this.text,
author: this.author,
created: this.created.getTime()
}
};
/**
* Removes comments with given id
* @param {Array} deleteIds
*/
Comment.bulkRemove = function(deleteIds) {
if (!deleteIds || !deleteIds.length) return false;
deleteIds.forEach(function(did) {
var c = Comment.query().equals('id', did).select()[0];
log.info('removing comment _id ', did);
c.remove();
});
return true;
};
/**
* Create a comment
* @static
*/
Comment.create = function(data) {
if (data.page && data.text) {
var comment = new Comment(data);
comment.save();
log.info('created comment @', data.page.path);
return true;
}
return false;
};