velocious 1.0.1 → 1.0.3
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 +36 -0
- package/bin/velocious.mjs +8 -0
- package/index.mjs +21 -0
- package/package.json +15 -7
- package/peak_flow.yml +12 -5
- package/spec/cli/commands/db/create-spec.mjs +25 -0
- package/spec/cli/commands/db/migrate-spec.mjs +37 -0
- package/spec/cli/commands/destroy/migration-spec.mjs +15 -0
- package/spec/cli/commands/generate/migration-spec.mjs +18 -0
- package/spec/cli/commands/init-spec.mjs +19 -0
- package/spec/cli/commands/test/test-files-finder-spec.mjs +12 -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/dummy-directory.mjs +11 -0
- package/spec/dummy/index.mjs +63 -0
- package/spec/dummy/src/config/configuration.example.mjs +19 -0
- package/spec/dummy/src/config/configuration.peakflow.mjs +20 -0
- package/spec/dummy/src/{routes.cjs → config/routes.mjs} +3 -2
- package/spec/dummy/src/database/migrations/20230728075328-create-projects.mjs +11 -0
- package/spec/dummy/src/database/migrations/20230728075329-create-tasks.mjs +13 -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} +7 -10
- 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 +50 -0
- package/src/cli/base-command.mjs +11 -0
- package/src/cli/commands/db/create.mjs +50 -0
- package/src/cli/commands/db/migrate.mjs +58 -0
- package/src/cli/commands/destroy/migration.mjs +35 -0
- package/src/cli/commands/generate/migration.mjs +36 -0
- package/src/cli/commands/init.mjs +60 -0
- package/src/cli/commands/test/index.mjs +14 -0
- package/src/cli/commands/test/test-files-finder.mjs +99 -0
- package/src/cli/commands/test/test-runner.mjs +19 -0
- package/src/cli/index.mjs +59 -0
- package/src/configuration-resolver.mjs +26 -0
- package/src/configuration.mjs +49 -0
- package/src/{controller.cjs → controller.mjs} +21 -4
- package/src/database/drivers/base.mjs +17 -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 +102 -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/create-database.mjs +4 -0
- package/src/database/drivers/mysql/sql/create-table.mjs +4 -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/drivers/sqlite/options.mjs +17 -0
- package/src/database/drivers/sqlite/query-parser.mjs +25 -0
- package/src/database/drivers/sqlite/sql/create-database.mjs +4 -0
- package/src/database/drivers/sqlite/sql/create-table.mjs +4 -0
- package/src/database/drivers/sqlite/sql/delete.mjs +19 -0
- package/src/database/drivers/sqlite/sql/insert.mjs +29 -0
- package/src/database/drivers/sqlite/sql/update.mjs +31 -0
- package/src/database/drivers/sqlite-expo/index.mjs +100 -0
- package/src/database/drivers/sqlite-expo/query.mjs +9 -0
- package/src/database/handler.mjs +7 -0
- package/src/database/index.mjs +15 -0
- package/src/database/migration/index.mjs +18 -0
- package/src/database/migrator/index.mjs +15 -0
- package/src/database/pool/index.mjs +112 -0
- package/src/database/query/base.mjs +11 -0
- package/src/database/query/create-database-base.mjs +20 -0
- package/src/database/query/create-table-base.mjs +69 -0
- package/src/database/query/delete-base.mjs +9 -0
- package/src/database/query/from-base.mjs +9 -0
- package/src/database/query/from-plain.mjs +10 -0
- package/src/database/query/from-table.mjs +12 -0
- 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/database/table-data/index.mjs +83 -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.mjs +65 -0
- 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} +17 -9
- package/src/routes/{resource-route.cjs → resource-route.mjs} +9 -5
- package/src/routes/root-route.mjs +6 -0
- package/src/spec/index.mjs +5 -0
- package/src/templates/configuration.mjs +17 -0
- package/src/templates/generate-migration.mjs +11 -0
- package/src/templates/routes.mjs +11 -0
- package/src/utils/file-exists.mjs +13 -0
- package/bin/velocious +0 -14
- package/index.cjs +0 -13
- 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/from-table.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/http-server/worker-handler/worker-thread.cjs +0 -49
- 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,112 @@
|
|
|
1
|
+
import {AsyncLocalStorage} from "node:async_hooks"
|
|
2
|
+
import Configuration from "../../configuration.mjs"
|
|
3
|
+
import {digg} from "diggerize"
|
|
4
|
+
|
|
5
|
+
const asyncLocalStorage = new AsyncLocalStorage()
|
|
6
|
+
let idSeq = 0
|
|
7
|
+
|
|
8
|
+
class VelociousDatabasePool {
|
|
9
|
+
static current() {
|
|
10
|
+
if (!global.velociousDatabasePool) global.velociousDatabasePool = new VelociousDatabasePool()
|
|
11
|
+
|
|
12
|
+
return global.velociousDatabasePool
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
constructor(args = {}) {
|
|
16
|
+
this.configuration = args.configuration || Configuration.current()
|
|
17
|
+
this.connections = []
|
|
18
|
+
this.connectionsInUse = {}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
checkin = (connection) => {
|
|
22
|
+
const id = connection.getIdSeq()
|
|
23
|
+
|
|
24
|
+
if (id in this.connectionsInUse) {
|
|
25
|
+
delete this.connectionsInUse[id]
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
connection.setIdSeq(undefined)
|
|
29
|
+
|
|
30
|
+
this.connections.push(connection)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async checkout() {
|
|
34
|
+
let connection = this.connections.shift()
|
|
35
|
+
|
|
36
|
+
if (!connection) {
|
|
37
|
+
connection = await this.spawnConnection()
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (connection.getIdSeq() !== undefined) throw new Error(`Connection already has an ID-seq - is it in use? ${connection.getIdSeq()}`)
|
|
41
|
+
|
|
42
|
+
const id = idSeq++
|
|
43
|
+
|
|
44
|
+
connection.setIdSeq(id)
|
|
45
|
+
this.connectionsInUse[id] = connection
|
|
46
|
+
|
|
47
|
+
return connection
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
getConfiguration = () => digg(this, "configuration", "database", "default", "master")
|
|
51
|
+
|
|
52
|
+
setCurrent() {
|
|
53
|
+
global.velociousDatabasePool = this
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async spawnConnection() {
|
|
57
|
+
const defaultConfig = this.getConfiguration()
|
|
58
|
+
const connection = await this.spawnConnectionWithConfiguration(defaultConfig)
|
|
59
|
+
|
|
60
|
+
return connection
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async spawnConnectionWithConfiguration(config) {
|
|
64
|
+
const driverPath = `../drivers/${digg(config, "type")}/index.mjs`
|
|
65
|
+
const DriverClassImport = await import(driverPath)
|
|
66
|
+
const DriverClass = DriverClassImport.default
|
|
67
|
+
const connection = new DriverClass(config)
|
|
68
|
+
|
|
69
|
+
await connection.connect()
|
|
70
|
+
|
|
71
|
+
return connection
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async withConnection(callback) {
|
|
75
|
+
const connection = await this.checkout()
|
|
76
|
+
const id = connection.getIdSeq()
|
|
77
|
+
|
|
78
|
+
await asyncLocalStorage.run(id, async () => {
|
|
79
|
+
try {
|
|
80
|
+
await callback()
|
|
81
|
+
} finally {
|
|
82
|
+
this.checkin(connection)
|
|
83
|
+
}
|
|
84
|
+
})
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
getCurrentConnection() {
|
|
88
|
+
const id = asyncLocalStorage.getStore()
|
|
89
|
+
|
|
90
|
+
if (id === undefined) {
|
|
91
|
+
throw new Error("ID hasn't been set for this async context")
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (!(id in this.connectionsInUse)) {
|
|
95
|
+
throw new Error(`Connection ${id} doesn't exist any more - has it been checked in again?`)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return this.connectionsInUse[id]
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const forwardMethods = ["createTableSql", "deleteSql", "insertSql", "query", "quote", "updateSql"]
|
|
103
|
+
|
|
104
|
+
for (const forwardMethod of forwardMethods) {
|
|
105
|
+
VelociousDatabasePool.prototype[forwardMethod] = function(...args) {
|
|
106
|
+
const connection = this.getCurrentConnection()
|
|
107
|
+
|
|
108
|
+
return connection[forwardMethod](...args)
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export default VelociousDatabasePool
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import QueryBase from "./base.mjs"
|
|
2
|
+
|
|
3
|
+
export default class VelociousDatabaseQueryCreateDatabaseBase extends QueryBase {
|
|
4
|
+
constructor({driver, databaseName, ifNotExists}) {
|
|
5
|
+
super({driver})
|
|
6
|
+
this.databaseName = databaseName
|
|
7
|
+
this.ifNotExists = ifNotExists
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
toSql() {
|
|
11
|
+
const {databaseName} = this
|
|
12
|
+
let sql = "CREATE DATABASE"
|
|
13
|
+
|
|
14
|
+
if (this.ifNotExists) sql += " IF NOT EXISTS"
|
|
15
|
+
|
|
16
|
+
sql += ` ${databaseName}`
|
|
17
|
+
|
|
18
|
+
return sql
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import QueryBase from "./base.mjs"
|
|
2
|
+
|
|
3
|
+
export default class VelociousDatabaseQueryCreateTableBase extends QueryBase {
|
|
4
|
+
constructor({driver, ifNotExists, tableData}) {
|
|
5
|
+
super({driver})
|
|
6
|
+
this.ifNotExists = ifNotExists
|
|
7
|
+
this.tableData = tableData
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
toSql() {
|
|
11
|
+
const {tableData} = this
|
|
12
|
+
let sql = "CREATE TABLE"
|
|
13
|
+
|
|
14
|
+
if (this.ifNotExists || tableData.getIfNotExists()) sql += " IF NOT EXISTS"
|
|
15
|
+
|
|
16
|
+
sql += ` ${tableData.getName()} (`
|
|
17
|
+
|
|
18
|
+
let columnCount = 0
|
|
19
|
+
|
|
20
|
+
for (const column of tableData.getColumns()) {
|
|
21
|
+
columnCount++
|
|
22
|
+
|
|
23
|
+
let maxlength = column.args.maxlength
|
|
24
|
+
let type = column.args.type
|
|
25
|
+
|
|
26
|
+
if (type == "string") {
|
|
27
|
+
type = "varchar"
|
|
28
|
+
maxlength ||= 255
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (columnCount > 1) sql += ", "
|
|
32
|
+
|
|
33
|
+
sql += `${this.driver.quoteColumn(column.name)} ${type}`
|
|
34
|
+
|
|
35
|
+
if (maxlength !== undefined) sql += `(${maxlength})`
|
|
36
|
+
|
|
37
|
+
if (column.args.autoIncrement) sql += " AUTO_INCREMENT"
|
|
38
|
+
if (column.args.primaryKey) sql += " PRIMARY KEY"
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
for (const index of tableData.getIndexes()) {
|
|
42
|
+
sql += ","
|
|
43
|
+
|
|
44
|
+
if (index.getUnique()) {
|
|
45
|
+
sql += " UNIQUE"
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
sql += " INDEX"
|
|
49
|
+
|
|
50
|
+
if (index.getName()) {
|
|
51
|
+
sql += ` ${index.getName()}`
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
sql += " ("
|
|
55
|
+
|
|
56
|
+
index.getColumns().forEach((column, columnIndex) => {
|
|
57
|
+
if (columnIndex > 0) sql += ", "
|
|
58
|
+
|
|
59
|
+
sql += this.driver.quoteColumn(column.name)
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
sql += ")"
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
sql += ")"
|
|
66
|
+
|
|
67
|
+
return sql
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import FromBase from "./from-base.mjs"
|
|
2
|
+
|
|
3
|
+
export default class VelociousDatabaseQueryFromTable extends FromBase {
|
|
4
|
+
constructor({driver, tableName}) {
|
|
5
|
+
super({driver})
|
|
6
|
+
this.tableName = tableName
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
toSql() {
|
|
10
|
+
return this.getOptions().quoteTableName(this.tableName)
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import OrderBase from "./order-base.mjs"
|
|
2
|
+
|
|
3
|
+
export default class VelociousDatabaseQueryOrderPlain extends OrderBase {
|
|
4
|
+
constructor({plain}) {
|
|
5
|
+
super()
|
|
6
|
+
this.plain = plain
|
|
7
|
+
this.reverseOrder = false
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
setReverseOrder() {
|
|
11
|
+
this.reverseOrder = true
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
toSql() {
|
|
15
|
+
if (this.reverseOrder) {
|
|
16
|
+
return `${this.plain} DESC`
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
return this.plain
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
import SelectBase from "./select-base.mjs"
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
constructor({tableName, columnName}) {
|
|
5
|
-
super()
|
|
3
|
+
export default class VelociousDatabaseQuerySelectTableAndColumn extends SelectBase {
|
|
4
|
+
constructor({query, tableName, columnName}) {
|
|
5
|
+
super({query})
|
|
6
6
|
this.columnName = columnName
|
|
7
7
|
this.tableName = tableName
|
|
8
8
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export default class VelociousDatabaseQueryUpdateBase {
|
|
2
|
+
constructor({conditions, data, driver, tableName}) {
|
|
3
|
+
this.conditions = conditions
|
|
4
|
+
this.data = data
|
|
5
|
+
this.driver = driver
|
|
6
|
+
this.tableName = tableName
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
getOptions() {
|
|
10
|
+
return this.driver.options()
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
toSql() {
|
|
14
|
+
throw new Error("'toSql' wasn't implemented")
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
import {digs} from "diggerize"
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
constructor({pretty, query
|
|
3
|
+
export default class VelociousDatabaseQueryParserFromParser {
|
|
4
|
+
constructor({pretty, query}) {
|
|
5
5
|
this.pretty = pretty
|
|
6
6
|
this.query = query
|
|
7
|
-
this.queryParserOptions = queryParserOptions
|
|
8
7
|
}
|
|
9
8
|
|
|
10
9
|
toSql() {
|
|
@@ -15,8 +14,6 @@ module.exports = class VelociousDatabaseQueryParserFromParser {
|
|
|
15
14
|
for (const fromKey in query._froms) {
|
|
16
15
|
const from = query._froms[fromKey]
|
|
17
16
|
|
|
18
|
-
from.setOptions(this.queryParserOptions)
|
|
19
|
-
|
|
20
17
|
if (fromKey > 0) {
|
|
21
18
|
sql += ","
|
|
22
19
|
}
|
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
import {digs} from "diggerize"
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
constructor({pretty, query
|
|
3
|
+
export default class VelocuiousDatabaseQueryParserJoinsParser {
|
|
4
|
+
constructor({pretty, query}) {
|
|
5
5
|
this.pretty = pretty
|
|
6
6
|
this.query = query
|
|
7
|
-
this.queryParserOptions = queryParserOptions
|
|
8
7
|
}
|
|
9
8
|
|
|
10
9
|
toSql() {
|
|
@@ -15,8 +14,6 @@ module.exports = class VelocuiousDatabaseQueryParserJoinsParser {
|
|
|
15
14
|
for (const joinKey in query._joins) {
|
|
16
15
|
const join = query._joins[joinKey]
|
|
17
16
|
|
|
18
|
-
join.setOptions(this.queryParserOptions)
|
|
19
|
-
|
|
20
17
|
if (joinKey == 0) {
|
|
21
18
|
if (pretty) {
|
|
22
19
|
sql += "\n\n"
|
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
|
|
1
|
+
import {digg} from "diggerize"
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
export default class VelociousDatabaseQueryParserOptions {
|
|
4
4
|
constructor(options) {
|
|
5
5
|
this.columnQuote = digg(options, "columnQuote")
|
|
6
|
+
this.driver = digg(options, "driver")
|
|
6
7
|
this.tableQuote = digg(options, "tableQuote")
|
|
8
|
+
this.stringQuote = digg(options, "stringQuote")
|
|
9
|
+
|
|
10
|
+
if (!this.driver) throw new Error("No driver given")
|
|
7
11
|
}
|
|
8
12
|
|
|
9
13
|
quoteColumnName(columnName) {
|
|
@@ -17,4 +21,11 @@ module.exports = class VelociousDatabaseQueryParserOptions {
|
|
|
17
21
|
|
|
18
22
|
return `${this.tableQuote}${tableName}${this.tableQuote}`
|
|
19
23
|
}
|
|
24
|
+
|
|
25
|
+
quote(value) {
|
|
26
|
+
if (typeof value == "number")
|
|
27
|
+
return value
|
|
28
|
+
|
|
29
|
+
return this.quoteString(value)
|
|
30
|
+
}
|
|
20
31
|
}
|
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
import {digs} from "diggerize"
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
constructor({pretty, query
|
|
3
|
+
export default class VelociousDatabaseQueryParserSelectParser {
|
|
4
|
+
constructor({pretty, query}) {
|
|
5
5
|
this.pretty = pretty
|
|
6
6
|
this.query = query
|
|
7
|
-
this.queryParserOptions = queryParserOptions
|
|
8
7
|
}
|
|
9
8
|
|
|
10
9
|
toSql() {
|
|
@@ -23,8 +22,6 @@ module.exports = class VelociousDatabaseQueryParserSelectParser {
|
|
|
23
22
|
for (const selectKey in query._selects) {
|
|
24
23
|
const selectValue = query._selects[selectKey]
|
|
25
24
|
|
|
26
|
-
selectValue.setOptions(this.queryParserOptions)
|
|
27
|
-
|
|
28
25
|
sql += selectValue.toSql()
|
|
29
26
|
|
|
30
27
|
if (selectKey + 1 < query._selects.length) {
|
|
@@ -37,6 +34,10 @@ module.exports = class VelociousDatabaseQueryParserSelectParser {
|
|
|
37
34
|
}
|
|
38
35
|
}
|
|
39
36
|
|
|
37
|
+
if (query._selects.length == 0) {
|
|
38
|
+
sql += "*"
|
|
39
|
+
}
|
|
40
|
+
|
|
40
41
|
return sql
|
|
41
42
|
}
|
|
42
43
|
}
|