velocious 1.0.474 → 1.0.475

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 (85) hide show
  1. package/README.md +7 -2
  2. package/build/configuration-types.js +38 -1
  3. package/build/configuration.js +41 -1
  4. package/build/database/drivers/base.js +51 -3
  5. package/build/database/drivers/mysql/index.js +74 -7
  6. package/build/database/drivers/sqlite/connection-sql-js.js +16 -5
  7. package/build/database/drivers/sqlite/index.web.js +29 -15
  8. package/build/database/drivers/sqlite/web-persistence.js +521 -0
  9. package/build/environment-handlers/node/cli/commands/generate/frontend-models.js +63 -0
  10. package/build/frontend-model-controller.js +127 -4
  11. package/build/frontend-model-resource/base-resource.js +5 -1
  12. package/build/frontend-models/base.js +146 -2
  13. package/build/frontend-models/query.js +3 -3
  14. package/build/frontend-models/resource-definition.js +304 -2
  15. package/build/routes/hooks/frontend-model-command-route-hook.js +8 -0
  16. package/build/src/configuration-types.d.ts +113 -2
  17. package/build/src/configuration-types.d.ts.map +1 -1
  18. package/build/src/configuration-types.js +35 -2
  19. package/build/src/configuration.d.ts +18 -1
  20. package/build/src/configuration.d.ts.map +1 -1
  21. package/build/src/configuration.js +37 -2
  22. package/build/src/database/drivers/base.d.ts +30 -0
  23. package/build/src/database/drivers/base.d.ts.map +1 -1
  24. package/build/src/database/drivers/base.js +47 -4
  25. package/build/src/database/drivers/mysql/index.d.ts +32 -3
  26. package/build/src/database/drivers/mysql/index.d.ts.map +1 -1
  27. package/build/src/database/drivers/mysql/index.js +65 -7
  28. package/build/src/database/drivers/sqlite/connection-sql-js.d.ts +8 -1
  29. package/build/src/database/drivers/sqlite/connection-sql-js.d.ts.map +1 -1
  30. package/build/src/database/drivers/sqlite/connection-sql-js.js +15 -6
  31. package/build/src/database/drivers/sqlite/index.web.d.ts +5 -5
  32. package/build/src/database/drivers/sqlite/index.web.d.ts.map +1 -1
  33. package/build/src/database/drivers/sqlite/index.web.js +27 -13
  34. package/build/src/database/drivers/sqlite/web-persistence.d.ts +77 -0
  35. package/build/src/database/drivers/sqlite/web-persistence.d.ts.map +1 -0
  36. package/build/src/database/drivers/sqlite/web-persistence.js +464 -0
  37. package/build/src/environment-handlers/node/cli/commands/generate/frontend-models.d.ts +30 -0
  38. package/build/src/environment-handlers/node/cli/commands/generate/frontend-models.d.ts.map +1 -1
  39. package/build/src/environment-handlers/node/cli/commands/generate/frontend-models.js +53 -1
  40. package/build/src/frontend-model-controller.d.ts +34 -0
  41. package/build/src/frontend-model-controller.d.ts.map +1 -1
  42. package/build/src/frontend-model-controller.js +113 -5
  43. package/build/src/frontend-model-resource/base-resource.d.ts +4 -2
  44. package/build/src/frontend-model-resource/base-resource.d.ts.map +1 -1
  45. package/build/src/frontend-model-resource/base-resource.js +7 -2
  46. package/build/src/frontend-models/base.d.ts +29 -0
  47. package/build/src/frontend-models/base.d.ts.map +1 -1
  48. package/build/src/frontend-models/base.js +131 -3
  49. package/build/src/frontend-models/query.js +4 -4
  50. package/build/src/frontend-models/resource-definition.d.ts +6 -0
  51. package/build/src/frontend-models/resource-definition.d.ts.map +1 -1
  52. package/build/src/frontend-models/resource-definition.js +272 -3
  53. package/build/src/routes/hooks/frontend-model-command-route-hook.d.ts.map +1 -1
  54. package/build/src/routes/hooks/frontend-model-command-route-hook.js +8 -1
  55. package/build/src/sync/device-identity.d.ts +240 -0
  56. package/build/src/sync/device-identity.d.ts.map +1 -0
  57. package/build/src/sync/device-identity.js +454 -0
  58. package/build/src/sync/local-mutation-log.d.ts +212 -0
  59. package/build/src/sync/local-mutation-log.d.ts.map +1 -0
  60. package/build/src/sync/local-mutation-log.js +404 -0
  61. package/build/src/sync/offline-grant.d.ts +157 -0
  62. package/build/src/sync/offline-grant.d.ts.map +1 -0
  63. package/build/src/sync/offline-grant.js +292 -0
  64. package/build/sync/device-identity.js +503 -0
  65. package/build/sync/local-mutation-log.js +448 -0
  66. package/build/sync/offline-grant.js +327 -0
  67. package/build/tsconfig.tsbuildinfo +1 -1
  68. package/package.json +3 -3
  69. package/src/configuration-types.js +38 -1
  70. package/src/configuration.js +41 -1
  71. package/src/database/drivers/base.js +51 -3
  72. package/src/database/drivers/mysql/index.js +74 -7
  73. package/src/database/drivers/sqlite/connection-sql-js.js +16 -5
  74. package/src/database/drivers/sqlite/index.web.js +29 -15
  75. package/src/database/drivers/sqlite/web-persistence.js +521 -0
  76. package/src/environment-handlers/node/cli/commands/generate/frontend-models.js +63 -0
  77. package/src/frontend-model-controller.js +127 -4
  78. package/src/frontend-model-resource/base-resource.js +5 -1
  79. package/src/frontend-models/base.js +146 -2
  80. package/src/frontend-models/query.js +3 -3
  81. package/src/frontend-models/resource-definition.js +304 -2
  82. package/src/routes/hooks/frontend-model-command-route-hook.js +8 -0
  83. package/src/sync/device-identity.js +503 -0
  84. package/src/sync/local-mutation-log.js +448 -0
  85. package/src/sync/offline-grant.js +327 -0
