Skip to content
Merged
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
77 changes: 63 additions & 14 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 @@ -166,7 +191,31 @@ export interface Tenants {
* For details on the new activity statuses, see the docstring for the `Tenants` interface type.
*
* @param {TenantInput | TenantInput[]} tenants The tenant or tenants to update.
* @returns {Promise<Tenant[]>} The updated tenant(s) as a list of Tenant.
* @returns {Promise<Tenant[]>} The updated tenants as a list of Tenant.
*/
update: (tenants: TenantBC | TenantUpdate | (TenantBC | TenantUpdate)[]) => Promise<Tenant[]>;
/**
* Activate the specified tenants for a collection in Weaviate.
* The collection must have been created with multi-tenancy enabled.
*
* @param {string | TenantBase | (string | TenantBase)[]} tenant The tenants to activate.
* @returns {Promise<Tenant[]>} The list of Tenants that have been activated.
*/
activate: (tenants: string | TenantBase | (string | TenantBase)[]) => Promise<Tenant[]>;
/**
* Deactivate the specified tenants for a collection in Weaviate.
* The collection must have been created with multi-tenancy enabled.
*
* @param {string | TenantBase | (string | TenantBase)[]} tenants The tenants to deactivate.
* @returns {Promise<Tenant[]>} The list of Tenants that have been deactivated.
*/
deactivate: (tenants: string | TenantBase | (string | TenantBase)[]) => Promise<Tenant[]>;
/**
* Offload the specified tenants for a collection in Weaviate.
* The collection must have been created with multi-tenancy enabled.
*
* @param {string | TenantBase | (string | TenantBase)[]} tenants The tenants to offload.
* @returns {Promise<Tenant[]>} The list of Tenants that have been offloaded.
*/
offload: (tenants: 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'));
});
});
Loading