1
- /* TODO:
2
- 1. Move this to @topcoder-framework
3
- 2. Cleanup the exported interfaces
4
- 3. Make "Client" a constructor parameter that implements a "Client" interface
5
- 4 "ExecuteSqlQuery" should return a Promise<T> where T is the type of the result for "read" queries, but should return "number" for "write" queries indicating either
6
- a) the number of rows affected or
7
- b) the ID of the row insertede
8
- */
9
-
10
- import { ColumnType , Operator , Query , QueryRequest , Value } from "../grpc/models/rdb/relational" ;
11
-
12
- import { relationalClient } from "../grpc/client/relational" ;
13
- import { TableColumns , TableColumn } from "./TableColumn" ;
1
+ const { GRPC_RDB_SERVER_HOST , GRPC_RDB_SERVER_PORT } = process . env ;
2
+
3
+ import {
4
+ ColumnType ,
5
+ Operator ,
6
+ Query ,
7
+ QueryRequest ,
8
+ RelationalClient ,
9
+ Value ,
10
+ } from "@topcoder-framework/client-relational" ;
11
+ import { TableColumn , TableColumns } from "./TableColumn" ;
14
12
15
13
export type Schema = {
16
14
dbSchema : string ;
@@ -26,21 +24,18 @@ interface ExecuteSqlQuery {
26
24
}
27
25
28
26
type JoinAndWhereClause = JoinClause & WhereClause & ExecuteSqlQuery ;
27
+ type JoinWhereLimitAndOffset = JoinAndWhereClause & LimitClause & OffsetClause ;
29
28
30
29
export interface SelectQuery {
31
- select ( columns : TableColumn [ ] ) : JoinAndWhereClause & LimitClause & OffsetClause ;
30
+ select ( columns : TableColumn [ ] ) : JoinWhereLimitAndOffset ;
32
31
}
33
32
34
33
export interface JoinClause {
35
34
join ( ) : JoinAndWhereClause ;
36
35
}
37
36
38
37
export interface WhereClause {
39
- where ( whereCriteria : {
40
- key : string ,
41
- operator : Operator ,
42
- value : Value
43
- } ) : JoinAndWhereClause & LimitClause & OffsetClause ;
38
+ where ( whereCriteria : { key : string ; operator : Operator ; value : Value } ) : JoinWhereLimitAndOffset ;
44
39
}
45
40
46
41
export interface LimitClause {
@@ -56,26 +51,37 @@ export interface InsertQuery<CreateInput> {
56
51
}
57
52
58
53
export interface UpdateQuery < UpdateInput > {
59
- update ( lookupCriteria : { [ key : string ] : unknown } , input : UpdateInput ) : ExecuteSqlQuery ;
54
+ update ( lookupCriteria : { [ key : string ] : unknown } , input : UpdateInput ) : ExecuteSqlQuery ;
60
55
}
61
56
62
57
export interface DeleteQuery {
63
58
delete ( ) : ExecuteSqlQuery ;
64
59
}
65
60
66
- export class QueryRunner < T , CreateInput extends { [ key : string ] : unknown } , UpdateInput extends { [ key : string ] : unknown } >
67
- implements
68
- SelectQuery , JoinClause , WhereClause , LimitClause , OffsetClause ,
61
+ export class QueryRunner <
62
+ T ,
63
+ CreateInput extends { [ key : string ] : unknown } ,
64
+ UpdateInput extends { [ key : string ] : unknown }
65
+ > implements
66
+ SelectQuery ,
67
+ JoinClause ,
68
+ WhereClause ,
69
+ LimitClause ,
70
+ OffsetClause ,
69
71
InsertQuery < CreateInput > ,
70
72
UpdateQuery < UpdateInput > ,
71
73
DeleteQuery ,
72
74
ExecuteSqlQuery
73
75
{
74
76
#query: Query | null = null ;
77
+ #client: RelationalClient ;
75
78
76
- constructor ( private schema : Schema ) { }
79
+ constructor ( private schema : Schema ) {
80
+ console . log ( "Connecting to GRPC server at" , GRPC_RDB_SERVER_HOST , GRPC_RDB_SERVER_PORT , "..." ) ;
81
+ this . #client = new RelationalClient ( GRPC_RDB_SERVER_HOST ! , parseInt ( GRPC_RDB_SERVER_PORT ! ) ) ;
82
+ }
77
83
78
- select ( columns : TableColumn [ ] ) : JoinAndWhereClause & LimitClause & OffsetClause {
84
+ select ( columns : TableColumn [ ] ) : JoinWhereLimitAndOffset {
79
85
this . #query = {
80
86
query : {
81
87
$case : "select" ,
@@ -85,7 +91,7 @@ export class QueryRunner<T, CreateInput extends {[key: string]: unknown}, Update
85
91
column : columns . map ( ( col ) => ( {
86
92
tableName : this . schema . tableName ,
87
93
name : col . name ,
88
- type : col . type
94
+ type : col . type ,
89
95
} ) ) ,
90
96
where : [ ] ,
91
97
join : [ ] ,
@@ -96,15 +102,13 @@ export class QueryRunner<T, CreateInput extends {[key: string]: unknown}, Update
96
102
} ,
97
103
} ,
98
104
} ;
105
+
106
+ console . log ( "Query" , JSON . stringify ( this . #query, null , 2 ) ) ;
99
107
return this ;
100
108
}
101
109
102
110
// TODO: use "convenience" methods from lib-util to build the clause
103
- where ( whereCriteria : {
104
- key : string ,
105
- operator : Operator ,
106
- value : Value
107
- } ) : JoinAndWhereClause & LimitClause & OffsetClause {
111
+ where ( whereCriteria : { key : string ; operator : Operator ; value : Value } ) : JoinWhereLimitAndOffset {
108
112
if ( this . #query?. query ?. $case != "select" ) {
109
113
throw new Error ( "Cannot set where clause on a non-select query" ) ;
110
114
}
@@ -119,15 +123,15 @@ export class QueryRunner<T, CreateInput extends {[key: string]: unknown}, Update
119
123
}
120
124
121
125
limit ( limit : number ) : OffsetClause & ExecuteSqlQuery {
122
- if ( this . #query?. query ?. $case != "select" ) {
126
+ if ( this . #query?. query ?. $case != "select" ) {
123
127
throw new Error ( "Cannot set limit on a non-select query" ) ;
124
128
}
125
129
this . #query. query . select . limit = limit ;
126
130
return this ;
127
131
}
128
132
129
133
offset ( offset : number ) : ExecuteSqlQuery {
130
- if ( this . #query?. query ?. $case != "select" ) {
134
+ if ( this . #query?. query ?. $case != "select" ) {
131
135
throw new Error ( "Cannot set offset on a non-select query" ) ;
132
136
}
133
137
this . #query. query . select . offset = offset ;
@@ -168,8 +172,8 @@ export class QueryRunner<T, CreateInput extends {[key: string]: unknown}, Update
168
172
} ) ) ,
169
173
] ,
170
174
idTable : this . schema . tableName ,
171
- idColumn : "project_phase_id" ,
172
- idSequence : "project_phase_id_seq" ,
175
+ idColumn : this . schema . idColumn ?? undefined ,
176
+ idSequence : this . schema . idSequence ?? undefined ,
173
177
} ,
174
178
} ,
175
179
} ;
@@ -194,7 +198,7 @@ export class QueryRunner<T, CreateInput extends {[key: string]: unknown}, Update
194
198
query : this . #query,
195
199
} ;
196
200
197
- const queryResponse = await relationalClient . query ( queryRequest ) ;
201
+ const queryResponse = await this . #client . query ( queryRequest ) ;
198
202
199
203
switch ( this . #query. query ?. $case ) {
200
204
case "select" :
0 commit comments