velocious 1.0.726 → 1.0.728

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 (155) hide show
  1. package/README.md +2 -2
  2. package/bin/velocious.js +2 -19
  3. package/build/application.js +32 -3
  4. package/build/background-jobs/main.js +9 -1
  5. package/build/background-jobs/worker.js +69 -6
  6. package/build/bin/velocious.js +2 -19
  7. package/build/cli/process-args.js +34 -0
  8. package/build/configuration-types.js +1 -1
  9. package/build/configuration.js +43 -2
  10. package/build/database/advisory-lock-lock-server-backend.js +30 -24
  11. package/build/database/advisory-lock-lock-server-embedded-client.js +74 -0
  12. package/build/database/advisory-lock-lock-server-engine.js +544 -0
  13. package/build/database/advisory-lock-lock-server-local-client.js +260 -0
  14. package/build/database/advisory-lock-lock-server-socket-path.js +17 -0
  15. package/build/database/advisory-lock-lock-server-transport.js +30 -0
  16. package/build/database/drivers/base.js +34 -12
  17. package/build/database/drivers/mssql/index.js +17 -18
  18. package/build/database/drivers/mssql/table.js +28 -25
  19. package/build/database/drivers/mysql/index.js +15 -18
  20. package/build/database/drivers/mysql/table.js +31 -23
  21. package/build/database/drivers/pgsql/index.js +15 -18
  22. package/build/database/drivers/pgsql/table.js +28 -26
  23. package/build/database/drivers/sqlite/base.js +7 -9
  24. package/build/database/drivers/sqlite/table.js +32 -26
  25. package/build/database/pool/async-tracked-multi-connection.js +1 -0
  26. package/build/database/pool/base.js +14 -4
  27. package/build/database/pool/single-multi-use.js +1 -0
  28. package/build/database/record/auditing.js +89 -11
  29. package/build/database/record/index.js +1 -1
  30. package/build/environment-handlers/base.js +50 -9
  31. package/build/environment-handlers/node/advisory-lock-lock-server-local-server.js +206 -0
  32. package/build/environment-handlers/node/cli/commands/server.js +1 -0
  33. package/build/environment-handlers/node.js +30 -3
  34. package/build/http-server/server-lock.js +55 -18
  35. package/build/src/application.d.ts +7 -4
  36. package/build/src/application.d.ts.map +1 -1
  37. package/build/src/application.js +27 -4
  38. package/build/src/background-jobs/main.d.ts +2 -1
  39. package/build/src/background-jobs/main.d.ts.map +1 -1
  40. package/build/src/background-jobs/main.js +10 -2
  41. package/build/src/background-jobs/worker.d.ts +42 -0
  42. package/build/src/background-jobs/worker.d.ts.map +1 -1
  43. package/build/src/background-jobs/worker.js +61 -5
  44. package/build/src/cli/process-args.d.ts +11 -0
  45. package/build/src/cli/process-args.d.ts.map +1 -0
  46. package/build/src/cli/process-args.js +34 -0
  47. package/build/src/configuration-types.d.ts +3 -1
  48. package/build/src/configuration-types.d.ts.map +1 -1
  49. package/build/src/configuration-types.js +2 -2
  50. package/build/src/configuration.d.ts +16 -1
  51. package/build/src/configuration.d.ts.map +1 -1
  52. package/build/src/configuration.js +39 -3
  53. package/build/src/database/advisory-lock-lock-server-backend.d.ts +10 -3
  54. package/build/src/database/advisory-lock-lock-server-backend.d.ts.map +1 -1
  55. package/build/src/database/advisory-lock-lock-server-backend.js +25 -22
  56. package/build/src/database/advisory-lock-lock-server-embedded-client.d.ts +24 -0
  57. package/build/src/database/advisory-lock-lock-server-embedded-client.d.ts.map +1 -0
  58. package/build/src/database/advisory-lock-lock-server-embedded-client.js +64 -0
  59. package/build/src/database/advisory-lock-lock-server-engine.d.ts +285 -0
  60. package/build/src/database/advisory-lock-lock-server-engine.d.ts.map +1 -0
  61. package/build/src/database/advisory-lock-lock-server-engine.js +460 -0
  62. package/build/src/database/advisory-lock-lock-server-local-client.d.ts +114 -0
  63. package/build/src/database/advisory-lock-lock-server-local-client.d.ts.map +1 -0
  64. package/build/src/database/advisory-lock-lock-server-local-client.js +227 -0
  65. package/build/src/database/advisory-lock-lock-server-socket-path.d.ts +9 -0
  66. package/build/src/database/advisory-lock-lock-server-socket-path.d.ts.map +1 -0
  67. package/build/src/database/advisory-lock-lock-server-socket-path.js +16 -0
  68. package/build/src/database/advisory-lock-lock-server-transport.d.ts +18 -0
  69. package/build/src/database/advisory-lock-lock-server-transport.d.ts.map +1 -0
  70. package/build/src/database/advisory-lock-lock-server-transport.js +27 -0
  71. package/build/src/database/drivers/base.d.ts +15 -2
  72. package/build/src/database/drivers/base.d.ts.map +1 -1
  73. package/build/src/database/drivers/base.js +33 -13
  74. package/build/src/database/drivers/mssql/index.d.ts +2 -2
  75. package/build/src/database/drivers/mssql/index.d.ts.map +1 -1
  76. package/build/src/database/drivers/mssql/index.js +17 -18
  77. package/build/src/database/drivers/mssql/table.d.ts.map +1 -1
  78. package/build/src/database/drivers/mssql/table.js +25 -23
  79. package/build/src/database/drivers/mysql/index.d.ts +2 -2
  80. package/build/src/database/drivers/mysql/index.d.ts.map +1 -1
  81. package/build/src/database/drivers/mysql/index.js +16 -19
  82. package/build/src/database/drivers/mysql/table.d.ts.map +1 -1
  83. package/build/src/database/drivers/mysql/table.js +27 -22
  84. package/build/src/database/drivers/pgsql/index.d.ts +2 -2
  85. package/build/src/database/drivers/pgsql/index.d.ts.map +1 -1
  86. package/build/src/database/drivers/pgsql/index.js +16 -19
  87. package/build/src/database/drivers/pgsql/table.d.ts.map +1 -1
  88. package/build/src/database/drivers/pgsql/table.js +25 -23
  89. package/build/src/database/drivers/sqlite/base.d.ts.map +1 -1
  90. package/build/src/database/drivers/sqlite/base.js +8 -10
  91. package/build/src/database/drivers/sqlite/table.d.ts.map +1 -1
  92. package/build/src/database/drivers/sqlite/table.js +29 -25
  93. package/build/src/database/pool/async-tracked-multi-connection.d.ts.map +1 -1
  94. package/build/src/database/pool/async-tracked-multi-connection.js +2 -1
  95. package/build/src/database/pool/base.d.ts +6 -0
  96. package/build/src/database/pool/base.d.ts.map +1 -1
  97. package/build/src/database/pool/base.js +14 -5
  98. package/build/src/database/pool/single-multi-use.d.ts.map +1 -1
  99. package/build/src/database/pool/single-multi-use.js +2 -1
  100. package/build/src/database/record/auditing.d.ts +14 -4
  101. package/build/src/database/record/auditing.d.ts.map +1 -1
  102. package/build/src/database/record/auditing.js +77 -12
  103. package/build/src/database/record/index.d.ts +4 -2
  104. package/build/src/database/record/index.d.ts.map +1 -1
  105. package/build/src/database/record/index.js +2 -2
  106. package/build/src/environment-handlers/base.d.ts +86 -7
  107. package/build/src/environment-handlers/base.d.ts.map +1 -1
  108. package/build/src/environment-handlers/base.js +48 -9
  109. package/build/src/environment-handlers/node/advisory-lock-lock-server-local-server.d.ts +2 -0
  110. package/build/src/environment-handlers/node/advisory-lock-lock-server-local-server.d.ts.map +1 -0
  111. package/build/src/environment-handlers/node/advisory-lock-lock-server-local-server.js +179 -0
  112. package/build/src/environment-handlers/node/cli/commands/server.d.ts.map +1 -1
  113. package/build/src/environment-handlers/node/cli/commands/server.js +2 -1
  114. package/build/src/environment-handlers/node.d.ts +10 -4
  115. package/build/src/environment-handlers/node.d.ts.map +1 -1
  116. package/build/src/environment-handlers/node.js +30 -4
  117. package/build/src/http-server/server-lock.d.ts +43 -14
  118. package/build/src/http-server/server-lock.d.ts.map +1 -1
  119. package/build/src/http-server/server-lock.js +54 -19
  120. package/build/src/tenants/default-tenant-database-provisioning.d.ts.map +1 -1
  121. package/build/src/tenants/default-tenant-database-provisioning.js +7 -1
  122. package/build/tenants/default-tenant-database-provisioning.js +8 -0
  123. package/package.json +2 -2
  124. package/src/application.js +32 -3
  125. package/src/background-jobs/main.js +9 -1
  126. package/src/background-jobs/worker.js +69 -6
  127. package/src/cli/process-args.js +34 -0
  128. package/src/configuration-types.js +1 -1
  129. package/src/configuration.js +43 -2
  130. package/src/database/advisory-lock-lock-server-backend.js +30 -24
  131. package/src/database/advisory-lock-lock-server-embedded-client.js +74 -0
  132. package/src/database/advisory-lock-lock-server-engine.js +544 -0
  133. package/src/database/advisory-lock-lock-server-local-client.js +260 -0
  134. package/src/database/advisory-lock-lock-server-socket-path.js +17 -0
  135. package/src/database/advisory-lock-lock-server-transport.js +30 -0
  136. package/src/database/drivers/base.js +34 -12
  137. package/src/database/drivers/mssql/index.js +17 -18
  138. package/src/database/drivers/mssql/table.js +28 -25
  139. package/src/database/drivers/mysql/index.js +15 -18
  140. package/src/database/drivers/mysql/table.js +31 -23
  141. package/src/database/drivers/pgsql/index.js +15 -18
  142. package/src/database/drivers/pgsql/table.js +28 -26
  143. package/src/database/drivers/sqlite/base.js +7 -9
  144. package/src/database/drivers/sqlite/table.js +32 -26
  145. package/src/database/pool/async-tracked-multi-connection.js +1 -0
  146. package/src/database/pool/base.js +14 -4
  147. package/src/database/pool/single-multi-use.js +1 -0
  148. package/src/database/record/auditing.js +89 -11
  149. package/src/database/record/index.js +1 -1
  150. package/src/environment-handlers/base.js +50 -9
  151. package/src/environment-handlers/node/advisory-lock-lock-server-local-server.js +206 -0
  152. package/src/environment-handlers/node/cli/commands/server.js +1 -0
  153. package/src/environment-handlers/node.js +30 -3
  154. package/src/http-server/server-lock.js +55 -18
  155. package/src/tenants/default-tenant-database-provisioning.js +8 -0
