velocious 1.0.540 → 1.0.541
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/package.json
CHANGED
|
@@ -3167,6 +3167,47 @@ class VelociousDatabaseRecord {
|
|
|
3167
3167
|
return await this._newQuery().findByOrFail(conditions)
|
|
3168
3168
|
}
|
|
3169
3169
|
|
|
3170
|
+
/**
|
|
3171
|
+
* Returns a scope whose eager finders run against an explicit `tenant` (and
|
|
3172
|
+
* therefore its database) instead of whatever tenant is ambient in
|
|
3173
|
+
* `Current.tenant()`. Use it to read a model that may live in a specific
|
|
3174
|
+
* tenant or in the default database from another tenant context — for example
|
|
3175
|
+
* `GithubWebhook.usingTenant(tenant).findBy({id})` — without depending on
|
|
3176
|
+
* which tenant happens to be active. The target tenant's connections are
|
|
3177
|
+
* ensured for the duration of each query.
|
|
3178
|
+
* @template {typeof VelociousDatabaseRecord} MC
|
|
3179
|
+
* @this {MC}
|
|
3180
|
+
* @param {?} tenant - Tenant descriptor to scope the queries to (as accepted by `configuration.runWithTenant`).
|
|
3181
|
+
* @returns {{find: (recordId: ?) => Promise<InstanceType<MC> | null>, findBy: (conditions: {[key: string]: string | number}) => Promise<InstanceType<MC> | null>, findByOrFail: (conditions: {[key: string]: string | number}) => Promise<InstanceType<MC>>}} - Eager finders scoped to the given tenant.
|
|
3182
|
+
*/
|
|
3183
|
+
static usingTenant(tenant) {
|
|
3184
|
+
const ModelClass = this
|
|
3185
|
+
|
|
3186
|
+
return {
|
|
3187
|
+
find: (recordId) => ModelClass._runUsingTenant(tenant, () => ModelClass.find(recordId)),
|
|
3188
|
+
findBy: (conditions) => ModelClass._runUsingTenant(tenant, () => ModelClass.findBy(conditions)),
|
|
3189
|
+
findByOrFail: (conditions) => ModelClass._runUsingTenant(tenant, () => ModelClass.findByOrFail(conditions))
|
|
3190
|
+
}
|
|
3191
|
+
}
|
|
3192
|
+
|
|
3193
|
+
/**
|
|
3194
|
+
* Runs `callback` with the tenant switched to `tenant` and that tenant's
|
|
3195
|
+
* connections ensured. Backs `usingTenant`.
|
|
3196
|
+
* @template T
|
|
3197
|
+
* @param {?} tenant - Tenant descriptor.
|
|
3198
|
+
* @param {() => Promise<T>} callback - Query to run under the tenant.
|
|
3199
|
+
* @returns {Promise<T>} - Resolves with the callback's result.
|
|
3200
|
+
*/
|
|
3201
|
+
static async _runUsingTenant(tenant, callback) {
|
|
3202
|
+
const configuration = this._getConfiguration()
|
|
3203
|
+
|
|
3204
|
+
// Do NOT ensureInitialized() out here: for a tenant-switched model whose
|
|
3205
|
+
// first initialization would resolve metadata from the ambient tenant's
|
|
3206
|
+
// database, that must happen under the requested tenant. The finders inside
|
|
3207
|
+
// `callback` call ensureInitialized() themselves, now within this scope.
|
|
3208
|
+
return await configuration.runWithTenant(tenant, () => configuration.ensureConnections({name: `usingTenant: ${this.getModelName()}`}, callback))
|
|
3209
|
+
}
|
|
3210
|
+
|
|
3170
3211
|
/**
|
|
3171
3212
|
* Runs find or create by.
|
|
3172
3213
|
* @template {typeof VelociousDatabaseRecord} MC
|