Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce client.withCodecs() #1161

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions packages/driver/src/baseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import {
} from "./ifaces";
import type {
RetryOptions,
Session,
SimpleRetryOptions,
SimpleTransactionOptions,
TransactionOptions,
Expand Down Expand Up @@ -190,7 +189,7 @@ export class ClientConnectionHolder {
args,
outputFormat,
expectedCardinality,
this.options.session,
this.options,
false /* privilegedMode */,
language,
);
Expand Down Expand Up @@ -581,33 +580,28 @@ export class Client implements Executor {
return new Client(this.pool, this.options.withRetryOptions(opts));
}

withSession(session: Session): Client {
return new Client(this.pool, this.options.withSession(session));
}

withModuleAliases(aliases: Record<string, string>) {
return new Client(
this.pool,
this.options.withSession(this.options.session.withModuleAliases(aliases)),
this.options.withModuleAliases(aliases),
);
}

withConfig(config: SimpleConfig): Client {
const newConfig = this.options.session.withConfig(config);
return new Client(this.pool, this.options.withSession(newConfig));
return new Client(this.pool, this.options.withConfig(config));
}

withGlobals(globals: Record<string, any>): Client {
return new Client(
this.pool,
this.options.withSession(this.options.session.withGlobals(globals)),
this.options.withGlobals(globals),
);
}

withQueryTag(tag: string | null): Client {
return new Client(
this.pool,
this.options.withSession(this.options.session.withQueryTag(tag)),
this.options.withQueryTag(tag),
);
}

Expand Down Expand Up @@ -775,7 +769,7 @@ export class Client implements Executor {
query,
OutputFormat.BINARY,
Cardinality.MANY,
this.options.session,
this.options,
);
const cardinality = util.parseCardinality(result[0]);

Expand Down
20 changes: 10 additions & 10 deletions packages/driver/src/baseConn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import * as chars from "./primitives/chars";
import Event from "./primitives/event";
import LRU from "./primitives/lru";
import type { SerializedSessionState } from "./options";
import { Session } from "./options";
import { Options } from "./options";

export const PROTO_VER: ProtocolVersion = [3, 0];
export const PROTO_VER_MIN: ProtocolVersion = [0, 9];
Expand Down Expand Up @@ -138,7 +138,7 @@ export class BaseRawConnection {
isLegacyProtocol = false;

protected stateCodec: ICodec = INVALID_CODEC;
protected stateCache = new WeakMap<Session, Uint8Array>();
protected stateCache = new WeakMap<Options, Uint8Array>();
lastStateUpdate: SerializedSessionState | null = null;

protected adminUIMode = false;
Expand Down Expand Up @@ -894,7 +894,7 @@ export class BaseRawConnection {
query: string,
outputFormat: OutputFormat,
expectedCardinality: Cardinality,
state: Session,
state: Options,
capabilitiesFlags: number,
options: QueryOptions | undefined,
language: Language,
Expand Down Expand Up @@ -936,7 +936,7 @@ export class BaseRawConnection {
);
wb.writeString(query);

if (!this.adminUIMode && state === Session.defaults()) {
if (!this.adminUIMode && state.isDefaultSession()) {
wb.writeBuffer(NULL_CODEC.tidBuffer);
wb.writeInt32(0);
} else {
Expand All @@ -959,7 +959,7 @@ export class BaseRawConnection {
query: string,
outputFormat: OutputFormat,
expectedCardinality: Cardinality,
state: Session,
state: Options,
capabilitiesFlags: number = RESTRICTED_CAPABILITIES,
options?: QueryOptions,
): Promise<ParseResult> {
Expand Down Expand Up @@ -1082,7 +1082,7 @@ export class BaseRawConnection {
args: QueryArgs,
outputFormat: OutputFormat,
expectedCardinality: Cardinality,
state: Session,
state: Options,
inCodec: ICodec,
outCodec: ICodec,
result: any[] | WriteBuffer,
Expand Down Expand Up @@ -1256,7 +1256,7 @@ export class BaseRawConnection {
args: QueryArgs = null,
outputFormat: OutputFormat,
expectedCardinality: Cardinality,
state: Session,
state: Options,
privilegedMode = false,
language: Language = Language.EDGEQL,
): Promise<{ result: any; warnings: errors.EdgeDBError[] }> {
Expand Down Expand Up @@ -1307,7 +1307,7 @@ export class BaseRawConnection {

if (
(!inCodec && args !== null) ||
(this.stateCodec === INVALID_CODEC && state !== Session.defaults())
(this.stateCodec === INVALID_CODEC && !state.isDefaultSession())
) {
[card, inCodec, outCodec, _, _, _, warnings] = await this._parse(
language,
Expand Down Expand Up @@ -1353,7 +1353,7 @@ export class BaseRawConnection {
}
}
} else {
if (state !== Session.defaults()) {
if (!state.isDefaultSession()) {
throw new errors.InterfaceError(
`setting session state is not supported in this version of ` +
`EdgeDB. Upgrade to EdgeDB 2.0 or newer.`,
Expand Down Expand Up @@ -1491,7 +1491,7 @@ export class BaseRawConnection {
undefined,
OutputFormat.NONE,
Cardinality.NO_RESULT,
Session.defaults(),
Options.defaults(),
true,
);
} catch {
Expand Down
6 changes: 3 additions & 3 deletions packages/driver/src/fetchConn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import {
type QueryArgs,
type QueryOptions,
} from "./ifaces";
import type { Session } from "./options";
import type { Options } from "./options";
import { WriteBuffer } from "./primitives/buffer";
import * as chars from "./primitives/chars";
import Event from "./primitives/event";
Expand Down Expand Up @@ -209,7 +209,7 @@ export class AdminUIFetchConnection extends BaseFetchConnection {
public async rawParse(
language: Language,
query: string,
state: Session,
state: Options,
options?: QueryOptions,
abortSignal?: AbortSignal | null,
): Promise<
Expand Down Expand Up @@ -239,7 +239,7 @@ export class AdminUIFetchConnection extends BaseFetchConnection {
public async rawExecute(
language: Language,
query: string,
state: Session,
state: Options,
outCodec?: ICodec,
options?: QueryOptions,
inCodec?: ICodec,
Expand Down
9 changes: 9 additions & 0 deletions packages/driver/src/ifaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ export interface Executor {
queryRequiredSingleJSON(query: string, args?: QueryArgs): Promise<string>;
}

interface UserCodec {
encode(value: any): any;
decode(value: any): any;
}

export type UserCodecs = {
[typeName: string]: UserCodec;
}

export interface KnownServerSettings {
suggested_pool_concurrency?: number;
system_config?: any;
Expand Down
2 changes: 1 addition & 1 deletion packages/driver/src/index.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export {
IsolationLevel,
RetryCondition,
RetryOptions,
Session,
Options,
} from "./options";
export { defaultBackoff, logWarnings, throwWarnings } from "./options";
export type { BackoffFunction } from "./options";
Expand Down
Loading
Loading