velocious 1.0.5 → 1.0.7

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 (173) hide show
  1. package/bin/{velocious.mjs → velocious.js} +1 -1
  2. package/package.json +7 -5
  3. package/peak_flow.yml +1 -1
  4. package/spec/cli/commands/db/{create-spec.mjs → create-spec.js} +3 -3
  5. package/spec/cli/commands/db/{migrate-spec.mjs → migrate-spec.js} +4 -2
  6. package/spec/cli/commands/destroy/{migration-spec.mjs → migration-spec.js} +2 -2
  7. package/spec/cli/commands/generate/{migration-spec.mjs → migration-spec.js} +3 -3
  8. package/spec/cli/commands/{init-spec.mjs → init-spec.js} +6 -6
  9. package/spec/cli/commands/test/{test-files-finder-spec.mjs → test-files-finder-spec.js} +2 -2
  10. package/spec/database/connection/drivers/mysql/{query-parser-spec.mjs → query-parser-spec.js} +6 -6
  11. package/spec/database/drivers/mysql/{connection-spec.mjs → connection-spec.js} +2 -2
  12. package/spec/database/record/create-spec.js +23 -0
  13. package/spec/database/record/{destroy-spec.mjs → destroy-spec.js} +2 -2
  14. package/spec/database/record/{find-spec.mjs → find-spec.js} +2 -3
  15. package/spec/database/record/query-spec.js +37 -0
  16. package/spec/database/record/{update-spec.mjs → update-spec.js} +2 -2
  17. package/spec/dummy/{index.mjs → index.js} +16 -4
  18. package/spec/dummy/src/config/configuration.example.js +36 -0
  19. package/spec/dummy/src/config/configuration.peakflow.js +37 -0
  20. package/spec/dummy/src/config/{routes.mjs → routes.js} +1 -1
  21. package/spec/dummy/src/database/migrations/{20230728075328-create-projects.mjs → 20230728075328-create-projects.js} +1 -2
  22. package/spec/dummy/src/database/migrations/{20230728075329-create-tasks.mjs → 20230728075329-create-tasks.js} +1 -2
  23. package/spec/dummy/src/database/migrations/20250605133926-create-project-translations.js +16 -0
  24. package/spec/dummy/src/models/project.js +9 -0
  25. package/spec/dummy/src/models/task.js +8 -0
  26. package/spec/dummy/src/routes/tasks/{controller.mjs → controller.js} +2 -2
  27. package/spec/http-server/{client-spec.mjs → client-spec.js} +3 -3
  28. package/spec/http-server/{get-spec.mjs → get-spec.js} +1 -1
  29. package/spec/http-server/{post-spec.mjs → post-spec.js} +2 -2
  30. package/spec/support/jasmine.json +2 -2
  31. package/src/{application.mjs → application.js} +3 -3
  32. package/src/big-brother.js +37 -0
  33. package/src/cli/commands/db/{create.mjs → create.js} +9 -7
  34. package/src/cli/commands/db/{migrate.mjs → migrate.js} +4 -5
  35. package/src/cli/commands/destroy/{migration.mjs → migration.js} +2 -2
  36. package/src/cli/commands/generate/{migration.mjs → migration.js} +5 -5
  37. package/src/cli/commands/generate/{model.mjs → model.js} +5 -5
  38. package/src/cli/commands/{init.mjs → init.js} +6 -6
  39. package/src/cli/commands/{server.mjs → server.js} +1 -1
  40. package/src/cli/commands/test/{index.mjs → index.js} +3 -3
  41. package/src/cli/commands/test/{test-files-finder.mjs → test-files-finder.js} +1 -1
  42. package/src/cli/{index.mjs → index.js} +4 -4
  43. package/src/{configuration-resolver.mjs → configuration-resolver.js} +2 -2
  44. package/src/{configuration.mjs → configuration.js} +39 -3
  45. package/src/database/drivers/{base.mjs → base.js} +23 -4
  46. package/src/database/drivers/mysql/column.js +8 -0
  47. package/src/database/drivers/mysql/{index.mjs → index.js} +44 -12
  48. package/src/database/drivers/{sqlite/options.mjs → mysql/options.js} +2 -1
  49. package/src/database/drivers/mysql/query-parser.js +4 -0
  50. package/src/database/drivers/mysql/sql/{create-database.mjs → create-database.js} +1 -1
  51. package/src/database/drivers/{sqlite/sql/create-table.mjs → mysql/sql/create-table.js} +1 -1
  52. package/src/database/drivers/mysql/sql/{delete.mjs → delete.js} +1 -1
  53. package/src/database/drivers/{sqlite/sql/insert.mjs → mysql/sql/insert.js} +1 -1
  54. package/src/database/drivers/mysql/sql/{update.mjs → update.js} +1 -1
  55. package/src/database/drivers/mysql/table.js +25 -0
  56. package/src/database/drivers/sqlite/base.js +108 -0
  57. package/src/database/drivers/sqlite/column.js +10 -0
  58. package/src/database/drivers/sqlite/{index.native.mjs → index.native.js} +19 -22
  59. package/src/database/drivers/sqlite/index.web.js +55 -0
  60. package/src/database/drivers/{mysql/options.mjs → sqlite/options.js} +3 -2
  61. package/src/database/drivers/sqlite/query-parser.js +4 -0
  62. package/src/database/drivers/sqlite/query.native.js +24 -0
  63. package/src/database/drivers/sqlite/query.web.js +34 -0
  64. package/src/database/drivers/sqlite/sql/create-index.js +4 -0
  65. package/src/database/drivers/{mysql/sql/create-table.mjs → sqlite/sql/create-table.js} +1 -1
  66. package/src/database/drivers/sqlite/sql/{delete.mjs → delete.js} +1 -1
  67. package/src/database/drivers/{mysql/sql/insert.mjs → sqlite/sql/insert.js} +1 -1
  68. package/src/database/drivers/sqlite/sql/{update.mjs → update.js} +1 -1
  69. package/src/database/drivers/sqlite/table.js +24 -0
  70. package/src/database/initializer-from-require-context.js +21 -0
  71. package/src/database/{migrate-from-require-context.mjs → migrate-from-require-context.js} +2 -2
  72. package/src/database/migration/index.js +50 -0
  73. package/src/database/{migrator.mjs → migrator.js} +4 -2
  74. package/src/database/pool/{async-tracked-multi-connection.mjs → async-tracked-multi-connection.js} +1 -1
  75. package/src/database/pool/{base.mjs → base.js} +6 -1
  76. package/src/database/pool/{single-multi-use.mjs → single-multi-use.js} +1 -1
  77. package/src/database/query/{base.mjs → base.js} +2 -1
  78. package/src/database/query/{create-database-base.mjs → create-database-base.js} +1 -1
  79. package/src/database/query/create-index-base.js +50 -0
  80. package/src/database/query/create-table-base.js +92 -0
  81. package/src/database/query/{delete-base.mjs → delete-base.js} +1 -1
  82. package/src/database/query/{from-plain.mjs → from-plain.js} +1 -1
  83. package/src/database/query/{from-table.mjs → from-table.js} +1 -1
  84. package/src/database/query/index.js +206 -0
  85. package/src/database/query/{join-plain.mjs → join-plain.js} +1 -1
  86. package/src/database/query/{order-plain.mjs → order-plain.js} +1 -1
  87. package/src/database/query/preloader/belongs-to.js +52 -0
  88. package/src/database/query/preloader/has-many.js +55 -0
  89. package/src/database/query/preloader.js +41 -0
  90. package/src/database/query/{select-plain.mjs → select-plain.js} +1 -1
  91. package/src/database/query/{select-table-and-column.mjs → select-table-and-column.js} +1 -1
  92. package/src/database/query/where-base.js +9 -0
  93. package/src/database/query/where-hash.js +35 -0
  94. package/src/database/query/where-plain.js +13 -0
  95. package/src/database/query-parser/base-query-parser.js +33 -0
  96. package/src/database/query-parser/group-parser.js +40 -0
  97. package/src/database/query-parser/joins-parser.js +71 -0
  98. package/src/database/query-parser/limit-parser.js +40 -0
  99. package/src/database/query-parser/{options.mjs → options.js} +2 -1
  100. package/src/database/query-parser/order-parser.js +39 -0
  101. package/src/database/query-parser/{select-parser.mjs → select-parser.js} +5 -1
  102. package/src/database/query-parser/where-parser.js +39 -0
  103. package/src/database/record/index.js +622 -0
  104. package/src/database/record/instance-relationships/base.js +28 -0
  105. package/src/database/record/instance-relationships/belongs-to.js +20 -0
  106. package/src/database/record/instance-relationships/has-many.js +47 -0
  107. package/src/database/record/relationships/base.js +32 -0
  108. package/src/database/record/relationships/belongs-to.js +12 -0
  109. package/src/database/record/relationships/has-many.js +12 -0
  110. package/src/database/table-data/{index.mjs → index.js} +15 -25
  111. package/src/http-server/client/{index.mjs → index.js} +3 -3
  112. package/src/http-server/client/request-buffer/{index.mjs → index.js} +4 -4
  113. package/src/http-server/client/{request-parser.mjs → request-parser.js} +2 -2
  114. package/src/http-server/client/{request-runner.mjs → request-runner.js} +3 -3
  115. package/src/http-server/client/{request.mjs → request.js} +1 -1
  116. package/src/http-server/{index.mjs → index.js} +3 -3
  117. package/src/http-server/{server-client.mjs → server-client.js} +1 -1
  118. package/src/http-server/worker-handler/{index.mjs → index.js} +2 -2
  119. package/src/http-server/worker-handler/{worker-script.mjs → worker-script.js} +1 -1
  120. package/src/http-server/worker-handler/{worker-thread.mjs → worker-thread.js} +12 -9
  121. package/src/routes/{app-routes.mjs → app-routes.js} +1 -1
  122. package/src/routes/{base-route.mjs → base-route.js} +2 -2
  123. package/src/routes/{get-route.mjs → get-route.js} +1 -1
  124. package/src/routes/{index.mjs → index.js} +1 -1
  125. package/src/routes/{resolver.mjs → resolver.js} +1 -1
  126. package/src/routes/{resource-route.mjs → resource-route.js} +1 -1
  127. package/src/routes/{root-route.mjs → root-route.js} +1 -1
  128. package/src/templates/{configuration.mjs → configuration.js} +3 -3
  129. package/src/templates/{generate-migration.mjs → generate-migration.js} +1 -1
  130. package/src/templates/generate-model.js +6 -0
  131. package/src/templates/{routes.mjs → routes.js} +1 -1
  132. package/src/utils/rest-args-error.js +9 -0
  133. package/spec/database/record/create-spec.mjs +0 -14
  134. package/spec/dummy/src/config/configuration.example.mjs +0 -21
  135. package/spec/dummy/src/config/configuration.peakflow.mjs +0 -22
  136. package/spec/dummy/src/models/task.mjs +0 -4
  137. package/src/database/drivers/mysql/query-parser.mjs +0 -25
  138. package/src/database/drivers/sqlite/base.mjs +0 -36
  139. package/src/database/drivers/sqlite/index.web.mjs +0 -45
  140. package/src/database/drivers/sqlite/query-parser.mjs +0 -25
  141. package/src/database/drivers/sqlite/query.native.mjs +0 -9
  142. package/src/database/drivers/sqlite/query.web.mjs +0 -9
  143. package/src/database/drivers/sqlite/table.mjs +0 -9
  144. package/src/database/migration/index.mjs +0 -18
  145. package/src/database/query/create-table-base.mjs +0 -69
  146. package/src/database/query/index.mjs +0 -144
  147. package/src/database/query-parser/joins-parser.mjs +0 -30
  148. package/src/database/record/index.mjs +0 -187
  149. package/src/templates/generate-model.mjs +0 -4
  150. /package/{index.mjs → index.js} +0 -0
  151. /package/spec/dummy/{dummy-directory.mjs → dummy-directory.js} +0 -0
  152. /package/src/cli/{base-command.mjs → base-command.js} +0 -0
  153. /package/src/cli/commands/test/{test-runner.mjs → test-runner.js} +0 -0
  154. /package/src/{controller.mjs → controller.js} +0 -0
  155. /package/src/database/drivers/mysql/{connect-connection.mjs → connect-connection.js} +0 -0
  156. /package/src/database/drivers/mysql/{query.mjs → query.js} +0 -0
  157. /package/src/database/{handler.mjs → handler.js} +0 -0
  158. /package/src/database/query/{from-base.mjs → from-base.js} +0 -0
  159. /package/src/database/query/{insert-base.mjs → insert-base.js} +0 -0
  160. /package/src/database/query/{join-base.mjs → join-base.js} +0 -0
  161. /package/src/database/query/{order-base.mjs → order-base.js} +0 -0
  162. /package/src/database/query/{select-base.mjs → select-base.js} +0 -0
  163. /package/src/database/query/{update-base.mjs → update-base.js} +0 -0
  164. /package/src/database/query-parser/{from-parser.mjs → from-parser.js} +0 -0
  165. /package/src/database/record/{record-not-found-error.mjs → record-not-found-error.js} +0 -0
  166. /package/src/{error-logger.mjs → error-logger.js} +0 -0
  167. /package/src/http-server/client/{params-to-object.mjs → params-to-object.js} +0 -0
  168. /package/src/http-server/client/request-buffer/{form-data-part.mjs → form-data-part.js} +0 -0
  169. /package/src/http-server/client/request-buffer/{header.mjs → header.js} +0 -0
  170. /package/src/http-server/client/{response.mjs → response.js} +0 -0
  171. /package/src/{logger.mjs → logger.js} +0 -0
  172. /package/src/spec/{index.mjs → index.js} +0 -0
  173. /package/src/utils/{file-exists.mjs → file-exists.js} +0 -0
