Skip to content

Commit

Permalink
Update to latest edgedb driver with new codec handling (#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaclarke authored Jan 16, 2025
1 parent d5af9e6 commit 42b4841
Show file tree
Hide file tree
Showing 17 changed files with 165 additions and 95 deletions.
6 changes: 5 additions & 1 deletion shared/common/components/resultGrid/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,11 @@ export const ResultGridContent = observer(function ResultGridContent({
indexOffset -
rowIndex
}
data={header.name ? data[dataIndex][header.name] : data[dataIndex]}
data={
header.key != null
? data[dataIndex][header.key]
: data[dataIndex]
}
/>
);
dataIndex += 1;
Expand Down
36 changes: 33 additions & 3 deletions shared/common/components/resultGrid/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {SetCodec} from "edgedb/dist/codecs/set";
import {DataGridState} from "../dataGrid/state";
import {assertNever} from "../../utils/assertNever";
import {NamedTupleCodec} from "edgedb/dist/codecs/namedtuple";
import {RecordCodec} from "edgedb/dist/codecs/record";

export function createResultGridState(codec: ICodec, data: any[]) {
return new ResultGridState(codec, data);
Expand All @@ -15,6 +16,7 @@ export interface GridHeader {
id: string;
parent: GridHeader | null;
name: string | null;
key: string | number | null;
multi: boolean;
codec: ICodec;
typename: string;
Expand Down Expand Up @@ -76,7 +78,7 @@ export class ResultGridState {
? tops.findIndex((top) => top > offsetRowIndex) - 1
: offsetRowIndex;
return {
data: parentData[dataIndex]?.[header.parent.name!] ?? [],
data: parentData[dataIndex]?.[header.parent.key!] ?? [],
indexOffset: indexOffset + (tops ? tops[dataIndex] : dataIndex),
endIndex: indexOffset + (tops ? tops[dataIndex + 1] : dataIndex + 1),
};
Expand All @@ -97,8 +99,8 @@ function _getRowTops(
if (!header.multi) continue;
const colHeight =
header.subHeaders && header.subHeaders[0].name !== null
? _getRowTops(topsMap, item[header.name!], header.subHeaders)
: item[header.name!].length;
? _getRowTops(topsMap, item[header.key!], header.subHeaders)
: item[header.key!].length;
if (colHeight > height) {
height = colHeight;
}
Expand Down Expand Up @@ -139,6 +141,7 @@ function _getHeaders(
id: parent ? `${parent.id}.${field.name}` : field.name,
parent,
name: field.name,
key: field.name,
multi,
codec: subcodec,
typename: getTypename(subcodec),
Expand All @@ -165,6 +168,7 @@ function _getHeaders(
id: `${header.id}.__multi__`,
parent: header,
name: null,
key: null,
multi: false,
codec: subcodec,
typename: getTypename(subcodec),
Expand All @@ -180,6 +184,31 @@ function _getHeaders(
}
}
return {headers, colCount};
} else if (codec instanceof RecordCodec) {
const subcodecs = codec.getSubcodecs();
const headers: GridHeader[] = [];
let i = 0;
const names = codec.getNames();
for (const name of names) {
const subcodec = subcodecs[i];
const startIndex = indexStart + i;
const header: GridHeader = {
id: name,
parent,
name: name,
key: i,
multi: false,
codec: subcodec,
typename: getTypename(subcodec),
depth,
startIndex,
span: 1,
subHeaders: null,
};
headers.push(header);
i++;
}
return {headers, colCount: headers.length};
}
throw new Error(`unexpected codec kind: ${codec.getKind()}`);
}
Expand All @@ -197,6 +226,7 @@ function getTypename(codec: ICodec): string {
case "scalar":
case "object":
case "sparse_object":
case "record":
return codec.getKnownTypeName();
case "array":
case "range":
Expand Down
5 changes: 3 additions & 2 deletions shared/common/decodeRawBuffer/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {_CodecsRegistry, _ReadBuffer, _ICodec} from "edgedb";
import {_CodecsRegistry, _ReadBuffer, _ICodec, Options} from "edgedb";
import type {ProtocolVersion} from "edgedb/dist/ifaces";

export type EdgeDBSet = Array<any> & {_codec: _ICodec};
Expand All @@ -7,6 +7,7 @@ export function decode(
registry: InstanceType<typeof _CodecsRegistry>,
outCodecBuf: Uint8Array,
resultBuf: Uint8Array,
options: Options,
protocolVersion: ProtocolVersion = [0, 10]
): EdgeDBSet | null {
let result: EdgeDBSet | null = null;
Expand All @@ -28,7 +29,7 @@ export function decode(

buf.sliceInto(codecReadBuf, len - 4);
codecReadBuf.discard(6);
const val = codec.decode(codecReadBuf);
const val = codec.decode(codecReadBuf, options.makeCodecContext());
result.push(val);
}

Expand Down
2 changes: 1 addition & 1 deletion shared/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "0.0.0",
"private": true,
"peerDependencies": {
"edgedb": "1.6.0-canary.20241206T104839",
"edgedb": "1.6.0-canary.20250111T054901",
"react": "^18.0.0",
"react-dom": "^18.0.0"
},
Expand Down
17 changes: 12 additions & 5 deletions shared/inspector/buildItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {_ICodec} from "edgedb";
import {CodecKind} from "edgedb/dist/codecs/ifaces";
import {ObjectCodec} from "edgedb/dist/codecs/object";
import {NamedTupleCodec} from "edgedb/dist/codecs/namedtuple";
import {RecordCodec} from "edgedb/dist/codecs/record";

import {buildScalarItem} from "./buildScalar";

Expand All @@ -16,6 +17,7 @@ export enum ItemType {
Object,
Tuple,
NamedTuple,
Record,
Scalar,
Other,
}
Expand All @@ -40,7 +42,7 @@ export interface ArrayLikeItem extends _BaseItem {
}

export interface ObjectLikeItem extends _BaseItem {
type: ItemType.Object | ItemType.NamedTuple;
type: ItemType.Object | ItemType.NamedTuple | ItemType.Record;
data: {[key: string]: any};
closingBracket: Item;
}
Expand Down Expand Up @@ -299,12 +301,16 @@ export function expandItem(
}
break;
case ItemType.NamedTuple:
case ItemType.Record:
{
const fieldNames = (item.codec as NamedTupleCodec).getNames();
const fieldNames = (
item.codec as NamedTupleCodec | RecordCodec
).getNames();
const isRecord = item.type === ItemType.Record;
const subCodecs = item.codec.getSubcodecs();

childItems = fieldNames.flatMap((fieldName, i) => {
const data = item.data[fieldName];
const data = item.data[isRecord ? i : fieldName];

const id = `${item.id}.${i}`;

Expand All @@ -317,13 +323,13 @@ export function expandItem(
label: (
<>
{fieldName}
<span> := </span>
<span>{isRecord ? ": " : " := "}</span>
</>
),
fieldName: fieldName,
},
data,
fieldName,
isRecord ? i : fieldName,
state.noMultiline,
i < item.data.length - 1
);
Expand Down Expand Up @@ -363,6 +369,7 @@ const itemTypes: {
object: {type: ItemType.Object, brackets: "{}"},
tuple: {type: ItemType.Tuple, brackets: "()"},
namedtuple: {type: ItemType.NamedTuple, brackets: "()"},
record: {type: ItemType.Record, brackets: "()"},
scalar: {type: ItemType.Scalar, brackets: ""},
};

Expand Down
2 changes: 1 addition & 1 deletion shared/inspector/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"version": "0.0.0",
"license": "UNLICENSED",
"peerDependencies": {
"edgedb": "1.6.0-canary.20241206T104839",
"edgedb": "1.6.0-canary.20250111T054901",
"react": "^18.0.0",
"react-dom": "^18.0.0"
},
Expand Down
5 changes: 3 additions & 2 deletions shared/inspector/renderJsonResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ export function _renderToJson(
.join(",\n")}\n${depth}]`;
}
case "object":
case "namedtuple": {
case "namedtuple":
case "record": {
const subcodecs = codec.getSubcodecs();
const explicitNum =
codecKind === "object"
Expand All @@ -114,7 +115,7 @@ export function _renderToJson(
depth +
` "${fieldName}": ` +
_renderToJson(
val[fieldName],
codecKind === "record" ? val[i] : val[fieldName],
subcodecs[i],
depth + " ",
implicitLimit
Expand Down
2 changes: 1 addition & 1 deletion shared/schemaGraph/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"version": "0.0.0",
"license": "UNLICENSED",
"peerDependencies": {
"edgedb": "1.6.0-canary.20241206T104839",
"edgedb": "1.6.0-canary.20250111T054901",
"react": "^18.0.0",
"react-dom": "^18.0.0"
},
Expand Down
2 changes: 1 addition & 1 deletion shared/studio/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"react-window": "^1.8.10"
},
"peerDependencies": {
"edgedb": "1.6.0-canary.20241206T104839",
"edgedb": "1.6.0-canary.20250111T054901",
"react": "^18.0.0",
"react-dom": "^18.0.0"
},
Expand Down
24 changes: 11 additions & 13 deletions shared/studio/state/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from "mobx-keystone";

import {AuthenticationError} from "edgedb";
import {Session} from "edgedb/dist/options";
import {Options} from "edgedb/dist/options";
import LRU from "edgedb/dist/primitives/lru";
import {Capabilities} from "edgedb/dist/baseConn";
import {AdminUIFetchConnection} from "edgedb/dist/fetchConn";
Expand All @@ -28,6 +28,7 @@ import {
EdgeDBSet,
QueryParams,
codecsRegistry,
baseOptions,
} from "../utils/decodeRawBuffer";
import {instanceCtx} from "./instance";
import {sessionStateCtx} from "./sessionState";
Expand Down Expand Up @@ -121,13 +122,12 @@ export function createAuthenticatedFetch({
};
}

function setQueryTag(session: Session, tag: string) {
session = new Session({...session});
(session as any).annotations = new Map<string, string>(
(session as any).annotations
);
(session as any).annotations.set("tag", tag);
return session;
function setQueryTag(options: Options, tag: string) {
const annos = new Map((options as any).annotations as Map<string, string>);
annos.set("tag", tag);
const clone = (options as any)._cloneWith({});
clone.annotations = annos;
return clone as Options;
}

@model("Connection")
Expand All @@ -154,7 +154,7 @@ export class Connection extends Model({
get _state() {
const sessionState = sessionStateCtx.get(this);

let state = Session.defaults();
let state = baseOptions;

if (sessionState?.activeState.globals.length) {
state = state.withGlobals(
Expand Down Expand Up @@ -283,10 +283,7 @@ export class Connection extends Model({
let state = this._state;

if (opts.ignoreSessionConfig) {
state = setQueryTag(
Session.defaults().withGlobals(state.globals),
"gel/ui"
);
state = setQueryTag(baseOptions.withGlobals(state.globals), "gel/ui");
}
if (opts.ignoreForceDatabaseError) {
state = state.withConfig({force_database_error: "false"});
Expand Down Expand Up @@ -408,6 +405,7 @@ export class Connection extends Model({
result: decode(
outCodecBuf,
resultBuf,
state,
this.conn.protocolVersion,
opts.newCodec
),
Expand Down
4 changes: 2 additions & 2 deletions shared/studio/state/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {

import {AuthenticationError} from "edgedb";

import {Session} from "edgedb/dist/options";
import {Options} from "edgedb/dist/options";
import {AdminUIFetchConnection} from "edgedb/dist/fetchConn";
import {OutputFormat, Cardinality} from "edgedb/dist/ifaces";
import {codecsRegistry} from "../utils/decodeRawBuffer";
Expand Down Expand Up @@ -94,7 +94,7 @@ export class InstanceState extends Model({
null,
OutputFormat.BINARY,
cardinality,
Session.defaults()
Options.defaults()
)
).result;
}
Expand Down
4 changes: 3 additions & 1 deletion shared/studio/tabs/queryEditor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import {ICodec} from "edgedb/dist/codecs/ifaces";
import {SplitViewDirection} from "@edgedb/common/ui/splitView/model";
import {createPortal} from "react-dom";
import {RelativeTime} from "@edgedb/common/utils/relativeTime";
import {RecordCodec} from "edgedb/dist/codecs/record";

export const QueryEditorView = observer(function QueryEditorView() {
const editorState = useTabState(QueryEditor);
Expand Down Expand Up @@ -550,7 +551,8 @@ export function outputModeToggle(
outputMode: OutputMode,
setOutputMode: (mode: OutputMode) => void
) {
const tableOutputAvailable = codec instanceof ObjectCodec;
const tableOutputAvailable =
codec instanceof ObjectCodec || codec instanceof RecordCodec;
const mode = tableOutputAvailable ? outputMode : OutputMode.Tree;

return {
Expand Down
3 changes: 2 additions & 1 deletion shared/studio/tabs/queryEditor/state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
QueryResultData,
} from "../../../idbStore";

import {EdgeDBSet, decode} from "../../../utils/decodeRawBuffer";
import {EdgeDBSet, baseOptions, decode} from "../../../utils/decodeRawBuffer";
import {
ErrorDetails,
extractErrorDetails,
Expand Down Expand Up @@ -416,6 +416,7 @@ export class QueryEditor extends Model({
? decode(
resultData.outCodecBuf,
resultData.resultBuf,
baseOptions,
resultData.protoVer ?? [1, 0]
)
: null
Expand Down
3 changes: 2 additions & 1 deletion shared/studio/tabs/repl/state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
extractErrorDetails,
} from "../../../utils/extractErrorDetails";
import {InspectorState, Item} from "@edgedb/inspector/state";
import {decode, EdgeDBSet} from "../../../utils/decodeRawBuffer";
import {baseOptions, decode, EdgeDBSet} from "../../../utils/decodeRawBuffer";
import {CommandResult, handleSlashCommand} from "./commands";
import {
clearReplHistory,
Expand Down Expand Up @@ -326,6 +326,7 @@ export class Repl extends Model({
? decode(
resultData.outCodecBuf,
resultData.resultBuf,
baseOptions,
resultData.protoVer ?? [1, 0]
)
: null
Expand Down
Loading

0 comments on commit 42b4841

Please sign in to comment.