UNPKG

7.09 kBPlain TextView Raw
1import { setSkipSync } from "./entities/user.entity";
2import UFS from "./ufs";
3import { LocalUFS } from "./ufs";
4
5export default interface Config {
6 disabledDb: boolean;
7 db: {
8 type: string;
9 host: string;
10 port: number;
11 username: string;
12 password: string;
13 database: string;
14 entities: string[];
15 synchronize: boolean;
16 logging: boolean;
17 };
18 disabledGraphQL: boolean;
19 publicFolders: string[];
20 viewFolders: string[];
21 translationFolders: string[];
22 middlewaresFolders: string[];
23 controllersFolders: string[];
24 migrationsFolders: string[];
25 disableMigrations: boolean;
26 sessionSecret: string;
27 sessionStore?: any;
28 tokenSecret: string;
29 mailer: {
30 sender: string;
31 host: string;
32 port: number;
33 secure: boolean;
34 auth: {
35 user: string;
36 pass: string;
37 }
38 };
39 defaultLanguage: string;
40 uploadPath: string;
41 cachePath: string;
42 jsonLimit?: string;
43 ufs: UFS;
44 onDatabaseInit: () => void;
45 chachingImages: boolean;
46}
47
48export class ConfigBuilder {
49 private config: Config;
50
51 public constructor(basePath: string) {
52 this.config = {
53 disabledDb: false,
54 disabledGraphQL: false,
55 db: {
56 type: "mysql",
57 host: "localhost",
58 port: 8889,
59 username: "root",
60 password: "root",
61 database: "koa_typescript2",
62 entities: [basePath + "/entities/*.entity.js"],
63 synchronize: true,
64 logging: false
65 },
66 publicFolders: [basePath + "/public"],
67 viewFolders: [basePath + "/views"],
68 translationFolders: [basePath + "/locale"],
69 middlewaresFolders: [basePath + "/middlewares"],
70 controllersFolders: [basePath + "/controllers"],
71 migrationsFolders: [basePath + "/migrations"],
72 disableMigrations: false,
73 sessionSecret: "session_secret",
74 sessionStore: null,
75 tokenSecret: "token_secret",
76 mailer: {
77 sender: 'Lynx Framework <lynx.framework@fakemail.com>',
78 host: '',
79 port: 587,
80 secure: false,
81 auth: {
82 user: '',
83 pass: ''
84 }
85 },
86 defaultLanguage: "it",
87 uploadPath: basePath + "/../uploads",
88 cachePath: basePath + "/../cache",
89 ufs: new LocalUFS(),
90 onDatabaseInit: () => {},
91 chachingImages: false
92 };
93 }
94
95 public setPublicFolders(folders: string[]): ConfigBuilder {
96 this.config.publicFolders = folders;
97 return this;
98 }
99
100 public setViewFolders(folders: string[]): ConfigBuilder {
101 this.config.viewFolders = folders;
102 return this;
103 }
104
105 public setTranslationFolders(folders: string[]): ConfigBuilder {
106 this.config.translationFolders = folders;
107 return this;
108 }
109
110 public setMiddlewaresFolders(folders: string[]): ConfigBuilder {
111 this.config.middlewaresFolders = folders;
112 return this;
113 }
114
115 public setControllersFolders(folders: string[]): ConfigBuilder {
116 this.config.controllersFolders = folders;
117 return this;
118 }
119
120 public setMigrationsFolders(folders: string[]): ConfigBuilder {
121 this.config.migrationsFolders = folders;
122 return this;
123 }
124
125 public setSessionSecret(secret: string): ConfigBuilder {
126 this.config.sessionSecret = secret;
127 return this;
128 }
129
130 public setTokenSecret(secret: string): ConfigBuilder {
131 this.config.tokenSecret = secret;
132 return this;
133 }
134
135 public setSessionStore(store: any): ConfigBuilder {
136 this.config.sessionStore = store;
137 return this;
138 }
139
140 public setUploadPath(path: string): ConfigBuilder {
141 this.config.uploadPath = path;
142 return this;
143 }
144
145 public setCachePath(path: string): ConfigBuilder {
146 this.config.cachePath = path;
147 return this;
148 }
149
150 public setDefaultLanguage(language: string): ConfigBuilder {
151 this.config.defaultLanguage = language;
152 return this;
153 }
154
155 public setEntitiesFolders(folders: string[]): ConfigBuilder {
156 this.config.db.entities = folders;
157 return this;
158 }
159
160 public setDatabaseType(type: string): ConfigBuilder {
161 this.config.disabledDb = false;
162 this.config.db.type = type;
163 return this;
164 }
165
166 public setDatabaseHost(host: string): ConfigBuilder {
167 this.config.disabledDb = false;
168 this.config.db.host = host;
169 return this;
170 }
171
172 public setDatabasePort(port: number): ConfigBuilder {
173 this.config.disabledDb = false;
174 this.config.db.port = port;
175 return this;
176 }
177
178 public setDatabaseLogin(username: string, password: string): ConfigBuilder {
179 this.config.disabledDb = false;
180 this.config.db.username = username;
181 this.config.db.password = password;
182 return this;
183 }
184
185 public setDatabase(database: string): ConfigBuilder {
186 this.config.disabledDb = false;
187 this.config.db.database = database;
188 return this;
189 }
190
191 public disableDB(): ConfigBuilder {
192 this.config.disabledDb = true;
193 return this;
194 }
195
196 public disableMigration(): ConfigBuilder {
197 this.config.disableMigrations = true;
198 return this;
199 }
200
201 public enableMigration(): ConfigBuilder {
202 this.config.disableMigrations = false;
203 return this;
204 }
205
206 public disableGraphQL(): ConfigBuilder {
207 this.config.disabledGraphQL = true;
208 return this;
209 }
210
211 public setMailerSender(address: string): ConfigBuilder {
212 this.config.mailer.sender = address;
213 return this;
214 }
215
216 public setMailerAuth(user: string, password: string): ConfigBuilder {
217 this.config.mailer.auth.user = user;
218 this.config.mailer.auth.pass = password;
219 return this;
220 }
221
222 public setMailerServer(host: string, port: number, secure: boolean): ConfigBuilder {
223 this.config.mailer.host = host;
224 this.config.mailer.port = port;
225 this.config.mailer.secure = secure;
226 return this;
227 }
228
229 public setCustomUserEntity(hasCustom: boolean): ConfigBuilder {
230 setSkipSync(!hasCustom);
231 return this;
232 }
233
234 public setJsonLimit(limit: string): ConfigBuilder {
235 this.config.jsonLimit = limit;
236 return this;
237 }
238
239 public setUFS(ufs: UFS): ConfigBuilder {
240 this.config.ufs = ufs;
241 return this;
242 }
243
244 public setOnDatabaseInit(cb: () => void): ConfigBuilder {
245 this.config.onDatabaseInit = cb;
246 return this;
247 }
248
249 public enableCachingImages(): ConfigBuilder {
250 this.config.chachingImages = true;
251 return this;
252 }
253
254 public build(): Config {
255 return this.config;
256 }
257}