velocious 1.0.2 → 1.0.3
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 +12 -2
- package/bin/velocious.mjs +3 -3
- package/index.mjs +5 -1
- package/package.json +4 -3
- package/peak_flow.yml +5 -2
- package/spec/cli/commands/db/create-spec.mjs +25 -0
- package/spec/cli/commands/db/migrate-spec.mjs +37 -0
- package/spec/cli/commands/destroy/migration-spec.mjs +15 -0
- package/spec/cli/commands/generate/migration-spec.mjs +18 -0
- package/spec/cli/commands/init-spec.mjs +19 -0
- package/spec/cli/commands/test/test-files-finder-spec.mjs +12 -0
- package/spec/database/drivers/mysql/connection-spec.mjs +2 -2
- package/spec/dummy/dummy-directory.mjs +11 -0
- package/spec/dummy/index.mjs +18 -24
- package/spec/dummy/src/config/configuration.example.mjs +19 -0
- package/spec/dummy/src/config/configuration.peakflow.mjs +20 -0
- package/spec/dummy/src/database/migrations/20230728075328-create-projects.mjs +11 -0
- package/spec/dummy/src/database/migrations/20230728075329-create-tasks.mjs +13 -0
- package/spec/http-server/client-spec.mjs +3 -13
- package/src/application.mjs +6 -12
- package/src/cli/base-command.mjs +11 -0
- package/src/cli/commands/db/create.mjs +44 -8
- package/src/cli/commands/db/migrate.mjs +58 -0
- package/src/cli/commands/destroy/migration.mjs +35 -0
- package/src/cli/commands/generate/migration.mjs +31 -7
- package/src/cli/commands/init.mjs +60 -0
- package/src/cli/commands/test/index.mjs +14 -0
- package/src/cli/commands/test/test-files-finder.mjs +99 -0
- package/src/cli/commands/test/test-runner.mjs +19 -0
- package/src/cli/index.mjs +36 -16
- package/src/configuration-resolver.mjs +26 -0
- package/src/configuration.mjs +24 -2
- package/src/database/drivers/base.mjs +8 -0
- package/src/database/drivers/mysql/index.mjs +25 -0
- package/src/database/drivers/mysql/sql/create-database.mjs +4 -0
- package/src/database/drivers/mysql/sql/create-table.mjs +4 -0
- package/src/database/drivers/sqlite/options.mjs +17 -0
- package/src/database/drivers/sqlite/query-parser.mjs +25 -0
- package/src/database/drivers/sqlite/sql/create-database.mjs +4 -0
- package/src/database/drivers/sqlite/sql/create-table.mjs +4 -0
- package/src/database/drivers/sqlite/sql/delete.mjs +19 -0
- package/src/database/drivers/sqlite/sql/insert.mjs +29 -0
- package/src/database/drivers/sqlite/sql/update.mjs +31 -0
- package/src/database/drivers/sqlite-expo/index.mjs +100 -0
- package/src/database/drivers/sqlite-expo/query.mjs +9 -0
- package/src/database/handler.mjs +0 -4
- package/src/database/migration/index.mjs +15 -2
- package/src/database/pool/index.mjs +87 -18
- package/src/database/query/base.mjs +11 -0
- package/src/database/query/create-database-base.mjs +20 -0
- package/src/database/query/create-table-base.mjs +69 -0
- package/src/database/query/delete-base.mjs +4 -10
- package/src/database/query/from-plain.mjs +3 -5
- package/src/database/query/from-table.mjs +2 -2
- package/src/database/record/index.mjs +1 -1
- package/src/database/table-data/index.mjs +83 -0
- package/src/http-server/worker-handler/worker-thread.mjs +17 -8
- package/src/routes/resolver.mjs +3 -1
- package/src/spec/index.mjs +5 -0
- package/src/templates/configuration.mjs +17 -0
- package/src/templates/generate-migration.mjs +11 -0
- package/src/templates/routes.mjs +11 -0
- package/src/utils/file-exists.mjs +13 -0
- package/spec/cli/generate/migration-spec.mjs +0 -9
- package/spec/dummy/src/config/database.example.mjs +0 -15
- package/spec/dummy/src/config/database.peakflow.mjs +0 -15
- package/spec/dummy/src/database/migrations/001-create-tasks.mjs +0 -12
|
@@ -1,12 +1,36 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import BaseCommand from "../../base-command.mjs"
|
|
2
|
+
import {dirname} from "path"
|
|
3
|
+
import {fileURLToPath} from "url"
|
|
4
|
+
import fileExists from "../../../utils/file-exists.mjs"
|
|
5
|
+
import fs from "node:fs/promises"
|
|
6
|
+
import inflection from "inflection"
|
|
7
|
+
import strftime from "strftime"
|
|
5
8
|
|
|
6
|
-
|
|
7
|
-
|
|
9
|
+
export default class DbGenerateMigration extends BaseCommand {
|
|
10
|
+
async execute() {
|
|
11
|
+
const migrationName = this.processArgs[1]
|
|
12
|
+
const migrationNameCamelized = inflection.camelize(migrationName.replaceAll("-", "_"))
|
|
8
13
|
const date = new Date()
|
|
14
|
+
const migrationNumber = strftime("%Y%m%d%H%M%S")
|
|
15
|
+
const migrationFileName = `${migrationNumber}-${migrationName}.mjs`
|
|
16
|
+
const __filename = fileURLToPath(`${import.meta.url}/../../..`)
|
|
17
|
+
const __dirname = dirname(__filename)
|
|
18
|
+
const templateFilePath = `${__dirname}/templates/generate-migration.mjs`
|
|
19
|
+
const migrationContentBuffer = await fs.readFile(templateFilePath)
|
|
20
|
+
const migrationContent = migrationContentBuffer.toString().replaceAll("__MIGRATION_NAME__", migrationNameCamelized)
|
|
21
|
+
const migrationDir = `${process.cwd()}/src/database/migrations`
|
|
22
|
+
const migrationPath = `${migrationDir}/${migrationFileName}`
|
|
23
|
+
|
|
24
|
+
if (this.args.testing) {
|
|
25
|
+
return {date, migrationContent, migrationName, migrationNameCamelized, migrationNumber, migrationPath }
|
|
26
|
+
} else {
|
|
27
|
+
if (!await fileExists(migrationDir)) {
|
|
28
|
+
await fs.mkdir(migrationDir, {recursive: true})
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
await fs.writeFile(migrationPath, migrationContent)
|
|
9
32
|
|
|
10
|
-
|
|
33
|
+
console.log(`create src/database/migrations/${migrationFileName}`)
|
|
34
|
+
}
|
|
11
35
|
}
|
|
12
36
|
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import BaseCommand from "../base-command.mjs"
|
|
2
|
+
import {dirname} from "path"
|
|
3
|
+
import fileExists from "../../utils/file-exists.mjs"
|
|
4
|
+
import {fileURLToPath} from "url"
|
|
5
|
+
import fs from "node:fs/promises"
|
|
6
|
+
|
|
7
|
+
export default class VelociousCliCommandsInit extends BaseCommand {
|
|
8
|
+
async execute() {
|
|
9
|
+
const __filename = fileURLToPath(`${import.meta.url}/../../..`)
|
|
10
|
+
const velocipusPath = dirname(__filename)
|
|
11
|
+
const projectPath = this.configuration?.directory || process.cwd()
|
|
12
|
+
const projectConfigPath = `${projectPath}/src/config`
|
|
13
|
+
const fileMappings = [
|
|
14
|
+
{
|
|
15
|
+
source: `${velocipusPath}/src/templates/configuration.mjs`,
|
|
16
|
+
target: `${projectConfigPath}/configuration.mjs`
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
source: `${velocipusPath}/src/templates/routes.mjs`,
|
|
20
|
+
target: `${projectConfigPath}/routes.mjs`
|
|
21
|
+
}
|
|
22
|
+
]
|
|
23
|
+
const paths = [
|
|
24
|
+
projectConfigPath,
|
|
25
|
+
`${projectPath}/database/migrations`
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
if (this.args.testing) {
|
|
29
|
+
return {
|
|
30
|
+
fileMappings
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
for (const path of paths) {
|
|
35
|
+
if (await fileExists(path)) {
|
|
36
|
+
console.log(`Config dir already exists: ${path}`)
|
|
37
|
+
} else {
|
|
38
|
+
console.log(`Config dir doesn't exists: ${path}`)
|
|
39
|
+
await fs.mkdir(path, {recursive: true})
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
for (const fileMapping of fileMappings) {
|
|
44
|
+
if (!await fileExists(fileMapping.source)) {
|
|
45
|
+
throw new Error(`Template doesn't exist: ${fileMapping.source}`)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (await fileExists(fileMapping.target)) {
|
|
49
|
+
console.log(`File already exists: ${fileMapping.target}`)
|
|
50
|
+
} else {
|
|
51
|
+
console.log(`File doesnt exist: ${fileMapping.target}`)
|
|
52
|
+
await fs.copyFile(fileMapping.source, fileMapping.target)
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const dontLoadConfiguration = true
|
|
59
|
+
|
|
60
|
+
export {dontLoadConfiguration}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import BaseCommand from "../../base-command.mjs"
|
|
2
|
+
import TestFilesFinder from "./test-files-finder.mjs"
|
|
3
|
+
import TestRunner from "./test-runner.mjs"
|
|
4
|
+
|
|
5
|
+
export default class VelociousCliCommandsInit extends BaseCommand {
|
|
6
|
+
async execute() {
|
|
7
|
+
const testFilesFinder = new TestFilesFinder({directory: this.directory(), processArgs: this.processArgs})
|
|
8
|
+
const testFiles = await testFilesFinder.findTestFiles()
|
|
9
|
+
|
|
10
|
+
const testRunner = new TestRunner(testFiles)
|
|
11
|
+
|
|
12
|
+
await testRunner.run()
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import fs from "fs/promises"
|
|
2
|
+
|
|
3
|
+
// Incredibly complex class to find files in multiple simultanious running promises to do it as fast as possible.
|
|
4
|
+
export default class TestFilesFinder {
|
|
5
|
+
static IGNORED_NAMES = [".git", "node_modules"]
|
|
6
|
+
|
|
7
|
+
constructor({directory, processArgs}) {
|
|
8
|
+
this.directory = directory
|
|
9
|
+
this.foundFiles = []
|
|
10
|
+
this.findingCount = 0
|
|
11
|
+
this.findingPromises = {}
|
|
12
|
+
this.processArgs = processArgs
|
|
13
|
+
this.testArgs = this.processArgs.filter((processArg, index) => index != 0)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async findTestFiles() {
|
|
17
|
+
await this.withFindingCount(async () => {
|
|
18
|
+
await this.findTestFilesInDir(this.directory)
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
await this.waitForFindingPromises()
|
|
22
|
+
|
|
23
|
+
return this.foundFiles
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
findingPromisesLength = () => Object.keys(this.findingPromises).length
|
|
27
|
+
|
|
28
|
+
async waitForFindingPromises() {
|
|
29
|
+
while (this.findingPromisesLength() > 0) {
|
|
30
|
+
await this.waitForFindingPromisesIteration()
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async waitForFindingPromisesIteration() {
|
|
35
|
+
const unfinishedPromises = []
|
|
36
|
+
|
|
37
|
+
for (const findingPromiseId in this.findingPromises) {
|
|
38
|
+
const findingPromise = this.findingPromises[findingPromiseId]
|
|
39
|
+
|
|
40
|
+
unfinishedPromises.push(findingPromise)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
await Promise.all(unfinishedPromises)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
withFindingCount(callback) {
|
|
47
|
+
return new Promise((resolve) => {
|
|
48
|
+
const findingPromise = callback()
|
|
49
|
+
const findingCount = this.findingCount
|
|
50
|
+
|
|
51
|
+
this.findingCount += 1
|
|
52
|
+
this.findingPromises[findingCount] = findingPromise
|
|
53
|
+
|
|
54
|
+
findingPromise.finally(() => {
|
|
55
|
+
delete this.findingPromises[findingCount]
|
|
56
|
+
|
|
57
|
+
resolve()
|
|
58
|
+
})
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async findTestFilesInDir(dir) {
|
|
63
|
+
await this.withFindingCount(async () => {
|
|
64
|
+
const files = await fs.readdir(dir)
|
|
65
|
+
|
|
66
|
+
for (const file of files) {
|
|
67
|
+
if (TestFilesFinder.IGNORED_NAMES.includes(file)) {
|
|
68
|
+
continue
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const fullPath = `${dir}/${file}`
|
|
72
|
+
const localPath = fullPath.replace(`${this.directory}/`, "")
|
|
73
|
+
const isDir = (await fs.stat(fullPath)).isDirectory()
|
|
74
|
+
|
|
75
|
+
if (isDir) {
|
|
76
|
+
this.findTestFilesInDir(fullPath)
|
|
77
|
+
} else {
|
|
78
|
+
if (this.isFileMatchingRequirements(file, localPath, fullPath)) {
|
|
79
|
+
this.foundFiles.push(fullPath)
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
})
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
isFileMatchingRequirements(file, localPath, fullPath) {
|
|
87
|
+
if (this.testArgs.length > 0) {
|
|
88
|
+
for (const testArg of this.testArgs) {
|
|
89
|
+
if (testArg == localPath) {
|
|
90
|
+
return true
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
} else if (file.match(/-spec\.mjs/)) {
|
|
94
|
+
return true
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return false
|
|
98
|
+
}
|
|
99
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export default class TestRunner {
|
|
2
|
+
constructor(testFiles) {
|
|
3
|
+
this.testFiles = testFiles
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
async importTestFiles() {
|
|
7
|
+
for (const testFile of this.testFiles) {
|
|
8
|
+
const importTestFile = await import(testFile)
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
async run() {
|
|
13
|
+
await this.importTestFiles()
|
|
14
|
+
|
|
15
|
+
console.log({foundTestFiles: this.testFiles})
|
|
16
|
+
|
|
17
|
+
throw new Error("stub")
|
|
18
|
+
}
|
|
19
|
+
}
|
package/src/cli/index.mjs
CHANGED
|
@@ -1,22 +1,18 @@
|
|
|
1
|
+
import configurationResolver from "../configuration-resolver.mjs"
|
|
1
2
|
import {dirname} from "path"
|
|
2
3
|
import {fileURLToPath} from "url"
|
|
3
|
-
import
|
|
4
|
+
import fileExists from "../utils/file-exists.mjs"
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
return true
|
|
10
|
-
} catch (error) {
|
|
11
|
-
return false
|
|
6
|
+
export default class VelociousCli {
|
|
7
|
+
constructor(args = {}) {
|
|
8
|
+
this.args = args
|
|
12
9
|
}
|
|
13
|
-
}
|
|
14
10
|
|
|
15
|
-
|
|
16
|
-
async execute({args}) {
|
|
11
|
+
async execute() {
|
|
17
12
|
const __filename = fileURLToPath(`${import.meta.url}/../..`)
|
|
18
13
|
const __dirname = dirname(__filename)
|
|
19
|
-
const commandParts = args[0].split(":")
|
|
14
|
+
const commandParts = this.args.processArgs[0].split(":")
|
|
15
|
+
const filePaths = []
|
|
20
16
|
let filePath = `${__dirname}/src/cli/commands`
|
|
21
17
|
|
|
22
18
|
for (let commandPart of commandParts) {
|
|
@@ -26,14 +22,38 @@ export default class VelociousCli {
|
|
|
26
22
|
filePath += `/${commandPart}`
|
|
27
23
|
}
|
|
28
24
|
|
|
25
|
+
filePaths.push(`${filePath}/index.mjs`)
|
|
29
26
|
filePath += ".mjs"
|
|
27
|
+
filePaths.push(filePath)
|
|
28
|
+
|
|
29
|
+
let fileFound
|
|
30
|
+
|
|
31
|
+
for (const aFilePath of filePaths) {
|
|
32
|
+
if (await fileExists(aFilePath)) {
|
|
33
|
+
fileFound = aFilePath
|
|
34
|
+
break
|
|
35
|
+
}
|
|
36
|
+
}
|
|
30
37
|
|
|
31
|
-
if (!
|
|
38
|
+
if (!fileFound) throw new Error(`Unknown command: ${this.args.processArgs[0]} which should have been one of ${filePaths.join(", ")}`)
|
|
32
39
|
|
|
33
|
-
const commandClassImport = await import(
|
|
40
|
+
const commandClassImport = await import(fileFound)
|
|
34
41
|
const CommandClass = commandClassImport.default
|
|
35
|
-
const commandInstance = new CommandClass({args})
|
|
36
42
|
|
|
37
|
-
await
|
|
43
|
+
await this.loadConfiguration()
|
|
44
|
+
|
|
45
|
+
const commandInstance = new CommandClass(this.args)
|
|
46
|
+
|
|
47
|
+
if (commandInstance.initialize) {
|
|
48
|
+
await commandInstance.initialize()
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return await commandInstance.execute()
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async loadConfiguration() {
|
|
55
|
+
this.configuration = await configurationResolver({directory: this.args.directory})
|
|
56
|
+
this.configuration.setCurrent()
|
|
57
|
+
this.args.configuration = this.configuration
|
|
38
58
|
}
|
|
39
59
|
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import Configuration from "./configuration.mjs"
|
|
2
|
+
|
|
3
|
+
const configurationResolver = async (args) => {
|
|
4
|
+
if (global.velociousConfiguration) {
|
|
5
|
+
return global.velociousConfiguration
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
const directory = args.directory || process.cwd()
|
|
9
|
+
const configurationPath = `${directory}/src/config/configuration.mjs`
|
|
10
|
+
let configuration
|
|
11
|
+
|
|
12
|
+
try {
|
|
13
|
+
const configurationImport = await import(configurationPath)
|
|
14
|
+
|
|
15
|
+
configuration = configurationImport.default
|
|
16
|
+
} catch (error) {
|
|
17
|
+
// This might happen during an "init" CLI command where we copy a sample configuration file.
|
|
18
|
+
if (error.code != "ERR_MODULE_NOT_FOUND") throw error
|
|
19
|
+
|
|
20
|
+
configuration = new Configuration(args)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return configuration
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export default configurationResolver
|
package/src/configuration.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import DatabasePool from "./database/pool/index.mjs"
|
|
1
2
|
import {digg} from "diggerize"
|
|
2
3
|
|
|
3
4
|
export default class VelociousConfiguration {
|
|
@@ -7,9 +8,10 @@ export default class VelociousConfiguration {
|
|
|
7
8
|
return global.velociousConfiguration
|
|
8
9
|
}
|
|
9
10
|
|
|
10
|
-
constructor({debug, directory}) {
|
|
11
|
-
if (!directory)
|
|
11
|
+
constructor({database, debug, directory}) {
|
|
12
|
+
if (!directory) directory = process.cwd()
|
|
12
13
|
|
|
14
|
+
this.database = database
|
|
13
15
|
this.debug = debug
|
|
14
16
|
this.directory = directory
|
|
15
17
|
}
|
|
@@ -18,10 +20,30 @@ export default class VelociousConfiguration {
|
|
|
18
20
|
await this.initializeRoutes()
|
|
19
21
|
}
|
|
20
22
|
|
|
23
|
+
getDatabasePool() {
|
|
24
|
+
if (!this.isDatabasePoolInitialized()) this.initializeDatabasePool()
|
|
25
|
+
|
|
26
|
+
return this.databasePool
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
initializeDatabasePool() {
|
|
30
|
+
if (!this.database) throw new Error("No 'database' was given")
|
|
31
|
+
if (this.databasePool) throw new Error("DatabasePool has already been initialized")
|
|
32
|
+
|
|
33
|
+
this.databasePool = new DatabasePool({configuration: this})
|
|
34
|
+
this.databasePool.setCurrent()
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
isDatabasePoolInitialized = () => Boolean(this.databasePool)
|
|
38
|
+
|
|
21
39
|
async initializeRoutes() {
|
|
22
40
|
// Every client need to make their own routes because they probably can't be shared across different worker threads
|
|
23
41
|
const routesImport = await import(`${this.directory}/src/config/routes.mjs`)
|
|
24
42
|
|
|
25
43
|
this.routes = digg(routesImport, "default", "routes")
|
|
26
44
|
}
|
|
45
|
+
|
|
46
|
+
setCurrent() {
|
|
47
|
+
global.velociousConfiguration = this
|
|
48
|
+
}
|
|
27
49
|
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import Base from "../base.mjs"
|
|
2
2
|
import connectConnection from "./connect-connection.mjs"
|
|
3
|
+
import CreateDatabase from "./sql/create-database.mjs"
|
|
4
|
+
import CreateTable from "./sql/create-table.mjs"
|
|
3
5
|
import Delete from "./sql/delete.mjs"
|
|
4
6
|
import {digg} from "diggerize"
|
|
5
7
|
import Insert from "./sql/insert.mjs"
|
|
@@ -35,6 +37,25 @@ export default class VelociousDatabaseDriversMysql extends Base{
|
|
|
35
37
|
return connectArgs
|
|
36
38
|
}
|
|
37
39
|
|
|
40
|
+
async close() {
|
|
41
|
+
await this.connection.end()
|
|
42
|
+
this.connection = undefined
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
createDatabaseSql(databaseName, args) {
|
|
46
|
+
const createArgs = Object.assign({databaseName, driver: this}, args)
|
|
47
|
+
const createDatabase = new CreateDatabase(createArgs)
|
|
48
|
+
|
|
49
|
+
return createDatabase.toSql()
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
createTableSql(tableData) {
|
|
53
|
+
const createArgs = Object.assign({tableData, driver: this})
|
|
54
|
+
const createTable = new CreateTable(createArgs)
|
|
55
|
+
|
|
56
|
+
return createTable.toSql()
|
|
57
|
+
}
|
|
58
|
+
|
|
38
59
|
async query(sql) {
|
|
39
60
|
return await query(this.connection, sql)
|
|
40
61
|
}
|
|
@@ -49,6 +70,10 @@ export default class VelociousDatabaseDriversMysql extends Base{
|
|
|
49
70
|
return this.connection.escape(string)
|
|
50
71
|
}
|
|
51
72
|
|
|
73
|
+
quoteColumn(string) {
|
|
74
|
+
return `\`${string}\``
|
|
75
|
+
}
|
|
76
|
+
|
|
52
77
|
deleteSql({tableName, conditions}) {
|
|
53
78
|
const deleteInstruction = new Delete({conditions, driver: this, tableName})
|
|
54
79
|
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import QueryParserOptions from "../../query-parser/options.mjs"
|
|
2
|
+
|
|
3
|
+
export default class VelociousDatabaseDriversMysqlOptions extends QueryParserOptions {
|
|
4
|
+
constructor(options) {
|
|
5
|
+
options.columnQuote = "`"
|
|
6
|
+
options.stringQuote = "'"
|
|
7
|
+
options.tableQuote = "`"
|
|
8
|
+
|
|
9
|
+
super(options)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
quote(string) {
|
|
13
|
+
if (!this.driver) throw new Error("Driver not set")
|
|
14
|
+
|
|
15
|
+
return this.driver.quote(string)
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import {digs} from "diggerize"
|
|
2
|
+
import FromParser from "../../query-parser/from-parser.mjs"
|
|
3
|
+
import JoinsParser from "../../query-parser/joins-parser.mjs"
|
|
4
|
+
import SelectParser from "../../query-parser/select-parser.mjs"
|
|
5
|
+
|
|
6
|
+
export default class VelociousDatabaseConnectionDriversMysqlQueryParser {
|
|
7
|
+
constructor({pretty, query}) {
|
|
8
|
+
if (!query) throw new Error("No query given")
|
|
9
|
+
|
|
10
|
+
this.pretty = pretty
|
|
11
|
+
this.query = query
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
toSql() {
|
|
15
|
+
const {pretty, query} = digs(this, "pretty", "query")
|
|
16
|
+
|
|
17
|
+
let sql = ""
|
|
18
|
+
|
|
19
|
+
sql += new SelectParser({pretty, query}).toSql()
|
|
20
|
+
sql += new FromParser({pretty, query}).toSql()
|
|
21
|
+
sql += new JoinsParser({pretty, query}).toSql()
|
|
22
|
+
|
|
23
|
+
return sql
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import DeleteBase from "../../../query/delete-base.mjs"
|
|
2
|
+
|
|
3
|
+
export default class VelociousDatabaseConnectionDriversMysqlSqlDelete extends DeleteBase {
|
|
4
|
+
toSql() {
|
|
5
|
+
let sql = `DELETE FROM ${this.getOptions().quoteTableName(this.tableName)} WHERE `
|
|
6
|
+
let count = 0
|
|
7
|
+
|
|
8
|
+
for (let columnName in this.conditions) {
|
|
9
|
+
if (count > 0) sql += " AND "
|
|
10
|
+
|
|
11
|
+
sql += this.getOptions().quoteColumnName(columnName)
|
|
12
|
+
sql += " = "
|
|
13
|
+
sql += this.getOptions().quote(this.conditions[columnName])
|
|
14
|
+
count++
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return sql
|
|
18
|
+
}
|
|
19
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import InsertBase from "../../../query/insert-base.mjs"
|
|
2
|
+
|
|
3
|
+
export default class VelociousDatabaseConnectionDriversMysqlSqlInsert extends InsertBase {
|
|
4
|
+
toSql() {
|
|
5
|
+
let sql = `INSERT INTO ${this.getOptions().quoteTableName(this.tableName)} (`
|
|
6
|
+
let count = 0
|
|
7
|
+
|
|
8
|
+
for (let columnName in this.data) {
|
|
9
|
+
if (count > 0) sql += ", "
|
|
10
|
+
|
|
11
|
+
sql += this.getOptions().quoteColumnName(columnName)
|
|
12
|
+
count++
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
sql += ") VALUES ("
|
|
16
|
+
count = 0
|
|
17
|
+
|
|
18
|
+
for (let columnName in this.data) {
|
|
19
|
+
if (count > 0) sql += ", "
|
|
20
|
+
|
|
21
|
+
sql += this.getOptions().quote(this.data[columnName])
|
|
22
|
+
count++
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
sql += ")"
|
|
26
|
+
|
|
27
|
+
return sql
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import UpdateBase from "../../../query/update-base.mjs"
|
|
2
|
+
|
|
3
|
+
export default class VelociousDatabaseConnectionDriversMysqlSqlUpdate extends UpdateBase {
|
|
4
|
+
toSql() {
|
|
5
|
+
let sql = `UPDATE ${this.getOptions().quoteTableName(this.tableName)} SET `
|
|
6
|
+
let count = 0
|
|
7
|
+
|
|
8
|
+
for (let columnName in this.data) {
|
|
9
|
+
if (count > 0) sql += ", "
|
|
10
|
+
|
|
11
|
+
sql += this.getOptions().quoteColumnName(columnName)
|
|
12
|
+
sql += " = "
|
|
13
|
+
sql += this.getOptions().quote(this.data[columnName])
|
|
14
|
+
count++
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
sql += " WHERE "
|
|
18
|
+
count = 0
|
|
19
|
+
|
|
20
|
+
for (let columnName in this.conditions) {
|
|
21
|
+
if (count > 0) sql += " AND "
|
|
22
|
+
|
|
23
|
+
sql += this.getOptions().quoteColumnName(columnName)
|
|
24
|
+
sql += " = "
|
|
25
|
+
sql += this.getOptions().quote(this.conditions[columnName])
|
|
26
|
+
count++
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return sql
|
|
30
|
+
}
|
|
31
|
+
}
|