velocious 1.0.0 → 1.0.2
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/README.md +26 -0
- package/bin/velocious.mjs +8 -0
- package/index.mjs +17 -0
- package/package.json +13 -6
- package/peak_flow.yml +9 -5
- package/spec/cli/generate/migration-spec.mjs +9 -0
- package/spec/database/connection/drivers/mysql/{query-parser-spec.cjs → query-parser-spec.mjs} +12 -8
- package/spec/database/drivers/mysql/connection-spec.mjs +21 -0
- package/spec/database/record/create-spec.mjs +14 -0
- package/spec/database/record/destroy-spec.mjs +17 -0
- package/spec/database/record/find-spec.mjs +29 -0
- package/spec/database/record/update-spec.mjs +15 -0
- package/spec/dummy/index.mjs +69 -0
- package/spec/dummy/src/config/database.example.mjs +15 -0
- package/spec/dummy/src/config/database.peakflow.mjs +15 -0
- package/spec/dummy/src/{routes.cjs → config/routes.mjs} +3 -2
- package/spec/dummy/src/database/migrations/001-create-tasks.mjs +12 -0
- package/spec/dummy/src/models/task.mjs +4 -0
- package/spec/dummy/src/routes/tasks/controller.mjs +26 -0
- package/spec/http-server/{client-spec.cjs → client-spec.mjs} +12 -5
- package/spec/http-server/{get-spec.cjs → get-spec.mjs} +2 -2
- package/spec/http-server/post-spec.mjs +72 -0
- package/spec/support/jasmine.json +4 -3
- package/src/application.mjs +56 -0
- package/src/cli/commands/db/create.mjs +14 -0
- package/src/cli/commands/generate/migration.mjs +12 -0
- package/src/cli/index.mjs +39 -0
- package/src/configuration.mjs +27 -0
- package/src/{controller.cjs → controller.mjs} +21 -4
- package/src/database/drivers/base.mjs +9 -0
- package/src/database/drivers/index.mjs +5 -0
- package/src/database/drivers/mysql/connect-connection.mjs +12 -0
- package/src/database/drivers/mysql/index.mjs +77 -0
- package/src/database/drivers/mysql/options.mjs +17 -0
- package/src/database/drivers/mysql/query-parser.mjs +25 -0
- package/src/database/drivers/mysql/query.mjs +26 -0
- package/src/database/drivers/mysql/sql/delete.mjs +19 -0
- package/src/database/drivers/mysql/sql/insert.mjs +29 -0
- package/src/database/drivers/mysql/sql/update.mjs +31 -0
- package/src/database/handler.mjs +11 -0
- package/src/database/index.mjs +15 -0
- package/src/database/migration/index.mjs +5 -0
- package/src/database/migrator/index.mjs +15 -0
- package/src/database/pool/index.mjs +43 -0
- package/src/database/query/delete-base.mjs +15 -0
- package/src/database/query/from-base.mjs +9 -0
- package/src/database/query/from-plain.mjs +12 -0
- package/src/database/query/{from-table.cjs → from-table.mjs} +2 -2
- package/src/database/query/index.mjs +144 -0
- package/src/database/query/insert-base.mjs +15 -0
- package/src/database/query/join-base.mjs +9 -0
- package/src/database/query/join-plain.mjs +12 -0
- package/src/database/query/order-base.mjs +9 -0
- package/src/database/query/order-plain.mjs +21 -0
- package/src/database/query/select-base.mjs +9 -0
- package/src/database/query/select-plain.mjs +12 -0
- package/src/database/query/{select-table-and-column.cjs → select-table-and-column.mjs} +4 -4
- package/src/database/query/update-base.mjs +16 -0
- package/src/database/query-parser/{from-parser.cjs → from-parser.mjs} +3 -6
- package/src/database/query-parser/{joins-parser.cjs → joins-parser.mjs} +3 -6
- package/src/database/query-parser/{options.cjs → options.mjs} +13 -2
- package/src/database/query-parser/{select-parser.cjs → select-parser.mjs} +7 -6
- package/src/database/record/index.mjs +187 -0
- package/src/database/record/record-not-found-error.mjs +1 -0
- package/src/{error-logger.js → error-logger.mjs} +1 -1
- package/src/http-server/client/{index.cjs → index.mjs} +10 -11
- package/src/http-server/client/params-to-object.mjs +68 -0
- package/src/http-server/client/request-buffer/form-data-part.mjs +42 -0
- package/src/http-server/client/request-buffer/header.mjs +7 -0
- package/src/http-server/client/request-buffer/index.mjs +229 -0
- package/src/http-server/client/request-parser.mjs +47 -0
- package/src/http-server/client/{request-runner.cjs → request-runner.mjs} +5 -5
- package/src/http-server/client/request.mjs +15 -0
- package/src/http-server/client/{response.cjs → response.mjs} +1 -1
- package/src/http-server/index.mjs +137 -0
- package/src/http-server/server-client.mjs +47 -0
- package/src/http-server/worker-handler/index.mjs +79 -0
- package/src/http-server/worker-handler/worker-script.mjs +4 -0
- package/src/http-server/worker-handler/{worker-thread.cjs → worker-thread.mjs} +18 -11
- package/src/{logger.cjs → logger.mjs} +2 -2
- package/src/routes/base-route.mjs +34 -0
- package/src/routes/{get-route.cjs → get-route.mjs} +5 -3
- package/src/routes/index.mjs +9 -0
- package/src/routes/{resolver.cjs → resolver.mjs} +15 -9
- package/src/routes/{resource-route.cjs → resource-route.mjs} +9 -5
- package/src/routes/root-route.mjs +6 -0
- package/bin/velocious +0 -14
- package/index.cjs +0 -11
- package/spec/dummy/config/databases.example.json +0 -10
- package/spec/dummy/config/databases.json +0 -0
- package/spec/dummy/config/databases.peakflow.json +0 -11
- package/spec/dummy/index.cjs +0 -40
- package/spec/dummy/src/models/task.cjs +0 -4
- package/spec/dummy/src/routes/tasks/controller.cjs +0 -18
- package/src/application.cjs +0 -36
- package/src/configuration.cjs +0 -14
- package/src/database/connection/drivers/mysql/index.cjs +0 -5
- package/src/database/connection/drivers/mysql/options.cjs +0 -7
- package/src/database/connection/drivers/mysql/query-parser.cjs +0 -26
- package/src/database/connection/index.cjs +0 -2
- package/src/database/handler.cjs +0 -5
- package/src/database/index.cjs +0 -9
- package/src/database/pool/index.cjs +0 -2
- package/src/database/query/from-base.cjs +0 -15
- package/src/database/query/from-plain.cjs +0 -12
- package/src/database/query/index.cjs +0 -59
- package/src/database/query/join-base.cjs +0 -15
- package/src/database/query/join-plain.cjs +0 -12
- package/src/database/query/select-base.cjs +0 -15
- package/src/database/query/select-plain.cjs +0 -12
- package/src/database/record/index.cjs +0 -5
- package/src/http-server/client/request-parser.cjs +0 -92
- package/src/http-server/client/request.cjs +0 -25
- package/src/http-server/index.cjs +0 -78
- package/src/http-server/worker-handler/index.cjs +0 -78
- package/src/http-server/worker-handler/socket-handler.cjs +0 -35
- package/src/http-server/worker-handler/worker-script.cjs +0 -4
- package/src/routes/base-route.cjs +0 -25
- package/src/routes/index.cjs +0 -9
- package/src/routes/root-route.cjs +0 -4
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import {dirname} from "path"
|
|
2
|
+
import {fileURLToPath} from "url"
|
|
3
|
+
import fs from "node:fs/promises"
|
|
4
|
+
|
|
5
|
+
const fileExists = async (path) => {
|
|
6
|
+
try {
|
|
7
|
+
await fs.access(path)
|
|
8
|
+
|
|
9
|
+
return true
|
|
10
|
+
} catch (error) {
|
|
11
|
+
return false
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export default class VelociousCli {
|
|
16
|
+
async execute({args}) {
|
|
17
|
+
const __filename = fileURLToPath(`${import.meta.url}/../..`)
|
|
18
|
+
const __dirname = dirname(__filename)
|
|
19
|
+
const commandParts = args[0].split(":")
|
|
20
|
+
let filePath = `${__dirname}/src/cli/commands`
|
|
21
|
+
|
|
22
|
+
for (let commandPart of commandParts) {
|
|
23
|
+
if (commandPart == "d") commandPart = "destroy"
|
|
24
|
+
if (commandPart == "g") commandPart = "generate"
|
|
25
|
+
|
|
26
|
+
filePath += `/${commandPart}`
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
filePath += ".mjs"
|
|
30
|
+
|
|
31
|
+
if (!fileExists(filePath)) throw new Error(`Unknown command: ${args[0]} which should have been in ${filePath}`)
|
|
32
|
+
|
|
33
|
+
const commandClassImport = await import(filePath)
|
|
34
|
+
const CommandClass = commandClassImport.default
|
|
35
|
+
const commandInstance = new CommandClass({args})
|
|
36
|
+
|
|
37
|
+
await commandInstance.execute()
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import {digg} from "diggerize"
|
|
2
|
+
|
|
3
|
+
export default class VelociousConfiguration {
|
|
4
|
+
static current() {
|
|
5
|
+
if (!global.velociousConfiguration) throw new Error("A Velocious configuration hasn't been set")
|
|
6
|
+
|
|
7
|
+
return global.velociousConfiguration
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
constructor({debug, directory}) {
|
|
11
|
+
if (!directory) throw new Error("No directory given")
|
|
12
|
+
|
|
13
|
+
this.debug = debug
|
|
14
|
+
this.directory = directory
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
async initialize() {
|
|
18
|
+
await this.initializeRoutes()
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async initializeRoutes() {
|
|
22
|
+
// Every client need to make their own routes because they probably can't be shared across different worker threads
|
|
23
|
+
const routesImport = await import(`${this.directory}/src/config/routes.mjs`)
|
|
24
|
+
|
|
25
|
+
this.routes = digg(routesImport, "default", "routes")
|
|
26
|
+
}
|
|
27
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import {digg, digs} from "diggerize"
|
|
2
|
+
import ejs from "ejs"
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
export default class VelociousController {
|
|
5
5
|
constructor({configuration, params, request, response}) {
|
|
6
6
|
if (!configuration) throw new Error("No configuration given")
|
|
7
7
|
if (!params) throw new Error("No params given")
|
|
@@ -15,7 +15,24 @@ module.exports = class VelociousController {
|
|
|
15
15
|
this.viewParams = {}
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
params = () => this._params
|
|
19
|
+
|
|
20
|
+
render(args = {}) {
|
|
21
|
+
if (args.json) {
|
|
22
|
+
return this.renderJsonArg(args)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return this.renderView()
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
renderJsonArg(args) {
|
|
29
|
+
const body = JSON.stringify(args.json)
|
|
30
|
+
|
|
31
|
+
this._response.addHeader("Content-Type", "application/json")
|
|
32
|
+
this._response.setBody(body)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
renderView() {
|
|
19
36
|
return new Promise((resolve, reject) => {
|
|
20
37
|
const actionName = digg(this, "_params", "action")
|
|
21
38
|
const controllerName = digg(this, "_params", "controller")
|
|
@@ -0,0 +1,12 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import Base from "../base.mjs"
|
|
2
|
+
import connectConnection from "./connect-connection.mjs"
|
|
3
|
+
import Delete from "./sql/delete.mjs"
|
|
4
|
+
import {digg} from "diggerize"
|
|
5
|
+
import Insert from "./sql/insert.mjs"
|
|
6
|
+
import Options from "./options.mjs"
|
|
7
|
+
import mysql from "mysql"
|
|
8
|
+
import query from "./query.mjs"
|
|
9
|
+
import QueryParser from "./query-parser.mjs"
|
|
10
|
+
import Update from "./sql/update.mjs"
|
|
11
|
+
|
|
12
|
+
export default class VelociousDatabaseDriversMysql extends Base{
|
|
13
|
+
async connect() {
|
|
14
|
+
const connection = mysql.createConnection(this.connectArgs())
|
|
15
|
+
|
|
16
|
+
await connectConnection(connection)
|
|
17
|
+
this.connection = connection
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
disconnect() {
|
|
21
|
+
this.connection.end()
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
connectArgs() {
|
|
25
|
+
const args = this.getArgs()
|
|
26
|
+
const connectArgs = []
|
|
27
|
+
const forward = ["database", "host", "password"]
|
|
28
|
+
|
|
29
|
+
for (const forwardValue of forward) {
|
|
30
|
+
if (forwardValue in args) connectArgs[forwardValue] = digg(args, forwardValue)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if ("username" in args) connectArgs["user"] = args["username"]
|
|
34
|
+
|
|
35
|
+
return connectArgs
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async query(sql) {
|
|
39
|
+
return await query(this.connection, sql)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
queryToSql(query) {
|
|
43
|
+
return new QueryParser({query}).toSql()
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
quote(string) {
|
|
47
|
+
if (!this.connection) throw new Error("Can't escape before connected")
|
|
48
|
+
|
|
49
|
+
return this.connection.escape(string)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
deleteSql({tableName, conditions}) {
|
|
53
|
+
const deleteInstruction = new Delete({conditions, driver: this, tableName})
|
|
54
|
+
|
|
55
|
+
return deleteInstruction.toSql()
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
insertSql({tableName, data}) {
|
|
59
|
+
const insert = new Insert({driver: this, tableName, data})
|
|
60
|
+
|
|
61
|
+
return insert.toSql()
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
options() {
|
|
65
|
+
if (!this._options) {
|
|
66
|
+
this._options = new Options({driver: this})
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return this._options
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
updateSql({conditions, data, tableName}) {
|
|
73
|
+
const update = new Update({conditions, data, driver: this, tableName})
|
|
74
|
+
|
|
75
|
+
return update.toSql()
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import QueryParserOptions from "../../query-parser/options.mjs"
|
|
2
|
+
|
|
3
|
+
export default class VelociousDatabaseDriversMysqlOptions extends QueryParserOptions {
|
|
4
|
+
constructor(options) {
|
|
5
|
+
options.columnQuote = "`"
|
|
6
|
+
options.stringQuote = "'"
|
|
7
|
+
options.tableQuote = "`"
|
|
8
|
+
|
|
9
|
+
super(options)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
quote(string) {
|
|
13
|
+
if (!this.driver) throw new Error("Driver not set")
|
|
14
|
+
|
|
15
|
+
return this.driver.quote(string)
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import {digs} from "diggerize"
|
|
2
|
+
import FromParser from "../../query-parser/from-parser.mjs"
|
|
3
|
+
import JoinsParser from "../../query-parser/joins-parser.mjs"
|
|
4
|
+
import SelectParser from "../../query-parser/select-parser.mjs"
|
|
5
|
+
|
|
6
|
+
export default class VelociousDatabaseConnectionDriversMysqlQueryParser {
|
|
7
|
+
constructor({pretty, query}) {
|
|
8
|
+
if (!query) throw new Error("No query given")
|
|
9
|
+
|
|
10
|
+
this.pretty = pretty
|
|
11
|
+
this.query = query
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
toSql() {
|
|
15
|
+
const {pretty, query} = digs(this, "pretty", "query")
|
|
16
|
+
|
|
17
|
+
let sql = ""
|
|
18
|
+
|
|
19
|
+
sql += new SelectParser({pretty, query}).toSql()
|
|
20
|
+
sql += new FromParser({pretty, query}).toSql()
|
|
21
|
+
sql += new JoinsParser({pretty, query}).toSql()
|
|
22
|
+
|
|
23
|
+
return sql
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export default async function query(connection, sql) {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
connection.query(sql, (error, results, fields) => {
|
|
4
|
+
if (error) {
|
|
5
|
+
reject(error)
|
|
6
|
+
} else {
|
|
7
|
+
const rows = []
|
|
8
|
+
|
|
9
|
+
for (const resultIndex in results) {
|
|
10
|
+
const result = {}
|
|
11
|
+
|
|
12
|
+
for (const fieldKey in fields) {
|
|
13
|
+
const field = fields[fieldKey].name
|
|
14
|
+
const value = results[resultIndex][field]
|
|
15
|
+
|
|
16
|
+
result[field] = value
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
rows.push(result)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
resolve(rows)
|
|
23
|
+
}
|
|
24
|
+
})
|
|
25
|
+
})
|
|
26
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import DeleteBase from "../../../query/delete-base.mjs"
|
|
2
|
+
|
|
3
|
+
export default class VelociousDatabaseConnectionDriversMysqlSqlDelete extends DeleteBase {
|
|
4
|
+
toSql() {
|
|
5
|
+
let sql = `DELETE FROM ${this.getOptions().quoteTableName(this.tableName)} WHERE `
|
|
6
|
+
let count = 0
|
|
7
|
+
|
|
8
|
+
for (let columnName in this.conditions) {
|
|
9
|
+
if (count > 0) sql += " AND "
|
|
10
|
+
|
|
11
|
+
sql += this.getOptions().quoteColumnName(columnName)
|
|
12
|
+
sql += " = "
|
|
13
|
+
sql += this.getOptions().quote(this.conditions[columnName])
|
|
14
|
+
count++
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return sql
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import InsertBase from "../../../query/insert-base.mjs"
|
|
2
|
+
|
|
3
|
+
export default class VelociousDatabaseConnectionDriversMysqlSqlInsert extends InsertBase {
|
|
4
|
+
toSql() {
|
|
5
|
+
let sql = `INSERT INTO ${this.getOptions().quoteTableName(this.tableName)} (`
|
|
6
|
+
let count = 0
|
|
7
|
+
|
|
8
|
+
for (let columnName in this.data) {
|
|
9
|
+
if (count > 0) sql += ", "
|
|
10
|
+
|
|
11
|
+
sql += this.getOptions().quoteColumnName(columnName)
|
|
12
|
+
count++
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
sql += ") VALUES ("
|
|
16
|
+
count = 0
|
|
17
|
+
|
|
18
|
+
for (let columnName in this.data) {
|
|
19
|
+
if (count > 0) sql += ", "
|
|
20
|
+
|
|
21
|
+
sql += this.getOptions().quote(this.data[columnName])
|
|
22
|
+
count++
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
sql += ")"
|
|
26
|
+
|
|
27
|
+
return sql
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import UpdateBase from "../../../query/update-base.mjs"
|
|
2
|
+
|
|
3
|
+
export default class VelociousDatabaseConnectionDriversMysqlSqlUpdate extends UpdateBase {
|
|
4
|
+
toSql() {
|
|
5
|
+
let sql = `UPDATE ${this.getOptions().quoteTableName(this.tableName)} SET `
|
|
6
|
+
let count = 0
|
|
7
|
+
|
|
8
|
+
for (let columnName in this.data) {
|
|
9
|
+
if (count > 0) sql += ", "
|
|
10
|
+
|
|
11
|
+
sql += this.getOptions().quoteColumnName(columnName)
|
|
12
|
+
sql += " = "
|
|
13
|
+
sql += this.getOptions().quote(this.data[columnName])
|
|
14
|
+
count++
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
sql += " WHERE "
|
|
18
|
+
count = 0
|
|
19
|
+
|
|
20
|
+
for (let columnName in this.conditions) {
|
|
21
|
+
if (count > 0) sql += " AND "
|
|
22
|
+
|
|
23
|
+
sql += this.getOptions().quoteColumnName(columnName)
|
|
24
|
+
sql += " = "
|
|
25
|
+
sql += this.getOptions().quote(this.conditions[columnName])
|
|
26
|
+
count++
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return sql
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import Drivers from "./drivers/index.mjs"
|
|
2
|
+
import Handler from "./handler.mjs"
|
|
3
|
+
import Migration from "./migration/index.mjs"
|
|
4
|
+
import Migrator from "./migrator/index.mjs"
|
|
5
|
+
import Query from "./query/index.mjs"
|
|
6
|
+
import Record from "./record/index.mjs"
|
|
7
|
+
|
|
8
|
+
export default {
|
|
9
|
+
Drivers,
|
|
10
|
+
Handler,
|
|
11
|
+
Migration,
|
|
12
|
+
Migrator,
|
|
13
|
+
Query,
|
|
14
|
+
Record
|
|
15
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export default class VelociousDatabaseMigrator {
|
|
2
|
+
constructor({path}) {
|
|
3
|
+
if (!path) throw new Error("No 'path' given")
|
|
4
|
+
|
|
5
|
+
this.path = path
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
migrateUp() {
|
|
9
|
+
throw new Error("stub")
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
migrateDown() {
|
|
13
|
+
throw new Error("stub")
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import Configuration from "../../configuration.mjs"
|
|
2
|
+
import {digg} from "diggerize"
|
|
3
|
+
|
|
4
|
+
export default class VelociousDatabasePool {
|
|
5
|
+
static current() {
|
|
6
|
+
if (!global.velociousDatabasePool) global.velociousDatabasePool = new VelociousDatabasePool()
|
|
7
|
+
|
|
8
|
+
return global.velociousDatabasePool
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
async connect() {
|
|
12
|
+
if (this.connection) {
|
|
13
|
+
console.warn("DatabasePoool#connect: Already connected")
|
|
14
|
+
} else {
|
|
15
|
+
this.connection = await this.spawnConnection()
|
|
16
|
+
|
|
17
|
+
if (!this.connection) throw new Error("spawnConnection didn't set a connection")
|
|
18
|
+
|
|
19
|
+
await this.connection.connect()
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
isConnected = () => Boolean(this.connection)
|
|
24
|
+
|
|
25
|
+
singleConnection() {
|
|
26
|
+
if (!this.connection) throw new Error("Not connected")
|
|
27
|
+
|
|
28
|
+
return this.connection
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async spawnConnection() {
|
|
32
|
+
const databaseConfigPath = `${Configuration.current().directory}/src/config/database.mjs`
|
|
33
|
+
const {databaseConfiguration} = await import(databaseConfigPath)
|
|
34
|
+
const config = databaseConfiguration()
|
|
35
|
+
const defaultConfig = digg(config, "default", "master")
|
|
36
|
+
const driverPath = `../drivers/${digg(defaultConfig, "type")}/index.mjs`
|
|
37
|
+
const DriverClassImport = await import(driverPath)
|
|
38
|
+
const DriverClass = DriverClassImport.default
|
|
39
|
+
const connection = new DriverClass(defaultConfig)
|
|
40
|
+
|
|
41
|
+
return connection
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export default class VelociousDatabaseQueryDeleteBase {
|
|
2
|
+
constructor({conditions, driver, tableName}) {
|
|
3
|
+
this.conditions = conditions
|
|
4
|
+
this.driver = driver
|
|
5
|
+
this.tableName = tableName
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
getOptions() {
|
|
9
|
+
return this.driver.options()
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
toSql() {
|
|
13
|
+
throw new Error("'toSql' wasn't implemented")
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
import FromBase from "./from-base.mjs"
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
export default class VelociousDatabaseQueryFromTable extends FromBase {
|
|
4
4
|
constructor({tableName}) {
|
|
5
5
|
super()
|
|
6
6
|
this.tableName = tableName
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import FromPlain from "./from-plain.mjs"
|
|
2
|
+
import JoinPlain from "./join-plain.mjs"
|
|
3
|
+
import OrderPlain from "./order-plain.mjs"
|
|
4
|
+
import SelectPlain from "./select-plain.mjs"
|
|
5
|
+
|
|
6
|
+
export default class VelociousDatabaseQuery {
|
|
7
|
+
constructor({driver, froms = [], joins = [], handler, limits = [], modelClass, orders = [], selects = [], wheres = []}) {
|
|
8
|
+
if (!driver) throw new Error("No driver given")
|
|
9
|
+
if (!handler) throw new Error("No handler given")
|
|
10
|
+
|
|
11
|
+
this.driver = driver
|
|
12
|
+
this.handler = handler
|
|
13
|
+
this.modelClass = modelClass
|
|
14
|
+
this._froms = froms
|
|
15
|
+
this._joins = joins
|
|
16
|
+
this._limits = limits
|
|
17
|
+
this._orders = orders
|
|
18
|
+
this._selects = selects
|
|
19
|
+
this._wheres = wheres
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
clone() {
|
|
23
|
+
const newQuery = new VelociousDatabaseQuery({
|
|
24
|
+
driver: this.driver,
|
|
25
|
+
froms: [...this._froms],
|
|
26
|
+
handler: this.handler.clone(),
|
|
27
|
+
joins: [...this._joins],
|
|
28
|
+
limits: [...this._limits],
|
|
29
|
+
modelClass: this.modelClass,
|
|
30
|
+
orders: [...this._orders],
|
|
31
|
+
selects: [...this._selects],
|
|
32
|
+
wheres: [...this._wheres]
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
return newQuery
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
getOptions() {
|
|
39
|
+
return this.driver.options()
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async first() {
|
|
43
|
+
const newQuery = this.clone()
|
|
44
|
+
const results = await newQuery.limit(1).reorder(this.modelClass.orderableColumn()).toArray()
|
|
45
|
+
|
|
46
|
+
return results[0]
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
from(from) {
|
|
50
|
+
if (typeof from == "string") from = new FromPlain({plain: from, query: this})
|
|
51
|
+
|
|
52
|
+
from.query = this
|
|
53
|
+
|
|
54
|
+
this._froms.push(from)
|
|
55
|
+
return this
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async last() {
|
|
59
|
+
return await this.clone().reverseOrder().first()
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
limit(value) {
|
|
63
|
+
this._limits.push(value)
|
|
64
|
+
return this
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
joins(join) {
|
|
68
|
+
if (typeof join == "string") join = new JoinPlain({plain: join, query: this})
|
|
69
|
+
|
|
70
|
+
join.query = this
|
|
71
|
+
|
|
72
|
+
this._joins.push(join)
|
|
73
|
+
return this
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
order(order) {
|
|
77
|
+
if (typeof order == "number" || typeof order == "string") order = new OrderPlain({plain: order, query: this})
|
|
78
|
+
|
|
79
|
+
order.query = this
|
|
80
|
+
|
|
81
|
+
this._orders.push(order)
|
|
82
|
+
return this
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
reorder(order) {
|
|
86
|
+
this._orders = []
|
|
87
|
+
this.order(order)
|
|
88
|
+
return this
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
reverseOrder() {
|
|
92
|
+
for (const order of this._orders) {
|
|
93
|
+
order.setReverseOrder(true)
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return this
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
select(select) {
|
|
100
|
+
if (Array.isArray(select)) {
|
|
101
|
+
for (const selectInArray of select) {
|
|
102
|
+
this.select(selectInArray)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return this
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (typeof select == "string") select = new SelectPlain({plain: select})
|
|
109
|
+
|
|
110
|
+
select.query = this
|
|
111
|
+
|
|
112
|
+
this._selects.push(select)
|
|
113
|
+
return this
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
async toArray() {
|
|
117
|
+
const sql = this.toSql()
|
|
118
|
+
const results = await this.driver.query(sql)
|
|
119
|
+
const models = []
|
|
120
|
+
|
|
121
|
+
for (const result of results) {
|
|
122
|
+
const model = new this.modelClass(result)
|
|
123
|
+
|
|
124
|
+
models.push(model)
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
return models
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
toSql() {
|
|
131
|
+
return this.driver.queryToSql(this)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
where(where) {
|
|
135
|
+
if (typeof where == "string") {
|
|
136
|
+
where = new WherePlain({plain: where})
|
|
137
|
+
} else if (typeof where == "object" && where.constructor.name == "object") {
|
|
138
|
+
where = new WhereHash({hash: where})
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
this._wheres.push(where)
|
|
142
|
+
return this
|
|
143
|
+
}
|
|
144
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export default class VelociousDatabaseQueryInsertBase {
|
|
2
|
+
constructor({driver, tableName, data}) {
|
|
3
|
+
this.data = data
|
|
4
|
+
this.driver = driver
|
|
5
|
+
this.tableName = tableName
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
getOptions() {
|
|
9
|
+
return this.driver.options()
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
toSql() {
|
|
13
|
+
throw new Error("'toSql' wasn't implemented")
|
|
14
|
+
}
|
|
15
|
+
}
|