Skip to content

Commit f9afd8e

Browse files
Move base-client/index.ts to base-client/base-client.ts
1 parent 46ea438 commit f9afd8e

File tree

2 files changed

+169
-167
lines changed

2 files changed

+169
-167
lines changed

packages/base-client/base-client.ts

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
import {
2+
type CredentialOptions,
3+
type CredentialFromOptions,
4+
Credential,
5+
} from "./credential";
6+
import { type Host, defaultHost } from "./host";
7+
import { type Database, defaultDatabase } from "./database";
8+
export { type Host, defaultHost, type Database, defaultDatabase };
9+
10+
export { type CredentialOptions, Credential };
11+
12+
export * from "./host";
13+
14+
export {
15+
type AuthenticatedCredential,
16+
type UnknownCredential,
17+
makeAuthHeaders,
18+
makeAuthPgParams,
19+
} from "./credential";
20+
21+
export type StrategyOptions = {
22+
[strategyName: string]: (args: any) => any;
23+
};
24+
25+
export interface ClientOptions<InputStrategyOptions = any> {
26+
credential?: CredentialOptions | null;
27+
host?: Host | null;
28+
database?: Database | null;
29+
strategies?: InputStrategyOptions;
30+
}
31+
32+
export interface QueryError {
33+
success: false;
34+
}
35+
36+
export type UnknownObjectShape = { readonly [column: string]: unknown };
37+
export type UnknownArrayShape = ReadonlyArray<unknown>;
38+
export type AnyArrayShape = ReadonlyArray<any>;
39+
40+
export type UnknownRowShape = UnknownObjectShape | UnknownArrayShape;
41+
42+
export interface ExecutionResultBase {
43+
success: boolean;
44+
45+
/**
46+
* HACK: Nothing enforces that this exists, it's up to to each DB to implement
47+
* the field, and that DB is free to add more fields. I just want to avoid another
48+
* layer of generic typechecking right now.
49+
*/
50+
fields: {
51+
/** The name of the column */
52+
name: string;
53+
/** The column index in the returned data */
54+
columnID: number | string;
55+
/** The machine readable format of the column*/
56+
format: string;
57+
/** The type that should be displayed for human consumption */
58+
formattedType: string;
59+
60+
// TODO: implemented by Splitgraph, but not by Seafowl (need to add some generic messiness)
61+
// dataTypeID: number;
62+
// dataTypeModifier: number;
63+
// dataTypeSize: number;
64+
// tableID: number;
65+
}[];
66+
}
67+
68+
export interface ExecutionResultWithObjectShapedRows<
69+
ObjectRowShape extends UnknownObjectShape
70+
> extends ExecutionResultBase {
71+
rows: ObjectRowShape[];
72+
readable: () => ReadableStream<ObjectRowShape>;
73+
}
74+
75+
export interface ExecutionResultWithArrayShapedRows<
76+
ArrayRowShape extends UnknownArrayShape
77+
> extends ExecutionResultBase {
78+
rows: ArrayRowShape[];
79+
readable: () => ReadableStream<ArrayRowShape>;
80+
}
81+
82+
export type ExecutionResultFromRowShape<RowShape extends UnknownRowShape> =
83+
RowShape extends UnknownArrayShape
84+
? ExecutionResultWithArrayShapedRows<RowShape>
85+
: RowShape extends UnknownObjectShape
86+
? ExecutionResultWithObjectShapedRows<RowShape>
87+
: never;
88+
89+
export interface CommonExecuteParameters {
90+
abortSignal: AbortSignal;
91+
}
92+
93+
export type CommonExecuteOptions = Partial<CommonExecuteParameters>;
94+
95+
export type ClientExecuteOptions = {
96+
rowMode: "array" | "object" | undefined;
97+
} & CommonExecuteOptions;
98+
99+
export interface Client {
100+
execute<RowShape extends UnknownArrayShape>(
101+
query: string,
102+
executeOptions: { rowMode: "array" } & CommonExecuteOptions
103+
): Promise<{
104+
response: ExecutionResultWithArrayShapedRows<RowShape> | null;
105+
error: QueryError | null;
106+
}>;
107+
108+
execute<RowShape extends UnknownObjectShape>(
109+
query: string,
110+
executeOptions?: { rowMode: "object" } & CommonExecuteOptions
111+
): Promise<{
112+
response: ExecutionResultWithObjectShapedRows<RowShape> | null;
113+
error: QueryError | null;
114+
}>;
115+
116+
execute<RowShape extends UnknownRowShape>(
117+
query: string,
118+
executeOptions: { rowMode?: "object" | "array" } & CommonExecuteOptions
119+
): Promise<{
120+
response: ExecutionResultFromRowShape<RowShape> | null;
121+
error: QueryError | null;
122+
}>;
123+
}
124+
125+
// type StrategyMapFromOptions<Options extends StrategyOptions> = StrategyOptions
126+
127+
// export type CredentialFromOptions<Opt extends BaseCredentialOptions> =
128+
// Opt extends KeypairCredentialOptions
129+
// ? KeypairCredential
130+
// : Opt extends AnonymousTokenCredentialOptions
131+
// ? AnonymousTokenCredential
132+
// : Opt extends AuthenticatedTokenCredentialOptions
133+
// ? AuthenticatedTokenCredential
134+
// : AnonymousTokenCredential;
135+
136+
export abstract class BaseClient<
137+
InputCredentialOptions extends CredentialOptions,
138+
InputStrategyOptions extends StrategyOptions = {}
139+
> implements Client
140+
{
141+
protected credential: CredentialFromOptions<InputCredentialOptions>;
142+
protected host: Host;
143+
protected database: Database;
144+
protected strategies: InputStrategyOptions;
145+
146+
constructor(opts: ClientOptions<InputStrategyOptions>) {
147+
this.credential = Credential(opts.credential || null);
148+
149+
this.host = opts.host ?? defaultHost;
150+
this.database = opts.database ?? defaultDatabase;
151+
152+
this.strategies = (opts.strategies ?? {}) as InputStrategyOptions;
153+
}
154+
155+
setCredential(newCredential: InputCredentialOptions | null) {
156+
this.credential = Credential(newCredential || null);
157+
}
158+
159+
// TODO: how many overloads can we move from implementation to here?
160+
abstract execute<RowShape extends UnknownRowShape>(
161+
query: string,
162+
executeOptions?: any & { rowMode?: "object" | "array" }
163+
): Promise<{
164+
response: ExecutionResultFromRowShape<RowShape> | null;
165+
error: QueryError | null;
166+
}>;
167+
}