@@ -0,0 +1,55 @@
1
+ import BetterLocalStorage from "better-localstorage"
2
+ import debounce from "debounce"
3
+ import {digg} from "diggerize"
4
+ import initSqlJs from "sql.js"
5
+ import query from "./query"
6
+
7
+ import Base from "./base.js"
8
+
9
+ export default class VelociousDatabaseDriversSqliteWeb extends Base {
10
+ async connect() {
11
+ this.betterLocaleStorage ||= new BetterLocalStorage()
12
+
13
+ const args = this.getArgs()
14
+
15
+ if (args.reset) {
16
+ await this.betterLocaleStorage.delete(this.localStorageName())
17
+ }
18
+
19
+ const SQL = await initSqlJs({
20
+ // Required to load the wasm binary asynchronously. Of course, you can host it wherever you want you can omit locateFile completely when running in Node.
21
+ locateFile: (file) => `https://sql.js.org/dist/${file}`
22
+ })
23
+
24
+ const databaseContent = await this.betterLocaleStorage.get(this.localStorageName())
25
+
26
+ this.connection = new SQL.Database(databaseContent)
27
+ }
28
+
29
+ localStorageName = () => `VelociousDatabaseDriversSqliteWeb---${digg(this.getArgs(), "name")}`
30
+ disconnect = () => this.saveDatabase()
31
+ saveDatabase = async () => {
32
+ const localStorageContent = this.connection.export()
33
+ await this.betterLocaleStorage.set(this.localStorageName(), localStorageContent)
34
+ }
35
+
36
+ saveDatabaseDebounce = debounce(this.saveDatabase, 500)
37
+
38
+ async close() {
39
+ await this.saveDatabase()
40
+ await this.connection.end()
41
+ this.connection = undefined
42
+ }
43
+
44
+ query = async (sql) => {
45
+ const result = await query(this.connection, sql)
46
+ const downcasedSQL = sql.toLowerCase().trim()
47
+
48
+ // Auto-save database in local storage in case we can find manipulating instructions in the SQL
49
+ if (downcasedSQL.startsWith("delete ") || downcasedSQL.startsWith("insert into ") || downcasedSQL.startsWith("update ")) {
50
+ this.saveDatabaseDebounce()
51
+ }
52
+
53
+ return result
54
+ }
55
+ }
@@ -1,8 +1,9 @@
1
- import QueryParserOptions from "../../query-parser/options.mjs"
1
+ import QueryParserOptions from "../../query-parser/options.js"
2
2
 
