velocious 1.0.97 → 1.0.99
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/eslint.config.js +1 -0
- package/package.json +2 -1
- package/spec/database/connection/drivers/mysql/query-parser-spec.js +4 -4
- package/spec/http-server/post-spec.js +2 -0
- package/src/application.js +27 -9
- package/src/configuration-resolver.js +29 -10
- package/src/configuration-types.js +44 -0
- package/src/configuration.js +63 -33
- package/src/database/drivers/base-column.js +6 -1
- package/src/database/drivers/base-columns-index.js +11 -1
- package/src/database/drivers/base-foreign-key.js +45 -0
- package/src/database/drivers/base-table.js +24 -2
- package/src/database/drivers/base.js +211 -39
- package/src/database/drivers/mssql/index.js +1 -3
- package/src/database/drivers/sqlite/sql/alter-table.js +4 -2
- package/src/database/handler.js +5 -0
- package/src/database/migration/index.js +79 -20
- package/src/database/migrator/files-finder.js +21 -22
- package/src/database/migrator/types.js +29 -0
- package/src/database/migrator.js +98 -59
- package/src/database/pool/async-tracked-multi-connection.js +42 -7
- package/src/database/pool/base-methods-forward.js +37 -0
- package/src/database/pool/base.js +79 -46
- package/src/database/pool/single-multi-use.js +18 -3
- package/src/database/query/alter-table-base.js +4 -4
- package/src/database/query/base.js +9 -2
- package/src/database/query/create-database-base.js +8 -0
- package/src/database/query/create-index-base.js +20 -5
- package/src/database/query/create-table-base.js +28 -9
- package/src/database/query/from-base.js +17 -0
- package/src/database/query/from-plain.js +8 -3
- package/src/database/query/from-table.js +8 -3
- package/src/database/query/index.js +43 -32
- package/src/database/query/join-base.js +28 -1
- package/src/database/query/join-object.js +67 -0
- package/src/database/query/join-plain.js +6 -1
- package/src/database/query/order-base.js +18 -0
- package/src/database/query/order-plain.js +8 -2
- package/src/database/query/select-base.js +15 -0
- package/src/database/query/select-plain.js +6 -1
- package/src/database/query/select-table-and-column.js +8 -2
- package/src/database/query/where-base.js +23 -1
- package/src/database/query/where-hash.js +15 -0
- package/src/database/query/where-plain.js +6 -0
- package/src/database/query-parser/base-query-parser.js +8 -2
- package/src/database/query-parser/from-parser.js +2 -0
- package/src/database/query-parser/joins-parser.js +10 -45
- package/src/database/query-parser/select-parser.js +2 -0
- package/src/database/record/index.js +1 -1
- package/src/database/table-data/index.js +39 -121
- package/src/database/table-data/table-column.js +54 -25
- package/src/database/table-data/table-foreign-key.js +5 -3
- package/src/database/table-data/table-index.js +12 -6
- package/src/database/table-data/table-reference.js +2 -0
- package/src/database/use-database.js +4 -2
- package/src/environment-handlers/base.js +41 -8
- package/src/environment-handlers/node/cli/commands/destroy/migration.js +3 -0
- package/src/environment-handlers/node/cli/commands/generate/migration.js +3 -0
- package/src/environment-handlers/node/cli/commands/generate/model.js +3 -0
- package/src/environment-handlers/node/cli/commands/init.js +3 -0
- package/src/environment-handlers/node.js +59 -28
- package/src/http-client/header.js +6 -0
- package/src/http-client/request.js +31 -5
- package/src/http-client/response.js +31 -7
- package/src/http-server/client/index.js +24 -4
- package/src/http-server/client/request-buffer/form-data-part.js +11 -0
- package/src/http-server/client/request-buffer/header.js +6 -0
- package/src/http-server/client/request-buffer/index.js +91 -13
- package/src/http-server/client/request-parser.js +26 -0
- package/src/http-server/client/request-runner.js +15 -3
- package/src/http-server/client/request.js +17 -0
- package/src/http-server/client/response.js +41 -1
- package/src/http-server/index.js +32 -4
- package/src/http-server/server-client.js +33 -2
- package/src/http-server/worker-handler/index.js +42 -9
- package/src/http-server/worker-handler/worker-script.js +2 -0
- package/src/http-server/worker-handler/worker-thread.js +34 -6
- package/src/logger.js +21 -15
- package/src/routes/app-routes.js +1 -1
- package/src/testing/test-files-finder.js +8 -4
- package/src/testing/test-runner.js +76 -24
- package/src/utils/backtrace-cleaner.js +6 -4
- package/src/utils/ensure-error.js +13 -0
- package/src/utils/file-exists.js +3 -1
- package/src/utils/rest-args-error.js +2 -0
|
@@ -1,8 +1,14 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
1
3
|
import SelectBase from "./select-base.js"
|
|
2
4
|
|
|
3
5
|
export default class VelociousDatabaseQuerySelectTableAndColumn extends SelectBase {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
+
/**
|
|
7
|
+
* @param {string} tableName
|
|
8
|
+
* @param {string} columnName
|
|
9
|
+
*/
|
|
10
|
+
constructor(tableName, columnName) {
|
|
11
|
+
super()
|
|
6
12
|
this.columnName = columnName
|
|
7
13
|
this.tableName = tableName
|
|
8
14
|
}
|
|
@@ -1,11 +1,33 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
1
3
|
export default class VelociousDatabaseQueryWhereBase {
|
|
2
4
|
/**
|
|
3
5
|
* @returns {import("../query-parser/options.js").default}
|
|
4
6
|
*/
|
|
5
7
|
getOptions() {
|
|
6
|
-
return this.
|
|
8
|
+
return this.getQuery().getOptions()
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @returns {import("./index.js").default}
|
|
13
|
+
*/
|
|
14
|
+
getQuery() {
|
|
15
|
+
if (!this.query) throw new Error("'query' hasn't been set")
|
|
16
|
+
|
|
17
|
+
return this.query
|
|
7
18
|
}
|
|
8
19
|
|
|
20
|
+
/**
|
|
21
|
+
* @param {import("./index.js").default} query
|
|
22
|
+
*/
|
|
23
|
+
setQuery(query) {
|
|
24
|
+
this.query = query
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @interface
|
|
29
|
+
* @returns {string}
|
|
30
|
+
*/
|
|
9
31
|
toSql() {
|
|
10
32
|
throw new Error("'toSql' wasn't implemented")
|
|
11
33
|
}
|
|
@@ -1,6 +1,16 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
1
3
|
import WhereBase from "./where-base.js"
|
|
2
4
|
|
|
5
|
+
/**
|
|
6
|
+
* @typedef {{[key: string]: any}} WhereHash
|
|
7
|
+
*/
|
|
8
|
+
|
|
3
9
|
export default class VelociousDatabaseQueryWhereHash extends WhereBase {
|
|
10
|
+
/**
|
|
11
|
+
* @param {import("./index.js").default} query
|
|
12
|
+
* @param {WhereHash} hash
|
|
13
|
+
*/
|
|
4
14
|
constructor(query, hash) {
|
|
5
15
|
super()
|
|
6
16
|
this.hash = hash
|
|
@@ -16,6 +26,11 @@ export default class VelociousDatabaseQueryWhereHash extends WhereBase {
|
|
|
16
26
|
return sql
|
|
17
27
|
}
|
|
18
28
|
|
|
29
|
+
/**
|
|
30
|
+
* @param {WhereHash} hash
|
|
31
|
+
* @param {string} [tableName]
|
|
32
|
+
* @param {number} index
|
|
33
|
+
*/
|
|
19
34
|
_whereSQLFromHash(hash, tableName, index = 0) {
|
|
20
35
|
const options = this.getOptions()
|
|
21
36
|
let sql = ""
|
|
@@ -1,6 +1,12 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
1
3
|
import WhereBase from "./where-base.js"
|
|
2
4
|
|
|
3
5
|
export default class VelociousDatabaseQueryWhereHash extends WhereBase {
|
|
6
|
+
/**
|
|
7
|
+
* @param {import("./index.js").default} query
|
|
8
|
+
* @param {string} plain
|
|
9
|
+
*/
|
|
4
10
|
constructor(query, plain) {
|
|
5
11
|
super()
|
|
6
12
|
this.plain = plain
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
2
3
|
import FromParser from "./from-parser.js"
|
|
3
4
|
import GroupParser from "./group-parser.js"
|
|
4
5
|
import JoinsParser from "./joins-parser.js"
|
|
@@ -8,6 +9,11 @@ import SelectParser from "./select-parser.js"
|
|
|
8
9
|
import WhereParser from "./where-parser.js"
|
|
9
10
|
|
|
10
11
|
export default class VelociousDatabaseBaseQueryParser {
|
|
12
|
+
/**
|
|
13
|
+
* @param {object} args
|
|
14
|
+
* @param {boolean} args.pretty
|
|
15
|
+
* @param {import("../query/index.js").default} args.query
|
|
16
|
+
*/
|
|
11
17
|
constructor({pretty, query}) {
|
|
12
18
|
if (!query) throw new Error("No query given")
|
|
13
19
|
|
|
@@ -16,7 +22,7 @@ export default class VelociousDatabaseBaseQueryParser {
|
|
|
16
22
|
}
|
|
17
23
|
|
|
18
24
|
toSql() {
|
|
19
|
-
const {pretty, query} =
|
|
25
|
+
const {pretty, query} = this
|
|
20
26
|
|
|
21
27
|
let sql = ""
|
|
22
28
|
|
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
import JoinPlain from "../query/join-plain.js"
|
|
1
|
+
// @ts-check
|
|
3
2
|
|
|
4
3
|
export default class VelocuiousDatabaseQueryParserJoinsParser {
|
|
4
|
+
/**
|
|
5
|
+
* @param {object} args
|
|
6
|
+
* @param {boolean} args.pretty
|
|
7
|
+
* @param {import("../query/index.js").default} args.query
|
|
8
|
+
*/
|
|
5
9
|
constructor({pretty, query}) {
|
|
6
10
|
this.pretty = pretty
|
|
7
11
|
this.query = query
|
|
@@ -9,39 +13,14 @@ export default class VelocuiousDatabaseQueryParserJoinsParser {
|
|
|
9
13
|
}
|
|
10
14
|
|
|
11
15
|
toSql() {
|
|
12
|
-
const {pretty, query} =
|
|
16
|
+
const {pretty, query} = this
|
|
13
17
|
let sql = ""
|
|
14
18
|
|
|
15
19
|
for (const joinKey in query._joins) {
|
|
16
20
|
const join = query._joins[joinKey]
|
|
17
21
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
if (pretty) {
|
|
21
|
-
sql += "\n\n"
|
|
22
|
-
} else {
|
|
23
|
-
sql += " "
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
sql += join.toSql()
|
|
28
|
-
} else if (typeof join == "object") {
|
|
29
|
-
sql = this.joinObject({join, modelClass: query.modelClass, sql})
|
|
30
|
-
} else {
|
|
31
|
-
throw new Error(`Unknown join object: ${join.constructor.name}`)
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
return sql
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
joinObject({join, modelClass, sql}) {
|
|
39
|
-
const {conn, pretty} = this
|
|
40
|
-
|
|
41
|
-
for (const joinKey in join) {
|
|
42
|
-
const joinValue = join[joinKey]
|
|
43
|
-
const relationship = modelClass.getRelationshipByName(joinKey)
|
|
44
|
-
const targetModelClass = relationship.getTargetModelClass()
|
|
22
|
+
join.setPretty(pretty)
|
|
23
|
+
join.setQuery(query)
|
|
45
24
|
|
|
46
25
|
if (pretty) {
|
|
47
26
|
sql += "\n\n"
|
|
@@ -49,21 +28,7 @@ export default class VelocuiousDatabaseQueryParserJoinsParser {
|
|
|
49
28
|
sql += " "
|
|
50
29
|
}
|
|
51
30
|
|
|
52
|
-
sql +=
|
|
53
|
-
|
|
54
|
-
if (relationship.getType() == "belongsTo") {
|
|
55
|
-
sql += `${conn.quoteTable(targetModelClass.tableName())}.${conn.quoteColumn(relationship.getPrimaryKey())} = `
|
|
56
|
-
sql += `${conn.quoteTable(modelClass.tableName())}.${conn.quoteColumn(relationship.getForeignKey())}`
|
|
57
|
-
} else if (relationship.getType() == "hasMany" || relationship.getType() == "hasOne") {
|
|
58
|
-
sql += `${conn.quoteTable(targetModelClass.tableName())}.${conn.quoteColumn(relationship.getForeignKey())} = `
|
|
59
|
-
sql += `${conn.quoteTable(modelClass.tableName())}.${conn.quoteColumn(relationship.getPrimaryKey())}`
|
|
60
|
-
} else {
|
|
61
|
-
throw new Error(`Unknown relationship type: ${relationship.getType()}`)
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
if (typeof joinValue == "object") {
|
|
65
|
-
sql = this.joinObject({join: joinValue, modelClass: targetModelClass, sql})
|
|
66
|
-
}
|
|
31
|
+
sql += join.toSql()
|
|
67
32
|
}
|
|
68
33
|
|
|
69
34
|
return sql
|
|
@@ -30,6 +30,8 @@ export default class VelociousDatabaseQueryParserSelectParser {
|
|
|
30
30
|
for (const selectKey in query._selects) {
|
|
31
31
|
const selectValue = query._selects[selectKey]
|
|
32
32
|
|
|
33
|
+
selectValue.setQuery(query)
|
|
34
|
+
|
|
33
35
|
sql += selectValue.toSql()
|
|
34
36
|
|
|
35
37
|
if (selectKey + 1 < query._selects.length) {
|
|
@@ -1,18 +1,32 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
1
3
|
import TableColumn from "./table-column.js"
|
|
2
4
|
import TableIndex from "./table-index.js"
|
|
3
5
|
import TableReference from "./table-reference.js"
|
|
4
6
|
|
|
7
|
+
/**
|
|
8
|
+
* @typedef {object} TableDataArgsType
|
|
9
|
+
* @property {boolean} ifNotExists
|
|
10
|
+
*/
|
|
11
|
+
|
|
5
12
|
export default class TableData {
|
|
13
|
+
/** @type {TableColumn[]} */
|
|
6
14
|
_columns = []
|
|
15
|
+
|
|
16
|
+
/** @type {import("./table-foreign-key.js").default[]} */
|
|
7
17
|
_foreignKeys = []
|
|
18
|
+
|
|
19
|
+
/** @type {TableIndex[]} */
|
|
8
20
|
_indexes = []
|
|
21
|
+
|
|
22
|
+
/** @type {TableReference[]} */
|
|
9
23
|
_references = []
|
|
10
24
|
|
|
11
25
|
/**
|
|
12
26
|
* @param {string} name
|
|
13
|
-
* @param {
|
|
27
|
+
* @param {TableDataArgsType} [args]
|
|
14
28
|
*/
|
|
15
|
-
constructor(name, args
|
|
29
|
+
constructor(name, args) {
|
|
16
30
|
if (!name) throw new Error(`Invalid table name: ${name}`)
|
|
17
31
|
|
|
18
32
|
this.args = args
|
|
@@ -20,10 +34,10 @@ export default class TableData {
|
|
|
20
34
|
}
|
|
21
35
|
|
|
22
36
|
/**
|
|
23
|
-
* @param {string} name
|
|
24
|
-
* @param {
|
|
37
|
+
* @param {string|TableColumn} name
|
|
38
|
+
* @param {import("./table-column.js").TableColumnArgsType} [args]
|
|
25
39
|
*/
|
|
26
|
-
addColumn(name, args
|
|
40
|
+
addColumn(name, args) {
|
|
27
41
|
if (name instanceof TableColumn) {
|
|
28
42
|
this.getColumns().push(name)
|
|
29
43
|
} else {
|
|
@@ -72,7 +86,7 @@ export default class TableData {
|
|
|
72
86
|
/**
|
|
73
87
|
* @returns {boolean}
|
|
74
88
|
*/
|
|
75
|
-
getIfNotExists() { return this.args
|
|
89
|
+
getIfNotExists() { return this.args?.ifNotExists || false }
|
|
76
90
|
|
|
77
91
|
/**
|
|
78
92
|
* @returns {TableReference[]}
|
|
@@ -81,123 +95,59 @@ export default class TableData {
|
|
|
81
95
|
|
|
82
96
|
/**
|
|
83
97
|
* @param {string} name
|
|
84
|
-
* @param {
|
|
85
|
-
* @param {boolean} args.autoIncrement
|
|
86
|
-
* @param {any} args.default
|
|
87
|
-
* @param {boolean} args.dropColumn
|
|
88
|
-
* @param {boolean|object} args.foreignKey
|
|
89
|
-
* @param {boolean|object} args.index
|
|
90
|
-
* @param {number} args.maxLength
|
|
91
|
-
* @param {boolean} args.null
|
|
92
|
-
* @param {boolean} args.primaryKey
|
|
98
|
+
* @param {import("./table-column.js").TableColumnArgsType} args
|
|
93
99
|
* @returns {void}
|
|
94
100
|
*/
|
|
95
|
-
bigint(name, args
|
|
101
|
+
bigint(name, args) { this.addColumn(name, Object.assign({isNewColumn: true, type: "bigint"}, args)) }
|
|
96
102
|
|
|
97
103
|
/**
|
|
98
104
|
* @param {string} name
|
|
99
|
-
* @param {
|
|
100
|
-
* @param {boolean} args.autoIncrement
|
|
101
|
-
* @param {any} args.default
|
|
102
|
-
* @param {boolean} args.dropColumn
|
|
103
|
-
* @param {boolean|object} args.foreignKey
|
|
104
|
-
* @param {boolean|object} args.index
|
|
105
|
-
* @param {number} args.maxLength
|
|
106
|
-
* @param {boolean} args.null
|
|
107
|
-
* @param {boolean} args.primaryKey
|
|
105
|
+
* @param {import("./table-column.js").TableColumnArgsType} args
|
|
108
106
|
* @returns {void}
|
|
109
107
|
*/
|
|
110
|
-
blob(name, args
|
|
108
|
+
blob(name, args) { this.addColumn(name, Object.assign({isNewColumn: true, type: "blob"}, args)) }
|
|
111
109
|
|
|
112
110
|
/**
|
|
113
111
|
* @param {string} name
|
|
114
|
-
* @param {
|
|
115
|
-
* @param {boolean} args.autoIncrement
|
|
116
|
-
* @param {any} args.default
|
|
117
|
-
* @param {boolean} args.dropColumn
|
|
118
|
-
* @param {boolean|object} args.foreignKey
|
|
119
|
-
* @param {boolean|object} args.index
|
|
120
|
-
* @param {number} args.maxLength
|
|
121
|
-
* @param {boolean} args.null
|
|
122
|
-
* @param {boolean} args.primaryKey
|
|
112
|
+
* @param {import("./table-column.js").TableColumnArgsType} args
|
|
123
113
|
* @returns {void}
|
|
124
114
|
*/
|
|
125
115
|
boolean(name, args) { this.addColumn(name, Object.assign({isNewColumn: true, type: "boolean"}, args)) }
|
|
126
116
|
|
|
127
117
|
/**
|
|
128
118
|
* @param {string} name
|
|
129
|
-
* @param {
|
|
130
|
-
* @param {boolean} args.autoIncrement
|
|
131
|
-
* @param {any} args.default
|
|
132
|
-
* @param {boolean} args.dropColumn
|
|
133
|
-
* @param {boolean|object} args.foreignKey
|
|
134
|
-
* @param {boolean|object} args.index
|
|
135
|
-
* @param {number} args.maxLength
|
|
136
|
-
* @param {boolean} args.null
|
|
137
|
-
* @param {boolean} args.primaryKey
|
|
119
|
+
* @param {import("./table-column.js").TableColumnArgsType} args
|
|
138
120
|
* @returns {void}
|
|
139
121
|
*/
|
|
140
122
|
datetime(name, args) { this.addColumn(name, Object.assign({isNewColumn: true, type: "datetime"}, args)) }
|
|
141
123
|
|
|
142
124
|
/**
|
|
143
125
|
* @param {string} name
|
|
144
|
-
* @param {
|
|
145
|
-
* @param {boolean} args.autoIncrement
|
|
146
|
-
* @param {any} args.default
|
|
147
|
-
* @param {boolean} args.dropColumn
|
|
148
|
-
* @param {boolean|object} args.foreignKey
|
|
149
|
-
* @param {boolean|object} args.index
|
|
150
|
-
* @param {number} args.maxLength
|
|
151
|
-
* @param {boolean} args.null
|
|
152
|
-
* @param {boolean} args.primaryKey
|
|
126
|
+
* @param {import("./table-column.js").TableColumnArgsType} args
|
|
153
127
|
* @returns {void}
|
|
154
128
|
*/
|
|
155
|
-
integer(name, args
|
|
129
|
+
integer(name, args) { this.addColumn(name, Object.assign({isNewColumn: true, type: "integer"}, args)) }
|
|
156
130
|
|
|
157
131
|
/**
|
|
158
132
|
* @param {string} name
|
|
159
|
-
* @param {
|
|
160
|
-
* @param {boolean} args.autoIncrement
|
|
161
|
-
* @param {any} args.default
|
|
162
|
-
* @param {boolean} args.dropColumn
|
|
163
|
-
* @param {boolean|object} args.foreignKey
|
|
164
|
-
* @param {boolean|object} args.index
|
|
165
|
-
* @param {number} args.maxLength
|
|
166
|
-
* @param {boolean} args.null
|
|
167
|
-
* @param {boolean} args.primaryKey
|
|
133
|
+
* @param {import("./table-column.js").TableColumnArgsType} args
|
|
168
134
|
* @returns {void}
|
|
169
135
|
*/
|
|
170
|
-
json(name, args
|
|
136
|
+
json(name, args) { this.addColumn(name, Object.assign({isNewColumn: true, type: "json"}, args)) }
|
|
171
137
|
|
|
172
138
|
/**
|
|
173
139
|
* @param {string} name
|
|
174
|
-
* @param {
|
|
175
|
-
* @param {boolean} args.autoIncrement
|
|
176
|
-
* @param {any} args.default
|
|
177
|
-
* @param {boolean} args.dropColumn
|
|
178
|
-
* @param {boolean|object} args.foreignKey
|
|
179
|
-
* @param {boolean|object} args.index
|
|
180
|
-
* @param {number} args.maxLength
|
|
181
|
-
* @param {boolean} args.null
|
|
182
|
-
* @param {boolean} args.primaryKey
|
|
140
|
+
* @param {import("./table-column.js").TableColumnArgsType} args
|
|
183
141
|
* @returns {void}
|
|
184
142
|
*/
|
|
185
|
-
tinyint(name, args
|
|
143
|
+
tinyint(name, args) { this.addColumn(name, Object.assign({isNewColumn: true, type: "tinyint"}, args)) }
|
|
186
144
|
|
|
187
145
|
/**
|
|
188
146
|
* @param {string} name
|
|
189
|
-
* @param {
|
|
190
|
-
* @param {boolean} args.autoIncrement
|
|
191
|
-
* @param {any} args.default
|
|
192
|
-
* @param {boolean} args.dropColumn
|
|
193
|
-
* @param {boolean|object} args.foreignKey
|
|
194
|
-
* @param {boolean|object} args.index
|
|
195
|
-
* @param {number} args.maxLength
|
|
196
|
-
* @param {boolean} args.null
|
|
197
|
-
* @param {boolean} args.primaryKey
|
|
147
|
+
* @param {import("./table-column.js").TableColumnArgsType} args
|
|
198
148
|
* @returns {void}
|
|
199
149
|
*/
|
|
200
|
-
references(name, args
|
|
150
|
+
references(name, args) {
|
|
201
151
|
const columnName = `${name}_id`
|
|
202
152
|
const reference = new TableReference(name, args)
|
|
203
153
|
const {polymorphic, ...restArgs} = args
|
|
@@ -219,62 +169,30 @@ export default class TableData {
|
|
|
219
169
|
|
|
220
170
|
/**
|
|
221
171
|
* @param {string} name
|
|
222
|
-
* @param {
|
|
223
|
-
* @param {boolean} args.autoIncrement
|
|
224
|
-
* @param {any} args.default
|
|
225
|
-
* @param {boolean} args.dropColumn
|
|
226
|
-
* @param {boolean|object} args.foreignKey
|
|
227
|
-
* @param {boolean|object} args.index
|
|
228
|
-
* @param {number} args.maxLength
|
|
229
|
-
* @param {boolean} args.null
|
|
230
|
-
* @param {boolean} args.primaryKey
|
|
172
|
+
* @param {import("./table-column.js").TableColumnArgsType} args
|
|
231
173
|
* @returns {void}
|
|
232
174
|
*/
|
|
233
175
|
string(name, args) { this.addColumn(name, Object.assign({isNewColumn: true, type: "string"}, args)) }
|
|
234
176
|
|
|
235
177
|
/**
|
|
236
178
|
* @param {string} name
|
|
237
|
-
* @param {
|
|
238
|
-
* @param {boolean} args.autoIncrement
|
|
239
|
-
* @param {any} args.default
|
|
240
|
-
* @param {boolean} args.dropColumn
|
|
241
|
-
* @param {boolean|object} args.foreignKey
|
|
242
|
-
* @param {boolean|object} args.index
|
|
243
|
-
* @param {number} args.maxLength
|
|
244
|
-
* @param {boolean} args.null
|
|
245
|
-
* @param {boolean} args.primaryKey
|
|
179
|
+
* @param {import("./table-column.js").TableColumnArgsType} args
|
|
246
180
|
* @returns {void}
|
|
247
181
|
*/
|
|
248
182
|
text(name, args) { this.addColumn(name, Object.assign({isNewColumn: true, type: "text"}, args)) }
|
|
249
183
|
|
|
250
184
|
/**
|
|
251
|
-
* @param {
|
|
252
|
-
* @param {boolean} args.autoIncrement
|
|
253
|
-
* @param {any} args.default
|
|
254
|
-
* @param {boolean} args.dropColumn
|
|
255
|
-
* @param {boolean|object} args.foreignKey
|
|
256
|
-
* @param {boolean|object} args.index
|
|
257
|
-
* @param {number} args.maxLength
|
|
258
|
-
* @param {boolean} args.null
|
|
259
|
-
* @param {boolean} args.primaryKey
|
|
185
|
+
* @param {import("./table-column.js").TableColumnArgsType} args
|
|
260
186
|
* @returns {void}
|
|
261
187
|
*/
|
|
262
|
-
timestamps(args
|
|
188
|
+
timestamps(args) {
|
|
263
189
|
this.datetime("created_at", args)
|
|
264
190
|
this.datetime("updated_at", args)
|
|
265
191
|
}
|
|
266
192
|
|
|
267
193
|
/**
|
|
268
194
|
* @param {string} name
|
|
269
|
-
* @param {
|
|
270
|
-
* @param {boolean} args.autoIncrement
|
|
271
|
-
* @param {any} args.default
|
|
272
|
-
* @param {boolean} args.dropColumn
|
|
273
|
-
* @param {boolean|object} args.foreignKey
|
|
274
|
-
* @param {boolean|object} args.index
|
|
275
|
-
* @param {number} args.maxLength
|
|
276
|
-
* @param {boolean} args.null
|
|
277
|
-
* @param {boolean} args.primaryKey
|
|
195
|
+
* @param {import("./table-column.js").TableColumnArgsType} args
|
|
278
196
|
* @returns {void}
|
|
279
197
|
*/
|
|
280
198
|
uuid(name, args) { this.addColumn(name, Object.assign({isNewColumn: true, type: "uuid"}, args)) }
|
|
@@ -1,26 +1,36 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @typedef {{unique: boolean}} IndexArgType
|
|
5
|
+
*/
|
|
6
|
+
|
|
1
7
|
import * as inflection from "inflection"
|
|
2
8
|
import restArgsError from "../../utils/rest-args-error.js"
|
|
3
9
|
import TableForeignKey from "./table-foreign-key.js"
|
|
4
10
|
|
|
11
|
+
/**
|
|
12
|
+
* @typedef {object} TableColumnArgsType
|
|
13
|
+
* @property {boolean} [autoIncrement]
|
|
14
|
+
* @property {any} [default]
|
|
15
|
+
* @property {boolean} [dropColumn]
|
|
16
|
+
* @property {boolean|object} [foreignKey]
|
|
17
|
+
* @property {boolean|IndexArgType} [index]
|
|
18
|
+
* @property {boolean} [isNewColumn]
|
|
19
|
+
* @property {number} [maxLength]
|
|
20
|
+
* @property {boolean} [null]
|
|
21
|
+
* @property {boolean} [polymorphic]
|
|
22
|
+
* @property {boolean} [primaryKey]
|
|
23
|
+
* @property {string} [type]
|
|
24
|
+
*/
|
|
25
|
+
|
|
5
26
|
export default class TableColumn {
|
|
6
27
|
/**
|
|
7
28
|
* @param {string} name
|
|
8
|
-
* @param {
|
|
9
|
-
* @param {boolean} args.autoIncrement
|
|
10
|
-
* @param {any} args.default
|
|
11
|
-
* @param {boolean} args.dropColumn
|
|
12
|
-
* @param {boolean|object} args.foreignKey
|
|
13
|
-
* @param {boolean|object} args.index
|
|
14
|
-
* @param {boolean} args.isNewColumn
|
|
15
|
-
* @param {number} args.maxLength
|
|
16
|
-
* @param {string} args.name
|
|
17
|
-
* @param {boolean} args.null
|
|
18
|
-
* @param {boolean} args.primaryKey
|
|
19
|
-
* @param {string} args.type
|
|
29
|
+
* @param {TableColumnArgsType} [args]
|
|
20
30
|
*/
|
|
21
31
|
constructor(name, args) {
|
|
22
32
|
if (args) {
|
|
23
|
-
const {autoIncrement, default: columnDefault, dropColumn, foreignKey, index, isNewColumn, maxLength,
|
|
33
|
+
const {autoIncrement, default: columnDefault, dropColumn, foreignKey, index, isNewColumn, maxLength, null: argsNull, polymorphic, primaryKey, type, ...restArgs} = args // eslint-disable-line no-unused-vars
|
|
24
34
|
|
|
25
35
|
if (Object.keys(args).length == 0) {
|
|
26
36
|
throw new Error("Empty args given")
|
|
@@ -39,7 +49,7 @@ export default class TableColumn {
|
|
|
39
49
|
getName() { return this.name }
|
|
40
50
|
|
|
41
51
|
/**
|
|
42
|
-
* @returns {string}
|
|
52
|
+
* @returns {string | undefined}
|
|
43
53
|
*/
|
|
44
54
|
getNewName() { return this._newName }
|
|
45
55
|
|
|
@@ -71,6 +81,7 @@ export default class TableColumn {
|
|
|
71
81
|
getDefault() { return this.args?.default }
|
|
72
82
|
|
|
73
83
|
/**
|
|
84
|
+
* @param {any} newDefault
|
|
74
85
|
* @returns {void}
|
|
75
86
|
*/
|
|
76
87
|
setDefault(newDefault) { this.args.default = newDefault }
|
|
@@ -81,29 +92,48 @@ export default class TableColumn {
|
|
|
81
92
|
getDropColumn() { return this.args?.dropColumn || false }
|
|
82
93
|
|
|
83
94
|
/**
|
|
84
|
-
* @returns {boolean|object}
|
|
95
|
+
* @returns {boolean | object | undefined}
|
|
85
96
|
*/
|
|
86
97
|
getForeignKey() { return this.args?.foreignKey }
|
|
87
98
|
|
|
88
99
|
/**
|
|
89
|
-
* @param {boolean|object} newForeignKey
|
|
100
|
+
* @param {boolean | object} newForeignKey
|
|
90
101
|
* @returns {void}
|
|
91
102
|
*/
|
|
92
103
|
setForeignKey(newForeignKey) { this.args.foreignKey = newForeignKey }
|
|
93
104
|
|
|
94
105
|
/**
|
|
95
|
-
* @returns {boolean|
|
|
106
|
+
* @returns {boolean|IndexArgType}
|
|
107
|
+
*/
|
|
108
|
+
getIndex() { return this.args?.index || false }
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* @returns {IndexArgType}
|
|
96
112
|
*/
|
|
97
|
-
|
|
113
|
+
getIndexArgs() {
|
|
114
|
+
if (typeof this.args?.index == "object") {
|
|
115
|
+
return this.args.index
|
|
116
|
+
} else {
|
|
117
|
+
return {unique: false}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
getIndexUnique() {
|
|
122
|
+
const index = this.args?.index
|
|
123
|
+
|
|
124
|
+
if (typeof index == "object" && index.unique === true) return true
|
|
125
|
+
|
|
126
|
+
return false
|
|
127
|
+
}
|
|
98
128
|
|
|
99
129
|
/**
|
|
100
|
-
* @param {boolean|
|
|
130
|
+
* @param {boolean|IndexArgType} newIndex
|
|
101
131
|
* @returns {void}
|
|
102
132
|
*/
|
|
103
133
|
setIndex(newIndex) { this.args.index = newIndex }
|
|
104
134
|
|
|
105
135
|
/**
|
|
106
|
-
* @returns {number}
|
|
136
|
+
* @returns {number | undefined}
|
|
107
137
|
*/
|
|
108
138
|
getMaxLength() { return this.args?.maxLength }
|
|
109
139
|
|
|
@@ -114,7 +144,7 @@ export default class TableColumn {
|
|
|
114
144
|
setMaxLength(newMaxLength) { this.args.maxLength = newMaxLength }
|
|
115
145
|
|
|
116
146
|
/**
|
|
117
|
-
* @returns {boolean}
|
|
147
|
+
* @returns {boolean | undefined}
|
|
118
148
|
*/
|
|
119
149
|
getNull() { return this.args?.null }
|
|
120
150
|
|
|
@@ -127,7 +157,7 @@ export default class TableColumn {
|
|
|
127
157
|
/**
|
|
128
158
|
* @returns {boolean}
|
|
129
159
|
*/
|
|
130
|
-
getPrimaryKey() { return this.args?.primaryKey }
|
|
160
|
+
getPrimaryKey() { return this.args?.primaryKey || false }
|
|
131
161
|
|
|
132
162
|
/**
|
|
133
163
|
* @param {boolean} newPrimaryKey
|
|
@@ -136,7 +166,7 @@ export default class TableColumn {
|
|
|
136
166
|
setPrimaryKey(newPrimaryKey) { this.args.primaryKey = newPrimaryKey }
|
|
137
167
|
|
|
138
168
|
/**
|
|
139
|
-
* @returns {string}
|
|
169
|
+
* @returns {string | undefined}
|
|
140
170
|
*/
|
|
141
171
|
getType() { return this.args?.type }
|
|
142
172
|
|
|
@@ -154,8 +184,7 @@ export default class TableColumn {
|
|
|
154
184
|
/**
|
|
155
185
|
* @param {object} args
|
|
156
186
|
* @param {boolean} args.forAlterTable
|
|
157
|
-
* @
|
|
158
|
-
* @param {T} args.driver
|
|
187
|
+
* @param {import("../drivers/base.js").default} args.driver
|
|
159
188
|
* @returns {string}
|
|
160
189
|
*/
|
|
161
190
|
getSQL({forAlterTable, driver, ...restArgs}) {
|
|
@@ -1,11 +1,13 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
1
3
|
import restArgsError from "../../utils/rest-args-error.js"
|
|
2
4
|
|
|
3
5
|
export default class TableForeignKey {
|
|
4
6
|
/**
|
|
5
7
|
* @param {object} args
|
|
6
8
|
* @param {string} args.columnName
|
|
7
|
-
* @param {boolean} args.isNewForeignKey
|
|
8
|
-
* @param {string} args.name
|
|
9
|
+
* @param {boolean} [args.isNewForeignKey]
|
|
10
|
+
* @param {string} [args.name]
|
|
9
11
|
* @param {string} args.tableName
|
|
10
12
|
* @param {string} args.referencedColumnName
|
|
11
13
|
* @param {string} args.referencedTableName
|
|
@@ -29,7 +31,7 @@ export default class TableForeignKey {
|
|
|
29
31
|
/**
|
|
30
32
|
* @returns {boolean}
|
|
31
33
|
*/
|
|
32
|
-
getIsNewForeignKey() { return this._isNewForeignKey }
|
|
34
|
+
getIsNewForeignKey() { return this._isNewForeignKey || false }
|
|
33
35
|
|
|
34
36
|
/**
|
|
35
37
|
* @returns {string}
|