velocious 1.0.568 → 1.0.570
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 +30 -0
- package/build/configuration.js +36 -0
- package/build/database/drivers/base.js +244 -93
- package/build/database/drivers/mssql/index.js +10 -7
- package/build/database/drivers/mysql/index.js +10 -5
- package/build/database/drivers/pgsql/index.js +4 -4
- package/build/database/drivers/sqlite/base.js +2 -2
- package/build/database/drivers/sqlite/connection-sql-js.js +62 -2
- package/build/database/drivers/sqlite/index.web.js +74 -0
- package/build/database/operation-connection.js +121 -0
- package/build/database/operation-lease.js +47 -0
- package/build/database/operation.js +150 -0
- package/build/database/pool/base.js +26 -1
- package/build/database/pool/single-multi-use.js +49 -0
- package/build/database/query/model-class-query.js +38 -7
- package/build/database/query/preloader/belongs-to.js +3 -2
- package/build/database/query/preloader/has-many.js +4 -3
- package/build/database/query/preloader/has-one.js +2 -1
- package/build/database/query/preloader/query-for-model.js +17 -0
- package/build/database/query/query-data.js +12 -3
- package/build/database/query/with-count.js +5 -3
- package/build/database/record/acts-as-list.js +11 -8
- package/build/database/record/attachments/store.js +15 -11
- package/build/database/record/auditing.js +12 -9
- package/build/database/record/counter-cache-magnitude.js +5 -3
- package/build/database/record/index.js +95 -14
- package/build/database/record/instance-relationships/belongs-to.js +4 -2
- package/build/database/record/instance-relationships/has-many.js +7 -5
- package/build/database/record/instance-relationships/has-one.js +4 -2
- package/build/database/record/validators/uniqueness.js +3 -2
- package/build/frontend-models/websocket-publishers.js +2 -2
- package/build/src/configuration.d.ts +12 -0
- package/build/src/configuration.d.ts.map +1 -1
- package/build/src/configuration.js +35 -1
- package/build/src/database/drivers/base.d.ts +96 -23
- package/build/src/database/drivers/base.d.ts.map +1 -1
- package/build/src/database/drivers/base.js +230 -90
- package/build/src/database/drivers/mssql/index.d.ts +4 -1
- package/build/src/database/drivers/mssql/index.d.ts.map +1 -1
- package/build/src/database/drivers/mssql/index.js +11 -8
- package/build/src/database/drivers/mysql/index.d.ts +2 -1
- package/build/src/database/drivers/mysql/index.d.ts.map +1 -1
- package/build/src/database/drivers/mysql/index.js +10 -6
- package/build/src/database/drivers/pgsql/index.d.ts +2 -1
- package/build/src/database/drivers/pgsql/index.d.ts.map +1 -1
- package/build/src/database/drivers/pgsql/index.js +5 -5
- package/build/src/database/drivers/sqlite/base.d.ts +1 -1
- package/build/src/database/drivers/sqlite/base.d.ts.map +1 -1
- package/build/src/database/drivers/sqlite/base.js +3 -3
- package/build/src/database/drivers/sqlite/connection-sql-js.d.ts +20 -0
- package/build/src/database/drivers/sqlite/connection-sql-js.d.ts.map +1 -1
- package/build/src/database/drivers/sqlite/connection-sql-js.js +56 -3
- package/build/src/database/drivers/sqlite/index.web.d.ts.map +1 -1
- package/build/src/database/drivers/sqlite/index.web.js +64 -1
- package/build/src/database/operation-connection.d.ts +67 -0
- package/build/src/database/operation-connection.d.ts.map +1 -0
- package/build/src/database/operation-connection.js +102 -0
- package/build/src/database/operation-lease.d.ts +27 -0
- package/build/src/database/operation-lease.d.ts.map +1 -0
- package/build/src/database/operation-lease.js +44 -0
- package/build/src/database/operation.d.ts +85 -0
- package/build/src/database/operation.d.ts.map +1 -0
- package/build/src/database/operation.js +132 -0
- package/build/src/database/pool/base.d.ts +14 -0
- package/build/src/database/pool/base.d.ts.map +1 -1
- package/build/src/database/pool/base.js +24 -2
- package/build/src/database/pool/single-multi-use.d.ts +1 -0
- package/build/src/database/pool/single-multi-use.d.ts.map +1 -1
- package/build/src/database/pool/single-multi-use.js +49 -1
- package/build/src/database/query/model-class-query.d.ts +14 -0
- package/build/src/database/query/model-class-query.d.ts.map +1 -1
- package/build/src/database/query/model-class-query.js +32 -8
- package/build/src/database/query/preloader/belongs-to.d.ts.map +1 -1
- package/build/src/database/query/preloader/belongs-to.js +4 -3
- package/build/src/database/query/preloader/has-many.d.ts.map +1 -1
- package/build/src/database/query/preloader/has-many.js +5 -4
- package/build/src/database/query/preloader/has-one.d.ts.map +1 -1
- package/build/src/database/query/preloader/has-one.js +3 -2
- package/build/src/database/query/preloader/query-for-model.d.ts +10 -0
- package/build/src/database/query/preloader/query-for-model.d.ts.map +1 -0
- package/build/src/database/query/preloader/query-for-model.js +16 -0
- package/build/src/database/query/query-data.d.ts.map +1 -1
- package/build/src/database/query/query-data.js +13 -4
- package/build/src/database/query/with-count.d.ts.map +1 -1
- package/build/src/database/query/with-count.js +6 -4
- package/build/src/database/record/acts-as-list.js +12 -9
- package/build/src/database/record/attachments/attachment-record.d.ts +9 -0
- package/build/src/database/record/attachments/attachment-record.d.ts.map +1 -1
- package/build/src/database/record/attachments/store.d.ts +4 -2
- package/build/src/database/record/attachments/store.d.ts.map +1 -1
- package/build/src/database/record/attachments/store.js +16 -12
- package/build/src/database/record/auditing.d.ts.map +1 -1
- package/build/src/database/record/auditing.js +13 -10
- package/build/src/database/record/counter-cache-magnitude.js +6 -4
- package/build/src/database/record/index.d.ts +42 -2
- package/build/src/database/record/index.d.ts.map +1 -1
- package/build/src/database/record/index.js +87 -15
- package/build/src/database/record/instance-relationships/belongs-to.d.ts.map +1 -1
- package/build/src/database/record/instance-relationships/belongs-to.js +4 -3
- package/build/src/database/record/instance-relationships/has-many.d.ts.map +1 -1
- package/build/src/database/record/instance-relationships/has-many.js +7 -5
- package/build/src/database/record/instance-relationships/has-one.d.ts.map +1 -1
- package/build/src/database/record/instance-relationships/has-one.js +4 -3
- package/build/src/database/record/validators/uniqueness.d.ts.map +1 -1
- package/build/src/database/record/validators/uniqueness.js +4 -3
- package/build/src/frontend-models/websocket-publishers.js +3 -3
- package/build/src/sync/server-sequence-allocator.d.ts +16 -4
- package/build/src/sync/server-sequence-allocator.d.ts.map +1 -1
- package/build/src/sync/server-sequence-allocator.js +37 -11
- package/build/src/sync/sync-client.d.ts.map +1 -1
- package/build/src/sync/sync-client.js +7 -3
- package/build/src/sync/sync-publisher.d.ts +2 -1
- package/build/src/sync/sync-publisher.d.ts.map +1 -1
- package/build/src/sync/sync-publisher.js +11 -6
- package/build/sync/server-sequence-allocator.js +43 -10
- package/build/sync/sync-client.js +6 -2
- package/build/sync/sync-publisher.js +10 -5
- package/build/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/configuration.js +36 -0
- package/src/database/drivers/base.js +244 -93
- package/src/database/drivers/mssql/index.js +10 -7
- package/src/database/drivers/mysql/index.js +10 -5
- package/src/database/drivers/pgsql/index.js +4 -4
- package/src/database/drivers/sqlite/base.js +2 -2
- package/src/database/drivers/sqlite/connection-sql-js.js +62 -2
- package/src/database/drivers/sqlite/index.web.js +74 -0
- package/src/database/operation-connection.js +121 -0
- package/src/database/operation-lease.js +47 -0
- package/src/database/operation.js +150 -0
- package/src/database/pool/base.js +26 -1
- package/src/database/pool/single-multi-use.js +49 -0
- package/src/database/query/model-class-query.js +38 -7
- package/src/database/query/preloader/belongs-to.js +3 -2
- package/src/database/query/preloader/has-many.js +4 -3
- package/src/database/query/preloader/has-one.js +2 -1
- package/src/database/query/preloader/query-for-model.js +17 -0
- package/src/database/query/query-data.js +12 -3
- package/src/database/query/with-count.js +5 -3
- package/src/database/record/acts-as-list.js +11 -8
- package/src/database/record/attachments/store.js +15 -11
- package/src/database/record/auditing.js +12 -9
- package/src/database/record/counter-cache-magnitude.js +5 -3
- package/src/database/record/index.js +95 -14
- package/src/database/record/instance-relationships/belongs-to.js +4 -2
- package/src/database/record/instance-relationships/has-many.js +7 -5
- package/src/database/record/instance-relationships/has-one.js +4 -2
- package/src/database/record/validators/uniqueness.js +3 -2
- package/src/frontend-models/websocket-publishers.js +2 -2
- package/src/sync/server-sequence-allocator.js +43 -10
- package/src/sync/sync-client.js +6 -2
- package/src/sync/sync-publisher.js +10 -5
|
@@ -403,9 +403,12 @@ export default class VelociousDatabaseDriversMysql extends Base{
|
|
|
403
403
|
* large result set is read incrementally instead of being buffered. Overrides the base
|
|
404
404
|
* buffered fallback with true server-side streaming.
|
|
405
405
|
* @param {string} sql - SQL string to stream.
|
|
406
|
+
* @param {import("../base.js").QueryOptions} [options] - Query ownership options.
|
|
406
407
|
* @yields {Record<string, unknown>} - The result rows, one at a time.
|
|
407
408
|
*/
|
|
408
|
-
async *queryStream(sql) {
|
|
409
|
+
async *queryStream(sql, options = {}) {
|
|
410
|
+
await this._waitForOperationLease(options.operationOwner)
|
|
411
|
+
|
|
409
412
|
if (!this.pool) await this.connect()
|
|
410
413
|
if (!this.pool) throw new Error("MySQL pool failed to initialize")
|
|
411
414
|
|
|
@@ -554,10 +557,11 @@ export default class VelociousDatabaseDriversMysql extends Base{
|
|
|
554
557
|
|
|
555
558
|
/**
|
|
556
559
|
* Runs last insert id.
|
|
560
|
+
* @param {import("../base.js").QueryOptions} [options] - Query ownership options.
|
|
557
561
|
* @returns {Promise<number>} - Resolves with the last insert id.
|
|
558
562
|
*/
|
|
559
|
-
async lastInsertID() {
|
|
560
|
-
const result = await this.query("SELECT LAST_INSERT_ID() AS last_insert_id")
|
|
563
|
+
async lastInsertID(options = {}) {
|
|
564
|
+
const result = await this.query("SELECT LAST_INSERT_ID() AS last_insert_id", options)
|
|
561
565
|
|
|
562
566
|
return digg(result, 0, "last_insert_id")
|
|
563
567
|
}
|
|
@@ -574,10 +578,11 @@ export default class VelociousDatabaseDriversMysql extends Base{
|
|
|
574
578
|
|
|
575
579
|
/**
|
|
576
580
|
* Runs start transaction action.
|
|
581
|
+
* @param {Pick<import("../base.js").QueryOptions, "operationOwner">} [options] - Transaction ownership.
|
|
577
582
|
* @returns {Promise<void>} - Resolves when complete.
|
|
578
583
|
*/
|
|
579
|
-
async _startTransactionAction() {
|
|
580
|
-
await this.query("START TRANSACTION")
|
|
584
|
+
async _startTransactionAction(options = {}) {
|
|
585
|
+
await this.query("START TRANSACTION", options)
|
|
581
586
|
}
|
|
582
587
|
|
|
583
588
|
/**
|
|
@@ -326,8 +326,8 @@ export default class VelociousDatabaseDriversPgsql extends Base{
|
|
|
326
326
|
})
|
|
327
327
|
}
|
|
328
328
|
|
|
329
|
-
async lastInsertID() {
|
|
330
|
-
const result = await this.query("SELECT LASTVAL() AS last_insert_id")
|
|
329
|
+
async lastInsertID(options = {}) {
|
|
330
|
+
const result = await this.query("SELECT LASTVAL() AS last_insert_id", options)
|
|
331
331
|
|
|
332
332
|
return digg(result, 0, "last_insert_id")
|
|
333
333
|
}
|
|
@@ -338,8 +338,8 @@ export default class VelociousDatabaseDriversPgsql extends Base{
|
|
|
338
338
|
return this._options
|
|
339
339
|
}
|
|
340
340
|
|
|
341
|
-
async _startTransactionAction() {
|
|
342
|
-
await this.query("START TRANSACTION")
|
|
341
|
+
async _startTransactionAction(options = {}) {
|
|
342
|
+
await this.query("START TRANSACTION", options)
|
|
343
343
|
}
|
|
344
344
|
|
|
345
345
|
/**
|
|
@@ -262,8 +262,8 @@ export default class VelociousDatabaseDriversSqliteBase extends Base {
|
|
|
262
262
|
})
|
|
263
263
|
}
|
|
264
264
|
|
|
265
|
-
async lastInsertID() {
|
|
266
|
-
const result = await this.query("SELECT LAST_INSERT_ROWID() AS last_insert_id")
|
|
265
|
+
async lastInsertID(options = {}) {
|
|
266
|
+
const result = await this.query("SELECT LAST_INSERT_ROWID() AS last_insert_id", options)
|
|
267
267
|
|
|
268
268
|
return digg(result, 0, "last_insert_id")
|
|
269
269
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// @ts-check
|
|
2
2
|
|
|
3
3
|
import debounce from "debounce"
|
|
4
|
+
import Mutex from "epic-locks/build/mutex.js"
|
|
4
5
|
import queryWeb from "./query.web.js"
|
|
5
6
|
|
|
6
7
|
export default class VelociousDatabaseDriversSqliteConnectionSqlJs {
|
|
@@ -12,6 +13,9 @@ export default class VelociousDatabaseDriversSqliteConnectionSqlJs {
|
|
|
12
13
|
*/
|
|
13
14
|
constructor(driver, connection, persistence) {
|
|
14
15
|
this.connection = connection
|
|
16
|
+
this.databaseSaveDeferred = false
|
|
17
|
+
this.databaseSaveMutex = new Mutex()
|
|
18
|
+
this.databaseTransactionStarting = false
|
|
15
19
|
this.driver = driver
|
|
16
20
|
this.persistence = persistence
|
|
17
21
|
}
|
|
@@ -30,6 +34,54 @@ export default class VelociousDatabaseDriversSqliteConnectionSqlJs {
|
|
|
30
34
|
await this.saveDatabase()
|
|
31
35
|
}
|
|
32
36
|
|
|
37
|
+
/**
|
|
38
|
+
* Flushes only when a mutation save is pending or was deferred by a transaction.
|
|
39
|
+
* @returns {Promise<void>} - Resolves when pending database bytes are stored.
|
|
40
|
+
*/
|
|
41
|
+
async flushPendingDatabaseSave() {
|
|
42
|
+
if (!this.saveDatabaseDebounce.isPending && !this.databaseSaveDeferred) return
|
|
43
|
+
|
|
44
|
+
await this.flushDatabaseSave()
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Drains active and queued persistence before atomically starting an outer transaction.
|
|
49
|
+
* @param {() => Promise<void>} callback - Starts the SQL transaction.
|
|
50
|
+
* @returns {Promise<void>} - Resolves after BEGIN succeeds.
|
|
51
|
+
*/
|
|
52
|
+
async withTransactionStart(callback) {
|
|
53
|
+
if (this.saveDatabaseDebounce.isPending) {
|
|
54
|
+
this.saveDatabaseDebounce.clear()
|
|
55
|
+
this.databaseSaveDeferred = true
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
await this.databaseSaveMutex.sync(async () => {
|
|
59
|
+
if (this.databaseSaveDeferred) {
|
|
60
|
+
this.databaseSaveDeferred = false
|
|
61
|
+
const databaseContent = this.connection.export()
|
|
62
|
+
|
|
63
|
+
await this.persistence.save(databaseContent)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
this.databaseTransactionStarting = true
|
|
67
|
+
|
|
68
|
+
try {
|
|
69
|
+
await callback()
|
|
70
|
+
} catch (error) {
|
|
71
|
+
this.databaseTransactionStarting = false
|
|
72
|
+
throw error
|
|
73
|
+
}
|
|
74
|
+
})
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Marks successful outer transaction admission complete after driver state is updated.
|
|
79
|
+
* @returns {void}
|
|
80
|
+
*/
|
|
81
|
+
completeTransactionStart() {
|
|
82
|
+
this.databaseTransactionStarting = false
|
|
83
|
+
}
|
|
84
|
+
|
|
33
85
|
/**
|
|
34
86
|
* Runs query.
|
|
35
87
|
* @param {string} sql - SQL string.
|
|
@@ -59,9 +111,17 @@ export default class VelociousDatabaseDriversSqliteConnectionSqlJs {
|
|
|
59
111
|
}
|
|
60
112
|
|
|
61
113
|
saveDatabase = async () => {
|
|
62
|
-
|
|
114
|
+
await this.databaseSaveMutex.sync(async () => {
|
|
115
|
+
if (this.driver.insideTransaction() || this.databaseTransactionStarting) {
|
|
116
|
+
this.databaseSaveDeferred = true
|
|
117
|
+
return
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
this.databaseSaveDeferred = false
|
|
121
|
+
const databaseContent = this.connection.export()
|
|
63
122
|
|
|
64
|
-
|
|
123
|
+
await this.persistence.save(databaseContent)
|
|
124
|
+
})
|
|
65
125
|
}
|
|
66
126
|
|
|
67
127
|
saveDatabaseDebounce = debounce(this.saveDatabase, 500)
|
|
@@ -64,6 +64,80 @@ export default class VelociousDatabaseDriversSqliteWeb extends Base {
|
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
+
/**
|
|
68
|
+
* Starts an outer transaction after draining SQL.js persistence admission.
|
|
69
|
+
* @param {Pick<import("../base.js").QueryOptions, "operationOwner">} [options] - Transaction ownership.
|
|
70
|
+
* @returns {Promise<void>} - Resolves when the transaction starts.
|
|
71
|
+
*/
|
|
72
|
+
async startTransaction(options = {}) {
|
|
73
|
+
if (!this.args?.getConnection) {
|
|
74
|
+
if (!this._connection) throw new Error("SQLite web connection has not been initialized")
|
|
75
|
+
|
|
76
|
+
try {
|
|
77
|
+
await super.startTransaction(options)
|
|
78
|
+
} finally {
|
|
79
|
+
this._connection.completeTransactionStart()
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
await super.startTransaction(options)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Coordinates SQL BEGIN with active and queued persistence exports.
|
|
90
|
+
* @param {Pick<import("../base.js").QueryOptions, "operationOwner">} [options] - Transaction ownership.
|
|
91
|
+
* @returns {Promise<void>} - Resolves when the transaction starts.
|
|
92
|
+
*/
|
|
93
|
+
async _startTransactionAction(options = {}) {
|
|
94
|
+
if (!this.args?.getConnection) {
|
|
95
|
+
if (!this._connection) throw new Error("SQLite web connection has not been initialized")
|
|
96
|
+
|
|
97
|
+
await this._connection.withTransactionStart(async () => {
|
|
98
|
+
await super._startTransactionAction(options)
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
return
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
await super._startTransactionAction(options)
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Commits and persists bytes after the outermost SQL.js transaction closes.
|
|
109
|
+
* @param {Pick<import("../base.js").QueryOptions, "operationOwner">} [options] - Transaction ownership.
|
|
110
|
+
* @returns {Promise<void>} - Resolves when committed bytes are persisted.
|
|
111
|
+
*/
|
|
112
|
+
async commitTransaction(options = {}) {
|
|
113
|
+
const outermostTransaction = this._transactionsCount === 1
|
|
114
|
+
|
|
115
|
+
await super.commitTransaction(options)
|
|
116
|
+
|
|
117
|
+
if (outermostTransaction && !this.args?.getConnection) {
|
|
118
|
+
if (!this._connection) throw new Error("SQLite web connection has not been initialized")
|
|
119
|
+
|
|
120
|
+
await this._connection.flushPendingDatabaseSave()
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Rolls back and persists bytes after the outermost SQL.js transaction closes.
|
|
126
|
+
* @param {Pick<import("../base.js").QueryOptions, "operationOwner">} [options] - Transaction ownership.
|
|
127
|
+
* @returns {Promise<void>} - Resolves when rolled-back bytes are persisted.
|
|
128
|
+
*/
|
|
129
|
+
async rollbackTransaction(options = {}) {
|
|
130
|
+
const outermostTransaction = this._transactionsCount === 1
|
|
131
|
+
|
|
132
|
+
await super.rollbackTransaction(options)
|
|
133
|
+
|
|
134
|
+
if (outermostTransaction && !this.args?.getConnection) {
|
|
135
|
+
if (!this._connection) throw new Error("SQLite web connection has not been initialized")
|
|
136
|
+
|
|
137
|
+
await this._connection.flushPendingDatabaseSave()
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
67
141
|
/**
|
|
68
142
|
* Runs get connection.
|
|
69
143
|
* @returns {ConnectionSqlJs | SqliteWebConnection} - The connection.
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Explicit connection facade that tags operation-owned asynchronous work while
|
|
5
|
+
* forwarding the driver's synchronous SQL-builder and capability APIs.
|
|
6
|
+
*/
|
|
7
|
+
export default class VelociousDatabaseOperationConnection {
|
|
8
|
+
/**
|
|
9
|
+
* Runs constructor.
|
|
10
|
+
* @param {object} args - Connection ownership.
|
|
11
|
+
* @param {import("./drivers/base.js").default} args.connection - Pinned physical connection.
|
|
12
|
+
* @param {import("./operation.js").default} args.operation - Owning operation.
|
|
13
|
+
* @param {symbol} args.owner - Opaque lease owner token.
|
|
14
|
+
*/
|
|
15
|
+
constructor({connection, operation, owner}) {
|
|
16
|
+
this._physicalConnection = connection
|
|
17
|
+
this._operation = operation
|
|
18
|
+
this._owner = owner
|
|
19
|
+
|
|
20
|
+
return new Proxy(this, {
|
|
21
|
+
get: (target, property, receiver) => {
|
|
22
|
+
if (Reflect.has(target, property)) {
|
|
23
|
+
const value = Reflect.get(target, property, receiver)
|
|
24
|
+
|
|
25
|
+
return typeof value === "function" ? value.bind(target) : value
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const value = Reflect.get(connection, property, receiver)
|
|
29
|
+
|
|
30
|
+
return typeof value === "function" ? value.bind(receiver) : value
|
|
31
|
+
},
|
|
32
|
+
set: (target, property, value, receiver) => {
|
|
33
|
+
if (Reflect.has(target, property)) {
|
|
34
|
+
return Reflect.set(target, property, value, receiver)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return Reflect.set(connection, property, value, connection)
|
|
38
|
+
}
|
|
39
|
+
})
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Runs a tagged SQL query on the pinned connection.
|
|
44
|
+
* @param {string} sql - SQL string.
|
|
45
|
+
* @param {import("./drivers/base.js").QueryOptions} [options] - Query options.
|
|
46
|
+
* @returns {Promise<import("./drivers/base.js").QueryResultType>} - Query result.
|
|
47
|
+
*/
|
|
48
|
+
async query(sql, options = {}) {
|
|
49
|
+
this._operation.assertActive()
|
|
50
|
+
|
|
51
|
+
return await this._physicalConnection.query(sql, {...options, operationOwner: this._owner})
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Streams an operation-owned SQL query.
|
|
56
|
+
* @param {string} sql - SQL string.
|
|
57
|
+
* @param {import("./drivers/base.js").QueryOptions} [options] - Query options.
|
|
58
|
+
* @yields {Record<string, unknown>} - Query rows.
|
|
59
|
+
*/
|
|
60
|
+
async *queryStream(sql, options = {}) {
|
|
61
|
+
this._operation.assertActive()
|
|
62
|
+
|
|
63
|
+
yield* this._physicalConnection.queryStream(sql, {...options, operationOwner: this._owner})
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Executes an operation-owned mutation and returns its affected row count.
|
|
68
|
+
* @param {string} sql - Mutation SQL string.
|
|
69
|
+
* @param {import("./drivers/base.js").QueryOptions} [options] - Query options.
|
|
70
|
+
* @returns {Promise<number>} - Affected row count.
|
|
71
|
+
*/
|
|
72
|
+
async affectedRows(sql, options = {}) {
|
|
73
|
+
this._operation.assertActive()
|
|
74
|
+
|
|
75
|
+
return await this._physicalConnection.affectedRows(sql, {...options, operationOwner: this._owner})
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Runs an operation-owned transaction or nested savepoint.
|
|
80
|
+
* @template T
|
|
81
|
+
* @param {() => Promise<T>} callback - Transaction callback.
|
|
82
|
+
* @returns {Promise<T>} - Callback result.
|
|
83
|
+
*/
|
|
84
|
+
async transaction(callback) {
|
|
85
|
+
this._operation.assertActive()
|
|
86
|
+
|
|
87
|
+
return /** @type {Promise<T>} */ (this._physicalConnection.transaction(callback, {operationOwner: this._owner}))
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Registers an operation-owned before-commit guard.
|
|
92
|
+
* @param {() => void | Promise<void>} callback - Guard callback.
|
|
93
|
+
* @returns {Promise<void>} - Resolves after registration.
|
|
94
|
+
*/
|
|
95
|
+
async beforeCommit(callback) {
|
|
96
|
+
this._operation.assertActive()
|
|
97
|
+
|
|
98
|
+
await this._physicalConnection.beforeCommit(callback, {operationOwner: this._owner})
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Registers an operation-owned after-commit callback.
|
|
103
|
+
* @param {() => void | Promise<void>} callback - Callback.
|
|
104
|
+
* @returns {Promise<void>} - Resolves after registration or execution.
|
|
105
|
+
*/
|
|
106
|
+
async afterCommit(callback) {
|
|
107
|
+
this._operation.assertActive()
|
|
108
|
+
|
|
109
|
+
await this._physicalConnection.afterCommit(callback, {operationOwner: this._owner})
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Returns the last inserted identifier through the operation lease.
|
|
114
|
+
* @returns {Promise<number>} - Last inserted identifier.
|
|
115
|
+
*/
|
|
116
|
+
async lastInsertID() {
|
|
117
|
+
this._operation.assertActive()
|
|
118
|
+
|
|
119
|
+
return await this._physicalConnection.lastInsertID({operationOwner: this._owner})
|
|
120
|
+
}
|
|
121
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Exclusive lease installed on a shared physical connection while one
|
|
5
|
+
* operation-scoped transaction owns it.
|
|
6
|
+
*/
|
|
7
|
+
export default class VelociousDatabaseOperationLease {
|
|
8
|
+
/**
|
|
9
|
+
* Runs constructor.
|
|
10
|
+
* @param {symbol} owner - Opaque operation owner token.
|
|
11
|
+
*/
|
|
12
|
+
constructor(owner) {
|
|
13
|
+
this.owner = owner
|
|
14
|
+
this.released = false
|
|
15
|
+
/**
|
|
16
|
+
* Resolves the lease waiters.
|
|
17
|
+
* @type {() => void} */
|
|
18
|
+
let release = () => {}
|
|
19
|
+
|
|
20
|
+
this.releasedPromise = new Promise((resolve) => {
|
|
21
|
+
release = () => resolve(undefined)
|
|
22
|
+
})
|
|
23
|
+
this.releasePromise = release
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Waits until the lease is released unless `owner` owns it.
|
|
28
|
+
* @param {symbol | undefined} owner - Candidate operation owner.
|
|
29
|
+
* @returns {Promise<void>} - Resolves when access is allowed.
|
|
30
|
+
*/
|
|
31
|
+
async wait(owner) {
|
|
32
|
+
if (owner === this.owner) return
|
|
33
|
+
|
|
34
|
+
await this.releasedPromise
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Releases all waiters exactly once.
|
|
39
|
+
* @returns {void}
|
|
40
|
+
*/
|
|
41
|
+
release() {
|
|
42
|
+
if (this.released) return
|
|
43
|
+
|
|
44
|
+
this.released = true
|
|
45
|
+
this.releasePromise()
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
import OperationConnection from "./operation-connection.js"
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Explicit owner for model work performed on one pinned transactional
|
|
7
|
+
* connection.
|
|
8
|
+
*/
|
|
9
|
+
export default class VelociousDatabaseOperation {
|
|
10
|
+
/**
|
|
11
|
+
* Runs constructor.
|
|
12
|
+
* @param {object} args - Operation ownership.
|
|
13
|
+
* @param {import("../configuration.js").default} args.configuration - Owning configuration.
|
|
14
|
+
* @param {string} args.configurationReuseKey - Physical database configuration key captured at checkout.
|
|
15
|
+
* @param {import("./drivers/base.js").default} args.connection - Pinned physical connection.
|
|
16
|
+
* @param {string} args.databaseIdentifier - Singular database identifier.
|
|
17
|
+
* @param {symbol} args.owner - Opaque pool lease owner.
|
|
18
|
+
*/
|
|
19
|
+
constructor({configuration, configurationReuseKey, connection, databaseIdentifier, owner}) {
|
|
20
|
+
this._active = true
|
|
21
|
+
this._configuration = configuration
|
|
22
|
+
this._configurationReuseKey = configurationReuseKey
|
|
23
|
+
this._databaseIdentifier = databaseIdentifier
|
|
24
|
+
this._connection = new OperationConnection({
|
|
25
|
+
connection,
|
|
26
|
+
operation: this,
|
|
27
|
+
owner
|
|
28
|
+
})
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Returns an operation-bound model query/create scope.
|
|
33
|
+
* @template {typeof import("./record/index.js").default} MC
|
|
34
|
+
* @param {MC} ModelClass - Model class to bind.
|
|
35
|
+
* @returns {import("./query/model-class-query.js").default<MC>} - Operation-bound scope.
|
|
36
|
+
*/
|
|
37
|
+
forModel(ModelClass) {
|
|
38
|
+
this.assertActive()
|
|
39
|
+
this.assertModel(ModelClass)
|
|
40
|
+
|
|
41
|
+
return ModelClass._newQuery({
|
|
42
|
+
driver: this.connection(),
|
|
43
|
+
operation: this
|
|
44
|
+
})
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Verifies that a model belongs to this operation's configuration and database.
|
|
49
|
+
* @param {typeof import("./record/index.js").default} ModelClass - Model class to verify.
|
|
50
|
+
* @returns {void}
|
|
51
|
+
*/
|
|
52
|
+
assertModel(ModelClass) {
|
|
53
|
+
if (ModelClass._getConfiguration() !== this._configuration) {
|
|
54
|
+
throw new Error(`${ModelClass.getModelName()} belongs to another Velocious configuration`)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const modelDatabaseIdentifier = ModelClass.getDatabaseIdentifier()
|
|
58
|
+
|
|
59
|
+
if (modelDatabaseIdentifier !== this._databaseIdentifier) {
|
|
60
|
+
throw new Error(`${ModelClass.getModelName()} uses database ${JSON.stringify(modelDatabaseIdentifier)}, not operation database ${JSON.stringify(this._databaseIdentifier)}`)
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Binds a loaded or built record to this operation.
|
|
66
|
+
* @template {import("./record/index.js").default} Model
|
|
67
|
+
* @param {Model} record - Record to bind.
|
|
68
|
+
* @returns {Model} - Bound record.
|
|
69
|
+
*/
|
|
70
|
+
bindRecord(record) {
|
|
71
|
+
this.assertActive()
|
|
72
|
+
this.assertModel(record.getModelClass())
|
|
73
|
+
record.bindDatabaseOperation(this)
|
|
74
|
+
|
|
75
|
+
return record
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Registers a callback owned by the current operation transaction frame.
|
|
80
|
+
* @param {() => void | Promise<void>} callback - Callback.
|
|
81
|
+
* @returns {Promise<void>} - Resolves after registration or execution.
|
|
82
|
+
*/
|
|
83
|
+
async afterCommit(callback) {
|
|
84
|
+
this.assertActive()
|
|
85
|
+
await this.connection().afterCommit(callback)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Registers a guard owned by the current transaction/savepoint frame.
|
|
90
|
+
* @param {(context: {operation: VelociousDatabaseOperation}) => void | Promise<void>} callback - Guard callback.
|
|
91
|
+
* @returns {Promise<void>} - Resolves after registration.
|
|
92
|
+
*/
|
|
93
|
+
async beforeCommit(callback) {
|
|
94
|
+
this.assertActive()
|
|
95
|
+
await this.connection().beforeCommit(() => callback({operation: this}))
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Runs a nested operation transaction/savepoint.
|
|
100
|
+
* @template T
|
|
101
|
+
* @param {() => Promise<T>} callback - Callback.
|
|
102
|
+
* @returns {Promise<T>} - Callback result.
|
|
103
|
+
*/
|
|
104
|
+
async transaction(callback) {
|
|
105
|
+
this.assertActive()
|
|
106
|
+
|
|
107
|
+
return await this.connection().transaction(callback)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Returns the deliberately exposed operation connection facade for raw SQL.
|
|
112
|
+
* @returns {import("./drivers/base.js").default} - Operation-bound connection.
|
|
113
|
+
*/
|
|
114
|
+
connection() {
|
|
115
|
+
this.assertActive()
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Narrows the Proxy facade to the complete driver surface that it forwards.
|
|
119
|
+
* @type {import("./drivers/base.js").default}
|
|
120
|
+
*/
|
|
121
|
+
const connectionFacade = /** @type {?} */ (this._connection)
|
|
122
|
+
|
|
123
|
+
return connectionFacade
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Raises when an operation handle has left its callback.
|
|
128
|
+
* @returns {void}
|
|
129
|
+
*/
|
|
130
|
+
assertActive() {
|
|
131
|
+
if (!this._active) throw new Error("Database operation has completed")
|
|
132
|
+
|
|
133
|
+
const currentReuseKey = this
|
|
134
|
+
._configuration
|
|
135
|
+
.getDatabasePool(this._databaseIdentifier)
|
|
136
|
+
.getConfigurationReuseKey()
|
|
137
|
+
|
|
138
|
+
if (currentReuseKey !== this._configurationReuseKey) {
|
|
139
|
+
throw new Error(`Database operation for ${JSON.stringify(this._databaseIdentifier)} belongs to a different physical database than the current tenant context`)
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Expires the operation and all scopes/records bound to it.
|
|
145
|
+
* @returns {void}
|
|
146
|
+
*/
|
|
147
|
+
complete() {
|
|
148
|
+
this._active = false
|
|
149
|
+
}
|
|
150
|
+
}
|
|
@@ -241,9 +241,21 @@ class VelociousDatabasePoolBase {
|
|
|
241
241
|
* @returns {boolean} - Whether connection matches current resolved configuration.
|
|
242
242
|
*/
|
|
243
243
|
connectionMatchesCurrentConfiguration(connection) {
|
|
244
|
+
return this.getConnectionConfigurationReuseKey(connection) === this.getConfigurationReuseKey()
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Returns the resolved database configuration key stamped on a connection at checkout.
|
|
249
|
+
* @param {import("../drivers/base.js").default} connection - Checked-out connection.
|
|
250
|
+
* @returns {string} - Connection configuration reuse key.
|
|
251
|
+
*/
|
|
252
|
+
getConnectionConfigurationReuseKey(connection) {
|
|
244
253
|
const connectionWithPoolKey = /** @type {import("../drivers/base.js").default & {[POOL_CONFIGURATION_KEY]?: string}} */ (connection)
|
|
254
|
+
const reuseKey = connectionWithPoolKey[POOL_CONFIGURATION_KEY]
|
|
255
|
+
|
|
256
|
+
if (!reuseKey) throw new Error("Database connection is missing its configuration reuse key")
|
|
245
257
|
|
|
246
|
-
return
|
|
258
|
+
return reuseKey
|
|
247
259
|
}
|
|
248
260
|
|
|
249
261
|
/**
|
|
@@ -357,6 +369,19 @@ class VelociousDatabasePoolBase {
|
|
|
357
369
|
throw new Error("'withConnection' not implemented")
|
|
358
370
|
}
|
|
359
371
|
|
|
372
|
+
/**
|
|
373
|
+
* Runs an operation on a freshly checked-out connection.
|
|
374
|
+
* @template T
|
|
375
|
+
* @param {ConnectionCheckoutOptions} options - Checkout options.
|
|
376
|
+
* @param {(connection: import("../drivers/base.js").default, owner: symbol) => Promise<T>} callback - Operation callback.
|
|
377
|
+
* @returns {Promise<T>} - Resolves with the callback result.
|
|
378
|
+
*/
|
|
379
|
+
async withOperationConnection(options, callback) {
|
|
380
|
+
const owner = Symbol("database-operation-owner")
|
|
381
|
+
|
|
382
|
+
return await this.withConnection(options, async (connection) => await callback(connection, owner))
|
|
383
|
+
}
|
|
384
|
+
|
|
360
385
|
/**
|
|
361
386
|
* Ensures a reusable connection exists for contexts where AsyncLocalStorage isn't set.
|
|
362
387
|
* Default implementation just checks out a connection.
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
// @ts-check
|
|
2
2
|
|
|
3
3
|
import BasePool from "./base.js"
|
|
4
|
+
import OperationLease from "../operation-lease.js"
|
|
4
5
|
|
|
5
6
|
export default class VelociousDatabasePoolSingleMultiUser extends BasePool {
|
|
6
7
|
activeCheckoutCount = 0
|
|
7
8
|
suppressedConnectionContextCount = 0
|
|
9
|
+
operationLeaseQueue = Promise.resolve()
|
|
8
10
|
|
|
9
11
|
/**
|
|
10
12
|
* Checkout names of the currently-active nested checkouts, innermost last. The
|
|
@@ -107,6 +109,53 @@ export default class VelociousDatabasePoolSingleMultiUser extends BasePool {
|
|
|
107
109
|
}
|
|
108
110
|
}
|
|
109
111
|
|
|
112
|
+
/**
|
|
113
|
+
* Runs an operation under an exclusive FIFO lease on the reusable physical connection.
|
|
114
|
+
* @template T
|
|
115
|
+
* @param {import("./base.js").ConnectionCheckoutOptions} options - Checkout options.
|
|
116
|
+
* @param {(connection: import("../drivers/base.js").default, owner: symbol) => Promise<T>} callback - Operation callback.
|
|
117
|
+
* @returns {Promise<T>} - Resolves with the callback result.
|
|
118
|
+
*/
|
|
119
|
+
async withOperationConnection(options, callback) {
|
|
120
|
+
const previousLease = this.operationLeaseQueue
|
|
121
|
+
/**
|
|
122
|
+
* Resolves this FIFO queue turn.
|
|
123
|
+
* @type {() => void} */
|
|
124
|
+
let releaseQueue = () => {}
|
|
125
|
+
const queueTurn = new Promise((resolve) => {
|
|
126
|
+
releaseQueue = () => resolve(undefined)
|
|
127
|
+
})
|
|
128
|
+
|
|
129
|
+
this.operationLeaseQueue = previousLease.then(async () => await queueTurn)
|
|
130
|
+
await previousLease
|
|
131
|
+
|
|
132
|
+
const owner = Symbol("single-pool-operation-owner")
|
|
133
|
+
const operationLease = new OperationLease(owner)
|
|
134
|
+
/** @type {import("../drivers/base.js").default | undefined} */
|
|
135
|
+
let connection
|
|
136
|
+
let operationLeaseInstalled = false
|
|
137
|
+
|
|
138
|
+
try {
|
|
139
|
+
connection = await this.checkout(options)
|
|
140
|
+
await connection.setOperationLease(operationLease)
|
|
141
|
+
operationLeaseInstalled = true
|
|
142
|
+
|
|
143
|
+
return await callback(connection, owner)
|
|
144
|
+
} finally {
|
|
145
|
+
operationLease.release()
|
|
146
|
+
|
|
147
|
+
try {
|
|
148
|
+
if (connection && operationLeaseInstalled) connection.clearOperationLease(operationLease)
|
|
149
|
+
} finally {
|
|
150
|
+
try {
|
|
151
|
+
if (connection) await this.checkin(connection)
|
|
152
|
+
} finally {
|
|
153
|
+
releaseQueue()
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
110
159
|
/**
|
|
111
160
|
* Runs without current connection context.
|
|
112
161
|
* @template T
|