velocious 1.0.3 → 1.0.5

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.
Files changed (57) hide show
  1. package/.github/dependabot.yml +1 -1
  2. package/README.md +7 -1
  3. package/bin/velocious.mjs +2 -2
  4. package/index.mjs +1 -21
  5. package/package.json +3 -1
  6. package/peak_flow.yml +1 -1
  7. package/spec/cli/commands/db/create-spec.mjs +1 -1
  8. package/spec/database/connection/drivers/mysql/query-parser-spec.mjs +6 -5
  9. package/spec/database/drivers/mysql/connection-spec.mjs +2 -3
  10. package/spec/dummy/index.mjs +6 -4
  11. package/spec/dummy/src/config/configuration.example.mjs +5 -3
  12. package/spec/dummy/src/config/configuration.peakflow.mjs +5 -3
  13. package/spec/dummy/src/models/task.mjs +2 -2
  14. package/spec/http-server/client-spec.mjs +5 -0
  15. package/src/application.mjs +5 -0
  16. package/src/cli/base-command.mjs +1 -1
  17. package/src/cli/commands/db/migrate.mjs +52 -5
  18. package/src/cli/commands/destroy/migration.mjs +1 -1
  19. package/src/cli/commands/generate/migration.mjs +1 -1
  20. package/src/cli/commands/generate/model.mjs +36 -0
  21. package/src/cli/commands/init.mjs +1 -1
  22. package/src/cli/commands/server.mjs +15 -0
  23. package/src/cli/index.mjs +1 -0
  24. package/src/configuration-resolver.mjs +2 -2
  25. package/src/configuration.mjs +35 -19
  26. package/src/controller.mjs +1 -1
  27. package/src/database/drivers/base.mjs +41 -0
  28. package/src/database/drivers/sqlite/base.mjs +36 -0
  29. package/src/database/drivers/sqlite/index.native.mjs +54 -0
  30. package/src/database/drivers/sqlite/index.web.mjs +45 -0
  31. package/src/database/drivers/sqlite/query.web.mjs +9 -0
  32. package/src/database/drivers/sqlite/table.mjs +9 -0
  33. package/src/database/migrate-from-require-context.mjs +62 -0
  34. package/src/database/migration/index.mjs +1 -1
  35. package/src/database/migrator.mjs +73 -0
  36. package/src/database/pool/async-tracked-multi-connection.mjs +81 -0
  37. package/src/database/pool/base.mjs +60 -0
  38. package/src/database/pool/single-multi-use.mjs +40 -0
  39. package/src/database/query/create-database-base.mjs +3 -1
  40. package/src/database/query/insert-base.mjs +4 -0
  41. package/src/database/query-parser/options.mjs +2 -2
  42. package/src/database/record/index.mjs +2 -2
  43. package/src/http-server/worker-handler/index.mjs +2 -1
  44. package/src/http-server/worker-handler/worker-thread.mjs +0 -1
  45. package/src/routes/app-routes.mjs +10 -0
  46. package/src/routes/resolver.mjs +1 -1
  47. package/src/templates/configuration.mjs +6 -4
  48. package/src/templates/generate-migration.mjs +2 -2
  49. package/src/templates/generate-model.mjs +4 -0
  50. package/src/templates/routes.mjs +1 -1
  51. package/src/database/drivers/index.mjs +0 -5
  52. package/src/database/drivers/sqlite/sql/create-database.mjs +0 -4
  53. package/src/database/drivers/sqlite-expo/index.mjs +0 -100
  54. package/src/database/index.mjs +0 -15
  55. package/src/database/migrator/index.mjs +0 -15
  56. package/src/database/pool/index.mjs +0 -112
  57. /package/src/database/drivers/{sqlite-expo/query.mjs → sqlite/query.native.mjs} +0 -0
@@ -5,5 +5,5 @@ updates:
5
5
  schedule:
6
6
  interval: weekly
7
7
  time: "01:00"
8
- timezone: Europe/Copenhagen
8
+ timezone: Europe/Berlin
9
9
  open-pull-requests-limit: 99
package/README.md CHANGED
@@ -22,13 +22,19 @@ npx velocious init
22
22
  # Migrations
23
23
 