@@ -50,6 +50,7 @@
50
50
  * @property {string} [logName] - Query log subject.
51
51
  * @property {boolean} [logQuery] - Whether to log the query.
52
52
  * @property {boolean} [processListComment] - Whether to add process-list comments to the query.
53
+ * @property {boolean} [sessionTimeZone] - Whether to ensure the configured database session time zone before the query.
53
54
  * @property {string} [sourceStack] - Stack captured at the caller boundary.
54
55
  */
55
56
 
@@ -237,6 +238,14 @@ export default class VelociousDatabaseDriversBase {
237
238
  // No-op by default
238
239
  }
239
240
 
241
+ /**
242
+ * Flushes pending writes that the driver delayed for persistence.
243
+ * @returns {Promise<void>} - Resolves when pending writes are durable.
244
+ */
245
+ async flushPendingWrites() {
246
+ // No-op by default
247
+ }
248
+
240
249
  /**
241
250
  * Runs set connection checkout name.
242
251
  * @param {string | undefined} name - Human-readable name for this active checkout.
@@ -1041,12 +1050,14 @@ export default class VelociousDatabaseDriversBase {
1041
1050
  let result
1042
1051
 
1043
1052
  try {
1053
+ const runQueryActualWithHooks = async () => await this._queryActualWithHooks(querySql, options)
1054
+
1044
1055
  if (requestTiming && tries === 1) {
1045
- result = await requestTiming.measureDbQuery(async () => await this._queryActual(querySql))
1056
+ result = await requestTiming.measureDbQuery(runQueryActualWithHooks)
1046
1057
  } else if (requestTiming) {
1047
- result = await requestTiming.measure("db", async () => await this._queryActual(querySql))
1058
+ result = await requestTiming.measure("db", runQueryActualWithHooks)
1048
1059
  } else {
1049
- result = await this._queryActual(querySql)
1060
+ result = await runQueryActualWithHooks()
1050
1061
  }
1051
1062
  } finally {
1052
1063
  this._activeQuery = previousActiveQuery
@@ -1070,6 +1081,42 @@ export default class VelociousDatabaseDriversBase {
1070
1081
  return result
1071
1082
  }
1072
1083
 
1084
+ /**
1085
+ * Runs query actual with before/after hooks.
1086
+ * @param {string} sql - SQL string.
1087
+ * @param {QueryOptions} options - Query options.
1088
+ * @returns {Promise<QueryResultType>} - Resolves with the query.
1089
+ */
1090
+ async _queryActualWithHooks(sql, options) {
1091
+ await this.beforeQuery(sql, options)
1092
+
1093
+ try {
1094
+ return await this._queryActual(sql)
1095
+ } finally {
1096
+ await this.afterQuery(sql, options)
1097
+ }
1098
+ }
1099
+
1100
+ /**
1101
+ * Hook that runs immediately before a SQL query is sent to the driver.
1102
+ * @param {string} _sql - SQL string.
1103
+ * @param {QueryOptions} _options - Query options.
1104
+ * @returns {Promise<void>} - Resolves when complete.
1105
+ */
1106
+ async beforeQuery(_sql, _options) {
1107
+ // No-op by default
1108
+ }
1109
+
1110
+ /**
1111
+ * Hook that runs immediately after a SQL query has completed or failed.
1112
+ * @param {string} _sql - SQL string.
1113
+ * @param {QueryOptions} _options - Query options.
1114
+ * @returns {Promise<void>} - Resolves when complete.
1115
+ */
1116
+ async afterQuery(_sql, _options) {
1117
+ // No-op by default
1118
+ }
1119
+
1073
1120
  /**
1074
1121
  * Runs get debug snapshot.
1075
1122
  * @returns {DatabaseConnectionDebugSnapshot} - Diagnostic snapshot for this connection.
@@ -1504,6 +1551,7 @@ export default class VelociousDatabaseDriversBase {
1504
1551
  }
1505
1552
  }
1506
1553
  })
1554
+ await this.flushPendingWrites()
1507
1555
  }
1508
1556
 
1509
1557
  /**
@@ -30,15 +30,20 @@ import Update from "./sql/update.js"
30
30
  const MYSQL_INDEFINITE_LOCK_TIMEOUT_SECONDS = 60 * 60 * 24 * 365
31
31
 
32
32
  export default class VelociousDatabaseDriversMysql extends Base{
33
+ /** @type {string | null} */
34
+ _desiredSessionTimeZone = "+00:00"
35
+
36
+ /** @type {string | null} */
37
+ _currentSessionTimeZone = null
38
+
33
39
  /**
34
40
  * Runs connect.
35
41
  * @returns {Promise<void>} - Resolves when complete.
36
42
  */
