Skip to content

Commit

Permalink
sync config
Browse files Browse the repository at this point in the history
  • Loading branch information
koyopro committed Dec 11, 2024
1 parent 51e4c77 commit a8eeea6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
10 changes: 7 additions & 3 deletions packages/accel-record-core/src/database.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Knex from "knex";
import { buildSyncClient } from "./database/sync.js";
import { buildSyncClient, SyncClient } from "./database/sync.js";
import { loadDmmf } from "./fields.js";
import { Model } from "./index.js";
import { loadI18n } from "./model/naming.js";
Expand Down Expand Up @@ -94,17 +94,21 @@ export interface Config {
* A function to transform the SQL before executing.
*/
sqlTransformer?: (sql: string) => string;

sync?: "thread" | "process";
}
let _config: Config = { type: "sqlite" };

const _rpcClient = buildSyncClient();
let _rpcClient: SyncClient | undefined;

let _queryCount: number = 0;
export const initAccelRecord = async (config: Config) => {
_config = Object.assign({}, config);
_config.logLevel ??= "WARN";
_config.sync ??= "thread";
if (_config.type == "postgresql") _config.type = "pg";

_rpcClient ||= buildSyncClient(_config.sync);
_rpcClient.launchWorker({ knexConfig: getKnexConfig(config) });
await loadDmmf();
await loadI18n();
Expand Down Expand Up @@ -162,5 +166,5 @@ const formatByEngine = (ret: any) => {
};

export const stopRpcClient = () => {
_rpcClient.stopWorker();
_rpcClient?.stopWorker();
};
9 changes: 2 additions & 7 deletions packages/accel-record-core/src/database/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { client } from "../synclib/worker.js";
// @ts-ignore
import SyncRpc, { stop } from "../sync-rpc/index.js";

export const buildSyncClient = (): SyncClient => {
return canSyncActions() ? new ThreadSyncClient() : new ProcessSyncClient();
export const buildSyncClient = (type: "thread" | "process"): SyncClient => {
return type == "process" ? new ProcessSyncClient() : new ThreadSyncClient();
};

export interface SyncClient {
Expand Down Expand Up @@ -42,8 +42,3 @@ export class ThreadSyncClient implements SyncClient {
client.stopWorker();
}
}

const canSyncActions = () => {
const v = Number(process.versions?.node?.split(".")?.[0]);
return isFinite(v) ? v >= 22 : false;
};
4 changes: 3 additions & 1 deletion tests/vitest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ export const dbConfig = () => {
};

beforeAll(async () => {
await initAccelRecord(dbConfig());
const v = Number(process.versions?.node?.split(".")?.[0]);
const sync = isFinite(v) && v >= 22 ? "thread" : "process";
await initAccelRecord({ ...dbConfig(), sync });
await Migration.migrate();
});

Expand Down

0 comments on commit a8eeea6

Please sign in to comment.