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.
- package/README.md +2 -2
- package/bin/velocious.js +2 -19
- package/build/application.js +32 -3
- package/build/background-jobs/main.js +9 -1
- package/build/background-jobs/worker.js +69 -6
- package/build/bin/velocious.js +2 -19
- package/build/cli/process-args.js +34 -0
- package/build/configuration-types.js +1 -1
- package/build/configuration.js +43 -2
- package/build/database/advisory-lock-lock-server-backend.js +30 -24
- package/build/database/advisory-lock-lock-server-embedded-client.js +74 -0
- package/build/database/advisory-lock-lock-server-engine.js +544 -0
- package/build/database/advisory-lock-lock-server-local-client.js +260 -0
- package/build/database/advisory-lock-lock-server-socket-path.js +17 -0
- package/build/database/advisory-lock-lock-server-transport.js +30 -0
- package/build/database/drivers/base.js +34 -12
- package/build/database/drivers/mssql/index.js +17 -18
- package/build/database/drivers/mssql/table.js +28 -25
- package/build/database/drivers/mysql/index.js +15 -18
- package/build/database/drivers/mysql/table.js +31 -23
- package/build/database/drivers/pgsql/index.js +15 -18
- package/build/database/drivers/pgsql/table.js +28 -26
- package/build/database/drivers/sqlite/base.js +7 -9
- package/build/database/drivers/sqlite/table.js +32 -26
- package/build/database/pool/async-tracked-multi-connection.js +1 -0
- package/build/database/pool/base.js +14 -4
- package/build/database/pool/single-multi-use.js +1 -0
- package/build/database/record/auditing.js +89 -11
- package/build/database/record/index.js +1 -1
- package/build/environment-handlers/base.js +50 -9
- package/build/environment-handlers/node/advisory-lock-lock-server-local-server.js +206 -0
- package/build/environment-handlers/node/cli/commands/server.js +1 -0
- package/build/environment-handlers/node.js +30 -3
- package/build/http-server/server-lock.js +55 -18
- package/build/src/application.d.ts +7 -4
- package/build/src/application.d.ts.map +1 -1
- package/build/src/application.js +27 -4
- package/build/src/background-jobs/main.d.ts +2 -1
- package/build/src/background-jobs/main.d.ts.map +1 -1
- package/build/src/background-jobs/main.js +10 -2
- package/build/src/background-jobs/worker.d.ts +42 -0
- package/build/src/background-jobs/worker.d.ts.map +1 -1
- package/build/src/background-jobs/worker.js +61 -5
- package/build/src/cli/process-args.d.ts +11 -0
- package/build/src/cli/process-args.d.ts.map +1 -0
- package/build/src/cli/process-args.js +34 -0
- package/build/src/configuration-types.d.ts +3 -1
- package/build/src/configuration-types.d.ts.map +1 -1
- package/build/src/configuration-types.js +2 -2
- package/build/src/configuration.d.ts +16 -1
- package/build/src/configuration.d.ts.map +1 -1
- package/build/src/configuration.js +39 -3
- package/build/src/database/advisory-lock-lock-server-backend.d.ts +10 -3
- package/build/src/database/advisory-lock-lock-server-backend.d.ts.map +1 -1
- package/build/src/database/advisory-lock-lock-server-backend.js +25 -22
- package/build/src/database/advisory-lock-lock-server-embedded-client.d.ts +24 -0
- package/build/src/database/advisory-lock-lock-server-embedded-client.d.ts.map +1 -0
- package/build/src/database/advisory-lock-lock-server-embedded-client.js +64 -0
- package/build/src/database/advisory-lock-lock-server-engine.d.ts +285 -0
- package/build/src/database/advisory-lock-lock-server-engine.d.ts.map +1 -0
- package/build/src/database/advisory-lock-lock-server-engine.js +460 -0
- package/build/src/database/advisory-lock-lock-server-local-client.d.ts +114 -0
- package/build/src/database/advisory-lock-lock-server-local-client.d.ts.map +1 -0
- package/build/src/database/advisory-lock-lock-server-local-client.js +227 -0
- package/build/src/database/advisory-lock-lock-server-socket-path.d.ts +9 -0
- package/build/src/database/advisory-lock-lock-server-socket-path.d.ts.map +1 -0
- package/build/src/database/advisory-lock-lock-server-socket-path.js +16 -0
- package/build/src/database/advisory-lock-lock-server-transport.d.ts +18 -0
- package/build/src/database/advisory-lock-lock-server-transport.d.ts.map +1 -0
- package/build/src/database/advisory-lock-lock-server-transport.js +27 -0
- package/build/src/database/drivers/base.d.ts +15 -2
- package/build/src/database/drivers/base.d.ts.map +1 -1
- package/build/src/database/drivers/base.js +33 -13
- package/build/src/database/drivers/mssql/index.d.ts +2 -2
- package/build/src/database/drivers/mssql/index.d.ts.map +1 -1
- package/build/src/database/drivers/mssql/index.js +17 -18
- package/build/src/database/drivers/mssql/table.d.ts.map +1 -1
- package/build/src/database/drivers/mssql/table.js +25 -23
- package/build/src/database/drivers/mysql/index.d.ts +2 -2
- package/build/src/database/drivers/mysql/index.d.ts.map +1 -1
- package/build/src/database/drivers/mysql/index.js +16 -19
- package/build/src/database/drivers/mysql/table.d.ts.map +1 -1
- package/build/src/database/drivers/mysql/table.js +27 -22
- package/build/src/database/drivers/pgsql/index.d.ts +2 -2
- package/build/src/database/drivers/pgsql/index.d.ts.map +1 -1
- package/build/src/database/drivers/pgsql/index.js +16 -19
- package/build/src/database/drivers/pgsql/table.d.ts.map +1 -1
- package/build/src/database/drivers/pgsql/table.js +25 -23
- package/build/src/database/drivers/sqlite/base.d.ts.map +1 -1
- package/build/src/database/drivers/sqlite/base.js +8 -10
- package/build/src/database/drivers/sqlite/table.d.ts.map +1 -1
- package/build/src/database/drivers/sqlite/table.js +29 -25
- package/build/src/database/pool/async-tracked-multi-connection.d.ts.map +1 -1
- package/build/src/database/pool/async-tracked-multi-connection.js +2 -1
- package/build/src/database/pool/base.d.ts +6 -0
- package/build/src/database/pool/base.d.ts.map +1 -1
- package/build/src/database/pool/base.js +14 -5
- package/build/src/database/pool/single-multi-use.d.ts.map +1 -1
- package/build/src/database/pool/single-multi-use.js +2 -1
- package/build/src/database/record/auditing.d.ts +14 -4
- package/build/src/database/record/auditing.d.ts.map +1 -1
- package/build/src/database/record/auditing.js +77 -12
- package/build/src/database/record/index.d.ts +4 -2
- package/build/src/database/record/index.d.ts.map +1 -1
- package/build/src/database/record/index.js +2 -2
- package/build/src/environment-handlers/base.d.ts +86 -7
- package/build/src/environment-handlers/base.d.ts.map +1 -1
- package/build/src/environment-handlers/base.js +48 -9
- package/build/src/environment-handlers/node/advisory-lock-lock-server-local-server.d.ts +2 -0
- package/build/src/environment-handlers/node/advisory-lock-lock-server-local-server.d.ts.map +1 -0
- package/build/src/environment-handlers/node/advisory-lock-lock-server-local-server.js +179 -0
- package/build/src/environment-handlers/node/cli/commands/server.d.ts.map +1 -1
- package/build/src/environment-handlers/node/cli/commands/server.js +2 -1
- package/build/src/environment-handlers/node.d.ts +10 -4
- package/build/src/environment-handlers/node.d.ts.map +1 -1
- package/build/src/environment-handlers/node.js +30 -4
- package/build/src/http-server/server-lock.d.ts +43 -14
- package/build/src/http-server/server-lock.d.ts.map +1 -1
- package/build/src/http-server/server-lock.js +54 -19
- package/build/src/tenants/default-tenant-database-provisioning.d.ts.map +1 -1
- package/build/src/tenants/default-tenant-database-provisioning.js +7 -1
- package/build/tenants/default-tenant-database-provisioning.js +8 -0
- package/package.json +2 -2
- package/src/application.js +32 -3
- package/src/background-jobs/main.js +9 -1
- package/src/background-jobs/worker.js +69 -6
- package/src/cli/process-args.js +34 -0
- package/src/configuration-types.js +1 -1
- package/src/configuration.js +43 -2
- package/src/database/advisory-lock-lock-server-backend.js +30 -24
- package/src/database/advisory-lock-lock-server-embedded-client.js +74 -0
- package/src/database/advisory-lock-lock-server-engine.js +544 -0
- package/src/database/advisory-lock-lock-server-local-client.js +260 -0
- package/src/database/advisory-lock-lock-server-socket-path.js +17 -0
- package/src/database/advisory-lock-lock-server-transport.js +30 -0
- package/src/database/drivers/base.js +34 -12
- package/src/database/drivers/mssql/index.js +17 -18
- package/src/database/drivers/mssql/table.js +28 -25
- package/src/database/drivers/mysql/index.js +15 -18
- package/src/database/drivers/mysql/table.js +31 -23
- package/src/database/drivers/pgsql/index.js +15 -18
- package/src/database/drivers/pgsql/table.js +28 -26
- package/src/database/drivers/sqlite/base.js +7 -9
- package/src/database/drivers/sqlite/table.js +32 -26
- package/src/database/pool/async-tracked-multi-connection.js +1 -0
- package/src/database/pool/base.js +14 -4
- package/src/database/pool/single-multi-use.js +1 -0
- package/src/database/record/auditing.js +89 -11
- package/src/database/record/index.js +1 -1
- package/src/environment-handlers/base.js +50 -9
- package/src/environment-handlers/node/advisory-lock-lock-server-local-server.js +206 -0
- package/src/environment-handlers/node/cli/commands/server.js +1 -0
- package/src/environment-handlers/node.js +30 -3
- package/src/http-server/server-lock.js +55 -18
- 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
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
-
|
|
26
|
-
|
|
26
|
+
for (const data of columnRows) {
|
|
27
|
+
const column = new Column(this, data)
|
|
27
28
|
|
|
28
|
-
|
|
29
|
-
|
|
29
|
+
columns.push(column)
|
|
30
|
+
}
|
|
30
31
|
|
|
31
|
-
|
|
32
|
-
})
|
|
32
|
+
return columns
|
|
33
33
|
}
|
|
34
34
|
|
|
35
35
|
async getForeignKeys() {
|
|
36
|
-
|
|
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
|
-
|
|
46
|
-
|
|
45
|
+
return await this.driver.query(sql)
|
|
46
|
+
})
|
|
47
|
+
const foreignKeys = []
|
|
47
48
|
|
|
48
|
-
|
|
49
|
-
|
|
49
|
+
for (const foreignKeyRow of foreignKeyRows) {
|
|
50
|
+
const foreignKey = new ForeignKey(foreignKeyRow)
|
|
50
51
|
|
|
51
|
-
|
|
52
|
-
|
|
52
|
+
foreignKeys.push(foreignKey)
|
|
53
|
+
}
|
|
53
54
|
|
|
54
|
-
|
|
55
|
-
})
|
|
55
|
+
return foreignKeys
|
|
56
56
|
}
|
|
57
57
|
|
|
58
58
|
async getIndexes() {
|
|
59
|
-
|
|
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
|
|
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
|
-
|
|
105
|
+
groupedRows.push(groupedIndexRow)
|
|
107
106
|
}
|
|
108
107
|
}
|
|
109
108
|
|
|
110
|
-
return
|
|
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
|
-
|
|
339
|
-
|
|
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
|
-
|
|
343
|
-
|
|
340
|
+
for (const row of tableRows) {
|
|
341
|
+
const table = new Table(this, /** @type {Record<string, string>} */ (row))
|
|
344
342
|
|
|
345
|
-
|
|
346
|
-
|
|
343
|
+
tables.push(table)
|
|
344
|
+
}
|
|
347
345
|
|
|
348
|
-
|
|
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<
|
|
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<
|
|
360
|
-
const
|
|
356
|
+
/** @type {Map<string, Array<Record<string, ReturnType<typeof JSON.parse>>>>} */
|
|
357
|
+
const rowsByTable = new Map()
|
|
361
358
|
|
|
362
|
-
if (tables.length === 0) return
|
|
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
|
|
396
|
+
const tableRows = rowsByTable.get(tableName) || []
|
|
400
397
|
|
|
401
|
-
|
|
402
|
-
|
|
398
|
+
tableRows.push(/** @type {Record<string, ReturnType<typeof JSON.parse>>} */ (row))
|
|
399
|
+
rowsByTable.set(tableName, tableRows)
|
|
403
400
|
}
|
|
404
401
|
|
|
405
|
-
return
|
|
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
|
-
|
|
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
|
-
|
|
92
|
+
return result
|
|
93
|
+
})
|
|
94
|
+
const columns = []
|
|
93
95
|
|
|
94
|
-
|
|
95
|
-
|
|
96
|
+
for (const data of columnRows) {
|
|
97
|
+
const column = new Column(this, data)
|
|
96
98
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
+
columns.push(column)
|
|
100
|
+
}
|
|
99
101
|
|
|
100
|
-
|
|
101
|
-
})
|
|
102
|
+
return columns
|
|
102
103
|
}
|
|
103
104
|
|
|
104
105
|
async getForeignKeys() {
|
|
105
|
-
|
|
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
|
-
|
|
130
|
-
|
|
130
|
+
return await this.getDriver().query(sql)
|
|
131
|
+
})
|
|
132
|
+
const foreignKeys = []
|
|
131
133
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
+
for (const foreignKeyRow of foreignKeyRows) {
|
|
135
|
+
const foreignKey = new ForeignKey(foreignKeyRow)
|
|
134
136
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
+
foreignKeys.push(foreignKey)
|
|
138
|
+
}
|
|
137
139
|
|
|
138
|
-
|
|
139
|
-
})
|
|
140
|
+
return foreignKeys
|
|
140
141
|
}
|
|
141
142
|
|
|
142
143
|
async getIndexes() {
|
|
143
|
-
|
|
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
|
-
|
|
170
|
-
|
|
168
|
+
return groupPgsqlIndexRows(indexRows)
|
|
169
|
+
})
|
|
170
|
+
const indexes = []
|
|
171
171
|
|
|
172
|
-
|
|
173
|
-
|
|
172
|
+
for (const indexData of groupedIndexRows) {
|
|
173
|
+
const columnsIndex = new ColumnsIndex(this, indexData)
|
|
174
174
|
|
|
175
|
-
|
|
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
|
-
|
|
136
|
-
|
|
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
|
-
|
|
140
|
-
|
|
138
|
+
for (const row of tableRows) {
|
|
139
|
+
const table = new Table({driver: this, row: /** @type {Record<string, string | number | null>} */ (row)})
|
|
141
140
|
|
|
142
|
-
|
|
143
|
-
|
|
141
|
+
tables.push(table)
|
|
142
|
+
}
|
|
144
143
|
|
|
145
|
-
|
|
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
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
|
-
|
|
31
|
-
|
|
31
|
+
for (const columnData of columnRows) {
|
|
32
|
+
const column = new Column({column: columnData, driver: this.driver, table: this})
|
|
32
33
|
|
|
33
|
-
|
|
34
|
-
|
|
34
|
+
columns.push(column)
|
|
35
|
+
}
|
|
35
36
|
|
|
36
|
-
|
|
37
|
-
})
|
|
37
|
+
return columns
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
async getForeignKeys() {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
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
|
-
|
|
46
|
-
|
|
46
|
+
for (const foreignKeyData of foreignKeysData) {
|
|
47
|
+
const foreignKey = new ForeignKey(foreignKeyData, {tableName: this.getName()})
|
|
47
48
|
|
|
48
|
-
|
|
49
|
-
|
|
49
|
+
foreignKeys.push(foreignKey)
|
|
50
|
+
}
|
|
50
51
|
|
|
51
|
-
|
|
52
|
-
})
|
|
52
|
+
return foreignKeys
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
async getIndexes() {
|
|
56
|
-
|
|
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
|
|
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
|
|
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 ${
|
|
71
|
+
if (!sql) throw new Error(`Could not find SQL for index ${indexName}`)
|
|
73
72
|
|
|
74
|
-
const indexData = /** @type {typeof
|
|
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
|
-
|
|
77
|
+
cachedIndexRows.push(indexData)
|
|
79
78
|
}
|
|
80
79
|
|
|
81
|
-
return
|
|
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
|
-
*
|
|
274
|
-
* @param {import("../../configuration-types.js").DatabaseConfigurationType}
|
|
275
|
-
* @returns {string} - Reuse key for the
|
|
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
|
-
|
|
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
|
|
220
|
+
const foreignKey = modelForeignKey(modelClass)
|
|
211
221
|
|
|
212
222
|
return {
|
|
213
223
|
auditClass,
|
|
214
224
|
dedicated: true,
|
|
215
|
-
foreignKey
|
|
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 {
|
|
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
|
|
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.
|
|
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
|
|
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
|
-
[
|
|
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
|
-
|
|
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
|
|
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(
|
|
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 = {}) {
|