velocious 1.0.93 → 1.0.95

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 (33) hide show
  1. package/package.json +1 -1
  2. package/src/application.js +19 -0
  3. package/src/cli/base-command.js +1 -0
  4. package/src/cli/commands/db/migrate.js +2 -0
  5. package/src/cli/commands/generate/base-models.js +7 -0
  6. package/src/cli/commands/generate/model.js +1 -1
  7. package/src/configuration-resolver.js +6 -3
  8. package/src/configuration.js +27 -4
  9. package/src/database/drivers/base-column.js +69 -0
  10. package/src/database/drivers/base-columns-index.js +19 -0
  11. package/src/database/drivers/base-foreign-key.js +12 -0
  12. package/src/database/drivers/base-table.js +32 -0
  13. package/src/database/drivers/base.js +125 -3
  14. package/src/database/migrator.js +17 -2
  15. package/src/database/query/drop-table-base.js +8 -0
  16. package/src/database/query/index.js +42 -0
  17. package/src/database/query-parser/from-parser.js +17 -6
  18. package/src/database/query-parser/group-parser.js +21 -8
  19. package/src/database/record/index.js +12 -11
  20. package/src/database/record/relationships/base.js +29 -6
  21. package/src/database/table-data/index.js +198 -6
  22. package/src/environment-handlers/base.js +58 -0
  23. package/src/environment-handlers/browser.js +0 -24
  24. package/src/environment-handlers/node/cli/commands/generate/base-models.js +123 -0
  25. package/src/environment-handlers/node.js +6 -1
  26. package/src/initializer.js +4 -0
  27. package/src/logger.js +54 -2
  28. package/src/routes/base-route.js +25 -4
  29. package/src/routes/get-route.js +3 -1
  30. package/src/routes/namespace-route.js +3 -1
  31. package/src/routes/post-route.js +3 -1
  32. package/src/routes/resource-route.js +3 -1
  33. package/src/utils/backtrace-cleaner.js +10 -0
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "velocious": "bin/velocious.js"
4
4
  },
5
5
  "name": "velocious",
6
- "version": "1.0.93",
6
+ "version": "1.0.95",
7
7
  "main": "index.js",
