velocious 1.0.91 → 1.0.93
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 +1 -1
- package/src/cli/commands/server.js +1 -24
- package/src/database/drivers/mysql/index.js +89 -11
- package/src/environment-handlers/browser.js +4 -0
- package/src/environment-handlers/node/cli/commands/server.js +30 -0
- package/src/environment-handlers/node.js +5 -0
- package/src/database/drivers/mysql/connect-connection.js +0 -12
package/package.json
CHANGED
|
@@ -1,30 +1,7 @@
|
|
|
1
|
-
import Application from "../../application.js"
|
|
2
1
|
import BaseCommand from "../base-command.js"
|
|
3
2
|
|
|
4
3
|
export default class VelociousCliCommandsServer extends BaseCommand{
|
|
5
4
|
async execute() {
|
|
6
|
-
|
|
7
|
-
this.newConfiguration = Object.assign({}, this.databasePool.getConfiguration())
|
|
8
|
-
this.databaseConnection = await this.databasePool.spawnConnectionWithConfiguration(this.newConfiguration)
|
|
9
|
-
|
|
10
|
-
await this.databaseConnection.connect()
|
|
11
|
-
|
|
12
|
-
const {parsedProcessArgs} = this.args
|
|
13
|
-
const host = parsedProcessArgs.h || parsedProcessArgs.host || "127.0.0.1"
|
|
14
|
-
const port = parsedProcessArgs.p || parsedProcessArgs.port || 3006
|
|
15
|
-
const application = new Application({
|
|
16
|
-
configuration: this.getConfiguration(),
|
|
17
|
-
httpServer: {
|
|
18
|
-
host,
|
|
19
|
-
port
|
|
20
|
-
},
|
|
21
|
-
type: "server"
|
|
22
|
-
})
|
|
23
|
-
const environment = this.getConfiguration().getEnvironment()
|
|
24
|
-
|
|
25
|
-
await application.initialize()
|
|
26
|
-
await application.startHttpServer()
|
|
27
|
-
console.log(`Started Velocious HTTP server on ${host}:${port} in ${environment} environment`)
|
|
28
|
-
await application.wait()
|
|
5
|
+
return await this.getConfiguration().getEnvironmentHandler().cliCommandsServer(this)
|
|
29
6
|
}
|
|
30
7
|
}
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import AlterTable from "./sql/alter-table.js"
|
|
2
2
|
import Base from "../base.js"
|
|
3
|
-
import connectConnection from "./connect-connection.js"
|
|
4
3
|
import CreateDatabase from "./sql/create-database.js"
|
|
5
4
|
import CreateIndex from "./sql/create-index.js"
|
|
6
5
|
import CreateTable from "./sql/create-table.js"
|
|
@@ -16,18 +15,29 @@ import Table from "./table.js"
|
|
|
16
15
|
import Update from "./sql/update.js"
|
|
17
16
|
|
|
18
17
|
export default class VelociousDatabaseDriversMysql extends Base{
|
|
18
|
+
/**
|
|
19
|
+
* @returns {Promise<void>}
|
|
20
|
+
*/
|
|
19
21
|
async connect() {
|
|
20
|
-
|
|
22
|
+
this.pool = mysql.createPool(Object.assign({connectionLimit: 1}, this.connectArgs()))
|
|
23
|
+
this.pool.on("error", this.onPoolError)
|
|
24
|
+
}
|
|
21
25
|
|
|
22
|
-
|
|
23
|
-
|
|
26
|
+
onPoolError = (error) => {
|
|
27
|
+
console.error("Velocious / MySQL driver / Pool error", error)
|
|
24
28
|
}
|
|
25
29
|
|
|
30
|
+
/**
|
|
31
|
+
* @returns {Promise<void>}
|
|
32
|
+
*/
|
|
26
33
|
async close() {
|
|
27
|
-
await this.
|
|
28
|
-
this.
|
|
34
|
+
await this.pool.end()
|
|
35
|
+
this.pool = undefined
|
|
29
36
|
}
|
|
30
37
|
|
|
38
|
+
/**
|
|
39
|
+
* @returns {Record<string, any>}
|
|
40
|
+
*/
|
|
31
41
|
connectArgs() {
|
|
32
42
|
const args = this.getArgs()
|
|
33
43
|
const connectArgs = []
|
|
@@ -42,6 +52,9 @@ export default class VelociousDatabaseDriversMysql extends Base{
|
|
|
42
52
|
return connectArgs
|
|
43
53
|
}
|
|
44
54
|
|
|
55
|
+
/**
|
|
56
|
+
* @returns {Promise<string[]>}
|
|
57
|
+
*/
|
|
45
58
|
async alterTableSql(tableData) {
|
|
46
59
|
const alterArgs = {tableData, driver: this}
|
|
47
60
|
const alterTable = new AlterTable(alterArgs)
|
|
@@ -49,6 +62,9 @@ export default class VelociousDatabaseDriversMysql extends Base{
|
|
|
49
62
|
return await alterTable.toSqls()
|
|
50
63
|
}
|
|
51
64
|
|
|
65
|
+
/**
|
|
66
|
+
* @returns {Promise<string[]>}
|
|
67
|
+
*/
|
|
52
68
|
createDatabaseSql(databaseName, args) {
|
|
53
69
|
const createArgs = Object.assign({databaseName, driver: this}, args)
|
|
54
70
|
const createDatabase = new CreateDatabase(createArgs)
|
|
@@ -56,6 +72,9 @@ export default class VelociousDatabaseDriversMysql extends Base{
|
|
|
56
72
|
return createDatabase.toSql()
|
|
57
73
|
}
|
|
58
74
|
|
|
75
|
+
/**
|
|
76
|
+
* @returns {string}
|
|
77
|
+
*/
|
|
59
78
|
createIndexSql(indexData) {
|
|
60
79
|
const createArgs = Object.assign({driver: this}, indexData)
|
|
61
80
|
const createIndex = new CreateIndex(createArgs)
|
|
@@ -63,6 +82,9 @@ export default class VelociousDatabaseDriversMysql extends Base{
|
|
|
63
82
|
return createIndex.toSql()
|
|
64
83
|
}
|
|
65
84
|
|
|
85
|
+
/**
|
|
86
|
+
* @returns {string[]}
|
|
87
|
+
*/
|
|
66
88
|
createTableSql(tableData) {
|
|
67
89
|
const createArgs = {tableData, driver: this}
|
|
68
90
|
const createTable = new CreateTable(createArgs)
|
|
@@ -70,20 +92,32 @@ export default class VelociousDatabaseDriversMysql extends Base{
|
|
|
70
92
|
return createTable.toSql()
|
|
71
93
|
}
|
|
72
94
|
|
|
95
|
+
/**
|
|
96
|
+
* @returns {Promise<string>}
|
|
97
|
+
*/
|
|
73
98
|
async currentDatabase() {
|
|
74
99
|
const rows = await this.query("SELECT DATABASE() AS db_name")
|
|
75
100
|
|
|
76
101
|
return digg(rows, 0, "db_name")
|
|
77
102
|
}
|
|
78
103
|
|
|
104
|
+
/**
|
|
105
|
+
* @returns {Promise<void>}
|
|
106
|
+
*/
|
|
79
107
|
async disableForeignKeys() {
|
|
80
108
|
await this.query("SET FOREIGN_KEY_CHECKS = 0")
|
|
81
109
|
}
|
|
82
110
|
|
|
111
|
+
/**
|
|
112
|
+
* @returns {Promise<void>}
|
|
113
|
+
*/
|
|
83
114
|
async enableForeignKeys() {
|
|
84
115
|
await this.query("SET FOREIGN_KEY_CHECKS = 1")
|
|
85
116
|
}
|
|
86
117
|
|
|
118
|
+
/**
|
|
119
|
+
* @returns {string[]}
|
|
120
|
+
*/
|
|
87
121
|
dropTableSql(tableName, args = {}) {
|
|
88
122
|
const dropArgs = Object.assign({tableName, driver: this}, args)
|
|
89
123
|
const dropTable = new DropTable(dropArgs)
|
|
@@ -91,41 +125,70 @@ export default class VelociousDatabaseDriversMysql extends Base{
|
|
|
91
125
|
return dropTable.toSql()
|
|
92
126
|
}
|
|
93
127
|
|
|
128
|
+
/**
|
|
129
|
+
* @returns {string}
|
|
130
|
+
*/
|
|
94
131
|
getType() { return "mysql" }
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* @returns {string}
|
|
135
|
+
*/
|
|
95
136
|
primaryKeyType() { return "bigint" }
|
|
96
137
|
|
|
138
|
+
/**
|
|
139
|
+
* @returns {Array<Record<string, any>>}
|
|
140
|
+
*/
|
|
97
141
|
async _queryActual(sql) {
|
|
98
142
|
try {
|
|
99
|
-
return await query(this.
|
|
143
|
+
return await query(this.pool, sql)
|
|
100
144
|
} catch (error) {
|
|
101
145
|
// Re-throw to un-corrupt stacktrace
|
|
102
146
|
throw new Error(error.message)
|
|
103
147
|
}
|
|
104
148
|
}
|
|
105
149
|
|
|
150
|
+
/**
|
|
151
|
+
* @returns {string}
|
|
152
|
+
*/
|
|
106
153
|
queryToSql(query) { return new QueryParser({query}).toSql() }
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* @returns {boolean}
|
|
157
|
+
*/
|
|
107
158
|
shouldSetAutoIncrementWhenPrimaryKey() { return true }
|
|
108
159
|
|
|
160
|
+
/**
|
|
161
|
+
* @returns {string}
|
|
162
|
+
*/
|
|
109
163
|
escape(value) {
|
|
110
|
-
if (!this.
|
|
164
|
+
if (!this.pool) throw new Error("Can't escape before connected")
|
|
111
165
|
|
|
112
|
-
const escapedValueWithQuotes = this.
|
|
166
|
+
const escapedValueWithQuotes = this.pool.escape(this._convertValue(value))
|
|
113
167
|
|
|
114
168
|
return escapedValueWithQuotes.slice(1, escapedValueWithQuotes.length - 1)
|
|
115
169
|
}
|
|
116
170
|
|
|
171
|
+
/**
|
|
172
|
+
* @returns {string}
|
|
173
|
+
*/
|
|
117
174
|
quote(value) {
|
|
118
|
-
if (!this.
|
|
175
|
+
if (!this.pool) throw new Error("Can't escape before connected")
|
|
119
176
|
|
|
120
|
-
return this.
|
|
177
|
+
return this.pool.escape(this._convertValue(value))
|
|
121
178
|
}
|
|
122
179
|
|
|
180
|
+
/**
|
|
181
|
+
* @returns {string}
|
|
182
|
+
*/
|
|
123
183
|
deleteSql({tableName, conditions}) {
|
|
124
184
|
const deleteInstruction = new Delete({conditions, driver: this, tableName})
|
|
125
185
|
|
|
126
186
|
return deleteInstruction.toSql()
|
|
127
187
|
}
|
|
128
188
|
|
|
189
|
+
/**
|
|
190
|
+
* @returns {string}
|
|
191
|
+
*/
|
|
129
192
|
insertSql(args) {
|
|
130
193
|
const insertArgs = Object.assign({driver: this}, args)
|
|
131
194
|
const insert = new Insert(insertArgs)
|
|
@@ -133,6 +196,9 @@ export default class VelociousDatabaseDriversMysql extends Base{
|
|
|
133
196
|
return insert.toSql()
|
|
134
197
|
}
|
|
135
198
|
|
|
199
|
+
/**
|
|
200
|
+
* @returns {Array<Table>}
|
|
201
|
+
*/
|
|
136
202
|
async getTables() {
|
|
137
203
|
const result = await this.query("SHOW FULL TABLES")
|
|
138
204
|
const tables = []
|
|
@@ -146,22 +212,34 @@ export default class VelociousDatabaseDriversMysql extends Base{
|
|
|
146
212
|
return tables
|
|
147
213
|
}
|
|
148
214
|
|
|
215
|
+
/**
|
|
216
|
+
* @returns {number}
|
|
217
|
+
*/
|
|
149
218
|
async lastInsertID() {
|
|
150
219
|
const result = await this.query("SELECT LAST_INSERT_ID() AS last_insert_id")
|
|
151
220
|
|
|
152
221
|
return digg(result, 0, "last_insert_id")
|
|
153
222
|
}
|
|
154
223
|
|
|
224
|
+
/**
|
|
225
|
+
* @returns {Options}
|
|
226
|
+
*/
|
|
155
227
|
options() {
|
|
156
228
|
if (!this._options) this._options = new Options({driver: this})
|
|
157
229
|
|
|
158
230
|
return this._options
|
|
159
231
|
}
|
|
160
232
|
|
|
233
|
+
/**
|
|
234
|
+
* @returns {void}
|
|
235
|
+
*/
|
|
161
236
|
async _startTransactionAction() {
|
|
162
237
|
await this.query("START TRANSACTION")
|
|
163
238
|
}
|
|
164
239
|
|
|
240
|
+
/**
|
|
241
|
+
* @returns {string}
|
|
242
|
+
*/
|
|
165
243
|
updateSql({conditions, data, tableName}) {
|
|
166
244
|
const update = new Update({conditions, data, driver: this, tableName})
|
|
167
245
|
|
|
@@ -31,6 +31,10 @@ export default class VelociousEnvironmentsHandlerBrowser extends Base {
|
|
|
31
31
|
throw new Error("Unsupported on browser")
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
+
async cliCommandsServer(_command) { // eslint-disable-line no-unused-vars
|
|
35
|
+
throw new Error("Unsupported on browser")
|
|
36
|
+
}
|
|
37
|
+
|
|
34
38
|
async cliCommandsTest(_command) { // eslint-disable-line no-unused-vars
|
|
35
39
|
throw new Error("Unsupported on browser")
|
|
36
40
|
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import Application from "../../../../application.js"
|
|
2
|
+
import BaseCommand from "../../../../cli/base-command.js"
|
|
3
|
+
|
|
4
|
+
export default class VelociousCliCommandsServer extends BaseCommand{
|
|
5
|
+
async execute() {
|
|
6
|
+
this.databasePool = this.getConfiguration().getDatabasePool()
|
|
7
|
+
this.newConfiguration = Object.assign({}, this.databasePool.getConfiguration())
|
|
8
|
+
this.databaseConnection = await this.databasePool.spawnConnectionWithConfiguration(this.newConfiguration)
|
|
9
|
+
|
|
10
|
+
await this.databaseConnection.connect()
|
|
11
|
+
|
|
12
|
+
const {parsedProcessArgs} = this.args
|
|
13
|
+
const host = parsedProcessArgs.h || parsedProcessArgs.host || "127.0.0.1"
|
|
14
|
+
const port = parsedProcessArgs.p || parsedProcessArgs.port || 3006
|
|
15
|
+
const application = new Application({
|
|
16
|
+
configuration: this.getConfiguration(),
|
|
17
|
+
httpServer: {
|
|
18
|
+
host,
|
|
19
|
+
port
|
|
20
|
+
},
|
|
21
|
+
type: "server"
|
|
22
|
+
})
|
|
23
|
+
const environment = this.getConfiguration().getEnvironment()
|
|
24
|
+
|
|
25
|
+
await application.initialize()
|
|
26
|
+
await application.startHttpServer()
|
|
27
|
+
console.log(`Started Velocious HTTP server on ${host}:${port} in ${environment} environment`)
|
|
28
|
+
await application.wait()
|
|
29
|
+
}
|
|
30
|
+
}
|
|
@@ -3,6 +3,7 @@ import CliCommandsDestroyMigration from "./node/cli/commands/destroy/migration.j
|
|
|
3
3
|
import CliCommandsInit from "./node/cli/commands/init.js"
|
|
4
4
|
import CliCommandsGenerateMigration from "./node/cli/commands/generate/migration.js"
|
|
5
5
|
import CliCommandsGenerateModel from "./node/cli/commands/generate/model.js"
|
|
6
|
+
import CliCommandsServer from "./node/cli/commands/server.js"
|
|
6
7
|
import CliCommandsTest from "./node/cli/commands/test.js"
|
|
7
8
|
import {dirname} from "path"
|
|
8
9
|
import {fileURLToPath} from "url"
|
|
@@ -63,6 +64,10 @@ export default class VelociousEnvironmentHandlerNode extends Base{
|
|
|
63
64
|
return await this.forwardCommand(command, CliCommandsGenerateModel)
|
|
64
65
|
}
|
|
65
66
|
|
|
67
|
+
async cliCommandsServer(command) {
|
|
68
|
+
return await this.forwardCommand(command, CliCommandsServer)
|
|
69
|
+
}
|
|
70
|
+
|
|
66
71
|
async cliCommandsTest(command) {
|
|
67
72
|
return await this.forwardCommand(command, CliCommandsTest)
|
|
68
73
|
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
// Async function to connect a MySQL connection
|
|
2
|
-
export default function connectConnection(connection) {
|
|
3
|
-
return new Promise((resolve, reject) => {
|
|
4
|
-
connection.connect((error) => {
|
|
5
|
-
if (error) {
|
|
6
|
-
reject(error)
|
|
7
|
-
} else {
|
|
8
|
-
resolve()
|
|
9
|
-
}
|
|
10
|
-
})
|
|
11
|
-
})
|
|
12
|
-
}
|