velocious 1.0.47 → 1.0.49
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 +44 -6
- package/spec/database/drivers/columns-spec.js +27 -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 +4 -3
- package/src/database/drivers/base-foreign-key.js +31 -0
- package/src/database/drivers/base-table.js +15 -1
- package/src/database/drivers/base.js +44 -1
- package/src/database/drivers/mssql/column.js +7 -14
- 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 +7 -5
- package/src/database/drivers/mssql/table.js +43 -1
- package/src/database/drivers/mysql/column.js +8 -3
- package/src/database/drivers/mysql/foreign-key.js +2 -5
- package/src/database/drivers/mysql/table.js +41 -0
- package/src/database/drivers/pgsql/column.js +8 -0
- package/src/database/drivers/pgsql/foreign-key.js +2 -5
- package/src/database/drivers/pgsql/table.js +56 -3
- package/src/database/drivers/sqlite/column.js +5 -3
- package/src/database/drivers/sqlite/columns-index.js +2 -2
- package/src/database/drivers/sqlite/foreign-key.js +8 -7
- package/src/database/drivers/sqlite/index.js +2 -0
- package/src/database/drivers/sqlite/index.native.js +3 -1
- package/src/database/drivers/sqlite/index.web.js +3 -1
- package/src/database/drivers/sqlite/sql/alter-table.js +79 -6
- package/src/database/migration/index.js +30 -18
- package/src/database/query/alter-table-base.js +6 -1
- package/src/database/query/create-index-base.js +28 -4
- package/src/database/query/create-table-base.js +14 -2
- package/src/database/table-data/index.js +12 -150
- package/src/database/table-data/table-column.js +145 -0
- package/src/database/table-data/table-foreign-key.js +23 -0
- package/src/database/table-data/table-index.js +18 -0
- package/src/database/table-data/table-reference.js +6 -0
|
@@ -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: ${foreignKey} (${typeof foreignKey})`)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
sql += ` REFERENCES ${options.quoteTableName(foreignKeyTable)}(${options.quoteColumnName(foreignKeyColumn)})`
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return sql
|
|
144
|
+
}
|
|
145
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import restArgsError from "../../utils/rest-args-error.js"
|
|
2
|
+
|
|
3
|
+
export default class TableForeignKey {
|
|
4
|
+
constructor({columnName, isNewForeignKey, name, tableName, referencedColumnName, referencedTableName, ...restArgs}) {
|
|
5
|
+
restArgsError(restArgs)
|
|
6
|
+
|
|
7
|
+
this._columnName = columnName
|
|
8
|
+
this._isNewForeignKey = isNewForeignKey
|
|
9
|
+
this._name = name
|
|
10
|
+
this._tableName = tableName
|
|
11
|
+
this._referencedColumnName = referencedColumnName
|
|
12
|
+
this._referencedTableName = referencedTableName
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
getColumnName() { return this._columnName }
|
|
16
|
+
getIsNewForeignKey() { return this._isNewForeignKey }
|
|
17
|
+
getTableName() { return this._tableName }
|
|
18
|
+
getReferencedColumnName() { return this._referencedColumnName }
|
|
19
|
+
getReferencedTableName() { return this._referencedTableName }
|
|
20
|
+
|
|
21
|
+
getName() { return this._name }
|
|
22
|
+
setName(newName) { this._name = newName }
|
|
23
|
+
}
|
|
@@ -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
|
+
}
|