8
8
  "scripts": {
9
9
  "lint": "eslint",
@@ -22,6 +22,9 @@ export default class VelociousApplication {
22
22
  */
23
23
  getType() { return this._type }
24
24
 
25
+ /**
26
+ * @returns {Promise<void>}
27
+ */
25
28
  async initialize() {
26
29
  const routes = await AppRoutes.getRoutes(this.configuration)
27
30
 
@@ -41,6 +44,10 @@ export default class VelociousApplication {
41
44
  return this.httpServer?.isActive()
42
45
  }
43
46
 
47
+ /**
48
+ * @param {function() : void} callback
49
+ * @returns {Promise<void>}
50
+ */
44
51
  async run(callback) {
45
52
  await this.start()
46
53
 
@@ -51,6 +58,9 @@ export default class VelociousApplication {
51
58
  }
52
59
  }
53
60
 
61
+ /**
62
+ * @returns {Promise<void>}
63
+ */
54
64
  async startHttpServer() {
55
65
  const {configuration, httpServerConfiguration} = digs(this, "configuration", "httpServerConfiguration")
56
66
  const port = httpServerConfiguration.port || 3006
@@ -63,11 +73,17 @@ export default class VelociousApplication {
63
73
  await this.httpServer.start()
64
74
  }
65
75
 
76
+ /**
77
+ * @returns {Promise<void>}
78
+ */
66
79
  async stop() {
67
80
  await this.logger.debug("Stopping server")
68
81
  await this.httpServer.stop()
69
82
  }
70
83
 
84
+ /**
85
+ * @returns {void}
86
+ */
71
87
  onHttpServerClose = () => {
72
88
  this.logger.debug("HTTP server closed")
73
89
 
@@ -76,6 +92,9 @@ export default class VelociousApplication {
76
92
  }
77
93
  }
78
94
 
95
+ /**
96
+ * @returns {Promise<void>}
97
+ */
79
98
  wait() {
80
99
  return new Promise((resolve) => {
81
100
  this.waitResolve = resolve
@@ -21,6 +21,7 @@ export default class VelociousCliBaseCommand {
21
21
 
22
22
  /**
23
23
  * @interface
24
+ * @returns {Promise<void>}
24
25
  */
25
26
  execute() {
26
27
  throw new Error("execute not implemented")
@@ -7,6 +7,8 @@ export default class DbMigrate extends BaseCommand {
7
7
  const migrations = await this.getEnvironmentHandler().findMigrations()
8
8
  const migrator = new Migrator({configuration: this.getConfiguration()})
9
9
 
10
+ console.log(`Running ${migrations.length} migrations`)
11
+
10
12
  await this.getConfiguration().ensureConnections(async () => {
11
13
  await migrator.prepare()
12
14
  await migrator.migrateFiles(migrations, digg(this.getEnvironmentHandler(), "requireMigration"))
@@ -0,0 +1,7 @@
1
+ import BaseCommand from "../../base-command.js"
2
+
3
+ export default class DbGenerateBaseModels extends BaseCommand {
4
+ async execute() {
5
+ return await this.getConfiguration().getEnvironmentHandler().cliCommandsGenerateBaseModels(this)
6
+ }
7
+ }
@@ -2,6 +2,6 @@ import BaseCommand from "../../base-command.js"
2
2
 
3
3
  export default class DbGenerateModel extends BaseCommand {
4
4
  async execute() {
5
- return await this.getConfiguration().getEnvironmentHandler().cliCommandsModelGenerate(this)
5
+ return await this.getConfiguration().getEnvironmentHandler().cliCommandsGenerateModel(this)
6
6
  }
7
7
  }
@@ -2,7 +2,12 @@ import Configuration from "./configuration.js"
2
2
  import envSense from "env-sense/src/use-env-sense.js"
3
3
  import fileExists from "./utils/file-exists.js"
4
4
 
5
- const configurationResolver = async (args = {}) => {
5
+ /**
6
+ * @param {object} args
7
+ * @param {string} args.directory
8
+ * @returns {Promise<Configuration>}
9
+ */
10
+ export default async function configurationResolver(args = {}) {
6
11
  if (Configuration.current(false)) {
7
12
  return Configuration.current()
8
13
  }
@@ -35,5 +40,3 @@ const configurationResolver = async (args = {}) => {
35
40
 
36
41
  return configuration
37
42
  }
38
-
39
- export default configurationResolver
@@ -50,10 +50,13 @@ export default class VelociousConfiguration {
50
50
  }
51
51
 
52
52
  /**
53
- * @returns {object}
53
+ * @returns {Record<string, any>}
54
54
  */
55
55
  getDatabaseConfiguration() {
56
56
  if (!this.database) throw new Error("No database configuration")
57
+ if (!this.database[this.getEnvironment()]) {
58
+ throw new Error(`No database configuration for environment: ${this.getEnvironment()} - ${Object.keys(this.database).join(", ")}`)
59
+ }
57
60
 
58
61
  return digg(this, "database", this.getEnvironment())
59
62
  }
@@ -174,6 +177,13 @@ export default class VelociousConfiguration {
174
177
  return modelClass
175
178
  }
176
179
 
180
+ /**
181
+ * @returns {Record<string, typeof import("./database/record/index.js").default>} A hash of all model classes, keyed by model name, as they were defined in the configuration. This is a direct reference to the model classes, not a copy.
182
+ */
183
+ getModelClasses() {
184
+ return this.modelClasses
185
+ }
186
+
177
187
  /**
178
188
  * @returns {string} The path to a config file that should be used for testing.
179
189
  */
@@ -199,13 +209,26 @@ export default class VelociousConfiguration {
199
209
  */
200
210
  isInitialized() { return this._isInitialized }
201
211
 
212
+ /**
213
+ * @param {object} args
214
+ * @param {string} args.type
215
+ * @returns {void}
216
+ */
217
+ async initializeModels(args = {type: "server"}) {
218
+ if (!this._modelsInitialized) {
219
+ this._modelsInitialized = true
220
+
221
+ if (this._initializeModels) {
222
+ await this._initializeModels({configuration: this, type: args.type})
223
+ }
224
+ }
225
+ }
226
+
202
227
  async initialize({type} = {}) {
203
228
  if (!this.isInitialized()) {
204
229
  this._isInitialized = true
205
230
 
206
- if (this._initializeModels) {
207
- await this._initializeModels({configuration: this, type})
208
- }
231
+ await this.initializeModels({type})
209
232
 
210
233
  if (this._initializers) {
211
234
  const initializers = await this._initializers({configuration: this})
@@ -2,6 +2,21 @@ import TableColumn from "../table-data/table-column.js"
2
2
  import TableData from "../table-data/index.js"
3
3
 
4
4
  export default class VelociousDatabaseDriversBaseColumn {
5
+ /**
6
+ * @interface
7
+ * @returns {boolean}
8
+ */
9
+ getAutoIncrement() {
10
+ throw new Error("getAutoIncrement not implemented")
11
+ }
12
+
13
+ /**
14
+ * @returns {any}
15
+ */
16
+ getDefault() {
17
+ throw new Error("getDefault not implemented")
18
+ }
19
+
5
20
  /**
6
21
  * @param {string} indexName
7
22
  * @returns {Promise<import('../table-data/table-index.js').default>}
@@ -32,10 +47,45 @@ export default class VelociousDatabaseDriversBaseColumn {
32
47
  }
33
48
  }
34
49
 
50
+ /**
51
+ * @returns {import("./base.js").default}
52
+ */
35
53
  getDriver() {
36
54
  return this.getTable().getDriver()
37
55
  }
38
56
 
57
+ /**
58
+ * @interface
59
+ * @returns {Promise<Array<import("./base-columns-index.js").default>>}
60
+ */
61
+ getIndexes() {
62
+ throw new Error("getIndexes not implemented")
63
+ }
64
+
65
+ /**
66
+ * @interface
67
+ * @returns {number}
68
+ */
69
+ getMaxLength() {
70
+ throw new Error("getMaxLength not implemented")
71
+ }
72
+
73
+ /**
74
+ * @interface
75
+ * @returns {string}
76
+ */
77
+ getName() {
78
+ throw new Error("getName not implemented")
79
+ }
80
+
81
+ /**
82
+ * @interface
83
+ * @returns {boolean}
84
+ */
85
+ getNull() {
86
+ throw new Error("getNull not implemented")
87
+ }
88
+
39
89
  /**
40
90
  * @returns {import("../query-parser/options.js").default}
41
91
  */
@@ -43,6 +93,17 @@ export default class VelociousDatabaseDriversBaseColumn {
43
93
  return this.getDriver().options()
44
94
  }
45
95
 
96
+ /**
97
+ * @interface
98
+ * @returns {boolean}
99
+ */
100
+ getPrimaryKey() {
101
+ throw new Error("getPrimaryKey not implemented")
102
+ }
103
+
104
+ /**
105
+ * @returns {import("./base-table.js").default}
106
+ */
46
107
  getTable() {
47
108
  if (!this.table) throw new Error("No table set on column")
48
109
 
@@ -63,4 +124,12 @@ export default class VelociousDatabaseDriversBaseColumn {
63
124
  type: this.getType()
64
125
  })
65
126
  }
127
+
128
+ /**
129
+ * @interface
130
+ * @returns {string}
131
+ */
132
+ getType() {
133
+ throw new Error("getType not implemented")
134
+ }
66
135
  }
@@ -1,15 +1,25 @@
1
1
  import { digg } from "diggerize"
2
2
 
3
3
  export default class VelociousDatabaseDriversBaseColumnsIndex {
4
+ /**
5
+ * @param {import("./base-table.js").default} table
6
+ * @param {object} data
7
+ */
4
8
  constructor(table, data) {
5
9
  this.data = data
6
10
  this.table = table
7
11
  }
8
12
 
13
+ /**
14
+ * @returns {import("./base.js").default}
15
+ */
9
16
  getDriver() {
10
17
  return this.getTable().getDriver()
11
18
  }
12
19
 
20
+ /**
21
+ * @returns {string}
22
+ */
13
23
  getName() {
14
24
  return digg(this, "data", "index_name")
15
25
  }
@@ -21,16 +31,25 @@ export default class VelociousDatabaseDriversBaseColumnsIndex {
21
31
  return this.getDriver().options()
22
32
  }
23
33
 
34
+ /**
35
+ * @returns {import("./base-table.js").default}
36
+ */
24
37
  getTable() {
25
38
  if (!this.table) throw new Error("No table set on column")
26
39
 
27
40
  return this.table
28
41
  }
29
42
 
43
+ /**
44
+ * @returns {boolean}
45
+ */
30
46
  isPrimaryKey() {
31
47
  return digg(this, "data", "is_primary_key")
32
48
  }
33
49
 
50
+ /**
51
+ * @returns {boolean}
52
+ */
34
53
  isUnique() {
35
54
  return digg(this, "data", "is_unique")
36
55
  }
@@ -1,10 +1,16 @@
1
1
  import TableForeignKey from "../table-data/table-foreign-key.js"
2
2
 
3
3
  export default class VelociousDatabaseDriversBaseForeignKey {
4
+ /**
5
+ * @param {object} data
6
+ */
4
7
  constructor(data) {
5
8
  this.data = data
6
9
  }
7
10
 
11
+ /**
12
+ * @returns {import("./base.js").default}
13
+ */
8
14
  getDriver() {
9
15
  return this.getTable().getDriver()
10
16
  }
@@ -16,12 +22,18 @@ export default class VelociousDatabaseDriversBaseForeignKey {
16
22
  return this.getDriver().options()
17
23
  }
18
24
 
25
+ /**
26
+ * @returns {import("./base-table.js").default}
27
+ */
19
28
  getTable() {
20
29
  if (!this.table) throw new Error("No table set on column")
21
30
 
22
31
  return this.table
23
32
  }
24
33
 
34
+ /**
35
+ * @returns {TableForeignKey}
36
+ */
25
37
  getTableDataForeignKey() {
26
38
  return new TableForeignKey({
27
39
  columnName: this.getColumnName(),
@@ -2,6 +2,10 @@ import {digg} from "diggerize"
2
2
  import TableData from "../table-data/index.js"
3
3
 
4
4
  export default class VelociousDatabaseDriversBaseTable {
5
+ /**
6
+ * @param {string} columnName
7
+ * @returns {import("./base-column.js").default}
8
+ */
5
9
  async getColumnByName(columnName) {
6
10
  const columnes = await this.getColumns()
7
11
  const column = columnes.find((column) => column.getName() == columnName)
@@ -9,12 +13,31 @@ export default class VelociousDatabaseDriversBaseTable {
9
13
  return column
10
14
  }
11
15
 
16
+ /**
17
+ * @interface
18
+ * @returns {Promise<Array<import("./base-column.js").default>>}
19
+ */
20
+ getColumns() {
21
+ throw new Error("getColumns not implemented")
22
+ }
23
+
24
+ /**
25
+ * @returns {import("./base.js").default}
26
+ */
12
27
  getDriver() {
13
28
  if (!this.driver) throw new Error("No driver set on table")
14
29
 
15
30
  return this.driver
16
31
  }
17
32
 
33
+ /**
34
+ * @interface
35
+ * @returns {string}
36
+ */
37
+ getName() {
38
+ throw new Error("getName not implemented")
39
+ }
40
+
18
41
  /**
19
42
  * @returns {import("../query-parser/options.js").default}
20
43
  */
@@ -22,6 +45,9 @@ export default class VelociousDatabaseDriversBaseTable {
22
45
  return this.getDriver().options()
23
46
  }
24
47
 
48
+ /**
49
+ * @returns {Promise<TableData>}
50
+ */
25
51
  async getTableData() {
26
52
  const tableData = new TableData(this.getName())
27
53
  const tableDataColumns = []
@@ -50,12 +76,18 @@ export default class VelociousDatabaseDriversBaseTable {
50
76
  return tableData
51
77
  }
52
78
 
79
+ /**
80
+ * @returns {Promise<number>}
81
+ */
53
82
  async rowsCount() {
54
83
  const result = await this.getDriver().query(`SELECT COUNT(*) AS count FROM ${this.getOptions().quoteTableName(this.getName())}`)
55
84
 
56
85
  return digg(result, 0, "count")
57
86
  }
58
87
 
88
+ /**
89
+ * @returns {Promise<Array<Record<string, any>>>}
90
+ */
59
91
  async truncate(args) {
60
92
  const databaseType = this.getDriver().getType()
61
93
  let sql