velocious 1.0.489 → 1.0.493
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/build/application.js +3 -0
- package/build/cli/commands/db/tenants/create.js +7 -5
- package/build/cli/commands/db/tenants/drop.js +6 -6
- package/build/configuration-types.js +8 -0
- package/build/configuration.js +29 -0
- package/build/database/query/index.js +30 -0
- package/build/database/query/model-class-query.js +17 -0
- package/build/database/record/index.js +16 -0
- package/build/src/application.d.ts.map +1 -1
- package/build/src/application.js +3 -1
- package/build/src/cli/commands/db/tenants/create.d.ts.map +1 -1
- package/build/src/cli/commands/db/tenants/create.js +8 -5
- package/build/src/cli/commands/db/tenants/drop.d.ts +1 -1
- package/build/src/cli/commands/db/tenants/drop.d.ts.map +1 -1
- package/build/src/cli/commands/db/tenants/drop.js +7 -6
- package/build/src/configuration-types.d.ts +24 -0
- package/build/src/configuration-types.d.ts.map +1 -1
- package/build/src/configuration-types.js +8 -1
- package/build/src/configuration.d.ts +6 -0
- package/build/src/configuration.d.ts.map +1 -1
- package/build/src/configuration.js +26 -1
- package/build/src/database/query/index.d.ts +25 -0
- package/build/src/database/query/index.d.ts.map +1 -1
- package/build/src/database/query/index.js +26 -1
- package/build/src/database/query/model-class-query.d.ts +10 -0
- package/build/src/database/query/model-class-query.d.ts.map +1 -1
- package/build/src/database/query/model-class-query.js +16 -1
- package/build/src/database/record/index.d.ts +7 -0
- package/build/src/database/record/index.d.ts.map +1 -1
- package/build/src/database/record/index.js +15 -1
- package/build/src/sync/query-scope.d.ts +18 -0
- package/build/src/sync/query-scope.d.ts.map +1 -0
- package/build/src/sync/query-scope.js +77 -0
- package/build/src/sync/server-change-feed.d.ts.map +1 -1
- package/build/src/sync/server-change-feed.js +2 -24
- package/build/src/sync/stable-json.d.ts +9 -0
- package/build/src/sync/stable-json.d.ts.map +1 -0
- package/build/src/sync/stable-json.js +27 -0
- package/build/src/sync/sync-api-client.d.ts +185 -5
- package/build/src/sync/sync-api-client.d.ts.map +1 -1
- package/build/src/sync/sync-api-client.js +309 -17
- package/build/src/sync/sync-api-controller.d.ts +8 -0
- package/build/src/sync/sync-api-controller.d.ts.map +1 -1
- package/build/src/sync/sync-api-controller.js +17 -1
- package/build/src/sync/sync-client-registry.d.ts +12 -0
- package/build/src/sync/sync-client-registry.d.ts.map +1 -0
- package/build/src/sync/sync-client-registry.js +27 -0
- package/build/src/sync/sync-client-types.d.ts +127 -0
- package/build/src/sync/sync-client-types.d.ts.map +1 -0
- package/build/src/sync/sync-client-types.js +3 -0
- package/build/src/sync/sync-client.d.ts +176 -0
- package/build/src/sync/sync-client.d.ts.map +1 -0
- package/build/src/sync/sync-client.js +380 -0
- package/build/src/sync/sync-envelope-replay-service.d.ts +153 -10
- package/build/src/sync/sync-envelope-replay-service.d.ts.map +1 -1
- package/build/src/sync/sync-envelope-replay-service.js +174 -14
- package/build/src/sync/sync-replay-upsert-applier.d.ts +102 -0
- package/build/src/sync/sync-replay-upsert-applier.d.ts.map +1 -0
- package/build/src/sync/sync-replay-upsert-applier.js +158 -0
- package/build/src/sync/sync-resource-base.d.ts +98 -0
- package/build/src/sync/sync-resource-base.d.ts.map +1 -0
- package/build/src/sync/sync-resource-base.js +122 -0
- package/build/src/sync/sync-scope-store.d.ts +128 -0
- package/build/src/sync/sync-scope-store.d.ts.map +1 -0
- package/build/src/sync/sync-scope-store.js +282 -0
- package/build/src/tenants/default-tenant-database-provisioning.d.ts +21 -0
- package/build/src/tenants/default-tenant-database-provisioning.d.ts.map +1 -0
- package/build/src/tenants/default-tenant-database-provisioning.js +106 -0
- package/build/src/tenants/tenant.d.ts.map +1 -1
- package/build/src/tenants/tenant.js +6 -5
- package/build/sync/query-scope.js +84 -0
- package/build/sync/server-change-feed.js +1 -25
- package/build/sync/stable-json.js +28 -0
- package/build/sync/sync-api-client.js +331 -17
- package/build/sync/sync-api-controller.js +20 -0
- package/build/sync/sync-client-registry.js +30 -0
- package/build/sync/sync-client-types.js +41 -0
- package/build/sync/sync-client.js +433 -0
- package/build/sync/sync-envelope-replay-service.js +190 -13
- package/build/sync/sync-replay-upsert-applier.js +179 -0
- package/build/sync/sync-resource-base.js +143 -0
- package/build/sync/sync-scope-store.js +327 -0
- package/build/tenants/default-tenant-database-provisioning.js +124 -0
- package/build/tenants/tenant.js +5 -5
- package/build/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/application.js +3 -0
- package/src/cli/commands/db/tenants/create.js +7 -5
- package/src/cli/commands/db/tenants/drop.js +6 -6
- package/src/configuration-types.js +8 -0
- package/src/configuration.js +29 -0
- package/src/database/query/index.js +30 -0
- package/src/database/query/model-class-query.js +17 -0
- package/src/database/record/index.js +16 -0
- package/src/sync/query-scope.js +84 -0
- package/src/sync/server-change-feed.js +1 -25
- package/src/sync/stable-json.js +28 -0
- package/src/sync/sync-api-client.js +331 -17
- package/src/sync/sync-api-controller.js +20 -0
- package/src/sync/sync-client-registry.js +30 -0
- package/src/sync/sync-client-types.js +41 -0
- package/src/sync/sync-client.js +433 -0
- package/src/sync/sync-envelope-replay-service.js +190 -13
- package/src/sync/sync-replay-upsert-applier.js +179 -0
- package/src/sync/sync-resource-base.js +143 -0
- package/src/sync/sync-scope-store.js +327 -0
- package/src/tenants/default-tenant-database-provisioning.js +124 -0
- package/src/tenants/tenant.js +5 -5
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Default tenant-database provisioning used by `db:tenants:create`,
|
|
5
|
+
* `db:tenants:drop`, and `Tenant.drop` when a tenant-database provider does not
|
|
6
|
+
* define its own `createDatabase`/`dropDatabase`. This is the generic,
|
|
7
|
+
* driver-agnostic lifecycle every app on the tenant framework needs, so it lives
|
|
8
|
+
* in the framework instead of being reimplemented in each consuming project.
|
|
9
|
+
*
|
|
10
|
+
* File-backed drivers (sqlite) have no server-side CREATE/DROP DATABASE — the file
|
|
11
|
+
* is created on connect and left in place on drop — so creating just ensures the
|
|
12
|
+
* tenant's connection and dropping is a no-op. Server drivers (mysql, pgsql, …)
|
|
13
|
+
* connect to the configured maintenance database (`useDatabase`) and run the
|
|
14
|
+
* driver's own `createDatabaseSql`/`dropDatabaseSql`.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
const SAFE_TENANT_DATABASE_NAME = /^[a-z0-9_]+$/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Whether the driver stores each database as a local file instead of on a server.
|
|
21
|
+
* @param {import("../configuration-types.js").DatabaseConfigurationType} databaseConfiguration - Resolved tenant database configuration.
|
|
22
|
+
* @returns {boolean} - True for file-backed drivers such as sqlite.
|
|
23
|
+
*/
|
|
24
|
+
function fileBackedDriver(databaseConfiguration) {
|
|
25
|
+
return databaseConfiguration.type === "sqlite"
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Validates the tenant database name against a strict allowlist before it is
|
|
30
|
+
* interpolated into DDL.
|
|
31
|
+
* @param {import("../configuration-types.js").DatabaseConfigurationType} databaseConfiguration - Resolved tenant database configuration.
|
|
32
|
+
* @returns {string} - The validated tenant database name.
|
|
33
|
+
*/
|
|
34
|
+
function guardedDatabaseName(databaseConfiguration) {
|
|
35
|
+
const databaseName = databaseConfiguration.database
|
|
36
|
+
|
|
37
|
+
if (!databaseName || !SAFE_TENANT_DATABASE_NAME.test(databaseName)) {
|
|
38
|
+
throw new Error(`Unsafe tenant database name: ${databaseName}`)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return databaseName
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Opens a driver connected to the server's maintenance database (`useDatabase`),
|
|
46
|
+
* from which `CREATE DATABASE` / `DROP DATABASE` for the tenant can be issued.
|
|
47
|
+
* @param {import("../configuration-types.js").DatabaseConfigurationType} databaseConfiguration - Resolved tenant database configuration.
|
|
48
|
+
* @param {import("../configuration.js").default} configuration - The Velocious configuration.
|
|
49
|
+
* @returns {import("../database/drivers/base.js").default} - A driver bound to the maintenance database.
|
|
50
|
+
*/
|
|
51
|
+
function maintenanceConnection(databaseConfiguration, configuration) {
|
|
52
|
+
const DriverClass = databaseConfiguration.driver
|
|
53
|
+
|
|
54
|
+
if (!DriverClass) {
|
|
55
|
+
throw new Error("Tenant database configuration is missing a driver.")
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (!databaseConfiguration.useDatabase) {
|
|
59
|
+
throw new Error("Tenant database configuration is missing a maintenance database in useDatabase.")
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return new DriverClass({...databaseConfiguration, database: databaseConfiguration.useDatabase}, configuration)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Creates the tenant database for one tenant.
|
|
67
|
+
* @param {{configuration: import("../configuration.js").default, databaseConfiguration: import("../configuration-types.js").DatabaseConfigurationType, tenant: ?}} args - Provisioning arguments.
|
|
68
|
+
* @returns {Promise<void>} - Resolves once the tenant database exists.
|
|
69
|
+
*/
|
|
70
|
+
export async function createTenantDatabase({configuration, databaseConfiguration, tenant}) {
|
|
71
|
+
if (fileBackedDriver(databaseConfiguration)) {
|
|
72
|
+
await configuration.runWithTenant(tenant, async () => {
|
|
73
|
+
await configuration.ensureConnections(async () => {})
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
return
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const databaseName = guardedDatabaseName(databaseConfiguration)
|
|
80
|
+
const connection = maintenanceConnection(databaseConfiguration, configuration)
|
|
81
|
+
|
|
82
|
+
await connection.connect()
|
|
83
|
+
|
|
84
|
+
try {
|
|
85
|
+
const sqls = connection.createDatabaseSql(databaseName, {
|
|
86
|
+
databaseCharset: databaseConfiguration.databaseCharset,
|
|
87
|
+
databaseCollation: databaseConfiguration.databaseCollation,
|
|
88
|
+
ifNotExists: true
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
for (const sql of sqls) {
|
|
92
|
+
await connection.query(sql)
|
|
93
|
+
}
|
|
94
|
+
} finally {
|
|
95
|
+
await connection.close()
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Drops the tenant database for one tenant. Uses `DROP DATABASE IF EXISTS`, so it
|
|
101
|
+
* is a safe no-op for a tenant that was never fully provisioned.
|
|
102
|
+
* @param {{configuration: import("../configuration.js").default, databaseConfiguration: import("../configuration-types.js").DatabaseConfigurationType}} args - Provisioning arguments.
|
|
103
|
+
* @returns {Promise<void>} - Resolves once the tenant database is gone.
|
|
104
|
+
*/
|
|
105
|
+
export async function dropTenantDatabase({configuration, databaseConfiguration}) {
|
|
106
|
+
if (fileBackedDriver(databaseConfiguration)) {
|
|
107
|
+
return
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const databaseName = guardedDatabaseName(databaseConfiguration)
|
|
111
|
+
const connection = maintenanceConnection(databaseConfiguration, configuration)
|
|
112
|
+
|
|
113
|
+
await connection.connect()
|
|
114
|
+
|
|
115
|
+
try {
|
|
116
|
+
const sqls = connection.dropDatabaseSql(databaseName, {ifExists: true})
|
|
117
|
+
|
|
118
|
+
for (const sql of sqls) {
|
|
119
|
+
await connection.query(sql)
|
|
120
|
+
}
|
|
121
|
+
} finally {
|
|
122
|
+
await connection.close()
|
|
123
|
+
}
|
|
124
|
+
}
|
package/src/tenants/tenant.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// @ts-check
|
|
2
2
|
|
|
3
3
|
import Current from "../current.js"
|
|
4
|
+
import {dropTenantDatabase} from "./default-tenant-database-provisioning.js"
|
|
4
5
|
import TenantIterator from "./tenant-iterator.js"
|
|
5
6
|
|
|
6
7
|
/**
|
|
@@ -80,10 +81,9 @@ export default class Tenant {
|
|
|
80
81
|
*/
|
|
81
82
|
static async drop({identifier, tenant, configuration = Current.configuration()}) {
|
|
82
83
|
const provider = configuration.getTenantDatabaseProvider(identifier)
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
}
|
|
84
|
+
const dropDatabase = typeof provider.dropDatabase === "function"
|
|
85
|
+
? provider.dropDatabase.bind(provider)
|
|
86
|
+
: dropTenantDatabase
|
|
87
87
|
|
|
88
88
|
await configuration.runWithTenant(tenant, async () => {
|
|
89
89
|
// Guard against an unresolved tenant. resolveDatabaseConfiguration falls back to the
|
|
@@ -94,7 +94,7 @@ export default class Tenant {
|
|
|
94
94
|
throw new Error(`Tenant database identifier ${identifier} is inactive for tenant: ${TenantIterator.tenantLabel(tenant)}`)
|
|
95
95
|
}
|
|
96
96
|
|
|
97
|
-
await
|
|
97
|
+
await dropDatabase({
|
|
98
98
|
configuration,
|
|
99
99
|
databaseConfiguration: configuration.resolveDatabaseConfiguration(identifier),
|
|
100
100
|
identifier,
|