OSDN Git Service

sequelizeに移行した
[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         var fs = require("fs");
44         async.waterfall([
45                 function(next){
46                         var Sequelize = require("sequelize");
47                         var pool = new Sequelize(config.db_name, config.db_user, config.db_password,{
48                                                 host:config.db_host,
49                                                 port:config.db_port
50                                         });
51
52                         var query = GetCreateQuery(Sequelize,config.alias,"profilelist"); 
53                         var profilelist = pool.define("profilelist",query);
54                         module.exports.GetProfileColletion = profilelist;
55
56                         var def = {
57                                 ip:{
58                                         type : "text",
59                                         length : 64,
60                                         primary : true,
61                                 },
62                                 type:{
63                                         type : "text",
64                                         length : 1,
65                                         primary : true,
66                                 },
67                         };
68                         var query = GetCreateQuery(Sequelize,def,"ipbanlist"); 
69                         ipbanlist = pool.define("ipbanlist",query);
70                         module.exports.GetIpBanColletion = ipbanlist;
71
72                         var def = {
73                                 number:
74                                         {
75                                                 type : "unsignednumber",
76                                                 length:2,
77                                                 isnotempty : true,
78                                                 primary : true,
79                                         },
80                                 password:{
81                                         type : "text",
82                                         length : 16,
83                                 },
84                                 hiddenlog:{
85                                         type : "bool",
86                                 },
87                         };
88                         var query = GetCreateQuery(Sequelize,def,"rooms"); 
89                         rooms = pool.define("rooms",query);
90                         module.exports.GetRoomInfomation = rooms;
91
92                         fs.exists("inited",function(exists){
93                                 next(null,exists);
94                         });
95                 },
96                 function(exists,next){
97                         if(exists)
98                                 next(null);
99                         else
100                                 fs.open("inited","a",function(err,fd){
101                                         fs.closeSync(fd);
102
103                                         module.exports.GetProfileColletion.drop();
104                                         module.exports.GetProfileColletion.sync();
105
106                                         module.exports.GetIpBanColletion.drop();
107                                         module.exports.GetIpBanColletion.sync();
108
109                                         module.exports.GetRoomInfomation.drop();
110                                         module.exports.GetRoomInfomation.sync();
111
112                                         next(null);
113                                 });
114                 }
115         ],function(err){
116                 callback(err);
117         });
118 }
119
120 function GetCreateQuery(Sequelize,def,tablename)
121 {
122         var result = {};
123         for(key in def)
124         {
125                 if(typeof(def[key].nodefinetable) != "undefined" && def[key].nodefinetable)
126                         continue;
127                 option = {};
128                 switch(def[key].type)
129                 {
130                         case "text":
131                                 option["type"] = Sequelize.STRING(def[key].length);
132                                 break;
133                         case "number":
134                                 option["type"] = Sequelize.INTEGER(def[key].length);
135                                 break;
136                         case "unsignednumber":
137                                 option["type"] = Sequelize.INTEGER(def[key].length).UNSIGNED;
138                                 break;
139                         case "mail":
140                                 option["type"] = Sequelize.STRING(def[key].length);
141                                 break;
142                         case "password":
143                                 option["type"] = Sequelize.STRING(def[key].length);
144                                 break;
145                         case "textarea":
146                                 option["type"] = Sequelize.TEXT;
147                                 break;
148                         case "bool":
149                                 option["type"] = Sequelize.BOOLEAN;
150                                 break
151                         case "datetime":
152                                 option["type"] = Sequelize.DATE;
153                                 break;
154                         default:
155                                 throw util.format("invaild %s type:%s",key,def[key].type);
156                 }
157                 if(typeof(def[key].isnotempty) != "undefined" && def[key].isnotempty)
158                         option["allowNull"] = true;
159                 else if(typeof(def[key].primary) != "undefined" && def[key].primary)
160                         option["primaryKey"] = true;
161                 result[key] = option;
162         }
163
164         return result;
165 }