OSDN Git Service

name_hashは被る可能性があるので、primaryではなくindexを指定するようにした
[webchat/WebChat.git] / init.js
1 var util = require("util");
2 var config = require("./configure.js");
3
4 var profilelist,ipbanlist,rooms;
5
6 module.exports.GetProfileColletion = {};
7 module.exports.GetIpBanColletion = {};
8 module.exports.GetRoomInfomation = {};
9 module.exports = function(callback){
10         var async = require("async");
11                 
12         if("name_hash" in config.alias)
13         {
14                 if(config.alias["name_hash"].type != "unsignednumber")
15                         throw "name_hashの型はunsignednumberでなければなりません";
16         }else{
17                 throw "name_hashが存在しません";
18         }
19         
20         if("name" in config.alias)
21         {
22                 if(config.alias["name"].type != "text")
23                         throw "nameの型はtextでなければなりません";
24         }else{
25                 throw "nameが存在しません";
26         }
27         
28         if("password" in config.alias)
29         {
30                 if(config.alias["password"].type != "password")
31                         throw "nameの型はpasswordでなければなりません";
32         }else{
33                 throw "passwordが存在しません";
34         }
35         
36         if("lastmodified" in config.alias)
37         {
38                 if(config.alias["lastmodified"].type != "datetime")
39                         throw "lastmodifiedの型はtextでなければなりません";
40         }else{
41                 throw "lastmodifiedが存在しません";
42         }
43
44         var Sequelize = require("sequelize");
45         var pool = new Sequelize(config.db_name, config.db_user, config.db_password,{
46                                 host:config.db_host,
47                                 port:config.db_port
48                         });
49
50         var fs = require("fs");
51
52         async.waterfall([
53                 function(next){
54                         var query = GetCreateQuery(Sequelize,config.alias,"profilelist"); 
55                         var profilelist = pool.define("profilelist",query);
56                         module.exports.GetProfileColletion = profilelist;
57
58                         var def = {
59                                 ip:{
60                                         type : "text",
61                                         length : 64,
62                                         primary : true,
63                                 },
64                                 type:{
65                                         type : "text",
66                                         length : 1,
67                                 },
68                         };
69                         var query = GetCreateQuery(Sequelize,def,"ipbanlist"); 
70                         ipbanlist = pool.define("ipbanlist",query);
71                         module.exports.GetIpBanColletion = ipbanlist;
72
73                         var def = {
74                                 number:
75                                         {
76                                                 type : "unsignednumber",
77                                                 length:2,
78                                                 isnotempty : true,
79                                                 primary : true,
80                                         },
81                                 password:{
82                                         type : "text",
83                                         length : 16,
84                                 },
85                                 hiddenlog:{
86                                         type : "bool",
87                                 },
88                         };
89                         var query = GetCreateQuery(Sequelize,def,"rooms"); 
90                         rooms = pool.define("rooms",query);
91                         module.exports.GetRoomInfomation = rooms;
92
93                         fs.exists("inited",function(exists){
94                                 next(null,exists);
95                         });
96                 },
97                 function(exists,next){
98                         if(exists)
99                                 next(true);
100                         else
101                                 fs.open("inited","a",function(err,fd){
102                                         fs.closeSync(fd);
103                                         next(null);
104                                 });
105                 },
106                 function(next){
107                         module.exports.GetProfileColletion.sync({force:true}).done(next);
108                 },
109                 function(result,next){
110                         pool.getQueryInterface().addIndex(
111                                 module.exports.GetProfileColletion.tableName,
112                                 ["name_hash"]
113                         ).done(next);
114                 },
115                 function(result,next){
116                         module.exports.GetIpBanColletion.sync({force:true}).done(next);
117                 },
118                 function(result,next){
119                         module.exports.GetRoomInfomation.sync({force:true}).done(next);
120                 }
121         ],function(err){
122                 if(err)
123                         callback(null);
124                 else
125                         callback(err);
126         });
127 }
128
129 function GetCreateQuery(Sequelize,def,tablename)
130 {
131         var result = {};
132         for(key in def)
133         {
134                 if(typeof(def[key].nodefinetable) != "undefined" && def[key].nodefinetable)
135                         continue;
136                 option = {};
137                 switch(def[key].type)
138                 {
139                         case "text":
140                                 option["type"] = Sequelize.STRING(def[key].length);
141                                 break;
142                         case "number":
143                                 option["type"] = Sequelize.INTEGER(def[key].length);
144                                 break;
145                         case "unsignednumber":
146                                 option["type"] = Sequelize.INTEGER(def[key].length).UNSIGNED;
147                                 break;
148                         case "mail":
149                                 option["type"] = Sequelize.STRING(def[key].length);
150                                 break;
151                         case "password":
152                                 option["type"] = Sequelize.STRING(def[key].length);
153                                 break;
154                         case "textarea":
155                                 option["type"] = Sequelize.TEXT;
156                                 break;
157                         case "bool":
158                                 option["type"] = Sequelize.BOOLEAN;
159                                 break
160                         case "datetime":
161                                 option["type"] = Sequelize.DATE;
162                                 break;
163                         default:
164                                 throw util.format("invaild %s type:%s",key,def[key].type);
165                 }
166                 if(typeof(def[key].isnotempty) != "undefined" && def[key].isnotempty)
167                         option["allowNull"] = true;
168                 if(typeof(def[key].primary) != "undefined" && def[key].primary)
169                         option["primaryKey"] = true;
170                 result[key] = option;
171         }
172
173         return result;
174 }