velocious 1.0.727 → 1.0.729

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 (112) 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.js +26 -0
  9. package/build/database/database-name.js +25 -0
  10. package/build/database/drivers/base.js +60 -12
  11. package/build/database/drivers/mssql/index.js +27 -18
  12. package/build/database/drivers/mssql/table.js +28 -25
  13. package/build/database/drivers/mysql/index.js +25 -18
  14. package/build/database/drivers/mysql/table.js +31 -23
  15. package/build/database/drivers/pgsql/index.js +25 -18
  16. package/build/database/drivers/pgsql/table.js +28 -26
  17. package/build/database/drivers/sqlite/base.js +20 -9
  18. package/build/database/drivers/sqlite/table.js +32 -26
  19. package/build/database/pool/async-tracked-multi-connection.js +1 -0
  20. package/build/database/pool/base.js +14 -4
  21. package/build/database/pool/single-multi-use.js +1 -0
  22. package/build/database/query/model-class-query.js +85 -0
  23. package/build/database/record/index.js +33 -0
  24. package/build/environment-handlers/node/cli/commands/server.js +1 -0
  25. package/build/http-server/server-lock.js +28 -4
  26. package/build/src/application.d.ts +7 -4
  27. package/build/src/application.d.ts.map +1 -1
  28. package/build/src/application.js +27 -4
  29. package/build/src/background-jobs/main.d.ts +2 -1
  30. package/build/src/background-jobs/main.d.ts.map +1 -1
  31. package/build/src/background-jobs/main.js +10 -2
  32. package/build/src/background-jobs/worker.d.ts +42 -0
  33. package/build/src/background-jobs/worker.d.ts.map +1 -1
  34. package/build/src/background-jobs/worker.js +61 -5
  35. package/build/src/cli/process-args.d.ts +11 -0
  36. package/build/src/cli/process-args.d.ts.map +1 -0
  37. package/build/src/cli/process-args.js +34 -0
  38. package/build/src/configuration.d.ts +13 -0
  39. package/build/src/configuration.d.ts.map +1 -1
  40. package/build/src/configuration.js +22 -1
  41. package/build/src/database/database-name.d.ts +9 -0
  42. package/build/src/database/database-name.d.ts.map +1 -0
  43. package/build/src/database/database-name.js +23 -0
  44. package/build/src/database/drivers/base.d.ts +31 -2
  45. package/build/src/database/drivers/base.d.ts.map +1 -1
  46. package/build/src/database/drivers/base.js +56 -13
  47. package/build/src/database/drivers/mssql/index.d.ts +7 -2
  48. package/build/src/database/drivers/mssql/index.d.ts.map +1 -1
  49. package/build/src/database/drivers/mssql/index.js +25 -18
  50. package/build/src/database/drivers/mssql/table.d.ts.map +1 -1
  51. package/build/src/database/drivers/mssql/table.js +25 -23
  52. package/build/src/database/drivers/mysql/index.d.ts +7 -2
  53. package/build/src/database/drivers/mysql/index.d.ts.map +1 -1
  54. package/build/src/database/drivers/mysql/index.js +24 -19
  55. package/build/src/database/drivers/mysql/table.d.ts.map +1 -1
  56. package/build/src/database/drivers/mysql/table.js +27 -22
  57. package/build/src/database/drivers/pgsql/index.d.ts +7 -2
  58. package/build/src/database/drivers/pgsql/index.d.ts.map +1 -1
  59. package/build/src/database/drivers/pgsql/index.js +24 -19
  60. package/build/src/database/drivers/pgsql/table.d.ts.map +1 -1
  61. package/build/src/database/drivers/pgsql/table.js +25 -23
  62. package/build/src/database/drivers/sqlite/base.d.ts +8 -0
  63. package/build/src/database/drivers/sqlite/base.d.ts.map +1 -1
  64. package/build/src/database/drivers/sqlite/base.js +19 -10
  65. package/build/src/database/drivers/sqlite/table.d.ts.map +1 -1
  66. package/build/src/database/drivers/sqlite/table.js +29 -25
  67. package/build/src/database/pool/async-tracked-multi-connection.d.ts.map +1 -1
  68. package/build/src/database/pool/async-tracked-multi-connection.js +2 -1
  69. package/build/src/database/pool/base.d.ts +6 -0
  70. package/build/src/database/pool/base.d.ts.map +1 -1
  71. package/build/src/database/pool/base.js +14 -5
  72. package/build/src/database/pool/single-multi-use.d.ts.map +1 -1
  73. package/build/src/database/pool/single-multi-use.js +2 -1
  74. package/build/src/database/query/model-class-query.d.ts +20 -0
  75. package/build/src/database/query/model-class-query.d.ts.map +1 -1
  76. package/build/src/database/query/model-class-query.js +75 -1
  77. package/build/src/database/record/index.d.ts +46 -0
  78. package/build/src/database/record/index.d.ts.map +1 -1
  79. package/build/src/database/record/index.js +30 -1
  80. package/build/src/environment-handlers/node/cli/commands/server.d.ts.map +1 -1
  81. package/build/src/environment-handlers/node/cli/commands/server.js +2 -1
  82. package/build/src/http-server/server-lock.d.ts +23 -5
  83. package/build/src/http-server/server-lock.d.ts.map +1 -1
  84. package/build/src/http-server/server-lock.js +27 -5
  85. package/build/src/tenants/default-tenant-database-provisioning.d.ts +14 -0
  86. package/build/src/tenants/default-tenant-database-provisioning.d.ts.map +1 -1
  87. package/build/src/tenants/default-tenant-database-provisioning.js +31 -17
  88. package/build/tenants/default-tenant-database-provisioning.js +33 -19
  89. package/package.json +2 -2
  90. package/src/application.js +32 -3
  91. package/src/background-jobs/main.js +9 -1
  92. package/src/background-jobs/worker.js +69 -6
  93. package/src/cli/process-args.js +34 -0
  94. package/src/configuration.js +26 -0
  95. package/src/database/database-name.js +25 -0
  96. package/src/database/drivers/base.js +60 -12
  97. package/src/database/drivers/mssql/index.js +27 -18
  98. package/src/database/drivers/mssql/table.js +28 -25
  99. package/src/database/drivers/mysql/index.js +25 -18
  100. package/src/database/drivers/mysql/table.js +31 -23
  101. package/src/database/drivers/pgsql/index.js +25 -18
  102. package/src/database/drivers/pgsql/table.js +28 -26
  103. package/src/database/drivers/sqlite/base.js +20 -9
  104. package/src/database/drivers/sqlite/table.js +32 -26
  105. package/src/database/pool/async-tracked-multi-connection.js +1 -0
  106. package/src/database/pool/base.js +14 -4
  107. package/src/database/pool/single-multi-use.js +1 -0
  108. package/src/database/query/model-class-query.js +85 -0
  109. package/src/database/record/index.js +33 -0
  110. package/src/environment-handlers/node/cli/commands/server.js +1 -0
  111. package/src/http-server/server-lock.js +28 -4
  112. package/src/tenants/default-tenant-database-provisioning.js +33 -19
