· 8 years ago · Jan 22, 2018, 09:14 PM
1var Store = require('ringo-sqlstore').Store;
2var strings = require("ringo/utils/strings");
3var auth= require("./userAuth");
4
5var store = new Store({
6 "url": "jdbc:mysql://127.0.0.1/chromatic",
7 "driver": "com.mysql.jdbc.Driver",
8 "username": "chromatic",
9 "password": "chromatic"
10});
11
12export("User", "Usergroup", "Photo", "File", "Collection", "Lightbox");
13
14var User = store.defineEntity("User", {
15 "table": "t_user",
16 "properties": {
17 "username": {
18 "type": "string",
19 "length": 500
20 },
21 "password": {
22 "type": "string",
23 "length": 128
24 },
25 "salt": {
26 "type": "string",
27 "length": 128
28 },
29 "company": {
30 "type": "string",
31 "length": 500
32 },
33 "title": {
34 "type": "string",
35 "length": 100
36 },
37 "firstName":{
38 "type": "string",
39 "length": 500
40 },
41 "lastName": {
42 "type": "string",
43 "length": 500
44 },
45 "country": {
46 "type": "string",
47 "length": 500
48 },
49 "zipCode": {
50 "type": "string",
51 "length": 100
52 },
53 "city": {
54 "type": "string",
55 "length": 500
56 },
57 "street": {
58 "type": "string",
59 "length": 500
60 },
61 "phone": {
62 "type": "string",
63 "length": 100
64 },
65 "blocked": "boolean",
66 "admin": "boolean",
67 "usergroups": {
68 "type": "collection",
69 "entity": "Usergroup",
70 "through": "RelUserUsergroup", // the entity to join with
71 "join": "RelUserUsergroup.user == User.id", // the join predicate
72 "foreignProperty": "RelUserUsergroup.usergroup" // the column containing the foreign key
73 },
74 "lightboxes": {
75 "type": "collection",
76 "entity": "Lightbox",
77 "foreignProperty": "owner"
78 }
79 }
80});
81
82User.create = function(username, password, firstName, lastName, company, title, country, zipCode, city, street, phone, blocked, admin) {
83 var salt = strings.random(10) + ((new Date().getTime() % 3571) * 691) + strings.random(5) + (Math.random() * 3209).toFixed(0);
84
85 var user = new User();
86 user.username = username;
87 user.password = auth.encryptPassword(password, salt);
88 user.salt = salt;
89
90 // Optional data
91 user.firstName = firstName || "";
92 user.lastName = lastName || "";
93 user.company = company || "";
94 user.title = title || "";
95 user.country = country || "";
96 user.zipCode = zipCode || "";
97 user.city = city || "";
98 user.street = street || "";
99 user.phone = phone || "";
100
101 user.blocked = blocked || false;
102 user.admin = admin || false;
103
104 user.save();
105};
106
107var Usergroup = store.defineEntity("Usergroup", {
108 "table": "t_usergroup",
109 "properties": {
110 "name": {
111 "type": "string",
112 "length": 500
113 },
114 "users": {
115 "type": "collection",
116 "entity": "User",
117 "through": "RelUserUsergroup", // the entity to join with
118 "join": "RelUserUsergroup.usergroup == Usergroup.id", // the join predicate
119 "foreignProperty": "RelUserUsergroup.user" // the column containing the foreign key
120 }
121 }
122});
123
124store.defineEntity("RelUserUsergroup", {
125 "table": "t_user_usergroup",
126 "properties": {
127 "user": {
128 "type": "object",
129 "entity": "User",
130 "column": "rel_user"
131 },
132 "usergroup": {
133 "type": "object",
134 "entity": "Usergroup",
135 "column": "rel_usergroup"
136 }
137 }
138});
139
140var Photo = store.defineEntity("Photo", {
141 "table": "t_photo",
142 "properties": {
143 "reference": {
144 "type": "string",
145 "length": 50
146 },
147 "photographer": {
148 "type": "string",
149 "length": 500
150 },
151 "copyright": {
152 "type": "string",
153 "length": 500
154 },
155 "headline": {
156 "type": "string",
157 "length": 500
158 },
159 "caption": {
160 "type": "text"
161 },
162 "keywords": {
163 "type": "text"
164 },
165 "uploadTime": {
166 "type": "date"
167 },
168 "takenOn": {
169 "type": "date"
170 },
171 "sublocation": {
172 "type": "string",
173 "length": 500
174 },
175 "city": {
176 "type": "string",
177 "length": 500
178 },
179 "province": {
180 "type": "string",
181 "length": 500
182 },
183 "countryCode": {
184 "type": "string",
185 "length": 3
186 },
187 "specialInstructions": {
188 "type": "text"
189 },
190 "files": {
191 "type": "collection",
192 "entity": "Files",
193 "foreignProperty": "photo"
194 },
195 "creator": {
196 "type": "object",
197 "entity": "User",
198 "column": "photo_f_user"
199 }
200 }
201});
202
203var File = store.defineEntity("File", {
204 "table": "t_file",
205 "properties": {
206 "path": {
207 "type": "string",
208 "length": 2000
209 },
210 "width": {
211 "type": "integer"
212 }
213 ,
214 "height": {
215 "type": "integer"
216 },
217 "photo": {
218 "type": "object",
219 "entity": "Photo",
220 "column": "file_f_photo"
221 }
222 }
223});
224
225var Collection = store.defineEntity("Collection", {
226 "table": "t_collection",
227 "properties": {
228 "name": {
229 "type": "string",
230 "length": 500
231 },
232 "photos": {
233 "type": "collection",
234 "entity": "Photo",
235 "through": "RelCollectionPhoto", // the entity to join with
236 "join": "RelCollectionPhoto.collection == Collection.id", // the join predicate
237 "foreignProperty": "RelCollectionPhoto.photo" // the column containing the foreign key
238 },
239 "usergroups": {
240 "type": "collection",
241 "entity": "Usergroup",
242 "through": "RelCollectionUsergroup", // the entity to join with
243 "join": "RelCollectionUsergroup.collection == Collection.id", // the join predicate
244 "foreignProperty": "RelCollectionUsergroup.usergroup" // the column containing the foreign key
245 }
246 }
247});
248
249store.defineEntity("RelCollectionPhoto", {
250 "table": "t_collection_photo",
251 "properties": {
252 "collection": {
253 "type": "object",
254 "entity": "Collection",
255 "column": "rel_collection"
256 },
257 "photo": {
258 "type": "object",
259 "entity": "Photo",
260 "column": "rel_photo"
261 }
262 }
263});
264
265store.defineEntity("RelCollectionUsergroup", {
266 "table": "t_collection_usergroup",
267 "properties": {
268 "collection": {
269 "type": "object",
270 "entity": "Collection",
271 "column": "rel_collection"
272 },
273 "usergroup": {
274 "type": "object",
275 "entity": "Usergroup",
276 "column": "rel_usergroup"
277 }
278 }
279});
280
281var Lightbox = store.defineEntity("Lightbox", {
282 "table": "t_lightbox",
283 "properties": {
284 "name": {
285 "type": "string",
286 "length": 500
287 },
288 "secretKey": {
289 "type": "string",
290 "length": 500
291 },
292 "owner": {
293 "type": "object",
294 "entity": "User",
295 "column": "lightbox_f_user"
296 },
297 "photos": {
298 "type": "collection",
299 "entity": "Photo",
300 "through": "RelLightboxPhoto", // the entity to join with
301 "join": "RelLightboxPhoto.lightbox == Lightbox.id", // the join predicate
302 "foreignProperty": "RelLightboxPhoto.photo" // the column containing the foreign key
303 }
304 }
305});
306
307store.defineEntity("RelLightboxPhoto", {
308 "table": "t_lightbox_photo",
309 "properties": {
310 "lightbox": {
311 "type": "object",
312 "entity": "Lightbox",
313 "column": "rel_lightbox"
314 },
315 "photo": {
316 "type": "object",
317 "entity": "Photo",
318 "column": "rel_photo"
319 }
320 }
321});