3
- export default class VelociousDatabaseDriversMysqlOptions extends QueryParserOptions {
3
+ export default class VelociousDatabaseDriversSqliteOptions extends QueryParserOptions {
4
4
  constructor(options) {
5
5
  options.columnQuote = "`"
6
+ options.indexQuote = "`"
6
7
  options.stringQuote = "'"
7
8
  options.tableQuote = "`"
8
9
 
@@ -0,0 +1,4 @@
1
+ import BaseQueryParser from "../../query-parser/base-query-parser.js"
2
+
3
+ export default class VelociousDatabaseConnectionDriversSqliteQueryParser extends BaseQueryParser {
4
+ }
@@ -0,0 +1,24 @@
1
+ export default async function query(connection, sql) {
2
+ const rows = []
3
+ let result
4
+
5
+ try {
6
+ result = await connection.getAllAsync(sql)
7
+ } catch (error) {
8
+ let sqlInErrorMessage = `${sql}`
9
+
10
+ if (sqlInErrorMessage.length >= 4096) {
11
+ sqlInErrorMessage = `${sqlInErrorMessage.substring(0, 4096)}...`
12
+ }
13
+
14
+ error.message += `\n\n${sqlInErrorMessage}`
15
+
16
+ throw error
17
+ }
18
+
19
+ for await (const entry of result) {
20
+ rows.push(entry)
21
+ }
22
+
23
+ return rows
24
+ }
@@ -0,0 +1,34 @@
1
+ export default async function query(connection, sql) {
2
+ const rows = []
3
+ let result
4
+
5
+ try {
6
+ result = connection.exec(sql)
7
+ } catch (error) {
8
+ let sqlInErrorMessage = `${sql}`
9
+
10
+ if (sqlInErrorMessage.length >= 4096) {
11
+ sqlInErrorMessage = `${sqlInErrorMessage.substring(0, 4096)}...`
12
+ }
13
+
14
+ error.message += `\n\n${sqlInErrorMessage}`
15
+
16
+ throw error
17
+ }
18
+
19
+ if (result[0]) {
20
+ const columns = result[0].columns
21
+
22
+ for (const rowValues of result[0].values) {
23
+ const row = {}
24
+
25
+ for (const columnIndex in columns) {
26
+ row[columns[columnIndex]] = rowValues[columnIndex]
27
+ }
28
+
29
+ rows.push(row)
30
+ }
31
+ }
32
+
33
+ return rows
34
+ }
@@ -0,0 +1,4 @@
1
+ import CreateIndexBase from "../../../query/create-index-base.js"
2
+
3
+ export default class VelociousDatabaseConnectionDriversMysqlSqlCreateIndex extends CreateIndexBase {
4
+ }
@@ -1,4 +1,4 @@
1
- import CreateTableBase from "../../../query/create-table-base.mjs"
1
+ import CreateTableBase from "../../../query/create-table-base.js"
2
2
 
3
3
  export default class VelociousDatabaseConnectionDriversMysqlSqlCreateTable extends CreateTableBase {
4
4
  }
@@ -1,4 +1,4 @@
1
- import DeleteBase from "../../../query/delete-base.mjs"
1
+ import DeleteBase from "../../../query/delete-base.js"
2
2
 
3
3
  export default class VelociousDatabaseConnectionDriversMysqlSqlDelete extends DeleteBase {
4
4
  toSql() {
@@ -1,4 +1,4 @@
1
- import InsertBase from "../../../query/insert-base.mjs"
1
+ import InsertBase from "../../../query/insert-base.js"
2
2
 
3
3
  export default class VelociousDatabaseConnectionDriversMysqlSqlInsert extends InsertBase {
4
4
  toSql() {
@@ -1,4 +1,4 @@
1
- import UpdateBase from "../../../query/update-base.mjs"
1
+ import UpdateBase from "../../../query/update-base.js"
2
2
 
3
3
  export default class VelociousDatabaseConnectionDriversMysqlSqlUpdate extends UpdateBase {
4
4
  toSql() {
@@ -0,0 +1,24 @@
1
+ import Column from "./column.js"
2
+ import {digg} from "diggerize"
3
+
4
+ export default class VelociousDatabaseDriversSqliteTable {
5
+ constructor({driver, row}) {
6
+ this.driver = driver
7
+ this.row = row
8
+ }
9
+
10
+ getColumns = async () => {
11
+ const result = await this.driver.query(`PRAGMA table_info('${this.getName()}')`)
12
+ const columns = []
13
+
14
+ for (const columnData of result) {
15
+ const column = new Column({column: columnData, driver: this.driver, table: this})
16
+
17
+ columns.push(column)
18
+ }
19
+
20
+ return columns
21
+ }
22
+
23
+ getName = () => digg(this, "row", "name")
24
+ }
@@ -0,0 +1,21 @@
1
+ export default class VelociousDatabaseInitializerFromRequireContext {
2
+ constructor({requireContext, ...restProps}) {
3
+ const restPropsKeys = Object.keys(restProps)
4
+
5
+ if (restPropsKeys.length > 0) throw new Error(`Unknown arguments: ${restPropsKeys.join(", ")}`)
6
+
7
+ this.requireContext = requireContext
8
+ }
9
+
10
+ async initialize({configuration}) {
11
+ for (const fileName of this.requireContext.keys()) {
12
+ const modelClass = this.requireContext(fileName).default
13
+
14
+ await modelClass.initializeRecord({configuration})
15
+
16
+ if (await modelClass.hasTranslationsTable()) {
17
+ await modelClass.getTranslationClass().initializeRecord({configuration})
18
+ }
19
+ }
20
+ }
21
+ }
@@ -1,4 +1,4 @@
1
- import Configuration from "../configuration.mjs"
1
+ import Configuration from "../configuration.js"
2
2
  import * as inflection from "inflection"
3
3
  import Migrator from "./migrator"
4
4
 
@@ -14,7 +14,7 @@ export default class VelociousDatabaseMigrateFromRequireContext {
14
14
 
15
15
  const files = requireContext.keys()
16
16
  .map((file) => {
17
- const match = file.match(/^\.\/(\d{14})-(.+)\.mjs$/)
17
+ const match = file.match(/^\.\/(\d{14})-(.+)\.js$/)
18
18
 
19
19
  if (!match) return null
20
20
 
@@ -0,0 +1,50 @@
1
+ import TableData from "../table-data/index.js"
2
+
3
+ export default class VelociousDatabaseMigration {
4
+ constructor({configuration}) {
5
+ this.configuration = configuration
6
+ }
7
+
8
+ async addIndex(tableName, columns, args) {
9
+ const databasePool = this.configuration.getDatabasePool()
10
+ const createIndexArgs = Object.assign(
11
+ {
12
+ columns,
13
+ tableName
14
+ },
15
+ args
16
+ )
17
+ const sql = databasePool.createIndexSql(createIndexArgs)
18
+
19
+ await databasePool.query(sql)
20
+ }
21
+
22
+ async createTable(tableName, callback) {
23
+ const tableData = new TableData(tableName)
24
+
25
+ tableData.integer("id", {null: false, primaryKey: true})
26
+
27
+ callback(tableData)
28
+
29
+ const databasePool = this.configuration.getDatabasePool()
30
+ const sqls = databasePool.createTableSql(tableData)
31
+
32
+ for (const sql of sqls) {
33
+ await databasePool.query(sql)
34
+ }
35
+ }
36
+
37
+ getConnection() {
38
+ const connection = this.configuration.getDatabasePool().getCurrentConnection()
39
+
40
+ if (!connection) throw new Error("Couldn't get current connection")
41
+
42
+ return connection
43
+ }
44
+
45
+ async tableExists(tableName) {
46
+ const exists = await this.getConnection().tableExists(tableName)
47
+
48
+ return exists
49
+ }
50
+ }
@@ -20,9 +20,11 @@ export default class VelociousDatabaseMigrator {
20
20
  schemaMigrationsTable.string("version", {null: false, primaryKey: true})
21
21
 
22
22
  await this.configuration.getDatabasePool().withConnection(async (db) => {
23
- const createSchemaMigrationsTableSql = db.createTableSql(schemaMigrationsTable)
23
+ const createSchemaMigrationsTableSqls = db.createTableSql(schemaMigrationsTable)
24
24
 
25
- await db.query(createSchemaMigrationsTableSql)
25
+ for (const createSchemaMigrationsTableSql of createSchemaMigrationsTableSqls) {
26
+ await db.query(createSchemaMigrationsTableSql)
27
+ }
26
28
  })
27
29
  }
28
30
 
@@ -1,5 +1,5 @@
1
1
  import {AsyncLocalStorage} from "async_hooks"
2
- import BasePool from "./base.mjs"
2
+ import BasePool from "./base.js"
3
3
 
4
4
  let idSeq = 0
5
5
 
@@ -1,4 +1,4 @@
1
- import Configuration from "../../configuration.mjs"
1
+ import Configuration from "../../configuration.js"
2
2
  import {digg} from "diggerize"
3
3
 
4
4
  class VelociousDatabasePoolBase {
@@ -35,6 +35,8 @@ class VelociousDatabasePoolBase {
35
35
  }
36
36
 
37
37
  const forwardMethods = [
38
+ "createIndex",
39
+ "createIndexSql",
38
40
  "createTable",
39
41
  "createTableSql",
40
42
  "delete",
@@ -52,6 +54,9 @@ const forwardMethods = [
52
54
  for (const forwardMethod of forwardMethods) {
53
55
  VelociousDatabasePoolBase.prototype[forwardMethod] = function(...args) {
54
56
  const connection = this.getCurrentConnection()
57
+ const connectionMethod = connection[forwardMethod]
58
+
59
+ if (!connectionMethod) throw new Error(`${forwardMethod} isn't defined on driver`)
55
60
 
56
61
  return connection[forwardMethod](...args)
57
62
  }
@@ -1,4 +1,4 @@
1
- import BasePool from "./base.mjs"
1
+ import BasePool from "./base.js"
2
2
 
3
3
  export default class VelociousDatabasePoolSingleMultiUser extends BasePool {
4
4
  static current() {
@@ -3,7 +3,8 @@ export default class VelociousDatabaseQueryBase {
3
3
  this.driver = driver
4
4
  }
5
5
 
6
- getOptions = () => this.driver?.options()
6
+ getDriver = () => this.driver
7
+ getOptions = () => this.getDriver()?.options()
7
8
 
8
9
  toSql() {
9
10
  throw new Error("'toSql' wasn't implemented")
@@ -1,5 +1,5 @@
1
1
  import {digs} from "diggerize"
2
- import QueryBase from "./base.mjs"
2
+ import QueryBase from "./base.js"
3
3
 
4
4
  export default class VelociousDatabaseQueryCreateDatabaseBase extends QueryBase {
5
5
  constructor({driver, databaseName, ifNotExists}) {
@@ -0,0 +1,50 @@
1
+ import {digs} from "diggerize"
2
+ import QueryBase from "./base.js"
3
+
4
+ export default class VelociousDatabaseQueryCreateIndexBase extends QueryBase {
5
+ constructor({columns, driver, ifNotExists, name, unique, tableName}) {
6
+ super({driver})
7
+ this.columns = columns
8
+ this.name = name
9
+ this.tableName = tableName
10
+ this.ifNotExists = ifNotExists
11
+ this.unique = unique
12
+ }
13
+
14
+ generateIndexName() {
15
+ let indexName = `index_on_${this.tableName}_`
16
+
17
+ for (const columnIndex in this.columns) {
18
+ if (columnIndex > 0) indexName += "_"
19
+
20
+ indexName += this.columns[columnIndex]
21
+ }
22
+
23
+ return indexName
24
+ }
25
+
26
+ toSql() {
27
+ const {tableName} = this
28
+ const {columnQuote, indexQuote, tableQuote} = digs(this.getOptions(), "columnQuote", "indexQuote", "tableQuote")
29
+ let sql = "CREATE"
30
+
31
+ if (this.unique) sql += " UNIQUE"
32
+
33
+ sql += " INDEX"
34
+
35
+ if (this.ifNotExists) sql += " IF NOT EXISTS"
36
+
37
+ sql += ` ${indexQuote}${this.name || this.generateIndexName()}${indexQuote}`
38
+ sql += ` ON ${tableQuote}${tableName}${tableQuote} (`
39
+
40
+ for (const columnIndex in this.columns) {
41
+ if (columnIndex > 0) sql += ", "
42
+
43
+ sql += `${columnQuote}${this.columns[columnIndex]}${columnQuote}`
44
+ }
45
+
46
+ sql += ")"
47
+
48
+ return sql
49
+ }
50
+ }
@@ -0,0 +1,92 @@
1
+ import CreateIndexBase from "./create-index-base.js"
2
+ import QueryBase from "./base.js"
3
+
4
+ export default class VelociousDatabaseQueryCreateTableBase extends QueryBase {
5
+ constructor({driver, ifNotExists, indexInCreateTable = true, tableData}) {
6
+ super({driver})
7
+ this.ifNotExists = ifNotExists
8
+ this.indexInCreateTable = indexInCreateTable
9
+ this.tableData = tableData
10
+ }
11
+
12
+ toSql() {
13
+ const {tableData} = this
14
+ const sqls = []
15
+
16
+ let sql = "CREATE TABLE"
17
+
18
+ if (this.ifNotExists || tableData.getIfNotExists()) sql += " IF NOT EXISTS"
19
+
20
+ sql += ` ${tableData.getName()} (`
21
+
22
+ let columnCount = 0
23
+
24
+ for (const column of tableData.getColumns()) {
25
+ columnCount++
26
+
27
+ let maxlength = column.args.maxlength
28
+ let type = column.args.type
29
+
30
+ if (type == "string") {
31
+ type = "varchar"
32
+ maxlength ||= 255
33
+ }
34
+
35
+ if (columnCount > 1) sql += ", "
36
+
37
+ sql += `${this.driver.quoteColumn(column.name)} ${type}`
38
+
39
+ if (maxlength !== undefined) sql += `(${maxlength})`
40
+
41
+ if (column.args.autoIncrement) sql += " AUTO_INCREMENT"
42
+ if (column.args.primaryKey) sql += " PRIMARY KEY"
43
+ if (column.args.null === false) sql += " NOT NULL"
44
+ }
45
+
46
+ if (this.indexInCreateTable) {
47
+ for (const index of tableData.getIndexes()) {
48
+ sql += ","
49
+
50
+ if (index.getUnique()) {
51
+ sql += " UNIQUE"
52
+ }
53
+
54
+ sql += " INDEX"
55
+
56
+ if (index.getName()) {
57
+ sql += ` ${index.getName()}`
58
+ }
59
+
60
+ sql += " ("
61
+
62
+ index.getColumns().forEach((column, columnIndex) => {
63
+ if (columnIndex > 0) sql += ", "
64
+
65
+ sql += this.driver.quoteColumn(column.name)
66
+ })
67
+
68
+ sql += ")"
69
+ }
70
+ }
71
+
72
+ sql += ")"
73
+
74
+ sqls.push(sql)
75
+
76
+ if (!this.indexInCreateTable) {
77
+ for (const index of tableData.getIndexes()) {
78
+ const createIndexArgs = {
79
+ columns: index.getColumns(),
80
+ driver: this.getDriver(),
81
+ tableName: tableData.getName(),
82
+ unique: index.getUnique()
83
+ }
84
+ const sql = new CreateIndexBase(createIndexArgs).toSql()
85
+
86
+ sqls.push(sql)
87
+ }
88
+ }
89
+
90
+ return [sql]
91
+ }
92
+ }
@@ -1,4 +1,4 @@
1
- import QueryBase from "./base.mjs"
1
+ import QueryBase from "./base.js"
2
2
 
3
3
  export default class VelociousDatabaseQueryDeleteBase extends QueryBase {
4
4
  constructor({conditions, driver, tableName}) {
@@ -1,4 +1,4 @@
1
- import FromBase from "./from-base.mjs"
1
+ import FromBase from "./from-base.js"
2
2
 
3
3
  export default class VelociousDatabaseQueryFromPlain extends FromBase {
4
4
  constructor({driver, plain}) {
@@ -1,4 +1,4 @@
1
- import FromBase from "./from-base.mjs"
1
+ import FromBase from "./from-base.js"
2
2
 
3
3
  export default class VelociousDatabaseQueryFromTable extends FromBase {
4
4
  constructor({driver, tableName}) {