@@ -18,22 +18,22 @@ export default class VelociousDatabaseDriversMysqlTable extends BaseTable {
18
18
  }
19
19
 
20
20
  async getColumns() {
21
- return await this.getDriver()._cachedTableSchemaMetadata(this.getName(), "columns", async () => {
22
- const result = await this.driver.query(`SHOW FULL COLUMNS FROM \`${this.getName()}\``)
23
- const columns = []
21
+ const columnRows = await this.getDriver()._cachedTableSchemaMetadata(this.getName(), "columns", async () => {
22
+ return await this.driver.query(`SHOW FULL COLUMNS FROM \`${this.getName()}\``)
23
+ })
24
+ const columns = []
24
25
 
25
- for (const data of result) {
26
- const column = new Column(this, data)
26
+ for (const data of columnRows) {
27
+ const column = new Column(this, data)
27
28
 
28
- columns.push(column)
29
- }
29
+ columns.push(column)
30
+ }
30
31
 
31
- return columns
32
- })
32
+ return columns
33
33
  }
34
34
 
35
35
  async getForeignKeys() {
36
- return await this.getDriver()._cachedTableSchemaMetadata(this.getName(), "foreignKeys", async () => {
36
+ const foreignKeyRows = await this.getDriver()._cachedTableSchemaMetadata(this.getName(), "foreignKeys", async () => {
37
37
  const sql = `
38
38
  SELECT TABLE_NAME, COLUMN_NAME, CONSTRAINT_NAME, REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME
39
39
  FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
@@ -42,21 +42,21 @@ export default class VelociousDatabaseDriversMysqlTable extends BaseTable {
42
42
  TABLE_NAME = ${this.driver.quote(this.getName())}
43
43
  `
44
44
 
45
- const foreignKeyRows = await this.driver.query(sql)
46
- const foreignKeys = []
45
+ return await this.driver.query(sql)
46
+ })
47
+ const foreignKeys = []
47
48
 
48
- for (const foreignKeyRow of foreignKeyRows) {
49
- const foreignKey = new ForeignKey(foreignKeyRow)
49
+ for (const foreignKeyRow of foreignKeyRows) {
50
+ const foreignKey = new ForeignKey(foreignKeyRow)
50
51
 
51
- foreignKeys.push(foreignKey)
52
- }
52
+ foreignKeys.push(foreignKey)
53
+ }
53
54
 
54
- return foreignKeys
55
- })
55
+ return foreignKeys
56
56
  }
