1- import { NodesStatusGetter } from '../../cluster/index.js' ;
2- import Connection from '../../connection/index.js' ;
3- import { BatchStats , NodeShardStatus , NodeStats } from '../../openapi/types.js' ;
1+ import { IConnection } from '../../connection/index.js' ;
2+ import {
3+ BatchStats ,
4+ NodeShardStatus ,
5+ NodeStats ,
6+ NodesStatusResponse ,
7+ WeaviateReplicateRequest ,
8+ WeaviateReplicateResponse ,
9+ WeaviateReplicationResponse ,
10+ WeaviateReplicationType ,
11+ WeaviateShardingState ,
12+ } from '../../openapi/types.js' ;
13+ import { DeepRequired } from '../../utils/types.js' ;
414
515export type Output = 'minimal' | 'verbose' | undefined ;
616
@@ -11,6 +21,44 @@ export type NodesOptions<O extends Output> = {
1121 output : O ;
1222} ;
1323
24+ export type QueryShardingStateOptions = {
25+ /** The name of the shard to query. If not provided, all shards will be queried. */
26+ shard ?: string ;
27+ } ;
28+
29+ export type ReplicateArgs = {
30+ /** The name of the collection in which to replicate a shard. */
31+ collection : string ;
32+ /** The name of the shard to replicate. */
33+ shard : string ;
34+ /** The name of the node from which to replicate the shard. */
35+ sourceNode : string ;
36+ /** The name of the node to which to replicate the shard. */
37+ targetNode : string ;
38+ /** The type of replication to perform. */
39+ replicationType : WeaviateReplicationType ;
40+ } ;
41+
42+ export type ShardingState = DeepRequired < WeaviateShardingState > ;
43+
44+ export type ReplicationOperation = DeepRequired < WeaviateReplicationResponse > ;
45+
46+ export type QueryReplicationOpsOptions = {
47+ /** The name of the collection to query. */
48+ collection ?: string ;
49+ /** The name of the shard to query. */
50+ shard ?: string ;
51+ /** The target node of the op to query. */
52+ targetNode ?: string ;
53+ /** Whether to include the status history in the response. */
54+ includeHistory ?: boolean ;
55+ } ;
56+
57+ export type GetReplicationOpOptions = {
58+ /** Whether to include the status history in the response. Defaults to false. */
59+ includeHistory ?: boolean ;
60+ } ;
61+
1462export type Node < O extends Output > = {
1563 name : string ;
1664 status : 'HEALTHY' | 'UNHEALTHY' | 'UNAVAILABLE' ;
@@ -21,14 +69,65 @@ export type Node<O extends Output> = {
2169 shards : O extends 'minimal' | undefined ? null : Required < NodeShardStatus > [ ] ;
2270} ;
2371
24- const cluster = ( connection : Connection ) => {
72+ const cluster = ( connection : IConnection ) => {
2573 return {
26- nodes : < O extends Output = undefined > ( opts ?: NodesOptions < O > ) : Promise < Node < O > [ ] > => {
27- let builder = new NodesStatusGetter ( connection ) . withOutput ( opts ?. output ? opts . output : 'minimal' ) ;
74+ nodes : < O extends Output = undefined > ( opts ?: NodesOptions < O > ) => {
75+ const params = new URLSearchParams ( ) ;
76+ let path = '/nodes' ;
2877 if ( opts ?. collection ) {
29- builder = builder . withClassName ( opts . collection ) ;
78+ path = path . concat ( `/${ opts . collection } ` ) ;
79+ }
80+ params . append ( 'output' , opts ?. output ? opts . output : 'minimal' ) ;
81+ return connection
82+ . get < NodesStatusResponse > ( `${ path } ?${ params . toString ( ) } ` )
83+ . then ( ( res ) => res . nodes as Node < O > [ ] ) ;
84+ } ,
85+ queryShardingState : ( collection : string , opts ?: QueryShardingStateOptions ) => {
86+ const params = new URLSearchParams ( ) ;
87+ params . append ( 'collection' , collection ) ;
88+ if ( opts ?. shard ) {
89+ params . append ( 'shard' , opts . shard ) ;
3090 }
31- return builder . do ( ) . then ( ( res ) => res . nodes ) as Promise < Node < O > [ ] > ;
91+ return connection
92+ . get < ShardingState | undefined > ( `/replication/sharding-state?${ params . toString ( ) } ` )
93+ . then ( ( res ) => res as ShardingState ) ;
94+ } ,
95+ replicate : ( args : ReplicateArgs ) : Promise < string > =>
96+ connection
97+ . postReturn < WeaviateReplicateRequest , WeaviateReplicateResponse > (
98+ `/replication/replicate` ,
99+ ( ( { replicationType, ...rest } ) => ( { type : replicationType , ...rest } ) ) ( args )
100+ )
101+ . then ( ( res ) => res . id ) ,
102+ replications : {
103+ cancel : ( id : string ) => connection . postEmpty ( `/replication/replicate/${ id } /cancel` , { } ) ,
104+ delete : ( id : string ) => connection . delete ( `/replication/replicate/${ id } ` , { } , false ) ,
105+ deleteAll : ( ) => connection . delete ( `/replication/replicate` , { } , false ) ,
106+ get : ( id : string , opts ?: GetReplicationOpOptions ) : Promise < ReplicationOperation | null > =>
107+ connection
108+ . get < ReplicationOperation | undefined > (
109+ `/replication/replicate/${ id } ?includeHistory=${
110+ opts ?. includeHistory ? opts ?. includeHistory : 'false'
111+ } `
112+ )
113+ . then ( ( res ) => ( res ? ( res as ReplicationOperation ) : null ) ) ,
114+ query : ( opts ?: QueryReplicationOpsOptions ) : Promise < ReplicationOperation [ ] > => {
115+ const { collection, shard, targetNode, includeHistory } = opts || { } ;
116+ const params = new URLSearchParams ( ) ;
117+ if ( collection ) {
118+ params . append ( 'collection' , collection ) ;
119+ }
120+ if ( shard ) {
121+ params . append ( 'shard' , shard ) ;
122+ }
123+ if ( targetNode ) {
124+ params . append ( 'targetNode' , targetNode ) ;
125+ }
126+ if ( includeHistory ) {
127+ params . append ( 'includeHistory' , includeHistory . toString ( ) ) ;
128+ }
129+ return connection . get < ReplicationOperation [ ] > ( `/replication/replicate?${ params . toString ( ) } ` ) ;
130+ } ,
32131 } ,
33132 } ;
34133} ;
@@ -43,4 +142,61 @@ export interface Cluster {
43142 * @returns {Promise<Node<O>[]> } The status of all nodes in the cluster.
44143 */
45144 nodes : < O extends Output = undefined > ( opts ?: NodesOptions < O > ) => Promise < Node < O > [ ] > ;
145+ /**
146+ * Query the sharding state of a specific collection.
147+ *
148+ * @param {string } collection The name of the collection to query.
149+ * @param {QueryShardingStateOptions } [opts] The options for the request.
150+ * @returns {Promise<ShardingState> } The sharding state of the collection.
151+ */
152+ queryShardingState : ( collection : string , opts ?: QueryShardingStateOptions ) => Promise < ShardingState > ;
153+ /**
154+ * Replicate a shard from one node to another.
155+ *
156+ * @param {ReplicateArgs } args The arguments for the replication request.
157+ * @returns {Promise<string> } The ID of the replication request.
158+ */
159+ replicate : ( args : ReplicateArgs ) => Promise < string > ;
160+ /**
161+ * Access replication operations.
162+ */
163+ replications : Replications ;
164+ }
165+
166+ export interface Replications {
167+ /**
168+ * Cancel a replication operation.
169+ *
170+ * @param {string } id The ID of the replication operation to cancel.
171+ * @returns {Promise<void> } A promise that resolves when the operation is cancelled.
172+ */
173+ cancel : ( id : string ) => Promise < void > ;
174+ /**
175+ * Delete a replication operation.
176+ *
177+ * @param {string } id The ID of the replication operation to delete.
178+ * @returns {Promise<void> } A promise that resolves when the operation is deleted.
179+ */
180+ delete : ( id : string ) => Promise < void > ;
181+ /**
182+ * Delete all replication operations.
183+ *
184+ * @returns {Promise<void> } A promise that resolves when all operations are deleted.
185+ */
186+ deleteAll : ( ) => Promise < void > ;
187+ /**
188+ * Get a specific replication operation by ID.
189+ *
190+ * @param {string } id The ID of the replication operation to get.
191+ * @param {boolean } [opts.includeHistory=false] Whether to include the status history in the response.
192+ * @returns {Promise<ReplicationOperation | null> } The replication operation or null if not found.
193+ */
194+ get : ( id : string , opts ?: { includeHistory ?: boolean } ) => Promise < ReplicationOperation | null > ;
195+ /**
196+ * Query all replication operations with optional filters.
197+ *
198+ * @param {QueryReplicationOpsOptions } [opts] Optional parameters for filtering the query.
199+ * @returns {Promise<ReplicationOperation[]> } A list of replication operations matching the query.
200+ */
201+ query : ( opts ?: QueryReplicationOpsOptions ) => Promise < ReplicationOperation [ ] > ;
46202}
0 commit comments