@@ -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,39 @@ 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
+ }
345
+
346
+ return tables
347
+ }
348
+
349
+ /**
350
+ * Lists database names on this server connection from `pg_database`.
351
+ * @returns {Promise<string[]>} - Resolves with the database names.
352
+ */
353
+ async listDatabases() {
354
+ const result = await this.query("SELECT datname FROM pg_database ORDER BY datname")
347
355
 
348
- return tables
349
- })
356
+ return result.map((row) => String(row.datname))
350
357
  }
351
358
 
352
359
  /**
353
360
  * Reads column metadata for many tables in one query instead of one catalog
354
361
  * join per table.
355
362
  * @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.
363
+ * @returns {Promise<Map<string, Array<Record<string, ReturnType<typeof JSON.parse>>>>>} - Raw column rows keyed by table name.
357
364
  */
358
365
  async batchTableColumns(tables) {
359
- /** @type {Map<string, Array<import("../base-column.js").default>>} */
360
- const columnsByTable = new Map()
366
+ /** @type {Map<string, Array<Record<string, ReturnType<typeof JSON.parse>>>>} */
367
+ const rowsByTable = new Map()
361
368
 
362
- if (tables.length === 0) return columnsByTable
369
+ if (tables.length === 0) return rowsByTable
363
370
 
364
371
  const tablesByName = new Map(tables.map((table) => [table.getName(), table]))
365
372
  const tableNamesSql = [...tablesByName.keys()].map((tableName) => this.quote(tableName)).join(", ")
@@ -396,13 +403,13 @@ export default class VelociousDatabaseDriversPgsql extends Base{
396
403
 
397
404
  if (!table) continue
398
405
 
399
- const columns = columnsByTable.get(tableName) || []
406
+ const tableRows = rowsByTable.get(tableName) || []
400
407
 
401
- columns.push(new Column(table, /** @type {Record<string, ReturnType<typeof JSON.parse>>} */ (row)))
402
- columnsByTable.set(tableName, columns)
408
+ tableRows.push(/** @type {Record<string, ReturnType<typeof JSON.parse>>} */ (row))
409
+ rowsByTable.set(tableName, tableRows)
403
410
  }
404
411
 
405
- return columnsByTable
412
+ return rowsByTable
406
413
  }
