Skip to content

Commit b94d15d

Browse files
committed
Use new OFFLOAD nomenclature and updated OpenAPI schema
1 parent 6a65486 commit b94d15d

File tree

9 files changed

+257
-95
lines changed

9 files changed

+257
-95
lines changed

src/collections/collection/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import generate, { Generate } from '../generate/index.js';
1313
import { Iterator } from '../iterator/index.js';
1414
import query, { Query } from '../query/index.js';
1515
import sort, { Sort } from '../sort/index.js';
16-
import tenants, { TenantInput, Tenants } from '../tenants/index.js';
16+
import tenants, { TenantBase, Tenants } from '../tenants/index.js';
1717
import { QueryMetadata, QueryProperty, QueryReference } from '../types/index.js';
1818

1919
export interface Collection<T = undefined, N = string> {
@@ -78,10 +78,11 @@ export interface Collection<T = undefined, N = string> {
7878
*
7979
* This method does not send a request to Weaviate. It only returns a new collection object that is specific to the tenant you specify.
8080
*
81-
* @param {string | TenantInput} tenant The tenant name or tenant object to use.
81+
* @typedef {TenantBase} TT A type that extends TenantBase.
82+
* @param {string | TT} tenant The tenant name or tenant object to use.
8283
* @returns {Collection<T, N>} A new collection object specific to the tenant you specified.
8384
*/
84-
withTenant: (tenant: string | TenantInput) => Collection<T, N>;
85+
withTenant: <TT extends TenantBase>(tenant: string | TT) => Collection<T, N>;
8586
}
8687

8788
export type IteratorOptions<T> = {
@@ -136,7 +137,7 @@ const collection = <T, N>(
136137
),
137138
withConsistency: (consistencyLevel: ConsistencyLevel) =>
138139
collection<T, N>(connection, capitalizedName, dbVersionSupport, consistencyLevel, tenant),
139-
withTenant: (tenant: string | TenantInput) =>
140+
withTenant: <TT extends TenantBase>(tenant: string | TT) =>
140141
collection<T, N>(
141142
connection,
142143
capitalizedName,

src/collections/deserialize/index.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { WeaviateDeserializationError } from '../../errors.js';
2+
import { Tenant as TenantREST } from '../../openapi/types.js';
23
import { BatchObject as BatchObjectGRPC, BatchObjectsReply } from '../../proto/v1/batch.js';
34
import { BatchDeleteReply } from '../../proto/v1/batch_delete.js';
45
import { ListValue, Properties as PropertiesGrpc, Value } from '../../proto/v1/properties.js';
56
import { MetadataResult, PropertiesResult, SearchReply } from '../../proto/v1/search_get.js';
7+
import { TenantActivityStatus, TenantsGetReply } from '../../proto/v1/tenants.js';
68
import { DbVersionSupport } from '../../utils/dbVersion.js';
79
import { referenceFromObjects } from '../references/utils.js';
10+
import { Tenant } from '../tenants/index.js';
811
import {
912
BatchObject,
1013
BatchObjectsReturn,
@@ -298,4 +301,49 @@ export class Deserialize {
298301
: (undefined as any),
299302
};
300303
}
304+
305+
private static activityStatusGRPC(status: TenantActivityStatus): Tenant['activityStatus'] {
306+
switch (status) {
307+
case TenantActivityStatus.TENANT_ACTIVITY_STATUS_COLD:
308+
return 'COLD';
309+
case TenantActivityStatus.TENANT_ACTIVITY_STATUS_HOT:
310+
return 'HOT';
311+
case TenantActivityStatus.TENANT_ACTIVITY_STATUS_FROZEN:
312+
return 'OFFLOADED';
313+
case TenantActivityStatus.TENANT_ACTIVITY_STATUS_FREEZING:
314+
return 'OFFLOADING';
315+
case TenantActivityStatus.TENANT_ACTIVITY_STATUS_UNFREEZING:
316+
return 'ONLOADING';
317+
default:
318+
throw new Error(`Unsupported tenant activity status: ${status}`);
319+
}
320+
}
321+
322+
public static activityStatusREST(status: TenantREST['activityStatus']): Tenant['activityStatus'] {
323+
switch (status) {
324+
case 'COLD':
325+
return 'COLD';
326+
case 'HOT':
327+
return 'HOT';
328+
case 'FROZEN':
329+
return 'OFFLOADED';
330+
case 'FREEZING':
331+
return 'OFFLOADING';
332+
case 'UNFREEZING':
333+
return 'ONLOADING';
334+
default:
335+
throw new Error(`Unsupported tenant activity status: ${status}`);
336+
}
337+
}
338+
339+
public static tenantsGet(reply: TenantsGetReply) {
340+
const tenants: Record<string, Tenant> = {};
341+
reply.tenants.forEach((t) => {
342+
tenants[t.name] = {
343+
name: t.name,
344+
activityStatus: Deserialize.activityStatusGRPC(t.activityStatus),
345+
};
346+
});
347+
return tenants;
348+
}
301349
}

src/collections/serialize/index.ts

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { v4 as uuidv4 } from 'uuid';
2-
import { WhereFilter } from '../../openapi/types.js';
2+
import { TenantActivityStatus, WhereFilter } from '../../openapi/types.js';
33
import {
44
BatchObject as BatchObjectGRPC,
55
BatchObject_MultiTargetRefProps,
@@ -82,6 +82,7 @@ import {
8282
import { ReferenceGuards } from '../references/classes.js';
8383
import { Beacon } from '../references/index.js';
8484
import { uuidToBeacon } from '../references/utils.js';
85+
import { Tenant, TenantCreate, TenantUpdate } from '../tenants/types.js';
8586
import {
8687
BatchObject,
8788
BatchObjects,
@@ -1135,4 +1136,73 @@ export class Serialize {
11351136
return { batch: batch, mapped: objs };
11361137
});
11371138
};
1139+
1140+
public static tenantsCreate(tenant: Tenant | TenantCreate): {
1141+
name: string;
1142+
activityStatus?: 'HOT' | 'COLD';
1143+
} {
1144+
let activityStatus: TenantActivityStatus;
1145+
switch (tenant.activityStatus) {
1146+
case 'ACTIVE':
1147+
activityStatus = 'HOT';
1148+
break;
1149+
case 'INACTIVE':
1150+
activityStatus = 'COLD';
1151+
break;
1152+
case 'HOT':
1153+
case 'COLD':
1154+
case undefined:
1155+
activityStatus = tenant.activityStatus;
1156+
break;
1157+
case 'OFFLOADED':
1158+
throw new WeaviateInvalidInputError(
1159+
'Cannot create a tenant with activity status OFFLOADED. Add objects to the tenant first and then you can update it to OFFLOADED.'
1160+
);
1161+
case 'OFFLOADING':
1162+
throw new WeaviateInvalidInputError(
1163+
'Cannot create a tenant with activity status OFFLOADING. This status is a read-only value that the server sets in the processing of making a tenant OFFLOADED.'
1164+
);
1165+
case 'ONLOADING':
1166+
throw new WeaviateInvalidInputError(
1167+
'Cannot create a tenant with activity status ONLOADING. This status is a read-only value that the server sets in the processing of making a tenant HOT.'
1168+
);
1169+
}
1170+
return {
1171+
name: tenant.name,
1172+
activityStatus,
1173+
};
1174+
}
1175+
1176+
public static tenantUpdate = (
1177+
tenant: Tenant | TenantUpdate
1178+
): { name: string; activityStatus: 'HOT' | 'COLD' | 'FROZEN' } => {
1179+
let activityStatus: TenantActivityStatus;
1180+
switch (tenant.activityStatus) {
1181+
case 'ACTIVE':
1182+
activityStatus = 'HOT';
1183+
break;
1184+
case 'INACTIVE':
1185+
activityStatus = 'COLD';
1186+
break;
1187+
case 'OFFLOADED':
1188+
activityStatus = 'FROZEN';
1189+
break;
1190+
case 'HOT':
1191+
case 'COLD':
1192+
activityStatus = tenant.activityStatus;
1193+
break;
1194+
case 'OFFLOADING':
1195+
throw new WeaviateInvalidInputError(
1196+
'Cannot create a tenant with activity status OFFLOADING. This status is a read-only value that the server sets in the processing of making a tenant OFFLOADED.'
1197+
);
1198+
case 'ONLOADING':
1199+
throw new WeaviateInvalidInputError(
1200+
'Cannot create a tenant with activity status ONLOADING. This status is a read-only value that the server sets in the processing of making a tenant HOT.'
1201+
);
1202+
}
1203+
return {
1204+
name: tenant.name,
1205+
activityStatus,
1206+
};
1207+
};
11381208
}

0 commit comments

Comments
 (0)