Skip to content

Commit ce44c5e

Browse files
committed
Add .activate, .deactivate, .offload helpers to collection.tenants
1 parent bba1c5e commit ce44c5e

File tree

2 files changed

+65
-9
lines changed

2 files changed

+65
-9
lines changed

src/collections/tenants/index.ts

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ const tenants = (
4141
});
4242
return result;
4343
});
44+
const update = async (tenants: TenantBC | TenantUpdate | (TenantBC | TenantUpdate)[]) => {
45+
const out: Tenant[] = [];
46+
for await (const res of Serialize.tenants(parseValueOrValueArray(tenants), Serialize.tenantUpdate).map(
47+
(tenants) =>
48+
new TenantsUpdater(connection, collection, tenants).do().then((res) => res.map(parseTenantREST))
49+
)) {
50+
out.push(...res);
51+
}
52+
return out;
53+
};
4454
return {
4555
create: (tenants: TenantBC | TenantCreate | (TenantBC | TenantCreate)[]) =>
4656
new TenantsCreator(connection, collection, parseValueOrValueArray(tenants).map(Serialize.tenantCreate))
@@ -72,15 +82,24 @@ const tenants = (
7282
collection,
7383
parseValueOrValueArray(tenants).map(parseStringOrTenant)
7484
).do(),
75-
update: async (tenants: TenantBC | TenantUpdate | (TenantBC | TenantUpdate)[]) => {
76-
const out: Tenant[] = [];
77-
for await (const res of Serialize.tenants(parseValueOrValueArray(tenants), Serialize.tenantUpdate).map(
78-
(tenants) =>
79-
new TenantsUpdater(connection, collection, tenants).do().then((res) => res.map(parseTenantREST))
80-
)) {
81-
out.push(...res);
82-
}
83-
return out;
85+
update,
86+
activate: (tenant: string | TenantBase) => {
87+
return update({
88+
name: parseStringOrTenant(tenant),
89+
activityStatus: 'ACTIVE',
90+
});
91+
},
92+
deactivate: (tenant: string | TenantBase) => {
93+
return update({
94+
name: parseStringOrTenant(tenant),
95+
activityStatus: 'INACTIVE',
96+
});
97+
},
98+
offload: (tenant: string | TenantBase) => {
99+
return update({
100+
name: parseStringOrTenant(tenant),
101+
activityStatus: 'OFFLOADED',
102+
});
84103
},
85104
};
86105
};
@@ -169,4 +188,28 @@ export interface Tenants {
169188
* @returns {Promise<Tenant[]>} The updated tenant(s) as a list of Tenant.
170189
*/
171190
update: (tenants: TenantBC | TenantUpdate | (TenantBC | TenantUpdate)[]) => Promise<Tenant[]>;
191+
/**
192+
* Activate the specified tenant for a collection in Weaviate.
193+
* The collection must have been created with multi-tenancy enabled.
194+
*
195+
* @param {TenantBase | string} tenant The tenant to activate.
196+
* @returns {Promise<Tenant[]>} The activated tenant as a list of Tenant.
197+
*/
198+
activate: (tenant: string | TenantBase) => Promise<Tenant[]>;
199+
/**
200+
* Deactivate the specified tenant for a collection in Weaviate.
201+
* The collection must have been created with multi-tenancy enabled.
202+
*
203+
* @param {TenantBase | string} tenant The tenant to deactivate.
204+
* @returns {Promise<Tenant[]>} The deactivated tenant as a list of Tenant
205+
*/
206+
deactivate: (tenant: string | TenantBase) => Promise<Tenant[]>;
207+
/**
208+
* Offload the specified tenant for a collection in Weaviate.
209+
* The collection must have been created with multi-tenancy enabled.
210+
*
211+
* @param {TenantBase | string} tenant The tenant to offload.
212+
* @returns {Promise<Tenant[]>} The offloaded tenant as a list of Tenant
213+
*/
214+
offload: (tenant: string | TenantBase) => Promise<Tenant[]>;
172215
}

src/collections/tenants/integration.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,17 @@ describe('Testing of the collection.tenants methods', () => {
178178
expect(Object.entries(updated).length).toBe(howMany);
179179
expect(Object.values(updated).every((tenant) => tenant.activityStatus === 'INACTIVE')).toBe(true);
180180
});
181+
182+
it('should be able to deactivate and activate a tenant using helper methods', async () => {
183+
const tenantName = 'hot';
184+
await collection.tenants
185+
.deactivate(tenantName)
186+
.then(() => collection.tenants.get())
187+
.then((tenants) => expect(tenants[tenantName].activityStatus).toBe('INACTIVE'));
188+
189+
await collection.tenants
190+
.activate(tenantName)
191+
.then(() => collection.tenants.get())
192+
.then((tenants) => expect(tenants[tenantName].activityStatus).toBe('ACTIVE'));
193+
});
181194
});

0 commit comments

Comments
 (0)