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

Refactor codec to TS generation for fine grained customization #889

Merged
merged 5 commits into from Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1 @@
select <array<tuple<str, str>>>$deep;
28 changes: 28 additions & 0 deletions integration-tests/lts/queries.test.ts
Expand Up @@ -6,6 +6,8 @@ import {
getMoviesStarring,
type GetMoviesStarringArgs,
type GetMoviesStarringReturns,
deepArrayInput,
type DeepArrayInputArgs,
} from "./dbschema/queries";
import { setupTests, teardownTests } from "./setupTeardown";

Expand Down Expand Up @@ -68,4 +70,30 @@ describe("queries", () => {

tc.assert<tc.IsExact<GetMoviesStarringReturns, result>>(true);
});

test("deep array input", async () => {
const result = await deepArrayInput(client, {
deep: [
['name', 'Stark'],
['color', 'red'],
] as const,
});

type result = typeof result;
tc.assert<
tc.IsExact<
result,
Array<[string, string]>
>
>(true);

tc.assert<
tc.IsExact<
DeepArrayInputArgs,
{
deep: ReadonlyArray<readonly [string, string]>;
}
>
>(true);
});
});
23 changes: 23 additions & 0 deletions packages/driver/src/baseClient.ts
Expand Up @@ -37,6 +37,7 @@ import Event from "./primitives/event";
import { LifoQueue } from "./primitives/queues";
import { BaseRawConnection } from "./baseConn";
import { ConnectWithTimeout, retryingConnect } from "./retry";
import { util } from "./reflection/util";
import { Transaction } from "./transaction";
import { sleep } from "./utils";

Expand Down Expand Up @@ -650,4 +651,26 @@ export class Client implements Executor {
await holder.release();
}
}

async parse(query: string) {
const holder = await this.pool.acquireHolder(this.options);
try {
const cxn = await holder._getConnection();
const result = await cxn._parse(
query,
OutputFormat.BINARY,
Cardinality.MANY,
this.options.session
);
const cardinality = util.parseCardinality(result[0]);

return {
in: result[1],
out: result[2],
cardinality,
};
} finally {
await holder.release();
}
}
}
6 changes: 0 additions & 6 deletions packages/driver/src/codecs/consts.ts
Expand Up @@ -57,9 +57,3 @@ export const KNOWN_TYPENAMES = (() => {
}
return res;
})();

export const NO_RESULT = 0x6e;
export const AT_MOST_ONE = 0x6f;
export const ONE = 0x41;
export const MANY = 0x6d;
export const AT_LEAST_ONE = 0x4d;
8 changes: 4 additions & 4 deletions packages/driver/src/codecs/object.ts
Expand Up @@ -16,9 +16,9 @@
* limitations under the License.
*/

import { Cardinality } from "../ifaces";
import { ICodec, Codec, uuid, CodecKind } from "./ifaces";
import { ReadBuffer, WriteBuffer } from "../primitives/buffer";
import { ONE, AT_LEAST_ONE } from "./consts";
import {
InvalidArgumentError,
MissingArgumentError,
Expand All @@ -34,7 +34,7 @@ export interface ObjectFieldInfo {
name: string;
implicit: boolean;
linkprop: boolean;
cardinality: number;
cardinality: Cardinality;
}

export class ObjectCodec extends Codec implements ICodec {
Expand Down Expand Up @@ -104,7 +104,7 @@ export class ObjectCodec extends Codec implements ICodec {
const arg = args[i];
if (arg == null) {
const card = this.cardinalities[i];
if (card === ONE || card === AT_LEAST_ONE) {
if (card === Cardinality.ONE || card === Cardinality.AT_LEAST_ONE) {
throw new MissingArgumentError(
`argument ${this.fields[i].name} is required, but received ${arg}`
);
Expand Down Expand Up @@ -154,7 +154,7 @@ export class ObjectCodec extends Codec implements ICodec {
elemData.writeInt32(0); // reserved bytes
if (val == null) {
const card = this.cardinalities[i];
if (card === ONE || card === AT_LEAST_ONE) {
if (card === Cardinality.ONE || card === Cardinality.AT_LEAST_ONE) {
throw new MissingArgumentError(
`argument ${this.fields[i].name} is required, but received ${val}`
);
Expand Down