From ce44c5eecf96ad020d2ba9dba9e258bf169424b4 Mon Sep 17 00:00:00 2001 From: Tommy Smith Date: Tue, 24 Jun 2025 11:49:58 +0100 Subject: [PATCH 1/3] 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')); + }); }); From 5efb23ef86fe343af6f480cbf40e012e1a4c782d Mon Sep 17 00:00:00 2001 From: Tommy Smith Date: Fri, 1 Aug 2025 10:19:49 +0100 Subject: [PATCH 2/3] Allow lists of tenants when using helper methods --- src/collections/tenants/index.ts | 56 ++++++++++++--------- src/collections/tenants/integration.test.ts | 2 +- 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/src/collections/tenants/index.ts b/src/collections/tenants/index.ts index 802e53af..a63cb5f4 100644 --- a/src/collections/tenants/index.ts +++ b/src/collections/tenants/index.ts @@ -52,7 +52,7 @@ const tenants = ( return out; }; return { - create: (tenants: TenantBC | TenantCreate | (TenantBC | TenantCreate)[]) => + create: (tenants) => new TenantsCreator(connection, collection, parseValueOrValueArray(tenants).map(Serialize.tenantCreate)) .do() .then((res) => res.map(parseTenantREST)), @@ -60,8 +60,8 @@ const tenants = ( const check = await dbVersionSupport.supportsTenantsGetGRPCMethod(); return check.supports ? getGRPC() : getREST(); }, - getByNames: (tenants: (string | T)[]) => getGRPC(tenants.map(parseStringOrTenant)), - getByName: async (tenant: string | T) => { + getByNames: (tenants) => getGRPC(tenants.map(parseStringOrTenant)), + getByName: async (tenant) => { const tenantName = parseStringOrTenant(tenant); if (await dbVersionSupport.supportsTenantGetRESTMethod().then((check) => !check.supports)) { return getGRPC([tenantName]).then((tenants) => tenants[tenantName] ?? null); @@ -76,30 +76,36 @@ const tenants = ( throw err; }); }, - remove: (tenants: string | T | (string | T)[]) => + remove: (tenants) => new TenantsDeleter( connection, collection, parseValueOrValueArray(tenants).map(parseStringOrTenant) ).do(), update, - activate: (tenant: string | TenantBase) => { - return update({ - name: parseStringOrTenant(tenant), - activityStatus: 'ACTIVE', - }); + activate: (tenant) => { + return update( + parseValueOrValueArray(tenant).map((tenant) => ({ + name: parseStringOrTenant(tenant), + activityStatus: 'ACTIVE', + })) + ); }, - deactivate: (tenant: string | TenantBase) => { - return update({ - name: parseStringOrTenant(tenant), - activityStatus: 'INACTIVE', - }); + deactivate: (tenant) => { + return update( + parseValueOrValueArray(tenant).map((tenant) => ({ + name: parseStringOrTenant(tenant), + activityStatus: 'INACTIVE', + })) + ); }, - offload: (tenant: string | TenantBase) => { - return update({ - name: parseStringOrTenant(tenant), - activityStatus: 'OFFLOADED', - }); + offload: (tenant) => { + return update( + parseValueOrValueArray(tenant).map((tenant) => ({ + name: parseStringOrTenant(tenant), + activityStatus: 'OFFLOADED', + })) + ); }, }; }; @@ -192,24 +198,24 @@ export interface Tenants { * 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. + * @param {string | TenantBase | (string | TenantBase)[]} tenant The tenant to activate. * @returns {Promise} The activated tenant as a list of Tenant. */ - activate: (tenant: string | TenantBase) => Promise; + activate: (tenant: string | TenantBase | (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. + * @param {string | TenantBase | (string | TenantBase)[]} tenant The tenant to deactivate. * @returns {Promise} The deactivated tenant as a list of Tenant */ - deactivate: (tenant: string | TenantBase) => Promise; + deactivate: (tenant: string | TenantBase | (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. + * @param {string | TenantBase | (string | TenantBase)[]} tenant The tenant to offload. * @returns {Promise} The offloaded tenant as a list of Tenant */ - offload: (tenant: string | TenantBase) => Promise; + offload: (tenant: string | TenantBase | (string | TenantBase)[]) => Promise; } diff --git a/src/collections/tenants/integration.test.ts b/src/collections/tenants/integration.test.ts index 1455980a..fe12c2d2 100644 --- a/src/collections/tenants/integration.test.ts +++ b/src/collections/tenants/integration.test.ts @@ -187,7 +187,7 @@ describe('Testing of the collection.tenants methods', () => { .then((tenants) => expect(tenants[tenantName].activityStatus).toBe('INACTIVE')); await collection.tenants - .activate(tenantName) + .activate([tenantName]) .then(() => collection.tenants.get()) .then((tenants) => expect(tenants[tenantName].activityStatus).toBe('ACTIVE')); }); From d76375130834c85fcf105c3b2b404327bab1a425 Mon Sep 17 00:00:00 2001 From: Tommy Smith Date: Fri, 1 Aug 2025 15:07:53 +0100 Subject: [PATCH 3/3] Update docstrings for methods --- src/collections/tenants/index.ts | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/collections/tenants/index.ts b/src/collections/tenants/index.ts index a63cb5f4..12dd4ea0 100644 --- a/src/collections/tenants/index.ts +++ b/src/collections/tenants/index.ts @@ -191,31 +191,31 @@ export interface Tenants { * For details on the new activity statuses, see the docstring for the `Tenants` interface type. * * @param {TenantInput | TenantInput[]} tenants The tenant or tenants to update. - * @returns {Promise} The updated tenant(s) as a list of Tenant. + * @returns {Promise} The updated tenants as a list of Tenant. */ update: (tenants: TenantBC | TenantUpdate | (TenantBC | TenantUpdate)[]) => Promise; /** - * Activate the specified tenant for a collection in Weaviate. + * Activate the specified tenants for a collection in Weaviate. * The collection must have been created with multi-tenancy enabled. * - * @param {string | TenantBase | (string | TenantBase)[]} tenant The tenant to activate. - * @returns {Promise} The activated tenant as a list of Tenant. + * @param {string | TenantBase | (string | TenantBase)[]} tenant The tenants to activate. + * @returns {Promise} The list of Tenants that have been activated. */ - activate: (tenant: string | TenantBase | (string | TenantBase)[]) => Promise; + activate: (tenants: string | TenantBase | (string | TenantBase)[]) => Promise; /** - * Deactivate the specified tenant for a collection in Weaviate. + * Deactivate the specified tenants for a collection in Weaviate. * The collection must have been created with multi-tenancy enabled. * - * @param {string | TenantBase | (string | TenantBase)[]} tenant The tenant to deactivate. - * @returns {Promise} The deactivated tenant as a list of Tenant + * @param {string | TenantBase | (string | TenantBase)[]} tenants The tenants to deactivate. + * @returns {Promise} The list of Tenants that have been deactivated. */ - deactivate: (tenant: string | TenantBase | (string | TenantBase)[]) => Promise; + deactivate: (tenants: string | TenantBase | (string | TenantBase)[]) => Promise; /** - * Offload the specified tenant for a collection in Weaviate. + * Offload the specified tenants for a collection in Weaviate. * The collection must have been created with multi-tenancy enabled. * - * @param {string | TenantBase | (string | TenantBase)[]} tenant The tenant to offload. - * @returns {Promise} The offloaded tenant as a list of Tenant + * @param {string | TenantBase | (string | TenantBase)[]} tenants The tenants to offload. + * @returns {Promise} The list of Tenants that have been offloaded. */ - offload: (tenant: string | TenantBase | (string | TenantBase)[]) => Promise; + offload: (tenants: string | TenantBase | (string | TenantBase)[]) => Promise; }