5htp-core 0.2.9 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +7 -5
- package/src/client/app/component.tsx +2 -2
- package/src/client/assets/css/text/titres.less +4 -0
- package/src/client/components/Dialog/card.tsx +1 -1
- package/src/client/components/Dialog/index.less +3 -3
- package/src/client/components/inputv3/date/index.tsx +1 -1
- package/src/client/components/inputv3/index.tsx +14 -4
- package/src/client/pages/_layout/index.tsx +3 -3
- package/src/client/pages/_messages/400.tsx +2 -2
- package/src/client/pages/_messages/401.tsx +2 -2
- package/src/client/pages/_messages/403.tsx +2 -2
- package/src/client/pages/_messages/404.tsx +2 -2
- package/src/client/pages/_messages/500.tsx +2 -2
- package/src/client/pages/useHeader.tsx +2 -1
- package/src/client/services/router/components/router.tsx +1 -1
- package/src/client/services/router/index.tsx +5 -1
- package/src/client/services/router/response/index.tsx +5 -2
- package/src/common/data/objets.ts +0 -25
- package/src/common/router/index.ts +8 -12
- package/src/common/router/layouts.ts +3 -1
- package/src/common/router/register.ts +5 -3
- package/src/common/router/response/index.ts +3 -3
- package/src/server/app/commands.ts +2 -11
- package/src/server/app/{config.ts → container/config.ts} +4 -1
- package/src/server/app/container/index.ts +84 -0
- package/src/server/app/index.ts +73 -136
- package/src/server/app/instance.ts +3 -0
- package/src/server/app/service/container.ts +136 -0
- package/src/server/app/service/index.ts +219 -0
- package/src/server/index.ts +9 -3
- package/src/server/services/cache/index.ts +42 -16
- package/src/server/services/cache/service.json +6 -0
- package/src/server/services/console/index.ts +35 -22
- package/src/server/services/console/service.json +6 -0
- package/src/server/services/cron/index.ts +11 -11
- package/src/server/services/cron/service.json +6 -0
- package/src/server/services/database/connection.ts +26 -34
- package/src/server/services/database/index.ts +41 -21
- package/src/server/services/database/metas.ts +12 -7
- package/src/server/services/database/repository.ts +0 -9
- package/src/server/services/database/service.json +6 -0
- package/src/server/services/database/stats.ts +3 -3
- package/src/server/services/disks/driver.ts +9 -7
- package/src/server/services/disks/drivers/local/index.ts +199 -0
- package/src/server/services/disks/drivers/local/service.json +6 -0
- package/src/server/services/disks/drivers/s3/index.ts +282 -0
- package/src/server/services/disks/drivers/s3/service.json +6 -0
- package/src/server/services/disks/index.ts +47 -16
- package/src/server/services/disks/service.json +6 -0
- package/src/server/services/email/index.ts +16 -4
- package/src/server/services/email/service.json +6 -0
- package/src/server/services/fetch/index.ts +52 -8
- package/src/server/services/fetch/service.json +6 -0
- package/src/server/services/router/http/index.ts +12 -9
- package/src/server/services/router/index.ts +95 -68
- package/src/server/services/router/request/api.ts +1 -1
- package/src/server/services/router/response/index.ts +13 -19
- package/src/server/services/router/service.json +6 -0
- package/src/server/services/router/service.ts +16 -29
- package/src/server/services/schema/{router.ts → router/index.ts} +19 -7
- package/src/server/services/schema/router/service.json +6 -0
- package/src/server/services/schema/service.json +6 -0
- package/src/server/services/security/encrypt/{aes.ts → aes/index.ts} +16 -3
- package/src/server/services/security/encrypt/aes/service.json +6 -0
- package/src/server/services/socket/index.ts +39 -13
- package/src/server/services/socket/service.json +6 -0
- package/src/server/services/users/index.ts +16 -4
- package/src/server/services/users/old.ts +1 -1
- package/src/server/services/users/router/index.ts +21 -14
- package/src/server/services/users/router/service.json +6 -0
- package/src/server/services/users/service.json +6 -0
- package/src/types/aliases.d.ts +5 -0
- package/tsconfig.common.json +2 -0
- package/src/server/app/service.ts +0 -109
- /package/src/server/{patch.ts → app/container/patch.ts} +0 -0
|
@@ -7,10 +7,11 @@ import path from 'path';
|
|
|
7
7
|
|
|
8
8
|
// Npm
|
|
9
9
|
import hInterval from 'human-interval';
|
|
10
|
-
import fs from 'fs-extra';
|
|
11
10
|
|
|
12
11
|
// Core
|
|
13
|
-
import
|
|
12
|
+
import type { Application } from '@server/app';
|
|
13
|
+
import Service, { AnyService, TRegisteredService } from '@server/app/service';
|
|
14
|
+
import type { default as DisksManager, Driver } from '../disks';
|
|
14
15
|
|
|
15
16
|
// Specific
|
|
16
17
|
import registerCommands from './commands';
|
|
@@ -56,7 +57,8 @@ type TCacheGetOnlyArgs = [
|
|
|
56
57
|
----------------------------------*/
|
|
57
58
|
|
|
58
59
|
export type Config = {
|
|
59
|
-
debug: boolean
|
|
60
|
+
debug: boolean,
|
|
61
|
+
disk: string, // TODO: keyof disks
|
|
60
62
|
}
|
|
61
63
|
|
|
62
64
|
export type Hooks = {
|
|
@@ -69,34 +71,58 @@ export type Hooks = {
|
|
|
69
71
|
export default class Cache extends Service<Config, Hooks, Application> {
|
|
70
72
|
|
|
71
73
|
public commands = registerCommands(this);
|
|
72
|
-
|
|
73
|
-
private cacheDir = this.app.path.cache;
|
|
74
74
|
|
|
75
75
|
public data: {[key: string]: CacheEntry | undefined} = {};
|
|
76
|
-
|
|
77
|
-
public async register() {
|
|
78
76
|
|
|
79
|
-
|
|
77
|
+
private disk: Driver;
|
|
78
|
+
|
|
79
|
+
public constructor(
|
|
80
|
+
parent: AnyService,
|
|
81
|
+
config: Config,
|
|
82
|
+
services: {
|
|
83
|
+
disks: TRegisteredService< DisksManager >,
|
|
84
|
+
},
|
|
85
|
+
app: Application,
|
|
86
|
+
) {
|
|
80
87
|
|
|
88
|
+
super(parent, config, services, app);
|
|
89
|
+
|
|
90
|
+
this.disk = this.services.disks.get(config.disk)
|
|
81
91
|
}
|
|
82
92
|
|
|
83
|
-
|
|
93
|
+
/*----------------------------------
|
|
94
|
+
- LIFECYCLE
|
|
95
|
+
----------------------------------*/
|
|
96
|
+
|
|
97
|
+
protected async start() {
|
|
84
98
|
|
|
85
99
|
setInterval(() => this.cleanMem(), 10000);
|
|
86
100
|
|
|
87
101
|
// Restore persisted data
|
|
88
|
-
await this.restore();
|
|
102
|
+
//await this.restore();
|
|
89
103
|
}
|
|
90
104
|
|
|
91
|
-
|
|
92
|
-
|
|
105
|
+
public async ready() {
|
|
106
|
+
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
public async shutdown() {
|
|
110
|
+
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/*----------------------------------
|
|
114
|
+
- ACTIONS
|
|
115
|
+
----------------------------------*/
|
|
116
|
+
private async restore() {
|
|
117
|
+
const files = await this.disk.readDir('data', 'cache')
|
|
93
118
|
for (const file of files) {
|
|
94
119
|
|
|
95
|
-
if (!file.endsWith('.json'))
|
|
120
|
+
if (!file.name.endsWith('.json'))
|
|
96
121
|
continue;
|
|
97
122
|
|
|
98
|
-
const entryKey = file.substring(0, file.length - 5);
|
|
99
|
-
|
|
123
|
+
const entryKey = file.name.substring(0, file.name.length - 5);
|
|
124
|
+
const filePath = path.join('cache', file.name);
|
|
125
|
+
this.data[ entryKey ] = await this.disk.readJSON('data', filePath);
|
|
100
126
|
console.log(LogPrefix, `Restored cache entry ${entryKey}`);
|
|
101
127
|
}
|
|
102
128
|
}
|
|
@@ -229,7 +255,7 @@ export default class Cache extends Service<Config, Hooks, Application> {
|
|
|
229
255
|
}
|
|
230
256
|
};
|
|
231
257
|
|
|
232
|
-
public del( key
|
|
258
|
+
public del( key: string ): void {
|
|
233
259
|
|
|
234
260
|
if (key === undefined) {
|
|
235
261
|
this.data = {};
|
|
@@ -3,12 +3,13 @@
|
|
|
3
3
|
----------------------------------*/
|
|
4
4
|
// Npm
|
|
5
5
|
import { v4 as uuid } from 'uuid';
|
|
6
|
-
import
|
|
6
|
+
import log from 'ololog';
|
|
7
7
|
import { format as formatSql } from 'sql-formatter';
|
|
8
8
|
import highlight from 'cli-highlight';
|
|
9
9
|
|
|
10
10
|
// Core libs
|
|
11
|
-
import
|
|
11
|
+
import type { Application } from '@server/app';
|
|
12
|
+
import Service from '@server/app/service';
|
|
12
13
|
import context from '@server/context';
|
|
13
14
|
import type ServerRequest from '@server/services/router/request';
|
|
14
15
|
import { SqlError } from '@server/services/database/debug';
|
|
@@ -73,7 +74,7 @@ export type TRequestLogs = {
|
|
|
73
74
|
time: number
|
|
74
75
|
}
|
|
75
76
|
|
|
76
|
-
export type
|
|
77
|
+
export type TDbQueryLog = ChannelInfos & {
|
|
77
78
|
date: Date,
|
|
78
79
|
query: string,
|
|
79
80
|
time: number,
|
|
@@ -84,6 +85,7 @@ export type TLog = ILogObject & ChannelInfos
|
|
|
84
85
|
/*----------------------------------
|
|
85
86
|
- TYPES: BUG REPORT
|
|
86
87
|
----------------------------------*/
|
|
88
|
+
|
|
87
89
|
export type ServerBug = {
|
|
88
90
|
// Context
|
|
89
91
|
hash: string,
|
|
@@ -129,9 +131,6 @@ const logFields = [
|
|
|
129
131
|
----------------------------------*/
|
|
130
132
|
export default class Console extends Service<Config, Hooks, Application> {
|
|
131
133
|
|
|
132
|
-
// Load before all
|
|
133
|
-
public priority: TPriority = 2;
|
|
134
|
-
|
|
135
134
|
// Services
|
|
136
135
|
public logger!: Logger;
|
|
137
136
|
|
|
@@ -139,7 +138,7 @@ export default class Console extends Service<Config, Hooks, Application> {
|
|
|
139
138
|
public logs: TLog[] = [];
|
|
140
139
|
public clients: TGuestLogs[] = [];
|
|
141
140
|
public requests: TRequestLogs[] = [];
|
|
142
|
-
public sqlQueries:
|
|
141
|
+
public sqlQueries: TDbQueryLog[] = [];
|
|
143
142
|
// Bug ID => Timestamp latest send
|
|
144
143
|
private sentBugs: {[bugId: string]: number} = {};
|
|
145
144
|
|
|
@@ -150,17 +149,17 @@ export default class Console extends Service<Config, Hooks, Application> {
|
|
|
150
149
|
public error = console.error;
|
|
151
150
|
|
|
152
151
|
/*----------------------------------
|
|
153
|
-
-
|
|
152
|
+
- LIFECYCLE
|
|
154
153
|
----------------------------------*/
|
|
155
|
-
public async register() {
|
|
156
154
|
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
public async start() {
|
|
155
|
+
protected async start() {
|
|
160
156
|
|
|
161
157
|
const envConfig = this.config[ this.app.env.profile ];
|
|
162
158
|
|
|
163
|
-
|
|
159
|
+
/*const origConsole = console;
|
|
160
|
+
console.log = (...args: unknown[]) => log(...args)*/
|
|
161
|
+
|
|
162
|
+
/*this.logger = new Logger({
|
|
164
163
|
overwriteConsole: true,
|
|
165
164
|
//type: this.app.env.profile === 'dev' ? 'pretty' : 'hidden',
|
|
166
165
|
requestId: (): string => {
|
|
@@ -168,6 +167,7 @@ export default class Console extends Service<Config, Hooks, Application> {
|
|
|
168
167
|
return channelId === undefined ? channelType : channelType + ':' + channelId;
|
|
169
168
|
},
|
|
170
169
|
displayRequestId: false,
|
|
170
|
+
hideLogPositionForProduction: this.app.env.profile === 'prod',
|
|
171
171
|
prettyInspectOptions: {
|
|
172
172
|
depth: 2
|
|
173
173
|
}
|
|
@@ -181,7 +181,7 @@ export default class Console extends Service<Config, Hooks, Application> {
|
|
|
181
181
|
warn: this.logEntry.bind(this),
|
|
182
182
|
error: this.logEntry.bind(this),
|
|
183
183
|
fatal: this.logEntry.bind(this),
|
|
184
|
-
}, envConfig.level)
|
|
184
|
+
}, envConfig.level);*/
|
|
185
185
|
|
|
186
186
|
setInterval(() => this.clean(), 10000);
|
|
187
187
|
|
|
@@ -189,6 +189,18 @@ export default class Console extends Service<Config, Hooks, Application> {
|
|
|
189
189
|
this.app.on('error', this.createBugReport.bind(this));
|
|
190
190
|
}
|
|
191
191
|
|
|
192
|
+
public async ready() {
|
|
193
|
+
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
public async shutdown() {
|
|
197
|
+
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/*----------------------------------
|
|
201
|
+
- ACTIONS
|
|
202
|
+
----------------------------------*/
|
|
203
|
+
|
|
192
204
|
private clean() {
|
|
193
205
|
/*this.config.debug && console.log(LogPrefix, `Clean logs buffer. Current size:`, this.logs.length, '/', this.config.bufferLimit);
|
|
194
206
|
const bufferOverflow = this.logs.length - this.config.bufferLimit;
|
|
@@ -196,10 +208,6 @@ export default class Console extends Service<Config, Hooks, Application> {
|
|
|
196
208
|
this.logs = this.logs.slice(bufferOverflow);*/
|
|
197
209
|
}
|
|
198
210
|
|
|
199
|
-
/*----------------------------------
|
|
200
|
-
- LOGGING
|
|
201
|
-
----------------------------------*/
|
|
202
|
-
|
|
203
211
|
public async createBugReport( error: Error, request?: ServerRequest ) {
|
|
204
212
|
|
|
205
213
|
// Print the error so it's accessible via logs
|
|
@@ -277,11 +285,11 @@ export default class Console extends Service<Config, Hooks, Application> {
|
|
|
277
285
|
this.logs.push(miniLog as TLog);
|
|
278
286
|
}
|
|
279
287
|
|
|
280
|
-
public client(client: TGuestLogs) {
|
|
288
|
+
public client( client: TGuestLogs ) {
|
|
281
289
|
this.clients.push(client);
|
|
282
290
|
}
|
|
283
291
|
|
|
284
|
-
public request(request: TRequestLogs) {
|
|
292
|
+
public request( request: TRequestLogs ) {
|
|
285
293
|
|
|
286
294
|
if (request.id === 'admin')
|
|
287
295
|
return;
|
|
@@ -289,6 +297,11 @@ export default class Console extends Service<Config, Hooks, Application> {
|
|
|
289
297
|
this.requests.push( request );
|
|
290
298
|
}
|
|
291
299
|
|
|
300
|
+
public database( dbQuery: TDbQueryLog ) {
|
|
301
|
+
|
|
302
|
+
this.requests.push( dbQuery );
|
|
303
|
+
}
|
|
304
|
+
|
|
292
305
|
/*----------------------------------
|
|
293
306
|
- READ
|
|
294
307
|
----------------------------------*/
|
|
@@ -333,7 +346,7 @@ export default class Console extends Service<Config, Hooks, Application> {
|
|
|
333
346
|
|
|
334
347
|
public getQueries( channelType: ChannelInfos["channelType"], channelId?: string ) {
|
|
335
348
|
|
|
336
|
-
const filters: Partial<
|
|
349
|
+
const filters: Partial<TDbQueryLog> = { channelType };
|
|
337
350
|
if (channelId !== undefined)
|
|
338
351
|
filters.channelId = channelId;
|
|
339
352
|
|
|
@@ -347,7 +360,7 @@ export default class Console extends Service<Config, Hooks, Application> {
|
|
|
347
360
|
|
|
348
361
|
public async getLogs( channelType: ChannelInfos["channelType"], channelId?: string ) {
|
|
349
362
|
|
|
350
|
-
const filters: Partial<
|
|
363
|
+
const filters: Partial<TDbQueryLog> = { channelType };
|
|
351
364
|
if (channelId !== undefined)
|
|
352
365
|
filters.channelId = channelId;
|
|
353
366
|
|
|
@@ -3,7 +3,8 @@
|
|
|
3
3
|
----------------------------------*/
|
|
4
4
|
|
|
5
5
|
// Core
|
|
6
|
-
import
|
|
6
|
+
import type { Application } from '@server/app';
|
|
7
|
+
import Service from '@server/app/service';
|
|
7
8
|
import { NotFound } from '@common/errors';
|
|
8
9
|
import context from '@server/context';
|
|
9
10
|
|
|
@@ -37,14 +38,10 @@ export default class CronManager extends Service<Config, Hooks, Application> {
|
|
|
37
38
|
public static timer: NodeJS.Timeout;
|
|
38
39
|
|
|
39
40
|
/*----------------------------------
|
|
40
|
-
-
|
|
41
|
+
- LIFECICLE
|
|
41
42
|
----------------------------------*/
|
|
42
43
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
public async start() {
|
|
44
|
+
protected async start() {
|
|
48
45
|
|
|
49
46
|
this.app.on('cleanup', () => this.cleanup());
|
|
50
47
|
|
|
@@ -56,10 +53,13 @@ export default class CronManager extends Service<Config, Hooks, Application> {
|
|
|
56
53
|
|
|
57
54
|
}, 10000);
|
|
58
55
|
}
|
|
59
|
-
|
|
60
|
-
public async
|
|
61
|
-
|
|
62
|
-
|
|
56
|
+
|
|
57
|
+
public async ready() {
|
|
58
|
+
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
public async shutdown() {
|
|
62
|
+
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
/*----------------------------------
|
|
@@ -6,13 +6,11 @@
|
|
|
6
6
|
import mysql from 'mysql2/promise';
|
|
7
7
|
|
|
8
8
|
// Core: general
|
|
9
|
-
import Application from '@server/app';
|
|
10
|
-
import Service from '@server/app/service';
|
|
11
9
|
import { Anomaly } from '@common/errors';
|
|
12
10
|
|
|
13
11
|
// Core: specific
|
|
12
|
+
import type SQL from '.';
|
|
14
13
|
import { SqlError } from './debug';
|
|
15
|
-
import type Console from '../console';
|
|
16
14
|
import MetadataParser, { TDatabasesList, TMetasTable, TColumnTypes, TMetasColonne } from './metas';
|
|
17
15
|
import { TMySQLTypeName, mysqlToJs, js as jsTypes } from './datatypes';
|
|
18
16
|
import Bucket from './bucket';
|
|
@@ -38,7 +36,8 @@ type ConnectionConfig = {
|
|
|
38
36
|
|
|
39
37
|
export type DatabaseServiceConfig = {
|
|
40
38
|
debug: boolean,
|
|
41
|
-
connections: ConnectionConfig[]
|
|
39
|
+
connections: ConnectionConfig[],
|
|
40
|
+
connectionsLimit: number
|
|
42
41
|
}
|
|
43
42
|
|
|
44
43
|
export type THooks = {
|
|
@@ -70,7 +69,7 @@ type TSelectQueryResult = any;
|
|
|
70
69
|
/*----------------------------------
|
|
71
70
|
- SERVICES
|
|
72
71
|
----------------------------------*/
|
|
73
|
-
export default class DatabaseManager
|
|
72
|
+
export default class DatabaseManager {
|
|
74
73
|
|
|
75
74
|
private initialized = false;
|
|
76
75
|
public connection!: mysql.Pool;
|
|
@@ -79,13 +78,19 @@ export default class DatabaseManager extends Service<DatabaseServiceConfig, THoo
|
|
|
79
78
|
public tables: TDatabasesList = {};
|
|
80
79
|
public metas = new MetadataParser(this);
|
|
81
80
|
|
|
81
|
+
public status: 'disconnected' | 'connected' = 'disconnected'
|
|
82
|
+
|
|
83
|
+
public constructor(
|
|
84
|
+
protected sql: SQL,
|
|
85
|
+
protected config: DatabaseServiceConfig,
|
|
86
|
+
public app = sql.app
|
|
87
|
+
) {
|
|
88
|
+
|
|
89
|
+
}
|
|
90
|
+
|
|
82
91
|
/*----------------------------------
|
|
83
92
|
- HOOKS
|
|
84
93
|
----------------------------------*/
|
|
85
|
-
|
|
86
|
-
public async register() {
|
|
87
|
-
|
|
88
|
-
}
|
|
89
94
|
|
|
90
95
|
public loading: Promise<void> | undefined = undefined;
|
|
91
96
|
public async start() {
|
|
@@ -99,13 +104,13 @@ export default class DatabaseManager extends Service<DatabaseServiceConfig, THoo
|
|
|
99
104
|
await this.connect(connectionConfig)
|
|
100
105
|
break;
|
|
101
106
|
} catch (error) {
|
|
102
|
-
console.warn(LogPrefix, `Failed to connect to ${connectionConfig.name}: ` + error);
|
|
107
|
+
this.config.debug && console.warn(LogPrefix, `Failed to connect to ${connectionConfig.name}: ` + error);
|
|
103
108
|
connectionErrors.push(connectionConfig.name + ': ' + error);
|
|
104
109
|
}
|
|
105
110
|
}
|
|
106
111
|
|
|
107
112
|
// Coudnt connect to any database
|
|
108
|
-
if (this.
|
|
113
|
+
if (this.status !== 'connected')
|
|
109
114
|
throw new Anomaly(`Couldnt connect to any database.`, { connectionErrors });
|
|
110
115
|
|
|
111
116
|
// Disconnect from the database when the app is terminated
|
|
@@ -124,7 +129,7 @@ export default class DatabaseManager extends Service<DatabaseServiceConfig, THoo
|
|
|
124
129
|
----------------------------------*/
|
|
125
130
|
public async connect(config: ConnectionConfig) {
|
|
126
131
|
|
|
127
|
-
console.info(LogPrefix, `Trying to connect to ${config.name} ...`);
|
|
132
|
+
this.config.debug && console.info(LogPrefix, `Trying to connect to ${config.name} ...`);
|
|
128
133
|
this.connection = mysql.createPool({
|
|
129
134
|
|
|
130
135
|
// Identification
|
|
@@ -136,7 +141,7 @@ export default class DatabaseManager extends Service<DatabaseServiceConfig, THoo
|
|
|
136
141
|
|
|
137
142
|
// Pool
|
|
138
143
|
waitForConnections: true,
|
|
139
|
-
connectionLimit:
|
|
144
|
+
connectionLimit: this.config.connectionsLimit,
|
|
140
145
|
queueLimit: 0,
|
|
141
146
|
|
|
142
147
|
// Dates & timezone
|
|
@@ -159,10 +164,10 @@ export default class DatabaseManager extends Service<DatabaseServiceConfig, THoo
|
|
|
159
164
|
}
|
|
160
165
|
})
|
|
161
166
|
|
|
162
|
-
this.connectionConfig = config;
|
|
163
|
-
|
|
164
167
|
this.tables = await this.metas.load( config.databases );
|
|
165
|
-
|
|
168
|
+
this.connectionConfig = config; // Memorise configuration if connection succeed
|
|
169
|
+
this.status = 'connected';
|
|
170
|
+
this.config.debug && console.info(LogPrefix, `Successfully connected to ${config.name}.`);
|
|
166
171
|
}
|
|
167
172
|
|
|
168
173
|
private typeCast( field: mysql.Field, next: Function ) {
|
|
@@ -331,8 +336,11 @@ export default class DatabaseManager extends Service<DatabaseServiceConfig, THoo
|
|
|
331
336
|
const startTime = Date.now();
|
|
332
337
|
return this.connection.query(query).then(([rows, fields]) => {
|
|
333
338
|
|
|
334
|
-
|
|
335
|
-
|
|
339
|
+
this.sql.runHook('afterQuery', {
|
|
340
|
+
date: new Date(),
|
|
341
|
+
query: query.trim(),
|
|
342
|
+
time: Date.now() - startTime,
|
|
343
|
+
});
|
|
336
344
|
|
|
337
345
|
return rows as unknown as TResult;
|
|
338
346
|
|
|
@@ -342,20 +350,4 @@ export default class DatabaseManager extends Service<DatabaseServiceConfig, THoo
|
|
|
342
350
|
|
|
343
351
|
})
|
|
344
352
|
}
|
|
345
|
-
|
|
346
|
-
private log( query: string, startTime: number ) {
|
|
347
|
-
|
|
348
|
-
const console = this.app.use<Console>('console');
|
|
349
|
-
if (!console) return;
|
|
350
|
-
|
|
351
|
-
const { channelType, channelId } = console.getChannel();
|
|
352
|
-
if (channelId !== 'admin')
|
|
353
|
-
console.sqlQueries.push({
|
|
354
|
-
channelType,
|
|
355
|
-
channelId,
|
|
356
|
-
date: new Date(),
|
|
357
|
-
query: query.trim(),
|
|
358
|
-
time: Date.now() - startTime,
|
|
359
|
-
});
|
|
360
|
-
}
|
|
361
353
|
}
|
|
@@ -9,9 +9,8 @@ import dottie from 'dottie';
|
|
|
9
9
|
const safeStringify = require('fast-safe-stringify'); // remplace les références circulairs par un [Circular]
|
|
10
10
|
|
|
11
11
|
// Core: general
|
|
12
|
-
import
|
|
13
|
-
import Service from '@server/app/service';
|
|
14
|
-
import { callableInstance } from '@common/data/objets';
|
|
12
|
+
import { Application, Services } from '@server/app';
|
|
13
|
+
import Service, { AnyService, TRegisteredServicesIndex } from '@server/app/service';
|
|
15
14
|
import { NotFound } from '@common/errors';
|
|
16
15
|
|
|
17
16
|
// Services
|
|
@@ -24,9 +23,7 @@ import type { TMetasTable } from './metas';
|
|
|
24
23
|
|
|
25
24
|
export { default as Repository } from './repository';
|
|
26
25
|
|
|
27
|
-
export type Config =
|
|
28
|
-
|
|
29
|
-
}
|
|
26
|
+
export type Config = DatabaseServiceConfig
|
|
30
27
|
|
|
31
28
|
export type Hooks = {
|
|
32
29
|
|
|
@@ -69,9 +66,6 @@ type TColsToUpsert<TData extends TObjetDonnees> = (
|
|
|
69
66
|
|
|
70
67
|
const LogPrefix = '[database]'
|
|
71
68
|
|
|
72
|
-
const equalities = (data: TObjetDonnees, keys = Object.keys(data)) =>
|
|
73
|
-
keys.map(k => '' + k + ' = ' + mysql.escape( data[k] ))
|
|
74
|
-
|
|
75
69
|
/*----------------------------------
|
|
76
70
|
- CORE
|
|
77
71
|
----------------------------------*/
|
|
@@ -82,23 +76,46 @@ export default class SQL extends Service<Config, Hooks, Application> {
|
|
|
82
76
|
|
|
83
77
|
public database: Database;
|
|
84
78
|
|
|
85
|
-
public constructor(
|
|
86
|
-
|
|
87
|
-
|
|
79
|
+
public constructor(
|
|
80
|
+
parent: AnyService,
|
|
81
|
+
config: Config,
|
|
82
|
+
drivers: TRegisteredServicesIndex,
|
|
83
|
+
app: Application,
|
|
84
|
+
) {
|
|
88
85
|
|
|
89
|
-
|
|
86
|
+
super(parent, config, drivers, app);
|
|
87
|
+
|
|
88
|
+
this.database = new Database(this, config);
|
|
90
89
|
}
|
|
91
90
|
|
|
92
|
-
public
|
|
93
|
-
|
|
91
|
+
public static createInstance(
|
|
92
|
+
parent: AnyService,
|
|
93
|
+
config: Config,
|
|
94
|
+
services: TRegisteredServicesIndex,
|
|
95
|
+
app: Application
|
|
96
|
+
) {
|
|
97
|
+
return Services.callableInstance(
|
|
98
|
+
new SQL(parent, config, services, app),
|
|
99
|
+
'sql'
|
|
100
|
+
)
|
|
94
101
|
}
|
|
95
102
|
|
|
103
|
+
/*----------------------------------
|
|
104
|
+
- LIFECYCLE
|
|
105
|
+
----------------------------------*/
|
|
106
|
+
|
|
96
107
|
public async start() {
|
|
108
|
+
|
|
97
109
|
await this.database.start();
|
|
110
|
+
|
|
98
111
|
}
|
|
99
112
|
|
|
100
|
-
public
|
|
101
|
-
|
|
113
|
+
public async ready() {
|
|
114
|
+
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
public async shutdown() {
|
|
118
|
+
|
|
102
119
|
}
|
|
103
120
|
|
|
104
121
|
/*----------------------------------
|
|
@@ -191,7 +208,7 @@ export default class SQL extends Service<Config, Hooks, Application> {
|
|
|
191
208
|
|
|
192
209
|
const keyword = prefix === '&' ? ' AND ' : ', '
|
|
193
210
|
|
|
194
|
-
value = Object.keys(value).length === 0 ? '1' : equalities(value).join( keyword );
|
|
211
|
+
value = Object.keys(value).length === 0 ? '1' : this.equalities(value).join( keyword );
|
|
195
212
|
|
|
196
213
|
// String: `SET :${column} = ${data}` => `SET balance = 10`
|
|
197
214
|
} else {
|
|
@@ -214,6 +231,9 @@ export default class SQL extends Service<Config, Hooks, Application> {
|
|
|
214
231
|
}).join(' ').trim();
|
|
215
232
|
}
|
|
216
233
|
|
|
234
|
+
public equalities = (data: TObjetDonnees, keys = Object.keys(data)) =>
|
|
235
|
+
keys.map(k => '' + k + ' = ' + mysql.escape( data[k] ))
|
|
236
|
+
|
|
217
237
|
/*----------------------------------
|
|
218
238
|
- OPERATIONS: LOW LEVELf
|
|
219
239
|
----------------------------------*/
|
|
@@ -336,8 +356,8 @@ export default class SQL extends Service<Config, Hooks, Application> {
|
|
|
336
356
|
}
|
|
337
357
|
|
|
338
358
|
// Create equalities
|
|
339
|
-
const egalitesData = equalities(data).join(', ')
|
|
340
|
-
const egalitesWhere = equalities(where).join(' AND ')
|
|
359
|
+
const egalitesData = this.equalities(data).join(', ')
|
|
360
|
+
const egalitesWhere = this.equalities(where).join(' AND ')
|
|
341
361
|
|
|
342
362
|
// Build query
|
|
343
363
|
return this.database.query(`
|
|
@@ -509,7 +529,7 @@ export default class SQL extends Service<Config, Hooks, Application> {
|
|
|
509
529
|
|
|
510
530
|
const whereSql = typeof where === 'function' && where['string'] !== undefined
|
|
511
531
|
? where['string']
|
|
512
|
-
: equalities(where).join(' AND ');
|
|
532
|
+
: this.equalities(where).join(' AND ');
|
|
513
533
|
|
|
514
534
|
return this.database.query(`DELETE FROM ${table} WHERE ${whereSql};`, opts);
|
|
515
535
|
}
|
|
@@ -4,10 +4,11 @@
|
|
|
4
4
|
|
|
5
5
|
// Npm
|
|
6
6
|
import fs from 'fs-extra';
|
|
7
|
+
import path from 'path';
|
|
7
8
|
import type mysql from 'mysql2/promise';
|
|
8
9
|
|
|
9
10
|
// Core
|
|
10
|
-
import
|
|
11
|
+
import Container from '@server/app/container';
|
|
11
12
|
import { ucfirst } from '@common/data/chaines';
|
|
12
13
|
|
|
13
14
|
// Database
|
|
@@ -120,13 +121,14 @@ export default class MySQLMetasParser {
|
|
|
120
121
|
public constructor(
|
|
121
122
|
private database: DatabaseConnection,
|
|
122
123
|
public app = database.app,
|
|
124
|
+
public debug = database.config.debug
|
|
123
125
|
) {
|
|
124
126
|
|
|
125
127
|
}
|
|
126
128
|
|
|
127
129
|
public async load( toLoad: string[] ) {
|
|
128
130
|
|
|
129
|
-
console.info(`${toLoad.length} databases to load`);
|
|
131
|
+
this.debug && console.info(`${toLoad.length} databases to load`);
|
|
130
132
|
if (toLoad.length === 0)
|
|
131
133
|
return {};
|
|
132
134
|
|
|
@@ -139,12 +141,12 @@ export default class MySQLMetasParser {
|
|
|
139
141
|
if (this.app.env.profile === 'dev')
|
|
140
142
|
this.genTypesDef(metas);
|
|
141
143
|
|
|
142
|
-
|
|
144
|
+
this.debug && console.log(`Databases are loaded.`);
|
|
143
145
|
return metas;
|
|
144
146
|
}
|
|
145
147
|
|
|
146
148
|
private async query( databases: string[] ) {
|
|
147
|
-
console.log(`Loading tables metadatas for the following databases: ${databases.join(', ')} ...`);
|
|
149
|
+
this.debug && console.log(`Loading tables metadatas for the following databases: ${databases.join(', ')} ...`);
|
|
148
150
|
return (await this.database.connection.query(`
|
|
149
151
|
SELECT
|
|
150
152
|
|
|
@@ -170,7 +172,7 @@ export default class MySQLMetasParser {
|
|
|
170
172
|
|
|
171
173
|
private importTables( dbColumns: TDatabaseColumn[] ): TDatabasesList {
|
|
172
174
|
|
|
173
|
-
console.log(`Processing ${dbColumns.length} rows of metadatas`);
|
|
175
|
+
this.debug && console.log(`Processing ${dbColumns.length} rows of metadatas`);
|
|
174
176
|
|
|
175
177
|
const tablesIndex: TDatabasesList = {};
|
|
176
178
|
|
|
@@ -366,8 +368,11 @@ export default class MySQLMetasParser {
|
|
|
366
368
|
}
|
|
367
369
|
}
|
|
368
370
|
|
|
369
|
-
fs.outputFileSync(
|
|
370
|
-
|
|
371
|
+
fs.outputFileSync(
|
|
372
|
+
path.join( Container.path.server.generated, 'models.ts'),
|
|
373
|
+
types.join('\n')
|
|
374
|
+
);
|
|
375
|
+
this.debug && console.log(LogPrefix, `Wrote database types to ${modelsTypesPath}`);
|
|
371
376
|
|
|
372
377
|
}
|
|
373
378
|
}
|
|
@@ -14,13 +14,4 @@ import type Database from '.';
|
|
|
14
14
|
----------------------------------*/
|
|
15
15
|
export default class QueriesRepository {
|
|
16
16
|
|
|
17
|
-
public constructor(
|
|
18
|
-
protected database: Database
|
|
19
|
-
) {
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
protected sql( strings: TemplateStringsArray, ...data: any[] ) {
|
|
24
|
-
return this.database.sql(strings, ...data);
|
|
25
|
-
}
|
|
26
17
|
}
|