1- import { Readable , Writable } from 'node:stream' ;
2-
31import { Logger } from 'winston' ;
42
3+ import { SyncStream , ReadStream , WriteStream } from '../grpc/plugin.js' ;
4+ import { Table } from '../schema/table.js' ;
5+
56export type BackendOptions = {
67 tableName : string ;
78 connection : string ;
@@ -19,7 +20,7 @@ export type SyncOptions = {
1920 skipDependentTables : boolean ;
2021 deterministicCQId : boolean ;
2122 backendOptions : BackendOptions ;
22- stream : Writable ;
23+ stream : SyncStream ;
2324} ;
2425
2526export type NewClientOptions = {
@@ -29,68 +30,61 @@ export type NewClientOptions = {
2930export type NewClientFunction = ( logger : Logger , spec : string , options : NewClientOptions ) => Promise < Client > ;
3031
3132export interface SourceClient {
32- close : ( ) => Promise < void > ;
33- tables : ( options : TableOptions ) => Promise < string [ ] > ;
33+ tables : ( options : TableOptions ) => Promise < Table [ ] > ;
3434 sync : ( options : SyncOptions ) => void ;
3535}
3636
3737export interface DestinationClient {
38- close : ( ) => Promise < void > ;
39- read : ( stream : Writable ) => void ;
40- write : ( stream : Readable ) => void ;
38+ read : ( stream : ReadStream ) => void ;
39+ write : ( stream : WriteStream ) => void ;
4140}
4241
43- export interface Client extends SourceClient , DestinationClient { }
42+ export interface Client extends SourceClient , DestinationClient {
43+ init : ( spec : string , options : NewClientOptions ) => Promise < void > ;
44+ close : ( ) => Promise < void > ;
45+ }
4446
45- export interface Plugin {
47+ export interface Plugin extends Client {
48+ setLogger : ( logger : Logger ) => void ;
4649 name : ( ) => string ;
4750 version : ( ) => string ;
48- write : ( stream : Readable ) => void ;
49- read : ( stream : Writable ) => void ;
50- setLogger : ( logger : Logger ) => void ;
51- sync : ( options : SyncOptions ) => void ;
52- tables : ( options : TableOptions ) => Promise < string [ ] > ;
53- init : ( spec : string , options : NewClientOptions ) => Promise < void > ;
54- close : ( ) => Promise < void > ;
5551}
5652
57- export const newUnimplementedSourceClient = ( ) : SourceClient => {
53+ export const newUnimplementedSource = ( ) : SourceClient => {
5854 return {
59- close : ( ) => Promise . reject ( new Error ( 'unimplemented' ) ) ,
6055 tables : ( ) => Promise . reject ( new Error ( 'unimplemented' ) ) ,
6156 sync : ( ) => Promise . reject ( new Error ( 'unimplemented' ) ) ,
6257 } ;
6358} ;
6459
65- export const newUnimplementedDestinationClient = ( ) : DestinationClient => {
60+ export const newUnimplementedDestination = ( ) : DestinationClient => {
6661 return {
67- close : ( ) => Promise . reject ( new Error ( 'unimplemented' ) ) ,
6862 read : ( ) => Promise . reject ( new Error ( 'unimplemented' ) ) ,
6963 write : ( ) => Promise . reject ( new Error ( 'unimplemented' ) ) ,
7064 } ;
7165} ;
7266
73- export const newUnimplementedClient : NewClientFunction = ( logger : Logger , spec : string , options : NewClientOptions ) => {
74- return Promise . resolve ( {
75- ...newUnimplementedSourceClient ( ) ,
76- ...newUnimplementedDestinationClient ( ) ,
77- } ) ;
78- } ;
79-
8067export const newPlugin = ( name : string , version : string , newClient : NewClientFunction ) : Plugin => {
8168 const plugin = {
8269 client : undefined as Client | undefined ,
8370 logger : undefined as Logger | undefined ,
8471 name : ( ) => name ,
8572 version : ( ) => version ,
86- write : ( stream : Readable ) => plugin . client ?. write ( stream ) ?? new Error ( 'client not initialized' ) ,
87- read : ( stream : Writable ) => plugin . client ?. read ( stream ) ?? new Error ( 'client not initialized' ) ,
73+ write : ( stream : WriteStream ) => {
74+ return plugin . client ?. write ( stream ) ?? new Error ( 'client not initialized' ) ;
75+ } ,
76+ read : ( stream : ReadStream ) => {
77+ return plugin . client ?. read ( stream ) ?? new Error ( 'client not initialized' ) ;
78+ } ,
8879 setLogger : ( logger : Logger ) => {
8980 plugin . logger = logger ;
9081 } ,
91- sync : ( options : SyncOptions ) => plugin . client ?. sync ( options ) ?? new Error ( 'client not initialized' ) ,
92- tables : ( options : TableOptions ) =>
93- plugin . client ?. tables ( options ) ?? Promise . reject ( new Error ( 'client not initialized' ) ) ,
82+ sync : ( options : SyncOptions ) => {
83+ return plugin . client ?. sync ( options ) ?? new Error ( 'client not initialized' ) ;
84+ } ,
85+ tables : ( options : TableOptions ) => {
86+ return plugin . client ?. tables ( options ) ?? Promise . reject ( new Error ( 'client not initialized' ) ) ;
87+ } ,
9488 init : async ( spec : string , options : NewClientOptions ) => {
9589 plugin . client = await newClient ( plugin . logger ! , spec , options ) ;
9690 } ,
0 commit comments