velocious 1.0.21 → 1.0.22
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 +2 -1
- package/src/cli/commands/init.js +3 -3
- package/src/configuration.js +6 -4
- package/src/database/drivers/sqlite/index.web.js +0 -1
- package/src/database/migration/index.js +20 -4
- package/src/database/query/create-table-base.js +7 -0
- package/src/database/table-data/index.js +5 -1
- package/src/database/use-database.js +40 -0
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"velocious": "bin/velocious.js"
|
|
4
4
|
},
|
|
5
5
|
"name": "velocious",
|
|
6
|
-
"version": "1.0.
|
|
6
|
+
"version": "1.0.22",
|
|
7
7
|
"main": "index.js",
|
|
8
8
|
"scripts": {
|
|
9
9
|
"test": "jasmine",
|
|
@@ -34,6 +34,7 @@
|
|
|
34
34
|
"debounce": "^2.2.0",
|
|
35
35
|
"diggerize": "^1.0.5",
|
|
36
36
|
"ejs": "^3.1.6",
|
|
37
|
+
"env-sense": "^1.0.0",
|
|
37
38
|
"escape-string-regexp": "^1.0.5",
|
|
38
39
|
"incorporator": "^1.0.2",
|
|
39
40
|
"inflection": "^3.0.0",
|
package/src/cli/commands/init.js
CHANGED
|
@@ -22,9 +22,9 @@ export default class VelociousCliCommandsInit extends BaseCommand {
|
|
|
22
22
|
]
|
|
23
23
|
const paths = [
|
|
24
24
|
projectConfigPath,
|
|
25
|
-
`${projectPath}/database/migrations`,
|
|
26
|
-
`${projectPath}/models`,
|
|
27
|
-
`${projectPath}/routes`
|
|
25
|
+
`${projectPath}/src/database/migrations`,
|
|
26
|
+
`${projectPath}/src/models`,
|
|
27
|
+
`${projectPath}/src/routes`
|
|
28
28
|
]
|
|
29
29
|
|
|
30
30
|
if (this.args.testing) {
|
package/src/configuration.js
CHANGED
|
@@ -97,11 +97,13 @@ export default class VelociousConfiguration {
|
|
|
97
97
|
isInitialized = () => this._isInitialized
|
|
98
98
|
|
|
99
99
|
async initialize() {
|
|
100
|
-
if (this.
|
|
101
|
-
|
|
102
|
-
|
|
100
|
+
if (!this.isInitialized()) {
|
|
101
|
+
if (this._initializeModels) {
|
|
102
|
+
await this._initializeModels({configuration: this})
|
|
103
|
+
}
|
|
103
104
|
|
|
104
|
-
|
|
105
|
+
this._isInitialized = true
|
|
106
|
+
}
|
|
105
107
|
}
|
|
106
108
|
|
|
107
109
|
registerModelClass(modelClass) {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import * as inflection from "inflection"
|
|
2
|
+
import restArgsError from "../../utils/rest-args-error.js"
|
|
2
3
|
import TableData, {TableColumn} from "../table-data/index.js"
|
|
3
4
|
|
|
4
5
|
export default class VelociousDatabaseMigration {
|
|
@@ -61,14 +62,29 @@ export default class VelociousDatabaseMigration {
|
|
|
61
62
|
}
|
|
62
63
|
}
|
|
63
64
|
|
|
64
|
-
async createTable(tableName,
|
|
65
|
+
async createTable(tableName, arg1, arg2) {
|
|
66
|
+
let args
|
|
67
|
+
let callback
|
|
68
|
+
|
|
69
|
+
if (typeof arg1 == "function") {
|
|
70
|
+
args = {}
|
|
71
|
+
callback = arg1
|
|
72
|
+
} else {
|
|
73
|
+
args = arg1
|
|
74
|
+
callback = arg2
|
|
75
|
+
}
|
|
76
|
+
|
|
65
77
|
const databasePool = this.configuration.getDatabasePool()
|
|
66
|
-
const
|
|
78
|
+
const {id = {}, ...restArgs} = args
|
|
79
|
+
const {default: idDefault, type: idType = databasePool.primaryKeyType(), ...restArgsId} = id
|
|
67
80
|
const tableData = new TableData(tableName)
|
|
68
81
|
|
|
69
|
-
|
|
82
|
+
restArgsError(restArgs)
|
|
83
|
+
restArgsError(restArgsId)
|
|
84
|
+
|
|
85
|
+
if (!(idType in tableData)) throw new Error(`Unsupported primary key type: ${idType}`)
|
|
70
86
|
|
|
71
|
-
tableData[
|
|
87
|
+
tableData[idType]("id", {autoIncrement: true, default: idDefault, null: false, primaryKey: true})
|
|
72
88
|
|
|
73
89
|
if (callback) {
|
|
74
90
|
callback(tableData)
|
|
@@ -47,6 +47,13 @@ export default class VelociousDatabaseQueryCreateTableBase extends QueryBase {
|
|
|
47
47
|
if (maxlength !== undefined) sql += `(${maxlength})`
|
|
48
48
|
|
|
49
49
|
if (column.getAutoIncrement() && this.driver.shouldSetAutoIncrementWhenPrimaryKey()) sql += " AUTO_INCREMENT"
|
|
50
|
+
|
|
51
|
+
if (typeof column.getDefault() == "function") {
|
|
52
|
+
sql += ` DEFAULT (${column.getDefault()()})`
|
|
53
|
+
} else if (column.getDefault()) {
|
|
54
|
+
sql += ` DEFAULT ${this.driver.quote(column.getDefault())}`
|
|
55
|
+
}
|
|
56
|
+
|
|
50
57
|
if (column.getPrimaryKey()) sql += " PRIMARY KEY"
|
|
51
58
|
if (column.getNull() === false) sql += " NOT NULL"
|
|
52
59
|
|
|
@@ -3,7 +3,7 @@ import restArgsError from "../../utils/rest-args-error.js"
|
|
|
3
3
|
class TableColumn {
|
|
4
4
|
constructor(name, args) {
|
|
5
5
|
if (args) {
|
|
6
|
-
const {autoIncrement, foreignKey, maxLength, name, null: argsNull, primaryKey, type, ...restArgs} = args
|
|
6
|
+
const {autoIncrement, default: columnDefault, foreignKey, maxLength, name, null: argsNull, primaryKey, type, ...restArgs} = args
|
|
7
7
|
|
|
8
8
|
restArgsError(restArgs)
|
|
9
9
|
}
|
|
@@ -13,6 +13,7 @@ class TableColumn {
|
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
getAutoIncrement = () => this.args?.autoIncrement
|
|
16
|
+
getDefault = () => this.args?.default
|
|
16
17
|
getForeignKey = () => this.args?.foreignKey
|
|
17
18
|
getMaxLength = () => this.args?.maxLength
|
|
18
19
|
getName = () => this.name
|
|
@@ -70,6 +71,7 @@ export default class TableData {
|
|
|
70
71
|
getReferences = () => this._references
|
|
71
72
|
|
|
72
73
|
bigint = (name, args = {}) => this._defineColumn(name, Object.assign({type: "bigint"}, args))
|
|
74
|
+
blob = (name, args = {}) => this._defineColumn(name, Object.assign({type: "blob"}, args))
|
|
73
75
|
boolean = (name, args) => this._defineColumn(name, Object.assign({type: "boolean"}, args))
|
|
74
76
|
datetime = (name, args) => this._defineColumn(name, Object.assign({type: "datetime"}, args))
|
|
75
77
|
integer = (name, args = {}) => this._defineColumn(name, Object.assign({type: "integer"}, args))
|
|
@@ -94,4 +96,6 @@ export default class TableData {
|
|
|
94
96
|
this.datetime("created_at", args)
|
|
95
97
|
this.datetime("updated_at", args)
|
|
96
98
|
}
|
|
99
|
+
|
|
100
|
+
uuid = (name, args) => this._defineColumn(name, Object.assign({type: "uuid"}, args))
|
|
97
101
|
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import DatabaseMigrateFromRequireContext from "./migrate-from-require-context.js"
|
|
2
|
+
import React from "react"
|
|
3
|
+
import useEnvSense from "env-sense/src/use-env-sense.js"
|
|
4
|
+
|
|
5
|
+
import Configuration from "../configuration.js"
|
|
6
|
+
import restArgsError from "../utils/rest-args-error.js"
|
|
7
|
+
|
|
8
|
+
const shared = {
|
|
9
|
+
loaded: false
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const loadMigrations = function loadMigrations({migrationsRequireContext, ...restArgs}) {
|
|
13
|
+
const {isServer} = useEnvSense()
|
|
14
|
+
const [loaded, setLoaded] = React.useState(shared.loaded)
|
|
15
|
+
|
|
16
|
+
const loadDatabase = React.useCallback(async () => {
|
|
17
|
+
await Configuration.current().getDatabasePool().withConnection(async () => {
|
|
18
|
+
const databaseMigrateFromRequireContext = new DatabaseMigrateFromRequireContext()
|
|
19
|
+
|
|
20
|
+
await databaseMigrateFromRequireContext.execute(migrationsRequireContext)
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
await Configuration.current().initialize()
|
|
24
|
+
|
|
25
|
+
shared.loaded = true
|
|
26
|
+
setLoaded(true)
|
|
27
|
+
}, [])
|
|
28
|
+
|
|
29
|
+
React.useMemo(() => {
|
|
30
|
+
if (!loaded && !isServer) {
|
|
31
|
+
loadDatabase()
|
|
32
|
+
}
|
|
33
|
+
}, [loaded])
|
|
34
|
+
|
|
35
|
+
restArgsError(restArgs)
|
|
36
|
+
|
|
37
|
+
return {loaded}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export default loadMigrations
|