velocious 1.0.31 → 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 +3 -23
- package/src/cli/commands/db/reset.js +3 -23
- package/src/database/migrator/files-finder.js +40 -0
- package/src/database/migrator.js +1 -0
- package/src/database/query/create-table-base.js +4 -0
- package/src/database/record/index.js +3 -1
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,33 +1,13 @@
|
|
|
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
|
|
|
@@ -1,33 +1,13 @@
|
|
|
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
|
|
|
@@ -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
|
@@ -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) {
|