Skip to content

Commit

Permalink
add interceptor (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
wirekang authored Jan 30, 2024
1 parent dbc16a4 commit b7df01e
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 19 deletions.
22 changes: 16 additions & 6 deletions public/playground.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _ar
function reject(value) { resume("throw", value); }
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
};
export function init(kyselyConstructor, adapter, introspector, queryCompiler) {
const kysely = new kyselyConstructor({
let interceptor;
export function init(kyselyConstructor, adapter, introspector, queryCompiler, i) {
interceptor = i;
interceptor.log = (...args) => {
dispatch("log", { args: args.map((it) => JSON.stringify(it)) });
};
const db = new kyselyConstructor({
dialect: new PlaygroundDialect(queryCompiler, adapter, introspector),
});
return { kysely };
return { db };
}
class PlaygroundDialect {
constructor(queryCompiler, adapter, introspector) {
Expand Down Expand Up @@ -69,18 +74,23 @@ class PlaygroundDriver {
return resolved;
}
}
const queryResult = { rows: [] };
class PlaygroundConnection {
executeQuery(compiledQuery) {
dispatch("executeQuery", { compiledQuery });
return Promise.resolve(queryResult);
return Promise.resolve(playgroundResult());
}
streamQuery(compiledQuery, chunkSize) {
dispatch("streamQuery", { compiledQuery, chunkSize });
return (function g() {
return __asyncGenerator(this, arguments, function* g_1() {
yield yield __await(queryResult);
yield yield __await(playgroundResult());
});
})();
}
}
function playgroundResult() {
if (interceptor === null || interceptor === void 0 ? void 0 : interceptor.result) {
return interceptor.result;
}
return { rows: [] };
}
2 changes: 2 additions & 0 deletions src/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@ function setupQueryEditorController() {
D.resultController.appendPadding();
}
break;
case "log":
D.resultController.appendMessage("trace", `log: ${output.args.join(", ")}`);
}
}
D.hightlighter.highlight();
Expand Down
14 changes: 13 additions & 1 deletion src/lib/executer/executer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ export class Executer {
case "rollbackTransaction":
outputs.push({ type: "transaction", type2: "rollback" });
break;
case "log":
outputs.push({ type: "log", args: detail.args });
break;
}
};
window.addEventListener("playground", cb);
Expand Down Expand Up @@ -89,7 +92,16 @@ export class Executer {

class ExecuterError extends Error {}

export type ExecuteOutput = ExecuteOutputQuery | ExecuteOutputError | ExecuteOutputTransaction;
export type ExecuteOutput =
| ExecuteOutputQuery
| ExecuteOutputError
| ExecuteOutputTransaction
| ExecuteOutputLog;

type ExecuteOutputLog = {
type: "log";
args: Array<unknown>;
};

type ExecuteOutputQuery = {
type: "query";
Expand Down
31 changes: 25 additions & 6 deletions src/lib/utility/playground-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,35 @@ export class PlaygroundUtils {
* [playground]
*
* A \`Kysely\` instance with \`Database\` type from \`type-editor\`.
*
* \`\`\`ts
* import type { Database } from "type-editor";
* const db: Kysely<Database>;
* \`\`\`
*/
declare const db: import("kysely").Kysely<import("type-editor").Database>;
// prettier-ignore
/**
* [playground]
*
* An interceptor from kysely-playground.
*/
let $playground: {
/**
* [playground]
*
* Set the result of \`execute()\`.
*
* default:
* \`{ rows: [] }\`
*/
result: import("kysely").QueryResult<any>;
/**
* [playground]
*
* Log messages to result panel.
*/
log: (...args: any[]) => any;
} = {} as any
// prettier-ignore
// @ts-ignore
const db = (await import("playground")).init(${k}.Kysely,new ${k}.${adapter}(),new ${k}.${introspector}(),new ${k}.${queryCompiler}()).kysely;
const { db } = (await import("playground")).init(${k}.Kysely,new ${k}.${adapter}(),new ${k}.${introspector}(),new ${k}.${queryCompiler}(),$playground);
// prettier-ignore
export {}
`.trim() + QUERY_EDITOR_HEADER_DELIMITER
Expand Down
30 changes: 24 additions & 6 deletions src/public/playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,30 @@ interface KyselyConstructor<T> extends Function {
new (config: KyselyConfig): Kysely<T>;
}

type PlaygroundInterceptor = {
result: QueryResult<any>;
log: (...args: Array<any>) => any;
};

let interceptor: Partial<PlaygroundInterceptor>;

export function init(
kyselyConstructor: KyselyConstructor<any>,
adapter: DialectAdapter,
introspector: DatabaseIntrospector,
queryCompiler: QueryCompiler,
i: Partial<PlaygroundInterceptor>,
) {
const kysely = new kyselyConstructor({
interceptor = i;
interceptor.log = (...args: unknown[]) => {
dispatch("log", { args: args.map((it) => JSON.stringify(it)) });
};

const db = new kyselyConstructor({
dialect: new PlaygroundDialect(queryCompiler, adapter, introspector),
});

return { kysely };
return { db };
}

class PlaygroundDialect implements Dialect {
Expand Down Expand Up @@ -94,12 +107,10 @@ class PlaygroundDriver implements Driver {
}
}

const queryResult: QueryResult<any> = { rows: [] };

class PlaygroundConnection implements DatabaseConnection {
executeQuery<R>(compiledQuery: CompiledQuery<unknown>): Promise<QueryResult<R>> {
dispatch("executeQuery", { compiledQuery });
return Promise.resolve(queryResult);
return Promise.resolve(playgroundResult());
}

streamQuery<R>(
Expand All @@ -108,7 +119,14 @@ class PlaygroundConnection implements DatabaseConnection {
): AsyncIterableIterator<QueryResult<R>> {
dispatch("streamQuery", { compiledQuery, chunkSize });
return (async function* g() {
yield queryResult;
yield playgroundResult();
})();
}
}

function playgroundResult(): QueryResult<any> {
if (interceptor?.result) {
return interceptor.result;
}
return { rows: [] };
}

0 comments on commit b7df01e

Please sign in to comment.