diff --git a/src/collections/tenants/index.ts b/src/collections/tenants/index.ts index 320f166d..12dd4ea0 100644 --- a/src/collections/tenants/index.ts +++ b/src/collections/tenants/index.ts @@ -41,8 +41,18 @@ 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)[]) => + create: (tenants) => new TenantsCreator(connection, collection, parseValueOrValueArray(tenants).map(Serialize.tenantCreate)) .do() .then((res) => res.map(parseTenantREST)), @@ -50,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); @@ -66,21 +76,36 @@ const tenants = ( throw err; }); }, - remove: (tenants: string | T | (string | T)[]) => + remove: (tenants) => new TenantsDeleter( connection, 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) => { + return update( + parseValueOrValueArray(tenant).map((tenant) => ({ + name: parseStringOrTenant(tenant), + activityStatus: 'ACTIVE', + })) + ); + }, + deactivate: (tenant) => { + return update( + parseValueOrValueArray(tenant).map((tenant) => ({ + name: parseStringOrTenant(tenant), + activityStatus: 'INACTIVE', + })) + ); + }, + offload: (tenant) => { + return update( + parseValueOrValueArray(tenant).map((tenant) => ({ + name: parseStringOrTenant(tenant), + activityStatus: 'OFFLOADED', + })) + ); }, }; }; @@ -166,7 +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 tenants for a collection in Weaviate. + * The collection must have been created with multi-tenancy enabled. + * + * @param {string | TenantBase | (string | TenantBase)[]} tenant The tenants to activate. + * @returns {Promise} The list of Tenants that have been activated. + */ + activate: (tenants: string | TenantBase | (string | TenantBase)[]) => Promise; + /** + * Deactivate the specified tenants for a collection in Weaviate. + * The collection must have been created with multi-tenancy enabled. + * + * @param {string | TenantBase | (string | TenantBase)[]} tenants The tenants to deactivate. + * @returns {Promise} The list of Tenants that have been deactivated. + */ + deactivate: (tenants: string | TenantBase | (string | TenantBase)[]) => Promise; + /** + * Offload the specified tenants for a collection in Weaviate. + * The collection must have been created with multi-tenancy enabled. + * + * @param {string | TenantBase | (string | TenantBase)[]} tenants The tenants to offload. + * @returns {Promise} The list of Tenants that have been offloaded. + */ + offload: (tenants: string | TenantBase | (string | TenantBase)[]) => Promise; } diff --git a/src/collections/tenants/integration.test.ts b/src/collections/tenants/integration.test.ts index b574759a..fe12c2d2 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')); + }); });