57
57
 
58
58
  async getIndexes() {
59
- return await this.getDriver()._cachedTableSchemaMetadata(this.getName(), "indexes", async () => {
59
+ const groupedIndexRows = await this.getDriver()._cachedTableSchemaMetadata(this.getName(), "indexes", async () => {
60
60
  const options = this.getOptions()
61
61
  const sql = `
62
62
  SELECT
@@ -76,7 +76,7 @@ export default class VelociousDatabaseDriversMysqlTable extends BaseTable {
76
76
  SEQ_IN_INDEX
77
77
  `
78
78
  const indexesRows = await this.getDriver().query(sql)
79
- const indexes = []
79
+ const groupedRows = []
80
80
  const indexRowsByName = new Map()
81
81
 
82
82
  for (const indexRow of indexesRows) {
@@ -100,15 +100,23 @@ export default class VelociousDatabaseDriversMysqlTable extends BaseTable {
100
100
  const groupedIndexRow = Object.assign({}, indexRow, {
101
101
  column_names: [indexRow.COLUMN_NAME]
102
102
  })
103
- const index = new ColumnsIndex(this, groupedIndexRow)
104
103
 
105
104
  indexRowsByName.set(indexRow.index_name, groupedIndexRow)
106
- indexes.push(index)
105
+ groupedRows.push(groupedIndexRow)
107
106
  }
108
107
  }
109
108
 
110
- return indexes
109
+ return groupedRows
111
110
  })
111
+ const indexes = []
112
+
113
+ for (const groupedIndexRow of groupedIndexRows) {
114
+ const index = new ColumnsIndex(this, groupedIndexRow)
115
+
116
+ indexes.push(index)
117
+ }
118
+
119
+ return indexes
112
120
  }
113
121
 
114
122
  /**
@@ -3,7 +3,6 @@
3
3
  import AlterTable from "./sql/alter-table.js"
4
4
  import wait from "awaitery/build/wait.js"
5
5
  import Base from "../base.js"
6
- import Column from "./column.js"
7
6
  import { Client, types as pgTypes } from "pg"
8
7
  import CreateDatabase from "./sql/create-database.js"
9
8
  import CreateIndex from "./sql/create-index.js"
@@ -335,31 +334,29 @@ export default class VelociousDatabaseDriversPgsql extends Base{
335
334
  * @returns {Promise<Array<import("../base-table.js").default>>} - Resolves with the tables.
336
335
  */
337
336
  async getTables() {
338
- return await this._cachedSchemaMetadata("tables", async () => {
339
- const result = await this.query("SELECT * FROM information_schema.tables WHERE table_catalog = CURRENT_DATABASE() AND table_schema = 'public'")
340
- const tables = []
337
+ const tableRows = await this._cachedSchemaMetadata("tables", async () => await this.query("SELECT * FROM information_schema.tables WHERE table_catalog = CURRENT_DATABASE() AND table_schema = 'public'"))
338
+ const tables = []
341
339
 
342
- for (const row of result) {
343
- const table = new Table(this, /** @type {Record<string, string>} */ (row))
340
+ for (const row of tableRows) {
341
+ const table = new Table(this, /** @type {Record<string, string>} */ (row))
344
342
 
345
- tables.push(table)
346
- }
343
+ tables.push(table)
344
+ }
347
345
 
348
- return tables
349
- })
346
+ return tables
350
347
  }
351
348
 
352
349
  /**
353
350
  * Reads column metadata for many tables in one query instead of one catalog
354
351
  * join per table.
355
352
  * @param {Array<import("../base-table.js").default>} tables - Tables to load columns for.
356
- * @returns {Promise<Map<string, Array<import("../base-column.js").default>>>} - Columns keyed by table name.
353
+ * @returns {Promise<Map<string, Array<Record<string, ReturnType<typeof JSON.parse>>>>>} - Raw column rows keyed by table name.
357
354
  */
358
355
  async batchTableColumns(tables) {
359
- /** @type {Map<string, Array<import("../base-column.js").default>>} */
360
- const columnsByTable = new Map()
356
+ /** @type {Map<string, Array<Record<string, ReturnType<typeof JSON.parse>>>>} */
357
+ const rowsByTable = new Map()
361
358
 
362
- if (tables.length === 0) return columnsByTable
359
+ if (tables.length === 0) return rowsByTable
363
360
 
364
361
  const tablesByName = new Map(tables.map((table) => [table.getName(), table]))
365
362
  const tableNamesSql = [...tablesByName.keys()].map((tableName) => this.quote(tableName)).join(", ")
@@ -396,13 +393,13 @@ export default class VelociousDatabaseDriversPgsql extends Base{
396
393
 
397
394
  if (!table) continue
398
395
 
399
- const columns = columnsByTable.get(tableName) || []
396
+ const tableRows = rowsByTable.get(tableName) || []
400
397
 
401
- columns.push(new Column(table, /** @type {Record<string, ReturnType<typeof JSON.parse>>} */ (row)))
402
- columnsByTable.set(tableName, columns)
398
+ tableRows.push(/** @type {Record<string, ReturnType<typeof JSON.parse>>} */ (row))
399
+ rowsByTable.set(tableName, tableRows)
403
400
  }
404
401
 
405
- return columnsByTable
402
+ return rowsByTable
406
403
  }
407
404
 
408
405
  /**
@@ -63,7 +63,7 @@ export default class VelociousDatabaseDriversPgsqlTable extends BaseTable {
63
63
  }
64
64
 
65
65
  async getColumns() {
66
- return await this.getDriver()._cachedTableSchemaMetadata(this.getName(), "columns", async () => {
66
+ const columnRows = await this.getDriver()._cachedTableSchemaMetadata(this.getName(), "columns", async () => {
67
67
  const result = await this.driver.query(`
68
68
  SELECT
69
69
  columns.*,
@@ -89,20 +89,21 @@ export default class VelociousDatabaseDriversPgsqlTable extends BaseTable {
89
89
  columns.table_schema = 'public' AND
90
90
  columns.table_name = '${this.getName()}'
91
91
  `)
92
- const columns = []
92
+ return result
93
+ })
94
+ const columns = []
93
95
 
94
- for (const data of result) {
95
- const column = new Column(this, data)
96
+ for (const data of columnRows) {
97
+ const column = new Column(this, data)
96
98
 
97
- columns.push(column)
98
- }
99
+ columns.push(column)
100
+ }
99
101
 
100
- return columns
101
- })
102
+ return columns
102
103
  }
103
104
 
104
105
  async getForeignKeys() {
105
- return await this.getDriver()._cachedTableSchemaMetadata(this.getName(), "foreignKeys", async () => {
106
+ const foreignKeyRows = await this.getDriver()._cachedTableSchemaMetadata(this.getName(), "foreignKeys", async () => {
106
107
  const sql = `
107
108
  SELECT
108
109
  tc.constraint_name,
@@ -126,21 +127,21 @@ export default class VelociousDatabaseDriversPgsqlTable extends BaseTable {
126
127
  tc.table_name = ${this.getDriver().quote(this.getName())}
127
128
  `
128
129
 
129
- const foreignKeyRows = await this.getDriver().query(sql)
130
- const foreignKeys = []
130
+ return await this.getDriver().query(sql)
131
+ })
132
+ const foreignKeys = []
131
133
 
132
- for (const foreignKeyRow of foreignKeyRows) {
133
- const foreignKey = new ForeignKey(foreignKeyRow)
134
+ for (const foreignKeyRow of foreignKeyRows) {
135
+ const foreignKey = new ForeignKey(foreignKeyRow)
134
136
 
135
- foreignKeys.push(foreignKey)
136
- }
137
+ foreignKeys.push(foreignKey)
138
+ }
137
139
 
138
- return foreignKeys
139
- })
140
+ return foreignKeys
140
141
  }
141
142
 
142
143
  async getIndexes() {
143
- return await this.getDriver()._cachedTableSchemaMetadata(this.getName(), "indexes", async () => {
144
+ const groupedIndexRows = await this.getDriver()._cachedTableSchemaMetadata(this.getName(), "indexes", async () => {
144
145
  const options = this.getOptions()
145
146
 
146
147
  const indexesRows = await this.getDriver().query(`
@@ -162,18 +163,19 @@ export default class VelociousDatabaseDriversPgsqlTable extends BaseTable {
162
163
  index_columns.ordinal_position
163
164
  `)
164
165
 
165
- const indexes = []
166
-
167
166
  const indexRows = indexesRows.map((indexRow) => normalizeIndexMetadataRow(indexRow))
168
167
 
169
- for (const indexData of groupPgsqlIndexRows(indexRows)) {
170
- const columnsIndex = new ColumnsIndex(this, indexData)
168
+ return groupPgsqlIndexRows(indexRows)
169
+ })
170
+ const indexes = []
171
171
 
172
- indexes.push(columnsIndex)
173
- }
172
+ for (const indexData of groupedIndexRows) {
173
+ const columnsIndex = new ColumnsIndex(this, indexData)
174
174
 
175
- return indexes
176
- })
175
+ indexes.push(columnsIndex)
176
+ }
177
+
178
+ return indexes
177
179
  }
178
180
 
179
181
  /**
@@ -132,18 +132,16 @@ export default class VelociousDatabaseDriversSqliteBase extends Base {
132
132
  * @returns {Promise<Array<import("../base-table.js").default>>} - Resolves with the tables.
133
133
  */
134
134
  async getTables() {
135
- return await this._cachedSchemaMetadata("tables", async () => {
136
- const result = await this.query("SELECT name FROM sqlite_master WHERE type = 'table' ORDER BY name")
137
- const tables = []
135
+ const tableRows = await this._cachedSchemaMetadata("tables", async () => await this.query("SELECT name FROM sqlite_master WHERE type = 'table' ORDER BY name"))
136
+ const tables = []
138
137
 
139
- for (const row of result) {
140
- const table = new Table({driver: this, row: /** @type {Record<string, string | number | null>} */ (row)})
138
+ for (const row of tableRows) {
139
+ const table = new Table({driver: this, row: /** @type {Record<string, string | number | null>} */ (row)})
141
140
 
142
- tables.push(table)
143
- }
141
+ tables.push(table)
142
+ }
144
143
 
145
- return tables
146
- })
144
+ return tables
147
145
  }
148
146
 
149
147
  /**
@@ -23,39 +23,39 @@ export default class VelociousDatabaseDriversSqliteTable extends BaseTable {
23
23
  * @returns {Promise<Array<import("../base-column.js").default>>} - Resolves with the columns.
24
24
  */
25
25
  async getColumns() {
26
- return await this.getDriver()._cachedTableSchemaMetadata(this.getName(), "columns", async () => {
27
- const result = await this.driver.query(`PRAGMA table_info('${this.getName()}')`)
28
- const columns = []
26
+ const columnRows = await this.getDriver()._cachedTableSchemaMetadata(this.getName(), "columns", async () => {
27
+ return await this.driver.query(`PRAGMA table_info('${this.getName()}')`)
28
+ })
29
+ const columns = []
29
30
 
30
- for (const columnData of result) {
31
- const column = new Column({column: columnData, driver: this.driver, table: this})
31
+ for (const columnData of columnRows) {
32
+ const column = new Column({column: columnData, driver: this.driver, table: this})
32
33
 
33
- columns.push(column)
34
- }
34
+ columns.push(column)
35
+ }
35
36
 
36
- return columns
37
- })
37
+ return columns
38
38
  }
39
39
 
40
40
  async getForeignKeys() {
41
- return await this.getDriver()._cachedTableSchemaMetadata(this.getName(), "foreignKeys", async () => {
42
- const foreignKeysData = await this.driver.query(`SELECT * FROM pragma_foreign_key_list(${this.driver.quote(this.getName())})`)
43
- const foreignKeys = []
41
+ const foreignKeysData = await this.getDriver()._cachedTableSchemaMetadata(this.getName(), "foreignKeys", async () => {
42
+ return await this.driver.query(`SELECT * FROM pragma_foreign_key_list(${this.driver.quote(this.getName())})`)
43
+ })
44
+ const foreignKeys = []
44
45
 
45
- for (const foreignKeyData of foreignKeysData) {
46
- const foreignKey = new ForeignKey(foreignKeyData, {tableName: this.getName()})
46
+ for (const foreignKeyData of foreignKeysData) {
47
+ const foreignKey = new ForeignKey(foreignKeyData, {tableName: this.getName()})
47
48
 
48
- foreignKeys.push(foreignKey)
49
- }
49
+ foreignKeys.push(foreignKey)
50
+ }
50
51
 
51
- return foreignKeys
52
- })
52
+ return foreignKeys
53
53
  }
54
54
 
55
55
  async getIndexes() {
56
- return await this.getDriver()._cachedTableSchemaMetadata(this.getName(), "indexes", async () => {
56
+ const indexDataRows = await this.getDriver()._cachedTableSchemaMetadata(this.getName(), "indexes", async () => {
57
57
  const rows = await this.getDriver().query(`PRAGMA index_list(${this.getOptions().quoteTableName(this.getName())})`)
58
- const indexes = []
58
+ const cachedIndexRows = []
59
59
 
60
60
  for (const row of rows) {
61
61
  const indexName = row.name
@@ -65,21 +65,27 @@ export default class VelociousDatabaseDriversSqliteTable extends BaseTable {
65
65
  continue
66
66
  }
67
67
 
68
- const columnsIndex = new ColumnsIndex(this, row)
69
- const indexMasterData = await this.getDriver().query(`SELECT * FROM sqlite_master WHERE type = 'index' AND name = ${this.getOptions().quote(columnsIndex.getName())}`)
68
+ const indexMasterData = await this.getDriver().query(`SELECT * FROM sqlite_master WHERE type = 'index' AND name = ${this.getOptions().quote(indexName)}`)
70
69
  const sql = indexMasterData[0]?.sql
71
70
 
72
- if (!sql) throw new Error(`Could not find SQL for index ${columnsIndex.getName()}`)
71
+ if (!sql) throw new Error(`Could not find SQL for index ${indexName}`)
73
72
 
74
- const indexData = /** @type {typeof columnsIndex.data & {columnNames?: string[]}} */ (columnsIndex.data)
73
+ const indexData = /** @type {Record<string, ReturnType<typeof JSON.parse>> & {columnNames?: string[]}} */ (row)
75
74
 
76
75
  indexData.columnNames = this._parseColumnsFromSQL(String(sql))
77
76
 
78
- indexes.push(columnsIndex)
77
+ cachedIndexRows.push(indexData)
79
78
  }
80
79
 
81
- return indexes
80
+ return cachedIndexRows
82
81
  })
82
+ const indexes = []
83
+
84
+ for (const cachedIndexData of indexDataRows) {
85
+ indexes.push(new ColumnsIndex(this, cachedIndexData))
86
+ }
87
+
88
+ return indexes
83
89
  }
84
90
 
85
91
  /**
@@ -1056,6 +1056,7 @@ export default class VelociousDatabasePoolAsyncTrackedMultiConnection extends Ba
1056
1056
  const DriverClass = databaseConfiguration.driver || this.driverClass
1057
1057
  if (!DriverClass) throw new Error("No driver class configured for frontend tenant SQLite deletion")
1058
1058
  await new DriverClass(databaseConfiguration, this.configuration).deleteDatabaseStorage()
1059
+ this.configuration.clearSchemaCachesForReuseKey(this.getConfigurationReuseKey(databaseConfiguration))
1059
1060
  }
1060
1061
 
1061
1062
  capturedConnectionInUse(/** @type {import("../../configuration-types.js").DatabaseConfigurationType} */ databaseConfiguration) {
@@ -270,11 +270,11 @@ class VelociousDatabasePoolBase {
270
270
  }
271
271
 
272
272
  /**
273
- * Runs get configuration reuse key.
274
- * @param {import("../../configuration-types.js").DatabaseConfigurationType} [databaseConfiguration] - Resolved configuration to identify.
275
- * @returns {string} - Reuse key for the currently resolved database configuration.
273
+ * Computes the physical-database reuse key for an explicit configuration, matching what a pool would stamp on its connections.
274
+ * @param {import("../../configuration-types.js").DatabaseConfigurationType} databaseConfiguration - Resolved configuration to identify.
275
+ * @returns {string} - Reuse key for the given database configuration.
276
276
  */
277
- getConfigurationReuseKey(databaseConfiguration = this.getConfiguration()) {
277
+ static configurationReuseKey(databaseConfiguration) {
278
278
  return stableStringify({
279
279
  database: databaseConfiguration.database,
280
280
  host: databaseConfiguration.host,
@@ -289,6 +289,15 @@ class VelociousDatabasePoolBase {
289
289
  })
290
290
  }
291
291
 
292
+ /**
293
+ * Runs get configuration reuse key.
294
+ * @param {import("../../configuration-types.js").DatabaseConfigurationType} [databaseConfiguration] - Resolved configuration to identify.
295
+ * @returns {string} - Reuse key for the currently resolved database configuration.
296
+ */
297
+ getConfigurationReuseKey(databaseConfiguration = this.getConfiguration()) {
298
+ return VelociousDatabasePoolBase.configurationReuseKey(databaseConfiguration)
299
+ }
300
+
292
301
  /**
293
302
  * Runs connection matches current configuration.
294
303
  * @param {import("../drivers/base.js").default} connection - Connection.
@@ -401,6 +410,7 @@ class VelociousDatabasePoolBase {
401
410
  this.clearSchemaCache()
402
411
  this.configuration.clearSchemaCachesForReuseKey(reuseKey)
403
412
  })
413
+ connection.setSchemaCacheMapProvider(() => this.configuration.schemaCacheMapForReuseKey(reuseKey))
404
414
  }
405
415
 
406
416
  /**
@@ -329,6 +329,7 @@ export default class VelociousDatabasePoolSingleMultiUser extends BasePool {
329
329
  if (!DriverClass) throw new Error("No driver class configured for frontend tenant SQLite deletion")
330
330
  const driver = new DriverClass(databaseConfiguration, this.configuration)
331
331
  await driver.deleteDatabaseStorage()
332
+ this.configuration.clearSchemaCachesForReuseKey(this.getConfigurationReuseKey(databaseConfiguration))
332
333
  }
333
334
 
334
335
  capturedConnectionInUse(/** @type {import("../../configuration-types.js").DatabaseConfigurationType} */ databaseConfiguration) {
@@ -1,6 +1,7 @@
1
1
  // @ts-check
2
2
 
3
3
  import UUID from "pure-uuid"
4
+ import * as inflection from "inflection"
4
5
  import HasManyRelationship from "./relationships/has-many.js"
5
6
  import {scalarModelPrimaryKey, scalarModelPrimaryKeyValue} from "../../utils/model-primary-key.js"
6
7
 
@@ -39,10 +40,19 @@ import {scalarModelPrimaryKey, scalarModelPrimaryKeyValue} from "../../utils/mod
39
40
  * @property {Record<string, ReturnType<typeof JSON.parse>> | null} [params] - Optional metadata to store with the audit.
40
41
  */
41
42
 
43
+ /**
44
+ * AuditedOptions type.
45
+ * @typedef {object} AuditedOptions
46
+ * @property {string[]} [only] - When set, only these attribute names are written to the audit trail.
47
+ * @property {string[]} [except] - Attribute names that must never be written to the audit trail.
48
+ */
49
+
42
50
  /**
43
51
  * AuditedModelClass type.
44
52
  * @typedef {typeof import("./index.js").default & {
45
53
  * _auditCallbacks?: Record<string, AuditCallback[]>,
54
+ * _auditIncludedAttributes?: Set<string> | null,
55
+ * _auditExcludedAttributes?: Set<string>,
46
56
  * _auditLifecycleCallbacksRegistered?: boolean,
47
57
  * _auditTableResolved?: boolean,
48
58
  * _auditTableData?: AuditTableData
@@ -207,12 +217,12 @@ async function buildAuditTableData(modelClass, connection, databaseIdentity) {
207
217
 
208
218
  if (dedicatedExists) {
209
219
  const auditClass = dedicatedAuditClass(modelClass, dedicatedTable)
210
- const modelKey = modelParamKey(modelClass)
220
+ const foreignKey = modelForeignKey(modelClass)
211
221
 
212
222
  return {
213
223
  auditClass,
214
224
  dedicated: true,
215
- foreignKey: `${modelKey}_id`,
225
+ foreignKey,
216
226
  tableName: dedicatedTable
217
227
  }
218
228
  }
@@ -411,16 +421,59 @@ function modelParamKey(modelClass) {
411
421
  return name.charAt(0).toLowerCase() + name.slice(1)
412
422
  }
413
423
 
424
+ /**
425
+ * Returns the database foreign-key column for a model class.
426
+ * @param {AuditedModelClass} modelClass - Audited model class.
427
+ * @returns {string} e.g. "project_id" for "Project".
428
+ */
429
+ function modelForeignKey(modelClass) {
430
+ return `${inflection.underscore(modelClass.getModelName())}_id`
431
+ }
432
+
414
433
  // ---------------------------------------------------------------------------
415
434
  // Registration (sync — callbacks only; table detection deferred)
416
435
  // ---------------------------------------------------------------------------
417
436
 
437
+ /**
438
+ * Validates audited() options and returns the attribute filter sets.
439
+ * Fails loudly at registration time on invalid option shapes.
440
+ * @param {AuditedModelClass} modelClass - Model class being registered (used in error messages).
441
+ * @param {AuditedOptions} options - Raw options passed to audited().
442
+ * @returns {{excludedAttributes: Set<string>, includedAttributes: Set<string> | null}} Validated filter sets.
443
+ */
444
+ function parseAuditAttributeFilterOptions(modelClass, options) {
445
+ if (options === null || typeof options !== "object" || Array.isArray(options)) {
446
+ throw new Error(`Invalid audited() options for ${modelClass.getModelName()}: expected an options object with 'only' and/or 'except' attribute name lists`)
447
+ }
448
+
449
+ for (const [optionName, optionValue] of [["only", options.only], ["except", options.except]]) {
450
+ if (optionValue === undefined) {
451
+ continue
452
+ }
453
+
454
+ if (!Array.isArray(optionValue) || optionValue.some((attributeName) => typeof attributeName !== "string" || attributeName.length <= 0)) {
455
+ throw new Error(`Invalid audited() '${optionName}' option for ${modelClass.getModelName()}: expected an array of attribute name strings`)
456
+ }
457
+ }
458
+
459
+ const includedAttributes = options.only === undefined ? null : new Set(options.only)
460
+ const excludedAttributes = new Set(options.except || [])
461
+
462
+ for (const attributeName of excludedAttributes) {
463
+ if (includedAttributes?.has(attributeName)) {
464
+ throw new Error(`Invalid audited() options for ${modelClass.getModelName()}: '${attributeName}' appears in both 'only' and 'except'`)
465
+ }
466
+ }
467
+
468
+ return {excludedAttributes, includedAttributes}
469
+ }
470
+
418
471
  /**
419
472
  * Registers lifecycle callbacks for automatic create/update/destroy auditing.
420
473
  * Table detection and relationship registration happen lazily on first usage.
421
474
  * Called synchronously from Model.audited() at module-load time.
422
475
  * @param {AuditedModelClass} modelClass - Model class to audit.
423
- * @param {{except?: string[]}} [options] - Audit options: `except` lists attribute names never written to the audit trail (e.g. high-frequency telemetry columns).
476
+ * @param {AuditedOptions} [options] - Audit options: `only` lists the only attribute names written to the audit trail; `except` lists attribute names never written to the audit trail (e.g. high-frequency telemetry columns). Invalid option shapes throw at registration time.
424
477
  * @returns {void}
425
478
  */
426
479
  function registerAuditing(modelClass, options = {}) {
@@ -428,10 +481,12 @@ function registerAuditing(modelClass, options = {}) {
428
481
  return
429
482
  }
430
483
 
431
- const auditableModelClass = /** @type {AuditedModelClass & {_auditExcludedAttributes?: Set<string>}} */ (modelClass)
484
+ const {excludedAttributes, includedAttributes} = parseAuditAttributeFilterOptions(modelClass, options)
485
+ const auditableModelClass = /** @type {AuditedModelClass & {_auditIncludedAttributes?: Set<string> | null, _auditExcludedAttributes?: Set<string>}} */ (modelClass)
432
486
 
433
487
  auditableModelClass._auditLifecycleCallbacksRegistered = true
434
- auditableModelClass._auditExcludedAttributes = new Set(options.except || [])
488
+ auditableModelClass._auditIncludedAttributes = includedAttributes
489
+ auditableModelClass._auditExcludedAttributes = excludedAttributes
435
490
  registerAuditRelationship(modelClass)
436
491
  modelClass.beforeCreate("captureCreateAuditChanges")
437
492
  modelClass.afterCreate("createCreateAudit")
@@ -666,14 +721,14 @@ async function createAuditWithCurrentConnection(record, args, modelClass) {
666
721
  const auditId = new UUID(4).format()
667
722
 
668
723
  if (tableData.dedicated) {
669
- const modelKey = modelParamKey(modelClass)
724
+ const foreignKey = modelForeignKey(modelClass)
670
725
 
671
726
  await db.query(db.insertSql({
672
727
  returnLastInsertedColumnNames: ["id"],
673
728
  tableName: tableData.tableName,
674
729
  data: {
675
730
  id: auditId,
676
- [`${modelKey}_id`]: recordId,
731
+ [foreignKey]: recordId,
677
732
  audit_action_id: auditActionId,
678
733
  audited_changes: auditedChanges,
679
734
  params,
@@ -805,8 +860,9 @@ function auditChangesForCurrentChanges(record) {
805
860
  const changes = record.changes()
806
861
  /** @type {AuditChanges} */
807
862
  const auditedChanges = {}
808
- const modelClass = /** @type {AuditedModelClass & {_auditExcludedAttributes?: Set<string>}} */ (record.getModelClass())
863
+ const modelClass = /** @type {AuditedModelClass & {_auditIncludedAttributes?: Set<string> | null, _auditExcludedAttributes?: Set<string>}} */ (record.getModelClass())
809
864
  const columnNameToAttributeName = modelClass.getColumnNameToAttributeNameMap()
865
+ const includedAttributes = modelClass._auditIncludedAttributes || null
810
866
  const excludedAttributes = modelClass._auditExcludedAttributes
811
867
 
812
868
  for (const [attributeName, change] of Object.entries(changes)) {
@@ -816,6 +872,10 @@ function auditChangesForCurrentChanges(record) {
816
872
  continue
817
873
  }
818
874
 
875
+ if (includedAttributes && !(includedAttributes.has(auditedAttributeName) || includedAttributes.has(attributeName))) {
876
+ continue
877
+ }
878
+
819
879
  auditedChanges[auditedAttributeName] = change[1]
820
880
  }
821
881
 
@@ -828,7 +888,25 @@ function auditChangesForCurrentChanges(record) {
828
888
  * @returns {AuditChanges} Current attributes keyed by attribute name.
829
889
  */
830
890
  function auditChangesForDestroy(record) {
831
- return {...record.attributes()}
891
+ const modelClass = /** @type {AuditedModelClass & {_auditIncludedAttributes?: Set<string> | null, _auditExcludedAttributes?: Set<string>}} */ (record.getModelClass())
892
+ const includedAttributes = modelClass._auditIncludedAttributes || null
893
+ const excludedAttributes = modelClass._auditExcludedAttributes
894
+ /** @type {AuditChanges} */
895
+ const auditedChanges = {}
896
+
897
+ for (const [attributeName, value] of Object.entries(record.attributes())) {
898
+ if (excludedAttributes?.has(attributeName)) {
899
+ continue
900
+ }
901
+
902
+ if (includedAttributes && !includedAttributes.has(attributeName)) {
903
+ continue
904
+ }
905
+
906
+ auditedChanges[attributeName] = value
907
+ }
908
+
909
+ return auditedChanges
832
910
  }
833
911
 
834
912
  // ---------------------------------------------------------------------------
@@ -856,10 +934,10 @@ function withoutAudit(modelClass, action) {
856
934
  const auditActionsActionSql = `${auditActionsTableSql}.${db.quoteColumn("action")}`
857
935
 
858
936
  if (tableData?.dedicated) {
859
- const modelKey = modelParamKey(modelClass)
937
+ const foreignKey = modelForeignKey(modelClass)
860
938
  const auditsTableSql = db.quoteTable(tableData.tableName)
861
939
  const auditActionIdSql = `${auditsTableSql}.${db.quoteColumn("audit_action_id")}`
862
- const modelIdSql = `${auditsTableSql}.${db.quoteColumn(`${modelKey}_id`)}`
940
+ const modelIdSql = `${auditsTableSql}.${db.quoteColumn(foreignKey)}`
863
941
 
864
942
  return modelClass
865
943
  .all()
@@ -740,7 +740,7 @@ class VelociousDatabaseRecord {
740
740
 
741
741
  /**
742
742
  * Enables automatic create/update/destroy auditing for this model.
743
- * @param {{except?: string[]}} [options] - Audit options: `except` lists attribute names that must never be written to the audit trail. Use it for high-frequency telemetry columns (heartbeats, progress counters) whose write volume turns the audit table into a deadlock partner for the parent row.
743
+ * @param {{except?: string[], only?: string[]}} [options] - Audit options: `except` lists attribute names that must never be written to the audit trail. Use it for high-frequency telemetry columns (heartbeats, progress counters) whose write volume turns the audit table into a deadlock partner for the parent row. `only` lists the only attribute names written to the audit trail; an update whose changes are all filtered out writes no audit row at all. `only` and `except` cannot be combined. Invalid option shapes throw at registration time.
744
744
  * @returns {void}
745
745
  */
746
746
  static audited(options = {}) {