Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
75 changes: 62 additions & 13 deletions src/collections/tenants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,27 @@ 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)),
get: async function () {
const check = await dbVersionSupport.supportsTenantsGetGRPCMethod();
return check.supports ? getGRPC() : getREST();
},
getByNames: <T extends TenantBase>(tenants: (string | T)[]) => getGRPC(tenants.map(parseStringOrTenant)),
getByName: async <T extends TenantBase>(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);
Expand All @@ -66,21 +76,36 @@ const tenants = (
throw err;
});
},
remove: <T extends TenantBase>(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',
}))
);
},
};
};
Expand Down Expand Up @@ -169,4 +194,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 {string | TenantBase | (string | TenantBase)[]} tenant The tenant to activate.
* @returns {Promise<Tenant[]>} The activated tenant as a list of Tenant.
*/
activate: (tenant: string | TenantBase | (string | TenantBase)[]) => Promise<Tenant[]>;
/**
* Deactivate the specified tenant for a collection in Weaviate.
* The collection must have been created with multi-tenancy enabled.
*
* @param {string | TenantBase | (string | TenantBase)[]} tenant The tenant to deactivate.
* @returns {Promise<Tenant[]>} The deactivated tenant as a list of Tenant
*/
deactivate: (tenant: string | TenantBase | (string | TenantBase)[]) => Promise<Tenant[]>;
/**
* Offload the specified tenant for a collection in Weaviate.
* The collection must have been created with multi-tenancy enabled.
*
* @param {string | TenantBase | (string | TenantBase)[]} tenant The tenant to offload.
* @returns {Promise<Tenant[]>} The offloaded tenant as a list of Tenant
*/
offload: (tenant: string | TenantBase | (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'));
});
});