packages/base-client/index.ts

Lines changed: 2 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -1,167 +1,2 @@
1-
import {
2-
type CredentialOptions,
3-
type CredentialFromOptions,
4-
Credential,
5-
} from "./credential";
6-
import { type Host, defaultHost } from "./host";
7-
import { type Database, defaultDatabase } from "./database";
8-
export { type Host, defaultHost, type Database, defaultDatabase };
9-
10-
export { type CredentialOptions, Credential };
11-
12-
export * from "./host";
13-
14-
export {
15-
type AuthenticatedCredential,
16-
type UnknownCredential,
17-
makeAuthHeaders,
18-
makeAuthPgParams,
19-
} from "./credential";
20-
21-
export type StrategyOptions = {
22-
[strategyName: string]: (args: any) => any;
23-
};
24-
25-
export interface ClientOptions<InputStrategyOptions = any> {
26-
credential?: CredentialOptions | null;
27-
host?: Host | null;
28-
database?: Database | null;
29-
strategies?: InputStrategyOptions;
30-
}
31-
32-
export interface QueryError {
33-
success: false;
34-
}
35-
36-
export type UnknownObjectShape = { readonly [column: string]: unknown };
37-
export type UnknownArrayShape = ReadonlyArray<unknown>;
38-
export type AnyArrayShape = ReadonlyArray<any>;
39-
40-
export type UnknownRowShape = UnknownObjectShape | UnknownArrayShape;
41-
42-
export interface ExecutionResultBase {
43-
success: boolean;
44-
45-
/**
46-
* HACK: Nothing enforces that this exists, it's up to to each DB to implement
47-
* the field, and that DB is free to add more fields. I just want to avoid another
48-
* layer of generic typechecking right now.
49-
*/
50-
fields: {
51-
/** The name of the column */
52-
name: string;
53-
/** The column index in the returned data */
54-
columnID: number | string;
55-
/** The machine readable format of the column*/
56-
format: string;
57-
/** The type that should be displayed for human consumption */
58-
formattedType: string;
59-
60-
// TODO: implemented by Splitgraph, but not by Seafowl (need to add some generic messiness)
61-
// dataTypeID: number;
62-
// dataTypeModifier: number;
63-
// dataTypeSize: number;
64-
// tableID: number;
65-
}[];
66-
}
67-
68-
export interface ExecutionResultWithObjectShapedRows<
69-
ObjectRowShape extends UnknownObjectShape
70-
> extends ExecutionResultBase {
71-
rows: ObjectRowShape[];
72-
readable: () => ReadableStream<ObjectRowShape>;
73-
}
74-
75-
export interface ExecutionResultWithArrayShapedRows<
76-
ArrayRowShape extends UnknownArrayShape
77-
> extends ExecutionResultBase {
78-
rows: ArrayRowShape[];
79-
readable: () => ReadableStream<ArrayRowShape>;
80-
}
81-
82-
export type ExecutionResultFromRowShape<RowShape extends UnknownRowShape> =
83-
RowShape extends UnknownArrayShape
84-
? ExecutionResultWithArrayShapedRows<RowShape>
85-
: RowShape extends UnknownObjectShape
86-
? ExecutionResultWithObjectShapedRows<RowShape>
87-
: never;
88-
89-
export interface CommonExecuteParameters {
90-
abortSignal: AbortSignal;
91-
}
92-
93-
export type CommonExecuteOptions = Partial<CommonExecuteParameters>;
94-
95-
export type ClientExecuteOptions = {
96-
rowMode: "array" | "object" | undefined;
97-
} & CommonExecuteOptions;
98-
99-
export interface Client {
100-
execute<RowShape extends UnknownArrayShape>(
101-
query: string,
102-
executeOptions: { rowMode: "array" } & CommonExecuteOptions
103-
): Promise<{
104-
response: ExecutionResultWithArrayShapedRows<RowShape> | null;
105-
error: QueryError | null;
106-
}>;
107-
108-
execute<RowShape extends UnknownObjectShape>(
109-
query: string,
110-
executeOptions?: { rowMode: "object" } & CommonExecuteOptions
111-
): Promise<{
112-
response: ExecutionResultWithObjectShapedRows<RowShape> | null;
113-
error: QueryError | null;
114-
}>;
115-
116-
execute<RowShape extends UnknownRowShape>(
117-
query: string,
118-
executeOptions: { rowMode?: "object" | "array" } & CommonExecuteOptions
119-
): Promise<{
120-
response: ExecutionResultFromRowShape<RowShape> | null;
121-
error: QueryError | null;
122-
}>;
123-
}
124-
125-
// type StrategyMapFromOptions<Options extends StrategyOptions> = StrategyOptions
126-
127-
// export type CredentialFromOptions<Opt extends BaseCredentialOptions> =
128-
// Opt extends KeypairCredentialOptions
129-
// ? KeypairCredential
130-
// : Opt extends AnonymousTokenCredentialOptions
131-
// ? AnonymousTokenCredential
132-
// : Opt extends AuthenticatedTokenCredentialOptions
133-
// ? AuthenticatedTokenCredential
134-
// : AnonymousTokenCredential;
135-
136-
export abstract class BaseClient<
137-
InputCredentialOptions extends CredentialOptions,
138-
InputStrategyOptions extends StrategyOptions = {}
139-
> implements Client
140-
{
141-
protected credential: CredentialFromOptions<InputCredentialOptions>;
142-
protected host: Host;
143-
protected database: Database;
144-
protected strategies: InputStrategyOptions;
145-
146-
constructor(opts: ClientOptions<InputStrategyOptions>) {
147-
this.credential = Credential(opts.credential || null);
148-
149-
this.host = opts.host ?? defaultHost;
150-
this.database = opts.database ?? defaultDatabase;
151-
152-
this.strategies = (opts.strategies ?? {}) as InputStrategyOptions;
153-
}
154-
155-
setCredential(newCredential: InputCredentialOptions | null) {
156-
this.credential = Credential(newCredential || null);
157-
}
158-
159-
// TODO: how many overloads can we move from implementation to here?
160-
abstract execute<RowShape extends UnknownRowShape>(
161-
query: string,
162-
executeOptions?: any & { rowMode?: "object" | "array" }
163-
): Promise<{
164-
response: ExecutionResultFromRowShape<RowShape> | null;
165-
error: QueryError | null;
166-
}>;
167-
}
1+
export * from "./base-client";
2+
export type * from "./base-client";

0 commit comments

Comments
 (0)