24
24
  ```bash
25
- npx velocious db:g:migration create_tasks
25
+ npx velocious g:migration create_tasks
26
26
  ```
27
27
 
28
28
  ```bash
29
29
  npx velocious db:migrate
30
30
  ```
31
31
 
32
+ # Models
33
+
34
+ ```bash
35
+ npx velocious g:model Option
36
+ ```
37
+
32
38
  # Testing
33
39
 
34
40
  ```bash
package/bin/velocious.mjs CHANGED
@@ -1,8 +1,8 @@
1
- #!/usr/bin/node
1
+ #!/usr/bin/env node
2
2
 
3
3
  import Cli from "../src/cli/index.mjs"
4
4
 
5
5
  const processArgs = process.argv.slice(2)
6
6
  const cli = new Cli({processArgs})
7
7
 
8
- cli.execute()
8
+ await cli.execute()
package/index.mjs CHANGED
@@ -1,21 +1 @@
1
- import Application from "./src/application.mjs"
2
- import Cli from "./src/cli/index.mjs"
3
- import Configuration from "./src/configuration.mjs"
4
- import Controller from "./src/controller.mjs"
5
- import Database from "./src/database/index.mjs"
6
- import DatabasePool from "./src/database/pool/index.mjs"
7
- import HttpServer from "./src/http-server/index.mjs"
8
- import Routes from "./src/routes/index.mjs"
9
- import Spec from "./src/spec/index.mjs"
10
-
11
- export {
12
- Application,
13
- Cli,
14
- Configuration,
15
- Controller,
16
- Database,
17
- DatabasePool,
18
- HttpServer,
19
- Routes,
20
- Spec
21
- }
1
+ export {}
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "velocious": "bin/velocious.mjs"
4
4
  },
5
5
  "name": "velocious",
6
- "version": "1.0.3",
6
+ "version": "1.0.5",
7
7
  "main": "index.mjs",
8
8
  "scripts": {
9
9
  "test": "jasmine",
@@ -32,6 +32,8 @@
32
32
  "escape-string-regexp": "^1.0.5",
33
33
  "incorporator": "^1.0.2",
34
34
  "inflection": "^3.0.0",
35
+ "sql-escape-string": "^1.1.0",
36
+ "sql.js": "^1.12.0",
35
37
  "strftime": "^0.10.2"
36
38
  }
37
39
  }
package/peak_flow.yml CHANGED
@@ -1,6 +1,6 @@
1
1
  before_install:
2
2
  - sudo mkdir -p /etc/apt/keyrings
3
- - curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
3
+ - curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --no-tty --batch --yes --dearmor -o /etc/apt/keyrings/nodesource.gpg
4
4
  - echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list
5
5
  - sudo apt-get update
6
6
  - sudo apt-get install -y nodejs
@@ -14,7 +14,7 @@ describe("Cli - Commands - db:create", () => {
14
14
  [
15
15
  {
16
16
  databaseName: 'velocious_test',
17
- sql: 'CREATE DATABASE IF NOT EXISTS velocious_test'
17
+ sql: 'CREATE DATABASE IF NOT EXISTS `velocious_test`'
18
18
  },
19
19
  {
20
20
  createSchemaMigrationsTableSql: 'CREATE TABLE IF NOT EXISTS schema_migrations (`version` varchar(255) PRIMARY KEY)'
@@ -1,4 +1,5 @@
1
- import Database from "../../../../../src/database/index.mjs"
1
+ import DatabaseHandler from "../../../../../src/database/handler.mjs"
2
+ import DatabaseQuery from "../../../../../src/database/query/index.mjs"
2
3
  import MysqlQueryParser from "../../../../../src/database/drivers/mysql/query-parser.mjs"
3
4
 
4
5
  import FromTable from "../../../../../src/database/query/from-table.mjs"
@@ -10,8 +11,8 @@ const mysqlDriver = new MysqlDriverClass()
10
11
 
11
12
  describe("database - connection - drivers - mysql - query parser", () => {
12
13
  it("generates sql with selects, joins and orders", () => {
13
- const handler = new Database.Handler()
14
- const query = new Database.Query({driver: mysqlDriver, handler})
14
+ const handler = new DatabaseHandler()
15
+ const query = new DatabaseQuery({driver: mysqlDriver, handler})
15
16
  .select(["tasks.id", "tasks.name"])
16
17
  .from("tasks")
17
18
  .joins("LEFT JOIN projects ON projects.id = tasks.project_id")
@@ -22,8 +23,8 @@ describe("database - connection - drivers - mysql - query parser", () => {
22
23
  })
23
24
 
24
25
  it("generates sql with selects, joins and orders", () => {
25
- const handler = new Database.Handler()
26
- const query = new Database.Query({driver: mysqlDriver, handler})
26
+ const handler = new DatabaseHandler()
27
+ const query = new DatabaseQuery({driver: mysqlDriver, handler})
27
28
  .select([
28
29
  new SelectTableAndColumn({tableName: "tasks", columnName: "id"}),
29
30
  new SelectTableAndColumn({tableName: "tasks", columnName: "name"})
@@ -1,13 +1,12 @@
1
- import Database from "../../../../src/database/index.mjs"
1
+ import DatabaseDriversMysql from "../../../../src/database/drivers/mysql/index.mjs"
2
2
  import configuration from "../../../dummy/src/config/configuration.mjs"
3
3
  import {digg} from "diggerize"
4
4
 
5
5
  const mysqlConfig = digg(configuration, "database", "default", "master")
6
- const Mysql = Database.Drivers.Mysql
7
6
 
8
7
  describe("Database - Drivers - Mysql - Connection", () => {
9
8
  it("connects", async () => {
10
- const mysql = new Mysql(mysqlConfig)
9
+ const mysql = new DatabaseDriversMysql(mysqlConfig)
11
10
 
12
11
  await mysql.connect()
13
12
 
@@ -3,11 +3,11 @@ import dummyConfiguration from "./src/config/configuration.mjs"
3
3
 
4
4
  export default class Dummy {
5
5
  static current() {
6
- if (!global.velociousDummy) {
7
- global.velociousDummy = new Dummy()
6
+ if (!this.velociousDummy) {
7
+ this.velociousDummy = new Dummy()
8
8
  }
9
9
 
10
- return global.velociousDummy
10
+ return this.velociousDummy
11
11
  }
12
12
 
13
13
  static async prepare() {
@@ -37,7 +37,9 @@ export default class Dummy {
37
37
  }
38
38
 
39
39
  async start() {
40
- if (!dummyConfiguration.isDatabasePoolInitialized()) await dummyConfiguration.initializeDatabasePool()
40
+ if (!dummyConfiguration.isDatabasePoolInitialized()) {
41
+ await dummyConfiguration.initializeDatabasePool()
42
+ }
41
43
 
42
44
  this.application = new Application({
43
45
  configuration: dummyConfiguration,
@@ -1,10 +1,14 @@
1
+ import AsyncTrackedMultiConnection from "../../../../src/database/pool/async-tracked-multi-connection.mjs"
1
2
  import Configuration from "../../../../src/configuration.mjs"
2
3
  import dummyDirectory from "../../dummy-directory.mjs"
4
+ import MysqlDriver from "../../../../src/database/drivers/mysql/index.mjs"
3
5
 
4
- const configuration = new Configuration({
6
+ export default new Configuration({
5
7
  database: {
6
8
  default: {
7
9
  master: {
10
+ driver: MysqlDriver,
11
+ poolType: AsyncTrackedMultiConnection,
8
12
  type: "mysql",
9
13
  host: "mariadb",
10
14
  username: "username",
@@ -15,5 +19,3 @@ const configuration = new Configuration({
15
19
  },
16
20
  directory: dummyDirectory()
17
21
  })
18
-
19
- export default configuration
@@ -1,10 +1,14 @@
1
+ import AsyncTrackedMultiConnection from "../../../../src/database/pool/async-tracked-multi-connection.mjs"
1
2
  import Configuration from "../../../../src/configuration.mjs"
2
3
  import dummyDirectory from "../../dummy-directory.mjs"
4
+ import MysqlDriver from "../../../../src/database/drivers/mysql/index.mjs"
3
5
 
4
- const configuration = new Configuration({
6
+ export default new Configuration({
5
7
  database: {
6
8
  default: {
7
9
  master: {
10
+ driver: MysqlDriver,
11
+ poolType: AsyncTrackedMultiConnection,
8
12
  type: "mysql",
9
13
  host: "mariadb",
10
14
  username: "peakflow",
@@ -16,5 +20,3 @@ const configuration = new Configuration({
16
20
  },
17
21
  directory: dummyDirectory()
18
22
  })
19
-
20
- export default configuration
@@ -1,4 +1,4 @@
1
- import Database from "../../../../src/database/index.mjs"
1
+ import DatabaseRecord from "../../../../src/database/record/index.mjs"
2
2
 
3
- export default class Task extends Database.Record {
3
+ export default class Task extends DatabaseRecord {
4
4
  }
@@ -1,3 +1,4 @@
1
+ import AppRoutes from "../../src/routes/app-routes.mjs"
1
2
  import Client from "../../src/http-server/client/index.mjs"
2
3
  import {digg} from "diggerize"
3
4
  import dummyConfiguration from "../dummy/src/config/configuration.mjs"
@@ -6,6 +7,10 @@ describe("http server - client", () => {
6
7
  it("spawns a request for each that it is fed", async () => {
7
8
  await dummyConfiguration.initialize()
8
9
 
10
+ const routes = await AppRoutes.getRoutes(dummyConfiguration)
11
+
12
+ dummyConfiguration.setRoutes(routes)
13
+
9
14
  const client = new Client({
10
15
  clientCount: 0,
11
16
  configuration: dummyConfiguration
@@ -1,3 +1,4 @@
1
+ import AppRoutes from "../src/routes/app-routes.mjs"
1
2
  import {digs} from "diggerize"
2
3
  import logger from "./logger.mjs"
3
4
  import HttpServer from "./http-server/index.mjs"
@@ -9,8 +10,12 @@ export default class VelociousApplication {
9
10
  }
10
11
 
11
12
  async initialize() {
13
+ const routes = await AppRoutes.getRoutes(this.configuration)
14
+
12
15
  await this.configuration.initialize()
13
16
 
17
+ this.configuration.setRoutes(routes)
18
+
14
19
  if (!this.configuration.isDatabasePoolInitialized()) {
15
20
  await this.configuration.initializeDatabasePool()
16
21
  }
@@ -7,5 +7,5 @@ export default class VelociousCliBaseCommand {
7
7
  this.processArgs = args.processArgs
8
8
  }
9
9
 
10
- directory = () => digg(this, "configuration", "directory")
10
+ directory = () => digg(this, "configuration").getDirectory()
11
11
  }
@@ -5,7 +5,7 @@ import inflection from "inflection"
5
5
 
6
6
  export default class DbMigrate extends BaseCommand {
7
7
  async execute() {
8
- const projectPath = digg(this.configuration, "directory")
8
+ const projectPath = this.configuration.getDirectory()
9
9
  const migrationsPath = `${projectPath}/src/database/migrations`
10
10
  let files = await fs.readdir(migrationsPath)
11
11
 
@@ -34,12 +34,59 @@ export default class DbMigrate extends BaseCommand {
34
34
  }
35
35
  }
36
36
 
37
- async runMigrationFile(migration) {
38
- if (!this.configuration.isDatabasePoolInitialized()) {
39
- await this.configuration.initializeDatabasePool()
37
+ async executeRequireContext(requireContext) {
38
+ const migrationFiles = requireContext.keys()
39
+
40
+ files = migrationFiles
41
+ .map((file) => {
42
+ const match = file.match(/^(\d{14})-(.+)\.mjs$/)
43
+
44
+ if (!match) return null
45
+
46
+ const date = parseInt(match[1])
47
+ const migrationName = match[2]
48
+ const migrationClassName = inflection.camelize(migrationName)
49
+
50
+ return {
51
+ file,
52
+ fullPath: `${migrationsPath}/${file}`,
53
+ date,
54
+ migrationClassName
55
+ }
56
+ })
57
+ .filter((migration) => Boolean(migration))
58
+ .sort((migration1, migration2) => migration1.date - migration2.date)
59
+
60
+ for (const migration of files) {
61
+ await this.runMigrationFileFromRequireContext(migration, requireContext)
40
62
  }
63
+ }
64
+
65
+ async runMigrationFileFromRequireContext(migration, requireContext) {
66
+ if (!this.configuration) throw new Error("No configuration set")
67
+ if (!this.configuration.isDatabasePoolInitialized()) await this.configuration.initializeDatabasePool()
68
+
69
+ await this.configuration.getDatabasePool().withConnection(async () => {
70
+ const MigrationClass = requireContext(migration.file).default
71
+ const migrationInstance = new MigrationClass({
72
+ configuration: this.configuration
73
+ })
74
+
75
+ if (migrationInstance.change) {
76
+ await migrationInstance.change()
77
+ } else if (migrationInstance.up) {
78
+ await migrationInstance.up()
79
+ } else {
80
+ throw new Error(`'change' or 'up' didn't exist on migration: ${migration.file}`)
81
+ }
82
+ })
83
+ }
84
+
85
+ async runMigrationFile(migration) {
86
+ if (!this.configuration) throw new Error("No configuration set")
87
+ if (!this.configuration.isDatabasePoolInitialized()) await this.configuration.initializeDatabasePool()
41
88
 
42
- await this.configuration.databasePool.withConnection(async () => {
89
+ await this.configuration.getDatabasePool().withConnection(async () => {
43
90
  const migrationImport = await import(migration.fullPath)
44
91
  const MigrationClass = migrationImport.default
45
92
  const migrationInstance = new MigrationClass({
@@ -4,7 +4,7 @@ import fs from "node:fs/promises"
4
4
  export default class DbDestroyMigration extends BaseCommand {
5
5
  async execute() {
6
6
  const migrationName = this.processArgs[1]
7
- const migrationDir = `${this.configuration.directory}/src/database/migrations`
7
+ const migrationDir = `${this.configuration.getDirectory()}/src/database/migrations`
8
8
  const migrationFiles = await fs.readdir(migrationDir)
9
9
  const destroyed = []
10
10
 
@@ -22,7 +22,7 @@ export default class DbGenerateMigration extends BaseCommand {
22
22
  const migrationPath = `${migrationDir}/${migrationFileName}`
23
23
 
24
24
  if (this.args.testing) {
25
- return {date, migrationContent, migrationName, migrationNameCamelized, migrationNumber, migrationPath }
25
+ return {date, migrationContent, migrationName, migrationNameCamelized, migrationNumber, migrationPath}
26
26
  } else {
27
27
  if (!await fileExists(migrationDir)) {
28
28
  await fs.mkdir(migrationDir, {recursive: true})
@@ -0,0 +1,36 @@
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
+
8
+ export default class DbGenerateModel extends BaseCommand {
9
+ async execute() {
10
+ const modelName = this.processArgs[1]
11
+ const modelNameCamelized = inflection.camelize(modelName.replaceAll("-", "_"))
12
+ const date = new Date()
13
+ const modelFileName = `${inflection.dasherize(inflection.underscore(modelName))}.mjs`
14
+ const __filename = fileURLToPath(`${import.meta.url}/../../..`)
15
+ const __dirname = dirname(__filename)
16
+ const templateFilePath = `${__dirname}/templates/generate-model.mjs`
17
+ const modelContentBuffer = await fs.readFile(templateFilePath)
18
+ const modelContent = modelContentBuffer.toString().replaceAll("__MODEL_NAME__", modelNameCamelized)
19
+ const modelsDir = `${process.cwd()}/src/models`
20
+ const modelPath = `${modelsDir}/${modelFileName}`
21
+
22
+ if (await fileExists(modelPath)) throw new Error(`Model file already exists: ${modelPath}`)
23
+
24
+ if (this.args.testing) {
25
+ return {date, modelContent, modelName, modelNameCamelized, modelPath}
26
+ } else {
27
+ if (!await fileExists(modelsDir)) {
28
+ await fs.mkdir(modelsDir, {recursive: true})
29
+ }
30
+
31
+ await fs.writeFile(modelPath, modelContent)
32
+
33
+ console.log(`create src/models/${modelFileName}`)
34
+ }
35
+ }
36
+ }
@@ -8,7 +8,7 @@ export default class VelociousCliCommandsInit extends BaseCommand {
8
8
  async execute() {
9
9
  const __filename = fileURLToPath(`${import.meta.url}/../../..`)
10
10
  const velocipusPath = dirname(__filename)
11
- const projectPath = this.configuration?.directory || process.cwd()
11
+ const projectPath = this.configuration?.getDirectory() || process.cwd()
12
12
  const projectConfigPath = `${projectPath}/src/config`
13
13
  const fileMappings = [
14
14
  {
@@ -0,0 +1,15 @@
1
+ import BaseCommand from "../base-command.mjs"
2
+
3
+ export default class DbCreate extends BaseCommand{
4
+ async execute() {
5
+ this.databasePool = this.configuration.getDatabasePool()
6
+ this.newConfiguration = Object.assign({}, this.databasePool.getConfiguration())
7
+
8
+ if (this.args.testing) this.result = []
9
+
10
+ this.databaseConnection = await this.databasePool.spawnConnectionWithConfiguration(this.newConfiguration)
11
+ await this.databaseConnection.connect()
12
+
13
+ throw new Error("stub")
14
+ }
15
+ }
package/src/cli/index.mjs CHANGED
@@ -18,6 +18,7 @@ export default class VelociousCli {
18
18
  for (let commandPart of commandParts) {
19
19
  if (commandPart == "d") commandPart = "destroy"
20
20
  if (commandPart == "g") commandPart = "generate"
21
+ if (commandPart == "s") commandPart = "server"
21
22
 
22
23
  filePath += `/${commandPart}`
23
24
  }
@@ -1,8 +1,8 @@
1
1
  import Configuration from "./configuration.mjs"
2
2
 
3
3
  const configurationResolver = async (args) => {
4
- if (global.velociousConfiguration) {
5
- return global.velociousConfiguration
4
+ if (Configuration.current(false)) {
5
+ return Configuration.current()
6
6
  }
7
7
 
8
8
  const directory = args.directory || process.cwd()
@@ -1,49 +1,65 @@
1
- import DatabasePool from "./database/pool/index.mjs"
2
1
  import {digg} from "diggerize"
3
2
 
4
3
  export default class VelociousConfiguration {
5
- static current() {
6
- if (!global.velociousConfiguration) throw new Error("A Velocious configuration hasn't been set")
4
+ static current(throwError = true) {
5
+ if (!this.velociousConfiguration && throwError) throw new Error("A Velocious configuration hasn't been set")
7
6
 
8
- return global.velociousConfiguration
7
+ return this.velociousConfiguration
9
8
  }
10
9
 
11
10
  constructor({database, debug, directory}) {
12
- if (!directory) directory = process.cwd()
13
-
14
11
  this.database = database
15
12
  this.debug = debug
16
- this.directory = directory
17
- }
18
-
19
- async initialize() {
20
- await this.initializeRoutes()
13
+ this._directory = directory
21
14
  }
22
15
 
23
16
  getDatabasePool() {
24
- if (!this.isDatabasePoolInitialized()) this.initializeDatabasePool()
17
+ if (!this.isDatabasePoolInitialized()) {
18
+ this.initializeDatabasePool()
19
+ }
25
20
 
26
21
  return this.databasePool
27
22
  }
28
23
 
24
+ getDatabasePoolType = () => {
25
+ const poolTypeClass = digg(this, "database", "default", "master", "poolType")
26
+
27
+ if (!poolTypeClass) {
28
+ throw new Error("No poolType given in database configuration")
29
+ }
30
+
31
+ return poolTypeClass
32
+ }
33
+
34
+ getDirectory = () => {
35
+ if (!this._directory) {
36
+ this._directory = process.cwd()
37
+ }
38
+
39
+ return this._directory
40
+ }
41
+
29
42
  initializeDatabasePool() {
30
43
  if (!this.database) throw new Error("No 'database' was given")
31
44
  if (this.databasePool) throw new Error("DatabasePool has already been initialized")
32
45
 
33
- this.databasePool = new DatabasePool({configuration: this})
46
+ const PoolType = this.getDatabasePoolType()
47
+
48
+ this.databasePool = new PoolType({configuration: this})
34
49
  this.databasePool.setCurrent()
35
50
  }
36
51
 
37
52
  isDatabasePoolInitialized = () => Boolean(this.databasePool)
38
53
 
39
- async initializeRoutes() {
40
- // Every client need to make their own routes because they probably can't be shared across different worker threads
41
- const routesImport = await import(`${this.directory}/src/config/routes.mjs`)
42
-
43
- this.routes = digg(routesImport, "default", "routes")
54
+ initialize() {
55
+ // Doesn't currently do anything.
44
56
  }
45
57
 
46
58
  setCurrent() {
47
- global.velociousConfiguration = this
59
+ this.constructor.velociousConfiguration = this
60
+ }
61
+
62
+ setRoutes(newRoutes) {
63
+ this.routes = newRoutes
48
64
  }
49
65
  }
@@ -36,7 +36,7 @@ export default class VelociousController {
36
36
  return new Promise((resolve, reject) => {
37
37
  const actionName = digg(this, "_params", "action")
38
38
  const controllerName = digg(this, "_params", "controller")
39
- const directory = digg(this, "_configuration", "directory")
39
+ const directory = digg(this, "_configuration").getDirectory()
40
40
  const viewPath = `${directory}/src/routes/${controllerName}/${actionName}.ejs`
41
41
  const {viewParams} = digs(this, "viewParams")
42
42
 
@@ -1,8 +1,23 @@
1
+ import Query from "../query/index.mjs"
2
+ import Handler from "../handler.mjs"
3
+
1
4
  export default class VelociousDatabaseDriversBase {
2
5
  constructor(args) {
3
6
  this._args = args
4
7
  }
5
8
 
9
+ async createTable(...args) {
10
+ const sql = this.createTableSql(...args)
11
+
12
+ await this.query(sql)
13
+ }
14
+
15
+ async delete(...args) {
16
+ const sql = this.deleteSql(...args)
17
+
18
+ await this.query(sql)
19
+ }
20
+
6
21
  getArgs() {
7
22
  return this._args
8
23
  }
@@ -11,7 +26,33 @@ export default class VelociousDatabaseDriversBase {
11
26
  return this.idSeq
12
27
  }
13
28
 
29
+ async insert(...args) {
30
+ const sql = this.insertSql(...args)
31
+
32
+ await this.query(sql)
33
+ }
34
+
35
+ async select(tableName) {
36
+ const handler = new Handler()
37
+ const query = new Query({
38
+ driver: this,
39
+ handler
40
+ })
41
+
42
+ const sql = query
43
+ .from(tableName)
44
+ .toSql()
45
+
46
+ return await this.query(sql)
47
+ }
48
+
14
49
  setIdSeq(id) {
15
50
  this.idSeq = id
16
51
  }
52
+
53
+ async update(...args) {
54
+ const sql = this.updateSql(...args)
55
+
56
+ await this.query(sql)
57
+ }
17
58
  }
@@ -0,0 +1,36 @@
1
+ import Base from "../base.mjs"
2
+ import CreateTable from "../sqlite/sql/create-table.mjs"
3
+ import Delete from "../sqlite/sql/delete.mjs"
4
+ import Insert from "../sqlite/sql/insert.mjs"
5
+ import QueryParser from "../sqlite/query-parser.mjs"
6
+ import Table from "./table"
7
+ import Update from "../sqlite/sql/update.mjs"
8
+
9
+ export default class VelociousDatabaseDriversSqliteBase extends Base {
10
+ createTableSql(tableData) {
11
+ const createArgs = Object.assign({tableData, driver: this})
12
+ const createTable = new CreateTable(createArgs)
13
+
14
+ return createTable.toSql()
15
+ }
16
+
17
+ deleteSql = ({tableName, conditions}) => new Delete({conditions, driver: this, tableName}).toSql()
18
+ insertSql = ({tableName, data}) => new Insert({driver: this, tableName, data}).toSql()
19
+
20
+ async getTables() {
21
+ const result = await this.query("SELECT name FROM sqlite_master WHERE type = 'table'")
22
+ const tables = []
23
+
24
+ for (const row of result) {
25
+ const table = new Table(row)
26
+
27
+ tables.push(table)
28
+ }
29
+
30
+ return tables
31
+ }
32
+
33
+ queryToSql = (query) => new QueryParser({query}).toSql()
34
+ quoteColumn = (string) => `\`${string}\``
35
+ updateSql = ({conditions, data, tableName}) => new Update({conditions, data, driver: this, tableName}).toSql()
36
+ }