velocious 1.0.30 → 1.0.32
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 +14 -0
- package/package.json +1 -1
- package/src/cli/commands/db/migrate.js +4 -24
- package/src/cli/commands/db/reset.js +4 -24
- package/src/database/migrator/files-finder.js +40 -0
- package/src/database/migrator.js +4 -3
- package/src/database/query/create-table-base.js +4 -0
- package/src/database/record/index.js +3 -1
- package/src/database/use-database.js +4 -3
- package/src/routes/base-route.js +7 -0
- package/src/routes/post-route.js +24 -0
package/README.md
CHANGED
|
@@ -66,6 +66,20 @@ export default class CreateEvents extends Migration {
|
|
|
66
66
|
}
|
|
67
67
|
```
|
|
68
68
|
|
|
69
|
+
Run migrations from anywhere
|
|
70
|
+
|
|
71
|
+
```js
|
|
72
|
+
const migrationsPath = `/some/dir/migrations`
|
|
73
|
+
const files = await new FilesFinder({path: migrationsPath}).findFiles()
|
|
74
|
+
|
|
75
|
+
await this.configuration.withConnections(async () => {
|
|
76
|
+
const migrator = new Migrator({configuration: this.configuration})
|
|
77
|
+
|
|
78
|
+
await migrator.prepare()
|
|
79
|
+
await migrator.migrateFiles(files, async (path) => await import(path))
|
|
80
|
+
})
|
|
81
|
+
```
|
|
82
|
+
|
|
69
83
|
# Querying
|
|
70
84
|
|
|
71
85
|
```js
|
package/package.json
CHANGED
|
@@ -1,39 +1,19 @@
|
|
|
1
1
|
import BaseCommand from "../../base-command.js"
|
|
2
|
-
import
|
|
3
|
-
import * as inflection from "inflection"
|
|
2
|
+
import FilesFinder from "../../../database/migrator/files-finder.js"
|
|
4
3
|
import Migrator from "../../../database/migrator.js"
|
|
5
4
|
|
|
6
5
|
export default class DbMigrate extends BaseCommand {
|
|
7
6
|
async execute() {
|
|
8
7
|
const projectPath = this.configuration.getDirectory()
|
|
9
8
|
const migrationsPath = `${projectPath}/src/database/migrations`
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
files = files
|
|
13
|
-
.map((file) => {
|
|
14
|
-
const match = file.match(/^(\d{14})-(.+)\.js$/)
|
|
15
|
-
|
|
16
|
-
if (!match) return null
|
|
17
|
-
|
|
18
|
-
const date = parseInt(match[1])
|
|
19
|
-
const migrationName = match[2]
|
|
20
|
-
const migrationClassName = inflection.camelize(migrationName.replaceAll("-", "_"))
|
|
21
|
-
|
|
22
|
-
return {
|
|
23
|
-
file,
|
|
24
|
-
fullPath: `${migrationsPath}/${file}`,
|
|
25
|
-
date,
|
|
26
|
-
migrationClassName
|
|
27
|
-
}
|
|
28
|
-
})
|
|
29
|
-
.filter((migration) => Boolean(migration))
|
|
30
|
-
.sort((migration1, migration2) => migration1.date - migration2.date)
|
|
9
|
+
const filesFinder = new FilesFinder({path: migrationsPath})
|
|
10
|
+
const files = await filesFinder.findFiles()
|
|
31
11
|
|
|
32
12
|
this.migrator = new Migrator({configuration: this.configuration})
|
|
33
13
|
|
|
34
14
|
await this.configuration.withConnections(async () => {
|
|
35
15
|
await this.migrator.prepare()
|
|
36
|
-
await this.migrator.migrateFiles(files)
|
|
16
|
+
await this.migrator.migrateFiles(files, async (importPath) => await import(importPath))
|
|
37
17
|
})
|
|
38
18
|
}
|
|
39
19
|
}
|
|
@@ -1,40 +1,20 @@
|
|
|
1
1
|
import BaseCommand from "../../base-command.js"
|
|
2
|
-
import
|
|
3
|
-
import * as inflection from "inflection"
|
|
2
|
+
import FilesFinder from "../../../database/migrator/files-finder.js"
|
|
4
3
|
import Migrator from "../../../database/migrator.js"
|
|
5
4
|
|
|
6
5
|
export default class DbReset extends BaseCommand {
|
|
7
6
|
async execute() {
|
|
8
7
|
const projectPath = this.configuration.getDirectory()
|
|
9
8
|
const migrationsPath = `${projectPath}/src/database/migrations`
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
files = files
|
|
13
|
-
.map((file) => {
|
|
14
|
-
const match = file.match(/^(\d{14})-(.+)\.js$/)
|
|
15
|
-
|
|
16
|
-
if (!match) return null
|
|
17
|
-
|
|
18
|
-
const date = parseInt(match[1])
|
|
19
|
-
const migrationName = match[2]
|
|
20
|
-
const migrationClassName = inflection.camelize(migrationName.replaceAll("-", "_"))
|
|
21
|
-
|
|
22
|
-
return {
|
|
23
|
-
file,
|
|
24
|
-
fullPath: `${migrationsPath}/${file}`,
|
|
25
|
-
date,
|
|
26
|
-
migrationClassName
|
|
27
|
-
}
|
|
28
|
-
})
|
|
29
|
-
.filter((migration) => Boolean(migration))
|
|
30
|
-
.sort((migration1, migration2) => migration1.date - migration2.date)
|
|
9
|
+
const filesFinder = new FilesFinder({path: migrationsPath})
|
|
10
|
+
const files = await filesFinder.findFiles()
|
|
31
11
|
|
|
32
12
|
this.migrator = new Migrator({configuration: this.configuration})
|
|
33
13
|
|
|
34
14
|
await this.configuration.withConnections(async () => {
|
|
35
15
|
await this.migrator.reset()
|
|
36
16
|
await this.migrator.prepare()
|
|
37
|
-
await this.migrator.migrateFiles(files)
|
|
17
|
+
await this.migrator.migrateFiles(files, async (importPath) => await import(importPath))
|
|
38
18
|
})
|
|
39
19
|
}
|
|
40
20
|
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import fs from "node:fs/promises"
|
|
2
|
+
import * as inflection from "inflection"
|
|
3
|
+
|
|
4
|
+
import restArgsError from "../../utils/rest-args-error.js"
|
|
5
|
+
|
|
6
|
+
export default class VelociousDatabaseMigratorFilesFinder {
|
|
7
|
+
constructor({path, ...restArgs}) {
|
|
8
|
+
restArgsError(restArgs)
|
|
9
|
+
|
|
10
|
+
if (!path) throw new Error("No path given")
|
|
11
|
+
|
|
12
|
+
this.path = path
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
async findFiles() {
|
|
16
|
+
let files = await fs.readdir(this.path)
|
|
17
|
+
|
|
18
|
+
files = files
|
|
19
|
+
.map((file) => {
|
|
20
|
+
const match = file.match(/^(\d{14})-(.+)\.js$/)
|
|
21
|
+
|
|
22
|
+
if (!match) return null
|
|
23
|
+
|
|
24
|
+
const date = parseInt(match[1])
|
|
25
|
+
const migrationName = match[2]
|
|
26
|
+
const migrationClassName = inflection.camelize(migrationName.replaceAll("-", "_"))
|
|
27
|
+
|
|
28
|
+
return {
|
|
29
|
+
file,
|
|
30
|
+
fullPath: `${this.path}/${file}`,
|
|
31
|
+
date,
|
|
32
|
+
migrationClassName
|
|
33
|
+
}
|
|
34
|
+
})
|
|
35
|
+
.filter((migration) => Boolean(migration))
|
|
36
|
+
.sort((migration1, migration2) => migration1.date - migration2.date)
|
|
37
|
+
|
|
38
|
+
return files
|
|
39
|
+
}
|
|
40
|
+
}
|
package/src/database/migrator.js
CHANGED
|
@@ -36,20 +36,20 @@ export default class VelociousDatabaseMigrator {
|
|
|
36
36
|
if (!this.migrationsVersions) throw new Error("Migrations versions hasn't been loaded yet")
|
|
37
37
|
if (!this.migrationsVersions[dbIdentifier]) throw new Error(`Migrations versions hasn't been loaded yet for db: ${dbIdentifier}`)
|
|
38
38
|
|
|
39
|
-
if (version in this.migrationsVersions) {
|
|
39
|
+
if (version in this.migrationsVersions[dbIdentifier]) {
|
|
40
40
|
return true
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
return false
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
async migrateFiles(files) {
|
|
46
|
+
async migrateFiles(files, importCallback) {
|
|
47
47
|
await this.configuration.withConnections(async () => {
|
|
48
48
|
for (const migration of files) {
|
|
49
49
|
await this.runMigrationFile({
|
|
50
50
|
migration,
|
|
51
51
|
requireMigration: async () => {
|
|
52
|
-
const migrationImport = await
|
|
52
|
+
const migrationImport = await importCallback(migration.fullPath)
|
|
53
53
|
|
|
54
54
|
return migrationImport.default
|
|
55
55
|
}
|
|
@@ -177,6 +177,7 @@ export default class VelociousDatabaseMigrator {
|
|
|
177
177
|
async runMigrationFile({migration, requireMigration}) {
|
|
178
178
|
if (!this.configuration) throw new Error("No configuration set")
|
|
179
179
|
if (!this.configuration.isDatabasePoolInitialized()) await this.configuration.initializeDatabasePool()
|
|
180
|
+
if (!this.migrationsVersions) await this.loadMigrationsVersions()
|
|
180
181
|
|
|
181
182
|
const dbs = await this.configuration.getCurrentConnections()
|
|
182
183
|
const migrationClass = await requireMigration()
|
|
@@ -42,6 +42,10 @@ export default class VelociousDatabaseQueryCreateTableBase extends QueryBase {
|
|
|
42
42
|
maxlength ||= 255
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
+
if (databaseType == "mssql" && type == "BOOLEAN") {
|
|
46
|
+
type = "BIT"
|
|
47
|
+
}
|
|
48
|
+
|
|
45
49
|
if (databaseType == "sqlite" && column.getAutoIncrement() && column.getPrimaryKey()) {
|
|
46
50
|
type = "INTEGER"
|
|
47
51
|
}
|
|
@@ -362,7 +362,9 @@ export default class VelociousDatabaseRecord {
|
|
|
362
362
|
}
|
|
363
363
|
|
|
364
364
|
static tableName() {
|
|
365
|
-
|
|
365
|
+
if (!this._tableName) this._tableName = inflection.underscore(inflection.pluralize(this.name))
|
|
366
|
+
|
|
367
|
+
return this._tableName
|
|
366
368
|
}
|
|
367
369
|
|
|
368
370
|
static setTableName(tableName) {
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import DatabaseMigrateFromRequireContext from "./migrate-from-require-context.js"
|
|
2
1
|
import React from "react"
|
|
3
2
|
import useEnvSense from "env-sense/src/use-env-sense.js"
|
|
4
3
|
|
|
5
4
|
import Configuration from "../configuration.js"
|
|
5
|
+
import Migrator from "./migrator.js"
|
|
6
6
|
import restArgsError from "../utils/rest-args-error.js"
|
|
7
7
|
|
|
8
8
|
const loadMigrations = function loadMigrations({migrationsRequireContext, ...restArgs}) {
|
|
@@ -15,9 +15,10 @@ const loadMigrations = function loadMigrations({migrationsRequireContext, ...res
|
|
|
15
15
|
|
|
16
16
|
try {
|
|
17
17
|
await Configuration.current().withConnections(async () => {
|
|
18
|
-
const
|
|
18
|
+
const migrator = new Migrator({configuration: Configuration.current()})
|
|
19
19
|
|
|
20
|
-
await
|
|
20
|
+
await migrator.prepare()
|
|
21
|
+
await migrator.migrateFilesFromRequireContext(migrationsRequireContext)
|
|
21
22
|
})
|
|
22
23
|
|
|
23
24
|
await Configuration.current().initialize()
|
package/src/routes/base-route.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import GetRoute from "./get-route.js"
|
|
2
|
+
import PostRoute from "./post-route.js"
|
|
2
3
|
import ResourceRoute from "./resource-route.js"
|
|
3
4
|
|
|
4
5
|
var VelociousBaseRoute
|
|
@@ -19,6 +20,12 @@ export function initBaseRoute() {
|
|
|
19
20
|
throw new Error(`No 'matchWithPath' implemented on ${this.constructor.name}`)
|
|
20
21
|
}
|
|
21
22
|
|
|
23
|
+
post(name, args) {
|
|
24
|
+
const route = new PostRoute({name, args})
|
|
25
|
+
|
|
26
|
+
this.routes.push(route)
|
|
27
|
+
}
|
|
28
|
+
|
|
22
29
|
resources(name, callback) {
|
|
23
30
|
const route = new ResourceRoute({name})
|
|
24
31
|
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import BaseRoute, {initBaseRoute} from "./base-route.js"
|
|
2
|
+
import escapeStringRegexp from "escape-string-regexp"
|
|
3
|
+
|
|
4
|
+
initBaseRoute()
|
|
5
|
+
|
|
6
|
+
export default class VelociousRoutePostRoute extends BaseRoute {
|
|
7
|
+
constructor({name}) {
|
|
8
|
+
super()
|
|
9
|
+
this.name = name
|
|
10
|
+
this.regExp = new RegExp(`^(${escapeStringRegexp(name)})(.*)$`)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
matchWithPath({params, path}) {
|
|
14
|
+
const match = path.match(this.regExp)
|
|
15
|
+
|
|
16
|
+
if (match) {
|
|
17
|
+
const [_beginnigSlash, _matchedName, restPath] = match
|
|
18
|
+
|
|
19
|
+
params.action = this.name
|
|
20
|
+
|
|
21
|
+
return {restPath}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|