velocious 1.0.674 → 1.0.676

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 (31) hide show
  1. package/README.md +4 -2
  2. package/build/database/query/model-class-query.js +25 -2
  3. package/build/database/query/order-base.js +9 -0
  4. package/build/database/query/order-column.js +10 -0
  5. package/build/database/query/order-plain.js +16 -0
  6. package/build/src/database/query/model-class-query.d.ts +10 -0
  7. package/build/src/database/query/model-class-query.d.ts.map +1 -1
  8. package/build/src/database/query/model-class-query.js +24 -3
  9. package/build/src/database/query/order-base.d.ts +6 -0
  10. package/build/src/database/query/order-base.d.ts.map +1 -1
  11. package/build/src/database/query/order-base.js +9 -1
  12. package/build/src/database/query/order-column.d.ts +5 -0
  13. package/build/src/database/query/order-column.d.ts.map +1 -1
  14. package/build/src/database/query/order-column.js +9 -1
  15. package/build/src/database/query/order-plain.d.ts +5 -0
  16. package/build/src/database/query/order-plain.d.ts.map +1 -1
  17. package/build/src/database/query/order-plain.js +14 -1
  18. package/build/src/testing/browser-frontend-tenant-database-scenarios.d.ts +11 -0
  19. package/build/src/testing/browser-frontend-tenant-database-scenarios.d.ts.map +1 -0
  20. package/build/src/testing/browser-frontend-tenant-database-scenarios.js +147 -0
  21. package/build/src/testing/browser-test-app.js +3 -1
  22. package/build/testing/browser-frontend-tenant-database-scenarios.js +158 -0
  23. package/build/testing/browser-test-app.js +2 -0
  24. package/package.json +1 -1
  25. package/scripts/test-browser.js +5 -0
  26. package/src/database/query/model-class-query.js +25 -2
  27. package/src/database/query/order-base.js +9 -0
  28. package/src/database/query/order-column.js +10 -0
  29. package/src/database/query/order-plain.js +16 -0
  30. package/src/testing/browser-frontend-tenant-database-scenarios.js +158 -0
  31. package/src/testing/browser-test-app.js +2 -0
@@ -27,6 +27,15 @@ export default class VelociousDatabaseQueryOrderBase {
27
27
  throw new Error("setReverseOrder not implemented")
28
28
  }
29
29
 
30
+ /**
31
+ * Runs reversed copy.
32
+ * @abstract
33
+ * @returns {import("./order-base.js").default} - A new independent order with the direction reversed.
34
+ */
35
+ reversedCopy() {
36
+ throw new Error("reversedCopy not implemented")
37
+ }
38
+
30
39
  toSql() {
31
40
  throw new Error("'toSql' wasn't implemented")
32
41
  }
@@ -59,6 +59,16 @@ export default class VelociousDatabaseQueryOrderColumn extends OrderBase {
59
59
  this.reverseOrder = reverseOrder
60
60
  }
61
61
 
