Skip to content

Commit c360306

Browse files
authored
Add .activate, .deactivate, .offload helpers to collection.tenants (#311)
* Add `.activate`, `.deactivate`, `.offload` helpers to `collection.tenants` * Allow lists of tenants when using helper methods * Update docstrings for methods
1 parent e915157 commit c360306

File tree

2 files changed

+76
-14
lines changed

2 files changed

+76
-14
lines changed

src/collections/tenants/index.ts

Lines changed: 63 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,27 @@ 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 {
45-
create: (tenants: TenantBC | TenantCreate | (TenantBC | TenantCreate)[]) =>
55+
create: (tenants) =>
4656
new TenantsCreator(connection, collection, parseValueOrValueArray(tenants).map(Serialize.tenantCreate))
4757
.do()
4858
.then((res) => res.map(parseTenantREST)),
4959
get: async function () {
5060
const check = await dbVersionSupport.supportsTenantsGetGRPCMethod();
5161
return check.supports ? getGRPC() : getREST();
5262
},
53-
getByNames: <T extends TenantBase>(tenants: (string | T)[]) => getGRPC(tenants.map(parseStringOrTenant)),
54-
getByName: async <T extends TenantBase>(tenant: string | T) => {
63+
getByNames: (tenants) => getGRPC(tenants.map(parseStringOrTenant)),
64+
getByName: async (tenant) => {
5565
const tenantName = parseStringOrTenant(tenant);
5666
if (await dbVersionSupport.supportsTenantGetRESTMethod().then((check) => !check.supports)) {
5767
return getGRPC([tenantName]).then((tenants) => tenants[tenantName] ?? null);
@@ -66,21 +76,36 @@ const tenants = (
6676
throw err;
6777
});
6878
},
69-
remove: <T extends TenantBase>(tenants: string | T | (string | T)[]) =>
79+
remove: (tenants) =>
7080
new TenantsDeleter(
7181
connection,
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) => {
87+
return update(
88+
parseValueOrValueArray(tenant).map((tenant) => ({
89+
name: parseStringOrTenant(tenant),
90+
activityStatus: 'ACTIVE',
91+
}))
92+
);
93+
},
94+
deactivate: (tenant) => {
95+
return update(
96+
parseValueOrValueArray(tenant).map((tenant) => ({
97+
name: parseStringOrTenant(tenant),
98+
activityStatus: 'INACTIVE',
99+
}))
100+
);
101+
},
102+
offload: (tenant) => {
103+
return update(
104+
parseValueOrValueArray(tenant).map((tenant) => ({
105+
name: parseStringOrTenant(tenant),
106+
activityStatus: 'OFFLOADED',
107+
}))
108+
);
84109
},
85110
};
86111
};
@@ -166,7 +191,31 @@ export interface Tenants {
166191
* For details on the new activity statuses, see the docstring for the `Tenants` interface type.
167192
*
168193
* @param {TenantInput | TenantInput[]} tenants The tenant or tenants to update.
169-
* @returns {Promise<Tenant[]>} The updated tenant(s) as a list of Tenant.
194+
* @returns {Promise<Tenant[]>} The updated tenants as a list of Tenant.
170195
*/
171196
update: (tenants: TenantBC | TenantUpdate | (TenantBC | TenantUpdate)[]) => Promise<Tenant[]>;
197+
/**
198+
* Activate the specified tenants for a collection in Weaviate.
199+
* The collection must have been created with multi-tenancy enabled.
200+
*
201+
* @param {string | TenantBase | (string | TenantBase)[]} tenant The tenants to activate.
202+
* @returns {Promise<Tenant[]>} The list of Tenants that have been activated.
203+
*/
204+
activate: (tenants: string | TenantBase | (string | TenantBase)[]) => Promise<Tenant[]>;
205+
/**
206+
* Deactivate the specified tenants for a collection in Weaviate.
207+
* The collection must have been created with multi-tenancy enabled.
208+
*
209+
* @param {string | TenantBase | (string | TenantBase)[]} tenants The tenants to deactivate.
210+
* @returns {Promise<Tenant[]>} The list of Tenants that have been deactivated.
211+
*/
212+
deactivate: (tenants: string | TenantBase | (string | TenantBase)[]) => Promise<Tenant[]>;
213+
/**
214+
* Offload the specified tenants for a collection in Weaviate.
215+
* The collection must have been created with multi-tenancy enabled.
216+
*
217+
* @param {string | TenantBase | (string | TenantBase)[]} tenants The tenants to offload.
218+
* @returns {Promise<Tenant[]>} The list of Tenants that have been offloaded.
219+
*/
220+
offload: (tenants: string | TenantBase | (string | TenantBase)[]) => Promise<Tenant[]>;
172221
}

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)