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',
});
Comment on lines +86 to +102
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it'll make sense to accept (string | TenantBC)[] to match update signature?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By having it be TenantBase, any of the TenantX types are acceptable in this method since the activityStatus is ignored by the methods; all that is relevant is the name which is the only field of TenantBase

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should've made it clearer in the original comment: I think these helper methods can accept an array of tenant names, so as to make it possible to, e.g., activate multiple tenants at once.

Just like it's possible to batch-update them.

},
};
};
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[]>;
Comment on lines +196 to +198
Copy link
Collaborator

@bevzzz bevzzz Jul 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: the activated tenant as a list of Tenant looks a tad contrived, since we already know there will only be 1 result in that list.

I'd have these methods accept a (string | TenantBase)[] as per the comment above (or below)

/**
* 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'));
});
});