Skip to content

Commit

Permalink
[accel-record-core] use synclib
Browse files Browse the repository at this point in the history
  • Loading branch information
koyopro committed Dec 9, 2024
1 parent 946ac81 commit cd8b2d6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
12 changes: 5 additions & 7 deletions packages/accel-record-core/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { fileURLToPath } from "url";
import { loadDmmf } from "./fields.js";
import { Model } from "./index.js";
import { loadI18n } from "./model/naming.js";
// @ts-ignore
import SyncRpc, { stop } from "./sync-rpc/index.js";
import { actions, stopWorker } from "./synclib/worker.js";

const log = (logLevel: LogLevel, ...args: any[]) => {
if (LogLevel.indexOf(logLevel) >= LogLevel.indexOf(_config.logLevel ?? "WARN")) {
Expand Down Expand Up @@ -108,9 +107,8 @@ export const initAccelRecord = async (config: Config) => {
_config.logLevel ??= "WARN";
if (_config.type == "postgresql") _config.type = "pg";

_rpcClient = SyncRpc(path.resolve(__dirname, "./worker.cjs"), {
knexConfig: getKnexConfig(config),
});
actions.init({ knexConfig: getKnexConfig(config) });
_rpcClient = 1;
await loadDmmf();
await loadI18n();

Expand Down Expand Up @@ -147,7 +145,7 @@ export const execSQL = (params: {
if (!_rpcClient || !_config) {
throw new Error("Please call initAccelRecord(config) first.");
}
const ret = _rpcClient(params);
const ret = actions.execSQL(params);
const time = Date.now() - startTime;
const color = /begin|commit|rollback/i.test(sql) ? "\x1b[36m" : "\x1b[32m";
log(params.logLevel ?? "DEBUG", ` \x1b[36mSQL(${time}ms) ${color}${sql}\x1b[39m`, bindings);
Expand All @@ -167,5 +165,5 @@ const formatByEngine = (ret: any) => {
};

export const stopRpcClient = () => {
stop();
stopWorker();
};
2 changes: 1 addition & 1 deletion packages/accel-record-core/src/synclib/worker.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable */
import Knex from "knex";
import fs from "fs";
import { launchSyncWorker } from ".";
import { launchSyncWorker } from "./index.js";

const log = (data: object) => {
fs.appendFile("query.log", JSON.stringify(data, null, 2) + "\n", (err) => {});
Expand Down

0 comments on commit cd8b2d6

Please sign in to comment.