37
43
  async connect() {
44
+ this.resetCurrentSessionTimeZone()
38
45
  this.pool = mysql.createPool(Object.assign({connectionLimit: 1}, this.connectArgs()))
39
46
  this.pool.on("error", this.onPoolError)
40
-
41
- await this.setSessionTimezoneToUtc()
42
47
  }
43
48
 
44
49
  /**
@@ -56,6 +61,7 @@ export default class VelociousDatabaseDriversMysql extends Base{
56
61
  async close() {
57
62
  await this.pool?.end()
58
63
  this.pool = undefined
64
+ this.resetCurrentSessionTimeZone()
59
65
  }
60
66
 
61
67
  /**
@@ -64,7 +70,7 @@ export default class VelociousDatabaseDriversMysql extends Base{
64
70
  * @returns {Promise<void>} - Resolves when complete.
65
71
  */
66
72
  async setConnectionCheckoutName(name) {
67
- await this.query(`SET @velocious_connection_checkout_name = ${name === undefined ? "NULL" : this.quote(name)}`, {logName: "Set Connection Checkout Name", processListComment: false})
73
+ await this.query(`SET @velocious_connection_checkout_name = ${name === undefined ? "NULL" : this.quote(name)}`, {logName: "Set Connection Checkout Name", processListComment: false, sessionTimeZone: false})
68
74
  await super.setConnectionCheckoutName(name)
69
75
  }
70
76
 
