Skip to content

Commit 72cf261

Browse files
committed
feat: use @topcoder-framework/lib-client for rdb types
1 parent 74c74b7 commit 72cf261

22 files changed

+373
-385
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"license": "ISC",
1818
"dependencies": {
1919
"@grpc/grpc-js": "^1.7.1",
20+
"@topcoder-framework/client-relational": "^0.4.23-ci.0",
2021
"dayjs": "^1.11.5",
2122
"dotenv": "^16.0.3",
2223
"grpc-server-reflection": "^0.1.5",

src/common/QueryRunner.ts

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
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";
1412

1513
export type Schema = {
1614
dbSchema: string;
@@ -26,21 +24,18 @@ interface ExecuteSqlQuery {
2624
}
2725

2826
type JoinAndWhereClause = JoinClause & WhereClause & ExecuteSqlQuery;
27+
type JoinWhereLimitAndOffset = JoinAndWhereClause & LimitClause & OffsetClause;
2928

3029
export interface SelectQuery {
31-
select(columns: TableColumn[]): JoinAndWhereClause & LimitClause & OffsetClause;
30+
select(columns: TableColumn[]): JoinWhereLimitAndOffset;
3231
}
3332

3433
export interface JoinClause {
3534
join(): JoinAndWhereClause;
3635
}
3736

3837
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;
4439
}
4540

4641
export interface LimitClause {
@@ -79,10 +74,14 @@ export class QueryRunner<
7974
ExecuteSqlQuery
8075
{
8176
#query: Query | null = null;
77+
#client: RelationalClient;
8278

83-
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+
}
8483

85-
select(columns: TableColumn[]): JoinAndWhereClause & LimitClause & OffsetClause {
84+
select(columns: TableColumn[]): JoinWhereLimitAndOffset {
8685
this.#query = {
8786
query: {
8887
$case: "select",
@@ -103,15 +102,13 @@ export class QueryRunner<
103102
},
104103
},
105104
};
105+
106+
console.log("Query", JSON.stringify(this.#query, null, 2));
106107
return this;
107108
}
108109

109110
// TODO: use "convenience" methods from lib-util to build the clause
110-
where(whereCriteria: {
111-
key: string;
112-
operator: Operator;
113-
value: Value;
114-
}): JoinAndWhereClause & LimitClause & OffsetClause {
111+
where(whereCriteria: { key: string; operator: Operator; value: Value }): JoinWhereLimitAndOffset {
115112
if (this.#query?.query?.$case != "select") {
116113
throw new Error("Cannot set where clause on a non-select query");
117114
}
@@ -201,7 +198,7 @@ export class QueryRunner<
201198
query: this.#query,
202199
};
203200

204-
const queryResponse = await relationalClient.query(queryRequest);
201+
const queryResponse = await this.#client.query(queryRequest);
205202

206203
switch (this.#query.query?.$case) {
207204
case "select":

src/common/TableColumn.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ColumnType } from "../../dist/grpc/models/rdb/relational";
1+
import { ColumnType } from "@topcoder-framework/client-relational";
22

33
export type TableColumn = {
44
name: string;

src/domain/LegacyChallenge.ts

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,53 +5,6 @@ import { Value } from "../models/google/protobuf/struct";
55
import { Project } from "../schema/project/Project";
66

77
class LegacyChallengeDomain {
8-
constructor(private tableName: string = "project") {}
9-
10-
// public async lookup(lookupCriteria: LookupCriteria): Promise<LegacyChallenge[]> {
11-
// const queryRequest: QueryRequest = {
12-
// query: {
13-
// query: {
14-
// $case: "select",
15-
// select: {
16-
// table: this.tableName,
17-
// join: [],
18-
// column: [
19-
// {
20-
// name: "project_id",
21-
// type: ColumnType.COLUMN_TYPE_INT,
22-
// },
23-
// ],
24-
// where: [
25-
// {
26-
// key: "project_id",
27-
// operator: Operator.OPERATOR_EQUAL,
28-
// value: {
29-
// value: {
30-
// $case: "intValue",
31-
// intValue: 123,
32-
// },
33-
// },
34-
// },
35-
// ],
36-
// groupBy: [],
37-
// orderBy: [],
38-
// limit: 1,
39-
// offset: 0,
40-
// },
41-
// },
42-
// },
43-
// };
44-
45-
// const queryResponse: QueryResponse = await relationalClient.query(queryRequest);
46-
47-
// if (queryResponse.result?.$case == "selectResult") {
48-
// const rows = queryResponse.result.selectResult.rows;
49-
// return rows.map((row) => LegacyChallenge.fromJSON(row.values));
50-
// }
51-
52-
// return [];
53-
// }
54-
558
public async checkChallengeExists(
569
legacyChallengeId: number
5710
): Promise<CheckChallengeExistsResponse> {

src/models/common/common.ts

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint-disable */
2+
import Long from "long";
23
import _m0 from "protobufjs/minimal";
34
import { Struct, Value } from "../google/protobuf/struct";
45
import { Timestamp } from "../google/protobuf/timestamp";
@@ -113,6 +114,7 @@ export enum Domain {
113114
DOMAIN_CHALLENGE_TRACK = 7,
114115
DOMAIN_CHALLENGE_PHASE = 8,
115116
DOMAIN_CHALLENGE_TIMELINE_TEMPLATE = 9,
117+
DOMAIN_SUBMISSION = 10,
116118
UNRECOGNIZED = -1,
117119
}
118120

@@ -148,6 +150,9 @@ export function domainFromJSON(object: any): Domain {
148150
case 9:
149151
case "DOMAIN_CHALLENGE_TIMELINE_TEMPLATE":
150152
return Domain.DOMAIN_CHALLENGE_TIMELINE_TEMPLATE;
153+
case 10:
154+
case "DOMAIN_SUBMISSION":
155+
return Domain.DOMAIN_SUBMISSION;
151156
case -1:
152157
case "UNRECOGNIZED":
153158
default:
@@ -177,6 +182,8 @@ export function domainToJSON(object: Domain): string {
177182
return "DOMAIN_CHALLENGE_PHASE";
178183
case Domain.DOMAIN_CHALLENGE_TIMELINE_TEMPLATE:
179184
return "DOMAIN_CHALLENGE_TIMELINE_TEMPLATE";
185+
case Domain.DOMAIN_SUBMISSION:
186+
return "DOMAIN_SUBMISSION";
180187
case Domain.UNRECOGNIZED:
181188
default:
182189
return "UNRECOGNIZED";
@@ -199,6 +206,10 @@ export interface ScanResult {
199206
items: { [key: string]: any }[];
200207
}
201208

209+
export interface CreateResult {
210+
kind?: { $case: "integerId"; integerId: number } | { $case: "stringId"; stringId: string };
211+
}
212+
202213
export interface LookupCriteria {
203214
key: string;
204215
value?: any;
@@ -414,6 +425,75 @@ export const ScanResult = {
414425
},
415426
};
416427

428+
function createBaseCreateResult(): CreateResult {
429+
return { kind: undefined };
430+
}
431+
432+
export const CreateResult = {
433+
encode(message: CreateResult, writer: _m0.Writer = _m0.Writer.create()): _m0.Writer {
434+
if (message.kind?.$case === "integerId") {
435+
writer.uint32(8).int64(message.kind.integerId);
436+
}
437+
if (message.kind?.$case === "stringId") {
438+
writer.uint32(18).string(message.kind.stringId);
439+
}
440+
return writer;
441+
},
442+
443+
decode(input: _m0.Reader | Uint8Array, length?: number): CreateResult {
444+
const reader = input instanceof _m0.Reader ? input : new _m0.Reader(input);
445+
let end = length === undefined ? reader.len : reader.pos + length;
446+
const message = createBaseCreateResult();
447+
while (reader.pos < end) {
448+
const tag = reader.uint32();
449+
switch (tag >>> 3) {
450+
case 1:
451+
message.kind = { $case: "integerId", integerId: longToNumber(reader.int64() as Long) };
452+
break;
453+
case 2:
454+
message.kind = { $case: "stringId", stringId: reader.string() };
455+
break;
456+
default:
457+
reader.skipType(tag & 7);
458+
break;
459+
}
460+
}
461+
return message;
462+
},
463+
464+
fromJSON(object: any): CreateResult {
465+
return {
466+
kind: isSet(object.integerId)
467+
? { $case: "integerId", integerId: Number(object.integerId) }
468+
: isSet(object.stringId)
469+
? { $case: "stringId", stringId: String(object.stringId) }
470+
: undefined,
471+
};
472+
},
473+
474+
toJSON(message: CreateResult): unknown {
475+
const obj: any = {};
476+
message.kind?.$case === "integerId" && (obj.integerId = Math.round(message.kind?.integerId));
477+
message.kind?.$case === "stringId" && (obj.stringId = message.kind?.stringId);
478+
return obj;
479+
},
480+
481+
create<I extends Exact<DeepPartial<CreateResult>, I>>(base?: I): CreateResult {
482+
return CreateResult.fromPartial(base ?? {});
483+
},
484+
485+
fromPartial<I extends Exact<DeepPartial<CreateResult>, I>>(object: I): CreateResult {
486+
const message = createBaseCreateResult();
487+
if (object.kind?.$case === "integerId" && object.kind?.integerId !== undefined && object.kind?.integerId !== null) {
488+
message.kind = { $case: "integerId", integerId: object.kind.integerId };
489+
}
490+
if (object.kind?.$case === "stringId" && object.kind?.stringId !== undefined && object.kind?.stringId !== null) {
491+
message.kind = { $case: "stringId", stringId: object.kind.stringId };
492+
}
493+
return message;
494+
},
495+
};
496+
417497
function createBaseLookupCriteria(): LookupCriteria {
418498
return { key: "", value: undefined };
419499
}
@@ -526,6 +606,25 @@ export const GoogleProtobufTypesPlaceholder = {
526606
},
527607
};
528608

609+
declare var self: any | undefined;
610+
declare var window: any | undefined;
611+
declare var global: any | undefined;
612+
var tsProtoGlobalThis: any = (() => {
613+
if (typeof globalThis !== "undefined") {
614+
return globalThis;
615+
}
616+
if (typeof self !== "undefined") {
617+
return self;
618+
}
619+
if (typeof window !== "undefined") {
620+
return window;
621+
}
622+
if (typeof global !== "undefined") {
623+
return global;
624+
}
625+
throw "Unable to locate global object";
626+
})();
627+
529628
type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;
530629

531630
type DeepPartial<T> = T extends Builtin ? T
@@ -551,6 +650,18 @@ function fromTimestamp(t: Timestamp): string {
551650
return new Date(millis).toISOString();
552651
}
553652

653+
function longToNumber(long: Long): number {
654+
if (long.gt(Number.MAX_SAFE_INTEGER)) {
655+
throw new tsProtoGlobalThis.Error("Value is larger than Number.MAX_SAFE_INTEGER");
656+
}
657+
return long.toNumber();
658+
}
659+
660+
if (_m0.util.Long !== Long) {
661+
_m0.util.Long = Long as any;
662+
_m0.configure();
663+
}
664+
554665
function isSet(value: any): boolean {
555666
return value !== null && value !== undefined;
556667
}

0 commit comments

Comments
 (0)