velocious 1.0.46 → 1.0.48
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/spec/cli/commands/db/migrate-spec.js +52 -8
- package/spec/dummy/src/database/migrations/20250915085450-change-authentication-tokens-user-id-nullable.js +11 -0
- package/spec/dummy/src/database/migrations/20250916111330-rename_authentication_tokens_token_to_user_token.js +11 -0
- package/src/database/drivers/base-column.js +28 -0
- package/src/database/drivers/base-foreign-key.js +31 -0
- package/src/database/drivers/base-table.js +29 -0
- package/src/database/drivers/base.js +19 -1
- package/src/database/drivers/mssql/column.js +22 -0
- package/src/database/drivers/mssql/columns-index.js +1 -1
- package/src/database/drivers/mssql/foreign-key.js +2 -5
- package/src/database/drivers/mssql/index.js +16 -6
- package/src/database/drivers/mssql/sql/alter-table.js +4 -0
- package/src/database/drivers/mssql/table.js +37 -1
- package/src/database/drivers/mysql/column.js +38 -4
- package/src/database/drivers/mysql/foreign-key.js +2 -5
- package/src/database/drivers/mysql/index.js +9 -1
- package/src/database/drivers/mysql/sql/alter-table.js +4 -0
- package/src/database/drivers/mysql/table.js +41 -0
- package/src/database/drivers/pgsql/column.js +22 -0
- package/src/database/drivers/pgsql/foreign-key.js +2 -5
- package/src/database/drivers/pgsql/index.js +10 -2
- package/src/database/drivers/pgsql/sql/alter-table.js +4 -0
- package/src/database/drivers/pgsql/table.js +32 -2
- package/src/database/drivers/sqlite/base.js +5 -5
- package/src/database/drivers/sqlite/column.js +28 -36
- package/src/database/drivers/sqlite/columns-index.js +13 -1
- package/src/database/drivers/sqlite/foreign-key.js +8 -7
- package/src/database/drivers/sqlite/index.js +1 -1
- package/src/database/drivers/sqlite/index.web.js +3 -1
- package/src/database/drivers/sqlite/query.js +1 -2
- package/src/database/drivers/sqlite/sql/alter-table.js +143 -1
- package/src/database/drivers/sqlite/table.js +38 -0
- package/src/database/migration/index.js +31 -15
- package/src/database/query/alter-table-base.js +32 -7
- package/src/database/query/create-index-base.js +28 -4
- package/src/database/query/create-table-base.js +21 -82
- package/src/database/table-data/index.js +36 -70
- package/src/database/table-data/table-column.js +145 -0
- package/src/database/table-data/table-foreign-key.js +21 -0
- package/src/database/table-data/table-index.js +18 -0
- package/src/database/table-data/table-reference.js +6 -0
|
@@ -12,7 +12,10 @@ export default class VelociousDatabaseQueryCreateIndexBase extends QueryBase {
|
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
generateIndexName() {
|
|
15
|
-
|
|
15
|
+
const databaseType = this.getDriver().getType()
|
|
16
|
+
let indexName = `index_on_`
|
|
17
|
+
|
|
18
|
+
if (databaseType == "sqlite") indexName += `${this.tableName}_`
|
|
16
19
|
|
|
17
20
|
for (const columnIndex in this.columns) {
|
|
18
21
|
if (columnIndex > 0) indexName += "_and_"
|
|
@@ -33,17 +36,34 @@ export default class VelociousDatabaseQueryCreateIndexBase extends QueryBase {
|
|
|
33
36
|
}
|
|
34
37
|
|
|
35
38
|
toSql() {
|
|
39
|
+
const databaseType = this.getDriver().getType()
|
|
40
|
+
const indexName = this.name || this.generateIndexName()
|
|
36
41
|
const options = this.getOptions()
|
|
37
42
|
const {tableName} = this
|
|
38
|
-
let sql = "
|
|
43
|
+
let sql = ""
|
|
44
|
+
|
|
45
|
+
if (this.ifNotExists && databaseType == "mssql") {
|
|
46
|
+
sql += `
|
|
47
|
+
IF NOT EXISTS (
|
|
48
|
+
SELECT 1
|
|
49
|
+
FROM sys.indexes
|
|
50
|
+
WHERE
|
|
51
|
+
name = ${options.quote(indexName)} AND
|
|
52
|
+
object_id = OBJECT_ID(${options.quote(`dbo.${tableName}`)})
|
|
53
|
+
)
|
|
54
|
+
BEGIN
|
|
55
|
+
`
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
sql += "CREATE"
|
|
39
59
|
|
|
40
60
|
if (this.unique) sql += " UNIQUE"
|
|
41
61
|
|
|
42
62
|
sql += " INDEX"
|
|
43
63
|
|
|
44
|
-
if (this.ifNotExists) sql += " IF NOT EXISTS"
|
|
64
|
+
if (this.ifNotExists && databaseType != "mssql") sql += " IF NOT EXISTS"
|
|
45
65
|
|
|
46
|
-
sql += ` ${options.quoteIndexName(
|
|
66
|
+
sql += ` ${options.quoteIndexName(indexName)}`
|
|
47
67
|
sql += ` ON ${options.quoteTableName(tableName)} (`
|
|
48
68
|
|
|
49
69
|
for (const columnIndex in this.columns) {
|
|
@@ -63,6 +83,10 @@ export default class VelociousDatabaseQueryCreateIndexBase extends QueryBase {
|
|
|
63
83
|
|
|
64
84
|
sql += ")"
|
|
65
85
|
|
|
86
|
+
if (this.ifNotExists && databaseType == "mssql") {
|
|
87
|
+
sql += " END"
|
|
88
|
+
}
|
|
89
|
+
|
|
66
90
|
return sql
|
|
67
91
|
}
|
|
68
92
|
}
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import CreateIndexBase from "./create-index-base.js"
|
|
2
|
-
import
|
|
2
|
+
import {digs} from "diggerize"
|
|
3
3
|
import QueryBase from "./base.js"
|
|
4
4
|
import restArgsError from "../../utils/rest-args-error.js"
|
|
5
|
+
import TableData from "../table-data/index.js"
|
|
5
6
|
|
|
6
7
|
export default class VelociousDatabaseQueryCreateTableBase extends QueryBase {
|
|
7
8
|
constructor({driver, ifNotExists, indexInCreateTable = true, tableData}) {
|
|
9
|
+
if (!(tableData instanceof TableData)) throw new Error("Invalid table data was given")
|
|
10
|
+
|
|
8
11
|
super({driver})
|
|
9
12
|
this.ifNotExists = ifNotExists
|
|
10
13
|
this.indexInCreateTable = indexInCreateTable
|
|
@@ -15,7 +18,7 @@ export default class VelociousDatabaseQueryCreateTableBase extends QueryBase {
|
|
|
15
18
|
const databaseType = this.getDatabaseType()
|
|
16
19
|
const driver = this.getDriver()
|
|
17
20
|
const options = this.getOptions()
|
|
18
|
-
const {tableData} = this
|
|
21
|
+
const {tableData} = digs(this, "tableData")
|
|
19
22
|
const sqls = []
|
|
20
23
|
const ifNotExists = this.ifNotExists || tableData.getIfNotExists()
|
|
21
24
|
let sql = ""
|
|
@@ -35,86 +38,9 @@ export default class VelociousDatabaseQueryCreateTableBase extends QueryBase {
|
|
|
35
38
|
for (const column of tableData.getColumns()) {
|
|
36
39
|
columnCount++
|
|
37
40
|
|
|
38
|
-
let maxlength = column.getMaxLength()
|
|
39
|
-
let type = column.getType().toUpperCase()
|
|
40
|
-
|
|
41
|
-
if (type == "DATETIME" && databaseType == "pgsql") {
|
|
42
|
-
type = "TIMESTAMP"
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
if (type == "STRING") {
|
|
46
|
-
type = "VARCHAR"
|
|
47
|
-
maxlength ||= 255
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
if (databaseType == "mssql" && type == "BOOLEAN") {
|
|
51
|
-
type = "BIT"
|
|
52
|
-
} else if (databaseType == "mssql" && type == "UUID") {
|
|
53
|
-
type = "VARCHAR"
|
|
54
|
-
maxlength ||= 36
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
if (databaseType == "sqlite" && column.getAutoIncrement() && column.getPrimaryKey()) {
|
|
58
|
-
type = "INTEGER"
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
if (databaseType == "pgsql" && column.getAutoIncrement() && column.getPrimaryKey()) {
|
|
62
|
-
type = "SERIAL"
|
|
63
|
-
}
|
|
64
|
-
|
|
65
41
|
if (columnCount > 1) sql += ", "
|
|
66
42
|
|
|
67
|
-
sql +=
|
|
68
|
-
|
|
69
|
-
if (maxlength !== undefined) sql += `(${maxlength})`
|
|
70
|
-
|
|
71
|
-
if (column.getAutoIncrement() && driver.shouldSetAutoIncrementWhenPrimaryKey()) {
|
|
72
|
-
if (databaseType == "mssql") {
|
|
73
|
-
sql += " IDENTITY"
|
|
74
|
-
} else if (databaseType == "pgsql") {
|
|
75
|
-
if (column.getAutoIncrement() && column.getPrimaryKey()) {
|
|
76
|
-
// Do nothing
|
|
77
|
-
} else {
|
|
78
|
-
throw new Error("pgsql auto increment must be primary key")
|
|
79
|
-
}
|
|
80
|
-
} else {
|
|
81
|
-
sql += " AUTO_INCREMENT"
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
if (typeof column.getDefault() == "function") {
|
|
86
|
-
const defaultValue = column.getDefault()()
|
|
87
|
-
|
|
88
|
-
sql += ` DEFAULT (`
|
|
89
|
-
|
|
90
|
-
if (databaseType == "pgsql" && defaultValue == "UUID()") {
|
|
91
|
-
sql += "gen_random_uuid()"
|
|
92
|
-
} else if (databaseType == "mssql" && defaultValue == "UUID()") {
|
|
93
|
-
sql += "NEWID()"
|
|
94
|
-
} else {
|
|
95
|
-
sql += defaultValue
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
sql += ")"
|
|
99
|
-
} else if (column.getDefault()) {
|
|
100
|
-
sql += ` DEFAULT ${options.quote(column.getDefault())}`
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
if (column.getPrimaryKey()) sql += " PRIMARY KEY"
|
|
104
|
-
if (column.getNull() === false) sql += " NOT NULL"
|
|
105
|
-
|
|
106
|
-
if (column.getForeignKey()) {
|
|
107
|
-
let foreignKeyTable, foreignKeyColumn
|
|
108
|
-
|
|
109
|
-
if (column.getForeignKey() === true) {
|
|
110
|
-
foreignKeyColumn = "id"
|
|
111
|
-
foreignKeyTable = inflection.pluralize(column.getName().replace(/_id$/, ""))
|
|
112
|
-
} else {
|
|
113
|
-
throw new Error(`Unknown foreign key type given: ${column.getForeignKey()} (${typeof column.getForeignKey()})`)
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
sql += ` REFERENCES ${options.quoteTableName(foreignKeyTable)}(${options.quoteColumnName(foreignKeyColumn)})`
|
|
117
|
-
}
|
|
43
|
+
sql += column.getSQL({driver, forAlterTable: false})
|
|
118
44
|
}
|
|
119
45
|
|
|
120
46
|
if (this.indexInCreateTable) {
|
|
@@ -142,10 +68,15 @@ export default class VelociousDatabaseQueryCreateTableBase extends QueryBase {
|
|
|
142
68
|
sql += ")"
|
|
143
69
|
}
|
|
144
70
|
|
|
71
|
+
// Create indexes for all columns with the index argument
|
|
145
72
|
for (const column of tableData.getColumns()) {
|
|
146
73
|
if (!column.getIndex()) continue
|
|
147
74
|
|
|
148
|
-
|
|
75
|
+
let indexName = `index_on_`
|
|
76
|
+
|
|
77
|
+
if (databaseType == "sqlite") sql += `${tableData.getName()}_`
|
|
78
|
+
|
|
79
|
+
indexName += column.getName()
|
|
149
80
|
|
|
150
81
|
sql += ","
|
|
151
82
|
|
|
@@ -174,6 +105,8 @@ export default class VelociousDatabaseQueryCreateTableBase extends QueryBase {
|
|
|
174
105
|
const createIndexArgs = {
|
|
175
106
|
columns: index.getColumns(),
|
|
176
107
|
driver: this.getDriver(),
|
|
108
|
+
ifNotExists: true,
|
|
109
|
+
name: index.getName(),
|
|
177
110
|
tableName: tableData.getName(),
|
|
178
111
|
unique: index.getUnique()
|
|
179
112
|
}
|
|
@@ -182,14 +115,20 @@ export default class VelociousDatabaseQueryCreateTableBase extends QueryBase {
|
|
|
182
115
|
sqls.push(sql)
|
|
183
116
|
}
|
|
184
117
|
|
|
118
|
+
// Create indexes for all columns with the index argument
|
|
185
119
|
for (const column of tableData.getColumns()) {
|
|
186
120
|
if (!column.getIndex()) continue
|
|
187
121
|
|
|
188
|
-
const indexName = `index_on_${column.getName()}`
|
|
189
122
|
const {unique, ...restIndexArgs} = column.getIndex()
|
|
190
123
|
|
|
191
124
|
restArgsError(restIndexArgs)
|
|
192
125
|
|
|
126
|
+
let indexName = `index_on_`
|
|
127
|
+
|
|
128
|
+
if (databaseType == "sqlite") indexName += `${tableData.getName()}_`
|
|
129
|
+
|
|
130
|
+
indexName += column.getName()
|
|
131
|
+
|
|
193
132
|
const createIndexArgs = {
|
|
194
133
|
columns: [column.getName()],
|
|
195
134
|
driver: this.getDriver(),
|
|
@@ -1,103 +1,69 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
constructor(name, args) {
|
|
5
|
-
if (args) {
|
|
6
|
-
const {autoIncrement, default: columnDefault, foreignKey, index, maxLength, name, null: argsNull, primaryKey, type, ...restArgs} = args
|
|
7
|
-
|
|
8
|
-
restArgsError(restArgs)
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
this.args = args
|
|
12
|
-
this.name = name
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
getAutoIncrement = () => this.args?.autoIncrement
|
|
16
|
-
getDefault = () => this.args?.default
|
|
17
|
-
getForeignKey = () => this.args?.foreignKey
|
|
18
|
-
getIndex = () => this.args?.index
|
|
19
|
-
getMaxLength = () => this.args?.maxLength
|
|
20
|
-
getName = () => this.name
|
|
21
|
-
getNull = () => this.args?.null
|
|
22
|
-
getPrimaryKey = () => this.args?.primaryKey
|
|
23
|
-
getType = () => this.args?.type
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
class TableIndex {
|
|
27
|
-
constructor(columns, args) {
|
|
28
|
-
if (args) {
|
|
29
|
-
const {name, unique, ...restArgs} = args
|
|
30
|
-
|
|
31
|
-
restArgsError(restArgs)
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
this.args = args
|
|
35
|
-
this.columns = columns
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
getColumns = () => this.columns
|
|
39
|
-
getName = () => this.args.name
|
|
40
|
-
getUnique = () => Boolean(this.args.unique)
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
class TableReference {
|
|
44
|
-
constructor(name, args) {
|
|
45
|
-
this.args = args
|
|
46
|
-
this.name = name
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export {TableColumn}
|
|
1
|
+
import TableColumn from "./table-column.js"
|
|
2
|
+
import TableIndex from "./table-index.js"
|
|
3
|
+
import TableReference from "./table-reference.js"
|
|
51
4
|
|
|
52
5
|
export default class TableData {
|
|
53
6
|
_columns = []
|
|
7
|
+
_foreignKeys = []
|
|
54
8
|
_indexes = []
|
|
55
9
|
_references = []
|
|
56
10
|
|
|
57
11
|
constructor(name, args = {}) {
|
|
12
|
+
if (!name) throw new Error(`Invalid table name: ${name}`)
|
|
13
|
+
|
|
58
14
|
this.args = args
|
|
59
15
|
this._name = name
|
|
60
16
|
}
|
|
61
17
|
|
|
62
|
-
|
|
63
|
-
|
|
18
|
+
addColumn(name, args = {}) {
|
|
19
|
+
if (name instanceof TableColumn) {
|
|
20
|
+
this._columns.push(name)
|
|
21
|
+
} else {
|
|
22
|
+
const column = new TableColumn(name, args)
|
|
64
23
|
|
|
65
|
-
|
|
24
|
+
this._columns.push(column)
|
|
25
|
+
}
|
|
66
26
|
}
|
|
67
27
|
|
|
68
|
-
getColumns
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
28
|
+
getColumns() { return this._columns }
|
|
29
|
+
|
|
30
|
+
addForeignKey(foreignKey) { this._foreignKeys.push(foreignKey) }
|
|
31
|
+
getForeignKeys() { return this._foreignKeys }
|
|
32
|
+
|
|
33
|
+
addIndex(index) { this._indexes.push(index) }
|
|
34
|
+
getIndexes() { return this._indexes }
|
|
35
|
+
|
|
36
|
+
getName() { return this._name }
|
|
37
|
+
setName(newName) { this._name = newName }
|
|
38
|
+
getIfNotExists() { return this.args.ifNotExists }
|
|
39
|
+
getReferences() { return this._references }
|
|
73
40
|
|
|
74
|
-
bigint
|
|
75
|
-
blob
|
|
76
|
-
boolean
|
|
77
|
-
datetime
|
|
78
|
-
integer
|
|
79
|
-
tinyint
|
|
41
|
+
bigint(name, args = {}) { this.addColumn(name, Object.assign({isNewColumn: true, type: "bigint"}, args)) }
|
|
42
|
+
blob(name, args = {}) { this.addColumn(name, Object.assign({isNewColumn: true, type: "blob"}, args)) }
|
|
43
|
+
boolean(name, args) { this.addColumn(name, Object.assign({isNewColumn: true, type: "boolean"}, args)) }
|
|
44
|
+
datetime(name, args) { this.addColumn(name, Object.assign({isNewColumn: true, type: "datetime"}, args)) }
|
|
45
|
+
integer(name, args = {}) { this.addColumn(name, Object.assign({isNewColumn: true, type: "integer"}, args)) }
|
|
46
|
+
tinyint(name, args = {}) { this.addColumn(name, Object.assign({isNewColumn: true, type: "tinyint"}, args)) }
|
|
80
47
|
|
|
81
48
|
references(name, args = {}) {
|
|
82
49
|
const columnName = `${name}_id`
|
|
83
|
-
const indexName = `index_on_${columnName}`
|
|
84
50
|
const reference = new TableReference(name, args)
|
|
85
|
-
const columnArgs = Object.assign({type: "bigint"}, args)
|
|
51
|
+
const columnArgs = Object.assign({isNewColumn: true, type: "bigint"}, args)
|
|
86
52
|
const column = new TableColumn(columnName, columnArgs)
|
|
87
|
-
const index = new TableIndex([column]
|
|
53
|
+
const index = new TableIndex([column])
|
|
88
54
|
|
|
89
55
|
this._columns.push(column)
|
|
90
56
|
this._indexes.push(index)
|
|
91
57
|
this._references.push(reference)
|
|
92
58
|
}
|
|
93
59
|
|
|
94
|
-
string
|
|
95
|
-
text
|
|
60
|
+
string(name, args) { this.addColumn(name, Object.assign({isNewColumn: true, type: "string"}, args)) }
|
|
61
|
+
text(name, args) { this.addColumn(name, Object.assign({isNewColumn: true, type: "text"}, args)) }
|
|
96
62
|
|
|
97
63
|
timestamps(args = {}) {
|
|
98
64
|
this.datetime("created_at", args)
|
|
99
65
|
this.datetime("updated_at", args)
|
|
100
66
|
}
|
|
101
67
|
|
|
102
|
-
uuid
|
|
68
|
+
uuid(name, args) { this.addColumn(name, Object.assign({isNewColumn: true, type: "uuid"}, args)) }
|
|
103
69
|
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import * as inflection from "inflection"
|
|
2
|
+
import restArgsError from "../../utils/rest-args-error.js"
|
|
3
|
+
import TableForeignKey from "./table-foreign-key.js"
|
|
4
|
+
|
|
5
|
+
export default class TableColumn {
|
|
6
|
+
constructor(name, args) {
|
|
7
|
+
if (args) {
|
|
8
|
+
const {autoIncrement, default: columnDefault, foreignKey, index, isNewColumn, maxLength, name, null: argsNull, primaryKey, type, ...restArgs} = args
|
|
9
|
+
|
|
10
|
+
if (Object.keys(args).length == 0) {
|
|
11
|
+
throw new Error("Empty args given")
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
restArgsError(restArgs)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
this.args = args || {}
|
|
18
|
+
this.name = name
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
getName() { return this.name }
|
|
22
|
+
|
|
23
|
+
getNewName() { return this._newName }
|
|
24
|
+
setNewName(newName) { this._newName = newName }
|
|
25
|
+
|
|
26
|
+
getActualName() { return this.getNewName() || this.getName() }
|
|
27
|
+
|
|
28
|
+
getAutoIncrement() { return this.args?.autoIncrement }
|
|
29
|
+
setAutoIncrement(newAutoIncrement) { this.args.autoIncrement = newAutoIncrement }
|
|
30
|
+
|
|
31
|
+
getDefault() { return this.args?.default }
|
|
32
|
+
setDefault(newDefault) { this.args.default = newDefault }
|
|
33
|
+
|
|
34
|
+
getForeignKey() { return this.args?.foreignKey }
|
|
35
|
+
setForeignKey(newForeignKey) { this.args.foreignKey = newForeignKey }
|
|
36
|
+
|
|
37
|
+
getIndex() { return this.args?.index }
|
|
38
|
+
setIndex(newIndex) { this.args.index = newIndex }
|
|
39
|
+
|
|
40
|
+
getMaxLength() { return this.args?.maxLength }
|
|
41
|
+
setMaxLength(newMaxLength) { this.args.maxLength = newMaxLength }
|
|
42
|
+
|
|
43
|
+
getNull() { return this.args?.null }
|
|
44
|
+
setNull(nullable) { this.args.null = nullable }
|
|
45
|
+
|
|
46
|
+
getPrimaryKey() { return this.args?.primaryKey }
|
|
47
|
+
setPrimaryKey(newPrimaryKey) { this.args.primaryKey = newPrimaryKey }
|
|
48
|
+
|
|
49
|
+
getType() { return this.args?.type }
|
|
50
|
+
setType(newType) { this.args.type = newType }
|
|
51
|
+
|
|
52
|
+
isNewColumn() { return this.args?.isNewColumn }
|
|
53
|
+
|
|
54
|
+
getSQL({forAlterTable, driver}) {
|
|
55
|
+
const databaseType = driver.getType()
|
|
56
|
+
const options = driver.options()
|
|
57
|
+
let maxlength = this.getMaxLength()
|
|
58
|
+
let type = this.getType()?.toUpperCase()
|
|
59
|
+
|
|
60
|
+
if (type == "DATETIME" && databaseType == "pgsql") {
|
|
61
|
+
type = "TIMESTAMP"
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (type == "STRING") {
|
|
65
|
+
type = "VARCHAR"
|
|
66
|
+
maxlength ||= 255
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (databaseType == "mssql" && type == "BOOLEAN") {
|
|
70
|
+
type = "BIT"
|
|
71
|
+
} else if (databaseType == "mssql" && type == "UUID") {
|
|
72
|
+
type = "VARCHAR"
|
|
73
|
+
maxlength ||= 36
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (databaseType == "sqlite" && this.getAutoIncrement() && this.getPrimaryKey()) {
|
|
77
|
+
type = "INTEGER"
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (databaseType == "pgsql" && this.getAutoIncrement() && this.getPrimaryKey()) {
|
|
81
|
+
type = "SERIAL"
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
let sql = `${options.quoteColumnName(this.getActualName())} `
|
|
85
|
+
|
|
86
|
+
if (databaseType == "pgsql" && forAlterTable) sql += "TYPE "
|
|
87
|
+
if (type) sql += type
|
|
88
|
+
if (type && maxlength !== undefined && maxlength !== null) sql += `(${maxlength})`
|
|
89
|
+
|
|
90
|
+
if (this.getAutoIncrement() && driver.shouldSetAutoIncrementWhenPrimaryKey()) {
|
|
91
|
+
if (databaseType == "mssql") {
|
|
92
|
+
sql += " IDENTITY"
|
|
93
|
+
} else if (databaseType == "pgsql") {
|
|
94
|
+
if (this.getAutoIncrement() && this.getPrimaryKey()) {
|
|
95
|
+
// Do nothing
|
|
96
|
+
} else {
|
|
97
|
+
throw new Error("pgsql auto increment must be primary key")
|
|
98
|
+
}
|
|
99
|
+
} else {
|
|
100
|
+
sql += " AUTO_INCREMENT"
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (typeof this.getDefault() == "function") {
|
|
105
|
+
const defaultValue = this.getDefault()()
|
|
106
|
+
|
|
107
|
+
sql += ` DEFAULT (`
|
|
108
|
+
|
|
109
|
+
if (databaseType == "pgsql" && defaultValue == "UUID()") {
|
|
110
|
+
sql += "gen_random_uuid()"
|
|
111
|
+
} else if (databaseType == "mssql" && defaultValue == "UUID()") {
|
|
112
|
+
sql += "NEWID()"
|
|
113
|
+
} else {
|
|
114
|
+
sql += defaultValue
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
sql += ")"
|
|
118
|
+
} else if (this.getDefault()) {
|
|
119
|
+
sql += ` DEFAULT ${options.quote(this.getDefault())}`
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
if (this.getPrimaryKey()) sql += " PRIMARY KEY"
|
|
123
|
+
if (this.getNull() === false) sql += " NOT NULL"
|
|
124
|
+
|
|
125
|
+
const foreignKey = this.getForeignKey()
|
|
126
|
+
|
|
127
|
+
if (foreignKey) {
|
|
128
|
+
let foreignKeyTable, foreignKeyColumn
|
|
129
|
+
|
|
130
|
+
if (foreignKey === true) {
|
|
131
|
+
foreignKeyColumn = "id"
|
|
132
|
+
foreignKeyTable = inflection.pluralize(this.getActualName().replace(/_id$/, ""))
|
|
133
|
+
} else if (foreignKey instanceof TableForeignKey) {
|
|
134
|
+
foreignKeyColumn = foreignKey.getReferencedColumnName()
|
|
135
|
+
foreignKeyTable = foreignKey.getReferencedTableName()
|
|
136
|
+
} else {
|
|
137
|
+
throw new Error(`Unknown foreign key type given: ${this.getForeignKey()} (${typeof this.getForeignKey()})`)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
sql += ` REFERENCES ${options.quoteTableName(foreignKeyTable)}(${options.quoteColumnName(foreignKeyColumn)})`
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return sql
|
|
144
|
+
}
|
|
145
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import restArgsError from "../../utils/rest-args-error.js"
|
|
2
|
+
|
|
3
|
+
export default class TableForeignKey {
|
|
4
|
+
constructor({columnName, name, tableName, referencedColumnName, referencedTableName, ...restArgs}) {
|
|
5
|
+
restArgsError(restArgs)
|
|
6
|
+
|
|
7
|
+
this._columnName = columnName
|
|
8
|
+
this._name = name
|
|
9
|
+
this._tableName = tableName
|
|
10
|
+
this._referencedColumnName = referencedColumnName
|
|
11
|
+
this._referencedTableName = referencedTableName
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
getColumnName() { return this._columnName }
|
|
15
|
+
getTableName() { return this._tableName }
|
|
16
|
+
getReferencedColumnName() { return this._referencedColumnName }
|
|
17
|
+
getReferencedTableName() { return this._referencedTableName }
|
|
18
|
+
|
|
19
|
+
getName() { return this._name }
|
|
20
|
+
setName(newName) { this._name = newName }
|
|
21
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import restArgsError from "../../utils/rest-args-error.js"
|
|
2
|
+
|
|
3
|
+
export default class TableIndex {
|
|
4
|
+
constructor(columns, args) {
|
|
5
|
+
if (args) {
|
|
6
|
+
const {name, unique, ...restArgs} = args
|
|
7
|
+
|
|
8
|
+
restArgsError(restArgs)
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
this.args = args
|
|
12
|
+
this.columns = columns
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
getColumns() { return this.columns }
|
|
16
|
+
getName() { return this.args?.name }
|
|
17
|
+
getUnique() { return Boolean(this.args?.unique) }
|
|
18
|
+
}
|