@@ -73,16 +79,77 @@ export default class VelociousDatabaseDriversMysql extends Base{
73
79
  * @returns {Promise<void>} - Resolves when complete.
74
80
  */
75
81
  async clearConnectionCheckoutName() {
76
- await this.query("SET @velocious_connection_checkout_name = NULL", {logName: "Clear Connection Checkout Name", processListComment: false})
82
+ await this.query("SET @velocious_connection_checkout_name = NULL", {logName: "Clear Connection Checkout Name", processListComment: false, sessionTimeZone: false})
77
83
  await super.clearConnectionCheckoutName()
78
84
  }
79
85
 
80
86
  /**
81
- * Sets the database session timezone to UTC so bare timestamp literals store UTC instants.
87
+ * Hook before every query.
88
+ * @param {string} _sql - SQL string.
89
+ * @param {import("../base.js").QueryOptions} options - Query options.
82
90
  * @returns {Promise<void>} - Resolves when complete.
83
91
  */
84
- async setSessionTimezoneToUtc() {
85
- await this.query("SET time_zone = '+00:00'", {logName: "Set Session Time Zone", processListComment: false})
92
+ async beforeQuery(_sql, options) {
93
+ if (options.sessionTimeZone !== false) await this.ensureSessionTimeZone()
94
+ }
95
+
96
+ /**
97
+ * Gets the desired database session time zone for this connection context.
98
+ * @returns {string | null} - Desired session time zone.
99
+ */
100
+ getDesiredSessionTimeZone() {
101
+ return this._desiredSessionTimeZone
102
+ }
103
+
104
+ /**
105
+ * Sets the desired database session time zone without querying MySQL immediately.
106
+ * @param {string | null} timeZone - Desired session time zone.
107
+ */
108
+ setDesiredSessionTimeZone(timeZone) {
109
+ this._desiredSessionTimeZone = timeZone
110
+ }
111
+
112
+ /**
113
+ * Gets the database session time zone last confirmed through SET time_zone.
114
+ * @returns {string | null} - Current known session time zone.
115
+ */
116
+ getCurrentSessionTimeZone() {
117
+ return this._currentSessionTimeZone
118
+ }
119
+
120
+ /**
121
+ * Clears the current known database session time zone when the physical connection changes.
122
+ */
123
+ resetCurrentSessionTimeZone() {
124
+ this._currentSessionTimeZone = null
125
+ }
126
+
127
+ /**
128
+ * Ensures MySQL has the desired session time zone before user SQL runs.
129
+ * @returns {Promise<boolean>} - True when SET time_zone was executed.
130
+ */
131
+ async ensureSessionTimeZone() {
132
+ const desiredSessionTimeZone = this.getDesiredSessionTimeZone()
133
+
134
+ if (desiredSessionTimeZone === null || this.getCurrentSessionTimeZone() === desiredSessionTimeZone) return false
135
+
136
+ await this.setSessionTimeZone(desiredSessionTimeZone)
137
+
138
+ return true
139
+ }
140
+
141
+ /**
142
+ * Sets the database session time zone if it changed from the last confirmed value.
143
+ * @param {string} timeZone - Session time zone value accepted by MySQL.
144
+ * @returns {Promise<boolean>} - True when SET time_zone was executed.
145
+ */
146
+ async setSessionTimeZone(timeZone) {
147
+ if (this.getCurrentSessionTimeZone() === timeZone) return false
148
+
149
+ await this._queryActual(`SET time_zone = ${this.quote(timeZone)}`)
150
+ this._currentSessionTimeZone = timeZone
151
+
152
+ return true
86
153
  }
87
154
 
88
155
  /**
@@ -8,16 +8,28 @@ export default class VelociousDatabaseDriversSqliteConnectionSqlJs {
8
8
  * Runs constructor.
9
9
  * @param {import("../base.js").default} driver - Database driver instance.
10
10
  * @param {import("sql.js").Database} connection - Connection.
11
+ * @param {import("./web-persistence.js").SqliteWebPersistence} persistence - Database persistence adapter.
11
12
  */
12
- constructor(driver, connection) {
13
+ constructor(driver, connection, persistence) {
13
14
  this.connection = connection
14
15
  this.driver = driver
16
+ this.persistence = persistence
15
17
  }
16
18
 
17
19
  async close() {
18
- await this.saveDatabase()
20
+ await this.flushDatabaseSave()
19
21
  await this.connection.close()
20
22
  }
23
+
24
+ /**
25
+ * Flushes any debounced database save and waits until persistence is complete.
26
+ * @returns {Promise<void>} - Resolves when the current database bytes are stored.
27
+ */
28
+ async flushDatabaseSave() {
29
+ this.saveDatabaseDebounce.clear()
30
+ await this.saveDatabase()
31
+ }
32
+
21
33
  /**
22
34
  * Runs query.
23
35
  * @param {string} sql - SQL string.
@@ -36,10 +48,9 @@ export default class VelociousDatabaseDriversSqliteConnectionSqlJs {
36
48
  }
37
49
 
38
50
  saveDatabase = async () => {
39
- const localStorageContent = this.connection.export()
51
+ const databaseContent = this.connection.export()
40
52
 
41
- // @ts-expect-error
42
- await this.driver.betterLocalStorage.set(this.driver.localStorageName(), localStorageContent)
53
+ await this.persistence.save(databaseContent)
43
54
  }
44
55
 
45
56
  saveDatabaseDebounce = debounce(this.saveDatabase, 500)
@@ -1,8 +1,8 @@
1
1
  // @ts-check
2
2
 
3
- import BetterLocalStorage from "better-localstorage"
4
3
  import ConnectionSqlJs from "./connection-sql-js.js"
5
4
  import initSqlJs from "sql.js"
5
+ import {createSqliteWebPersistence, deleteSqliteWebPersistences, sqliteWebPersistenceKey} from "./web-persistence.js"
6
6
 
7
7
  import Base from "./base.js"
8
8
 
@@ -12,10 +12,6 @@ import Base from "./base.js"
12
12
  */
13
13
 
14
14
  export default class VelociousDatabaseDriversSqliteWeb extends Base {
15
- /**
16
- * Better local storage.
17
- * @type {BetterLocalStorage | undefined} */
18
- betterLocalStorage = undefined
19
15
  /**
20
16
  * Connection.
21
17
  * @type {ConnectionSqlJs | undefined} */
@@ -39,16 +35,14 @@ export default class VelociousDatabaseDriversSqliteWeb extends Base {
39
35
  this.args = this.getArgs()
40
36
 
41
37
  if (!this.args.getConnection) {
42
- this.betterLocalStorage ||= new BetterLocalStorage()
43
-
44
38
  if (this.args.reset) {
45
- await this.betterLocalStorage.delete(this.localStorageName())
39
+ await deleteSqliteWebPersistences({databaseName: this.databaseName()})
46
40
  }
47
41
 
42
+ const persistence = await createSqliteWebPersistence({databaseName: this.databaseName()})
48
43
  const SQL = await initSqlJs({locateFile: this.sqlJsLocateFile()})
49
-
50
- const databaseContent = await this.betterLocalStorage.get(this.localStorageName())
51
- const connectionSqlJs = new ConnectionSqlJs(this, new SQL.Database(databaseContent))
44
+ const databaseContent = await persistence.load()
45
+ const connectionSqlJs = new ConnectionSqlJs(this, new SQL.Database(databaseContent), persistence)
52
46
 
53
47
  this._connection = connectionSqlJs
54
48
  }
@@ -58,6 +52,18 @@ export default class VelociousDatabaseDriversSqliteWeb extends Base {
58
52
  await this.getConnection().close()
59
53
  }
60
54
 
55
+ /**
56
+ * Flushes pending SQL.js local persistence writes.
57
+ * @returns {Promise<void>} - Resolves when pending writes are durable.
58
+ */
59
+ async flushPendingWrites() {
60
+ if (!this.args?.getConnection) {
61
+ if (!this._connection) throw new Error("SQLite web connection has not been initialized")
62
+
63
+ await this._connection.flushDatabaseSave()
64
+ }
65
+ }
66
+
61
67
  /**
62
68
  * Runs get connection.
63
69
  * @returns {ConnectionSqlJs | SqliteWebConnection} - The connection.
@@ -72,11 +78,19 @@ export default class VelociousDatabaseDriversSqliteWeb extends Base {
72
78
  }
73
79
 
74
80
  localStorageName() {
75
- if (!this.args?.name) {
76
- throw new Error("No name given in arguments for SQLite Web database")
77
- }
81
+ return sqliteWebPersistenceKey(this.databaseName())
82
+ }
83
+
84
+ /**
85
+ * Returns the configured database name.
86
+ * @returns {string} - Database name.
87
+ */
88
+ databaseName() {
89
+ const name = this.args?.name
90
+
91
+ if (typeof name !== "string" || name.length < 1) throw new Error("No name given in arguments for SQLite Web database")
78
92
 
79
- return `VelociousDatabaseDriversSqliteWeb---${this.args?.name}`
93
+ return name
80
94
  }
81
95
 
82
96
  /**