407
414
 
408
415
  /**
@@ -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,29 @@ 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
145
+ }
146
+
147
+ /**
148
+ * SQLite has no server-side database catalog: the driver's own file is the
149
+ * only database it can list. `PRAGMA database_list` reports the attached
150
+ * schemas, so the listing stays driver-native instead of a raw-SQL caller
151
+ * convention.
152
+ * @returns {Promise<string[]>} - Resolves with the attached database names.
153
+ */
154
+ async listDatabases() {
155
+ const result = await this.query("PRAGMA database_list")
156
+
157
+ return result.map((row) => String(row.name))
147
158
  }
148
159
 
149
160
  /**
@@ -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) {
@@ -869,6 +869,91 @@ export default class VelociousDatabaseQueryModelClassQuery extends DatabaseQuery
869
869
  await driver.query(sql, {logName: this.queryLogName("Update All"), signal: this._signal})
870
870
  }
871
871
 
872
+ /**
873
+ * Executes a bulk DELETE of all rows matching the query's WHERE clause in one
874
+ * statement. Unlike `destroyAll()`, which loads records and destroys them one
875
+ * by one, this bypasses model lifecycle callbacks and validations — the
876
+ * cleanup-teardown shape where a fixed handful of statements must purge a
877
+ * scope regardless of row count.
878
+ * @returns {Promise<number>} - Affected row count.
879
+ */
880
+ async deleteAll() {
881
+ const driver = this.driver
882
+ const tableName = this.getModelClass().tableName()
883
+ const joinsSql = new JoinsParser({pretty: false, query: this}).toSql()
884
+ const whereSql = new WhereParser({pretty: false, query: this}).toSql()
885
+ let sql
886
+
887
+ if (joinsSql.length > 0) {
888
+ // Subquery form for cross-driver compatibility (same rule as updateAll):
889
+ // drivers like SQLite cannot DELETE with JOINs.
890
+ const primaryKey = scalarModelPrimaryKey(this.getModelClass().primaryKey(), `${this.getModelClass().name}.deleteAll() with joins`)
891
+ const pk = driver.quoteColumn(primaryKey)
892
+ const qt = driver.quoteTable(tableName)
893
+
894
+ sql = `DELETE FROM ${qt} WHERE ${pk} IN (SELECT ${qt}.${pk} FROM ${qt}${joinsSql}${whereSql})`
895
+ } else {
896
+ sql = `DELETE FROM ${driver.quoteTable(tableName)}${whereSql}`
897
+ }
898
+
899
+ return await driver.affectedRows(sql, {logName: this.queryLogName("Delete All"), signal: this._signal})
900
+ }
901
+
902
+ /**
903
+ * Bulk-unlinks the rows matching this query: sets the given belongsTo
904
+ * relationship's foreign key column to NULL in one statement. The column is
905
+ * resolved through the relationship definition (never a caller-supplied
906
+ * column name), and the live schema must allow NULL for that column — a
907
+ * non-nullable FK fails loudly instead of issuing a doomed UPDATE. Lifecycle
908
+ * callbacks do not run (same contract as `updateAll`).
909
+ * @param {string} relationshipName - Declared belongsTo relationship to unlink.
910
+ * @returns {Promise<number>} - Affected row count.
911
+ */
912
+ async unlinkAll(relationshipName) {
913
+ const modelClass = this.getModelClass()
914
+ const relationship = modelClass.getRelationshipByName(relationshipName)
915
+
916
+ if (relationship.getType() != "belongsTo") {
917
+ throw new Error(`${modelClass.name}#${relationshipName} is a ${relationship.getType()} relationship — unlinkAll requires a belongsTo relationship`)
918
+ }
919
+
920
+ if (relationship.getPolymorphic()) {
921
+ // A polymorphic FK value is only meaningful together with its type column,
922
+ // which the framework cannot infer from the relationship alone. Fail closed
923
+ // instead of unlinking same-id rows of other target types; use
924
+ // `where({subjectId, subjectType}).updateAll({subjectId: null})` for that shape.
925
+ throw new Error(`${modelClass.name}#${relationshipName} is polymorphic — unlinkAll requires a non-polymorphic belongsTo relationship`)
926
+ }
927
+
928
+ const driver = this.driver
929
+ const tableName = modelClass.tableName()
930
+ const foreignKeyColumn = relationship.getForeignKey()
931
+ const column = modelClass.getColumnsHash()[foreignKeyColumn]
932
+
933
+ if (!column) throw new Error(`Couldn't unlink ${modelClass.name}#${relationshipName}: column ${foreignKeyColumn} was not found on ${tableName}`)
934
+
935
+ if (!column.getNull()) {
936
+ throw new Error(`Couldn't unlink ${modelClass.name}#${relationshipName}: column ${foreignKeyColumn} is NOT NULL — destroy the dependent rows instead of unlinking them`)
937
+ }
938
+
939
+ const joinsSql = new JoinsParser({pretty: false, query: this}).toSql()
940
+ const whereSql = new WhereParser({pretty: false, query: this}).toSql()
941
+ const setSql = `${driver.quoteColumn(foreignKeyColumn)} = NULL`
942
+ let sql
943
+
944
+ if (joinsSql.length > 0) {
945
+ const primaryKey = scalarModelPrimaryKey(modelClass.primaryKey(), `${modelClass.name}.unlinkAll() with joins`)
946
+ const pk = driver.quoteColumn(primaryKey)
947
+ const qt = driver.quoteTable(tableName)
948
+
949
+ sql = `UPDATE ${qt} SET ${setSql} WHERE ${pk} IN (SELECT ${qt}.${pk} FROM ${qt}${joinsSql}${whereSql})`
950
+ } else {
951
+ sql = `UPDATE ${driver.quoteTable(tableName)} SET ${setSql}${whereSql}`
952
+ }
953
+
954
+ return await driver.affectedRows(sql, {logName: this.queryLogName("Unlink All"), signal: this._signal})
955
+ }
956
+
872
957
  /**
873
958
  * Runs find.
874
959
  * @param {import("../../utils/model-primary-key.js").ModelPrimaryKeyValue} recordId - Record id.
@@ -3494,6 +3494,39 @@ class VelociousDatabaseRecord {
3494
3494
  return await this._newQuery().destroyAll()
3495
3495
  }
3496
3496
 
3497
+ /**
3498
+ * Bulk-deletes all rows of this model matching the given where hash in one
3499
+ * SQL statement. Unlike `destroyAll()`, no records are loaded and no
3500
+ * lifecycle callbacks or validations run — the cleanup/teardown shape where
3501
+ * a purge must cost one statement per table regardless of row count.
3502
+ * @template {typeof VelociousDatabaseRecord} MC
3503
+ * @this {MC}
3504
+ * @param {import("../query/index.js").WhereArgumentType} where - Where conditions (same resolution as `where(...)`).
3505
+ * @returns {Promise<number>} - Affected row count.
3506
+ */
3507
+ static async deleteWhere(where) {
3508
+ await this.ensureInitialized()
3509
+
3510
+ return await this._newQuery().where(where).deleteAll()
3511
+ }
3512
+
3513
+ /**
3514
+ * Bulk-unlinks rows of this model in one SQL statement: sets the given
3515
+ * belongsTo relationship's foreign key to NULL for every row matching the
3516
+ * where hash. The column comes from the relationship definition and the live
3517
+ * schema must allow NULL. No lifecycle callbacks run.
3518
+ * @template {typeof VelociousDatabaseRecord} MC
3519
+ * @this {MC}
3520
+ * @param {string} relationshipName - Declared belongsTo relationship to unlink.
3521
+ * @param {import("../query/index.js").WhereArgumentType} where - Where conditions (same resolution as `where(...)`).
3522
+ * @returns {Promise<number>} - Affected row count.
3523
+ */
3524
+ static async unlinkWhere(relationshipName, where) {
3525
+ await this.ensureInitialized()
3526
+
3527
+ return await this._newQuery().where(where).unlinkAll(relationshipName)
3528
+ }
3529
+
3497
3530
  /**
3498
3531
  * Runs pluck.
3499
3532
  * @template {typeof VelociousDatabaseRecord} MC
@@ -145,6 +145,7 @@ export default class VelociousCliCommandsServer extends BaseCommand{
145
145
  const application = new Application({
146
146
  configuration,
147
147
  httpServer,
148
+ httpServerLockOptOut: parsedProcessArgs["no-server-lock"] === true,
148
149
  type: "server"
149
150
  })
150
151
  const environment = configuration.getEnvironment()