Skip to content

Add .activate, .deactivate, .offload helpers to collection.tenants #311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 52 additions & 9 deletions src/collections/tenants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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',
});
},
};
};
Expand Down Expand Up @@ -169,4 +188,28 @@ export interface Tenants {
* @returns {Promise<Tenant[]>} The updated tenant(s) as a list of Tenant.
*/
update: (tenants: TenantBC | TenantUpdate | (TenantBC | TenantUpdate)[]) => Promise<Tenant[]>;
/**
* 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<Tenant[]>} The activated tenant as a list of Tenant.
*/
activate: (tenant: string | TenantBase) => Promise<Tenant[]>;
/**
* 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<Tenant[]>} The deactivated tenant as a list of Tenant
*/
deactivate: (tenant: string | TenantBase) => Promise<Tenant[]>;
/**
* 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<Tenant[]>} The offloaded tenant as a list of Tenant
*/
offload: (tenant: string | TenantBase) => Promise<Tenant[]>;
}
13 changes: 13 additions & 0 deletions src/collections/tenants/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
});
});