From ce44c5eecf96ad020d2ba9dba9e258bf169424b4 Mon Sep 17 00:00:00 2001 From: Tommy Smith Date: Tue, 24 Jun 2025 11:49:58 +0100 Subject: [PATCH] Add `.activate`, `.deactivate`, `.offload` helpers to `collection.tenants` --- src/collections/tenants/index.ts | 61 ++++++++++++++++++--- src/collections/tenants/integration.test.ts | 13 +++++ 2 files changed, 65 insertions(+), 9 deletions(-) diff --git a/src/collections/tenants/index.ts b/src/collections/tenants/index.ts index 320f166d..802e53af 100644 --- a/src/collections/tenants/index.ts +++ b/src/collections/tenants/index.ts @@ -41,6 +41,16 @@ const tenants = ( }); return result; }); + const update = async (tenants: TenantBC | TenantUpdate | (TenantBC | TenantUpdate)[]) => { + const out: Tenant[] = []; + for await (const res of Serialize.tenants(parseValueOrValueArray(tenants), Serialize.tenantUpdate).map( + (tenants) => + new TenantsUpdater(connection, collection, tenants).do().then((res) => res.map(parseTenantREST)) + )) { + out.push(...res); + } + return out; + }; return { create: (tenants: TenantBC | TenantCreate | (TenantBC | TenantCreate)[]) => new TenantsCreator(connection, collection, parseValueOrValueArray(tenants).map(Serialize.tenantCreate)) @@ -72,15 +82,24 @@ const tenants = ( collection, parseValueOrValueArray(tenants).map(parseStringOrTenant) ).do(), - update: async (tenants: TenantBC | TenantUpdate | (TenantBC | TenantUpdate)[]) => { - const out: Tenant[] = []; - for await (const res of Serialize.tenants(parseValueOrValueArray(tenants), Serialize.tenantUpdate).map( - (tenants) => - new TenantsUpdater(connection, collection, tenants).do().then((res) => res.map(parseTenantREST)) - )) { - out.push(...res); - } - return out; + update, + activate: (tenant: string | TenantBase) => { + return update({ + name: parseStringOrTenant(tenant), + activityStatus: 'ACTIVE', + }); + }, + deactivate: (tenant: string | TenantBase) => { + return update({ + name: parseStringOrTenant(tenant), + activityStatus: 'INACTIVE', + }); + }, + offload: (tenant: string | TenantBase) => { + return update({ + name: parseStringOrTenant(tenant), + activityStatus: 'OFFLOADED', + }); }, }; }; @@ -169,4 +188,28 @@ export interface Tenants { * @returns {Promise} The updated tenant(s) as a list of Tenant. */ update: (tenants: TenantBC | TenantUpdate | (TenantBC | TenantUpdate)[]) => Promise; + /** + * Activate the specified tenant for a collection in Weaviate. + * The collection must have been created with multi-tenancy enabled. + * + * @param {TenantBase | string} tenant The tenant to activate. + * @returns {Promise} The activated tenant as a list of Tenant. + */ + activate: (tenant: string | TenantBase) => Promise; + /** + * Deactivate the specified tenant for a collection in Weaviate. + * The collection must have been created with multi-tenancy enabled. + * + * @param {TenantBase | string} tenant The tenant to deactivate. + * @returns {Promise} The deactivated tenant as a list of Tenant + */ + deactivate: (tenant: string | TenantBase) => Promise; + /** + * Offload the specified tenant for a collection in Weaviate. + * The collection must have been created with multi-tenancy enabled. + * + * @param {TenantBase | string} tenant The tenant to offload. + * @returns {Promise} The offloaded tenant as a list of Tenant + */ + offload: (tenant: string | TenantBase) => Promise; } diff --git a/src/collections/tenants/integration.test.ts b/src/collections/tenants/integration.test.ts index b574759a..1455980a 100644 --- a/src/collections/tenants/integration.test.ts +++ b/src/collections/tenants/integration.test.ts @@ -178,4 +178,17 @@ describe('Testing of the collection.tenants methods', () => { expect(Object.entries(updated).length).toBe(howMany); expect(Object.values(updated).every((tenant) => tenant.activityStatus === 'INACTIVE')).toBe(true); }); + + it('should be able to deactivate and activate a tenant using helper methods', async () => { + const tenantName = 'hot'; + await collection.tenants + .deactivate(tenantName) + .then(() => collection.tenants.get()) + .then((tenants) => expect(tenants[tenantName].activityStatus).toBe('INACTIVE')); + + await collection.tenants + .activate(tenantName) + .then(() => collection.tenants.get()) + .then((tenants) => expect(tenants[tenantName].activityStatus).toBe('ACTIVE')); + }); });