62
+ /**
63
+ * Runs reversed copy.
64
+ * @returns {VelociousDatabaseQueryOrderColumn} - A new independent order reversing the effective (rendered) direction.
65
+ */
66
+ reversedCopy() {
67
+ const direction = this.reverseOrder ? this.direction : reverseDirection(this.direction)
68
+
69
+ return new VelociousDatabaseQueryOrderColumn(this.query, {column: this.column, direction, tableName: this.tableName})
70
+ }
71
+
62
72
  /**
63
73
  * Runs to sql.
64
74
  * @returns {string} - SQL string.
@@ -18,6 +18,22 @@ export default class VelociousDatabaseQueryOrderPlain extends OrderBase {
18
18
  this.reverseOrder = true
19
19
  }
20
20
 
21
+ /**
22
+ * Runs reversed copy.
23
+ * @returns {VelociousDatabaseQueryOrderPlain} - A new independent order reversing the effective (rendered) direction; a directionless plain order renders DESC.
24
+ */
25
+ reversedCopy() {
26
+ const effective = this.reverseOrder ? `${this.plain} DESC` : this.plain
27
+ const match = effective.match(/^(.*\S)\s+(ASC|DESC)$/i)
28
+
29
+ if (match) {
30
+ const direction = match[2].toUpperCase() == "ASC" ? "DESC" : "ASC"
31
+ return new VelociousDatabaseQueryOrderPlain(this.query, `${match[1]} ${direction}`)
32
+ }
33
+
34
+ return new VelociousDatabaseQueryOrderPlain(this.query, `${effective} DESC`)
35
+ }
36
+
21
37
  toSql() {
22
38
  if (this.reverseOrder) {
23
39
  return `${this.plain} DESC`
@@ -0,0 +1,158 @@
1
+ // @ts-check
2
+
3
+ import Configuration, { CurrentConfigurationNotSetError } from "../configuration.js"
4
+ import DatabaseRecord from "../database/record/index.js"
5
+ import BrowserEnvironmentHandler from "../environment-handlers/browser.js"
6
+ import Migration from "../database/migration/index.js"
7
+ import SingleMultiUsePool from "../database/pool/single-multi-use.js"
8
+ import SqliteWebDriver from "../database/drivers/sqlite/index.web.js"
9
+ import Tenant from "../tenants/tenant.js"
10
+
11
+ const DATABASE_IDENTIFIER = "projectTenant"
12
+ const SCHEMA_GENERATION = "frontend-tenant-browser-persistence-1"
13
+
14
+ /**
15
+ * Returns the current configuration without requiring one to exist.
16
+ * @returns {Configuration | undefined} Current configuration when installed.
17
+ */
18
+ function currentConfigurationOrUndefined() {
19
+ try {
20
+ return Configuration.current()
21
+ } catch (error) {
22
+ if (!(error instanceof CurrentConfigurationNotSetError)) throw error
23
+
24
+ return undefined
25
+ }
26
+ }
27
+
28
+ /**
29
+ * Builds the real browser-backed two-project configuration.
30
+ * @returns {Configuration} Browser tenant configuration.
31
+ */
32
+ function buildConfiguration() {
33
+ return new Configuration({
34
+ database: {
35
+ test: {
36
+ projectTenant: {
37
+ driver: SqliteWebDriver,
38
+ locateFile: () => "/sql-wasm.wasm",
39
+ migrations: true,
40
+ name: "velocious-browser-project-proof-template",
41
+ poolType: SingleMultiUsePool,
42
+ tenantOnly: true,
43
+ type: "sqlite"
44
+ }
45
+ }
46
+ },
47
+ directory: "/frontend-tenant-browser-persistence-scenario",
48
+ environment: "test",
49
+ environmentHandler: new BrowserEnvironmentHandler(),
50
+ frontendTenantSqlite: {maxOpenHandles: 2},
51
+ initializeModels: async () => {},
52
+ locale: "en",
53
+ localeFallbacks: {en: ["en"]},
54
+ locales: ["en"],
55
+ tenantDatabaseResolver: ({identifier, tenant}) => {
56
+ if (identifier !== DATABASE_IDENTIFIER || !tenant || typeof tenant !== "object") return
57
+ const projectSlug = /** @type {{projectSlug?: string}} */ (tenant).projectSlug
58
+
59
+ if (projectSlug !== "alpha" && projectSlug !== "beta") return
60
+
61
+ return {name: `velocious-browser-project-proof-${projectSlug}`}
62
+ }
63
+ })
64
+ }
65
+
66
+ /**
67
+ * Exercises two durable physical tenant databases inside the real browser.
68
+ * @returns {Promise<{alphaNames: ReturnType<typeof JSON.parse>[], betaNames: ReturnType<typeof JSON.parse>[], identitiesAreDistinct: boolean, openCount: number}>} Serializable proof result.
69
+ */
70
+ export default async function runFrontendTenantDatabasePersistenceScenario() {
71
+ const previousConfiguration = currentConfigurationOrUndefined()
72
+ const configuration = buildConfiguration()
73
+
74
+ class BrowserProjectRecord extends DatabaseRecord {}
75
+ class CreateBrowserProjectRecords extends Migration {
76
+ async up() {
77
+ await this.execute("CREATE TABLE browser_project_records(id integer PRIMARY KEY AUTOINCREMENT, name varchar(255) NOT NULL)")
78
+ }
79
+ }
80
+
81
+ BrowserProjectRecord.setTableName("browser_project_records")
82
+ BrowserProjectRecord.switchesTenantDatabase(DATABASE_IDENTIFIER)
83
+ BrowserProjectRecord.registerRecordClass({configuration})
84
+ CreateBrowserProjectRecords.onDatabases([DATABASE_IDENTIFIER])
85
+
86
+ const migrationFile = "20260919090000-create-browser-project-records.js"
87
+ /**
88
+ * Loads the one migration bundled into this browser proof.
89
+ * @type {import("../database/migrator/types.js").RequireMigrationContextType}
90
+ */
91
+ const migrations = (fileName) => {
92
+ if (fileName !== migrationFile) throw new Error(`Unknown browser tenant proof migration: ${fileName}`)
93
+
94
+ return {default: CreateBrowserProjectRecords}
95
+ }
96
+
97
+ migrations.keys = () => [migrationFile]
98
+ migrations.id = "frontend-tenant-browser-persistence-scenario"
99
+
100
+ const alpha = Tenant.handle({projectSlug: "alpha"}, configuration)
101
+ const beta = Tenant.handle({projectSlug: "beta"}, configuration)
102
+
103
+ configuration.setCurrent()
104
+
105
+ try {
106
+ await Promise.all([
107
+ alpha.delete({databaseIdentifier: DATABASE_IDENTIFIER}),
108
+ beta.delete({databaseIdentifier: DATABASE_IDENTIFIER})
109
+ ])
110
+ await Promise.all([
111
+ alpha.initialize({databaseIdentifier: DATABASE_IDENTIFIER, migrations, schemaGeneration: SCHEMA_GENERATION}),
112
+ beta.initialize({databaseIdentifier: DATABASE_IDENTIFIER, migrations, schemaGeneration: SCHEMA_GENERATION})
113
+ ])
114
+
115
+ for (let index = 0; index < 12; index++) {
116
+ await Promise.all([
117
+ alpha.databaseOperation({databaseIdentifier: DATABASE_IDENTIFIER}, async (operation) => {
118
+ await operation.forModel(BrowserProjectRecord).create({name: `alpha-${index}`})
119
+ }),
120
+ beta.databaseOperation({databaseIdentifier: DATABASE_IDENTIFIER}, async (operation) => {
121
+ await operation.forModel(BrowserProjectRecord).create({name: `beta-${index}`})
122
+ })
123
+ ])
124
+ }
125
+
126
+ await Promise.all([
127
+ alpha.close({databaseIdentifier: DATABASE_IDENTIFIER, flush: true}),
128
+ beta.close({databaseIdentifier: DATABASE_IDENTIFIER, flush: true})
129
+ ])
130
+ await Promise.all([
131
+ alpha.initialize({databaseIdentifier: DATABASE_IDENTIFIER, migrations, schemaGeneration: SCHEMA_GENERATION}),
132
+ beta.initialize({databaseIdentifier: DATABASE_IDENTIFIER, migrations, schemaGeneration: SCHEMA_GENERATION})
133
+ ])
134
+
135
+ const [alphaNames, betaNames] = await Promise.all([
136
+ alpha.databaseOperation({databaseIdentifier: DATABASE_IDENTIFIER}, async (operation) => {
137
+ return await operation.forModel(BrowserProjectRecord).order("id").pluck("name")
138
+ }),
139
+ beta.databaseOperation({databaseIdentifier: DATABASE_IDENTIFIER}, async (operation) => {
140
+ return await operation.forModel(BrowserProjectRecord).order("id").pluck("name")
141
+ })
142
+ ])
143
+
144
+ return {
145
+ alphaNames,
146
+ betaNames,
147
+ identitiesAreDistinct: alpha.databaseIdentity(DATABASE_IDENTIFIER) !== beta.databaseIdentity(DATABASE_IDENTIFIER),
148
+ openCount: configuration.inspectFrontendTenantSqliteHandles().openCount
149
+ }
150
+ } finally {
151
+ await Promise.all([
152
+ alpha.delete({databaseIdentifier: DATABASE_IDENTIFIER}),
153
+ beta.delete({databaseIdentifier: DATABASE_IDENTIFIER})
154
+ ])
155
+ if (previousConfiguration) previousConfiguration.setCurrent()
156
+ await configuration.closeDatabaseConnections()
157
+ }
158
+ }
@@ -3,6 +3,7 @@
3
3
  import SystemTestBrowserHelper from "system-testing/build/system-test-browser-helper.js"
4
4
  import BrowserEnvironmentHandler from "../environment-handlers/browser.js"
5
5
  import runFrontendModelEventHookScenario from "./browser-frontend-model-event-hook-scenarios.js"
6
+ import runFrontendTenantDatabasePersistenceScenario from "./browser-frontend-tenant-database-scenarios.js"
6
7
  import runUseDatabaseSelectionTransitionScenario from "./browser-use-database-hook-scenarios.js"
7
8
 
8
9
  const root = document.getElementById("root") || (() => {
@@ -29,6 +30,7 @@ systemTestBrowserHelper.enableOnBrowser()
29
30
  Object.assign(globalThis, {velociousBrowserTest: {
30
31
  BrowserEnvironmentHandler,
31
32
  runFrontendModelEventHookScenario,
33
+ runFrontendTenantDatabasePersistenceScenario,
32
34
  runUseDatabaseSelectionTransitionScenario,
33
35
  systemTestBrowserHelper
34
36
  }})