-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
103 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { getKnexConfig, LogLevel } from "../database.js"; | ||
import { sync } from "../synclib/worker.js"; | ||
// @ts-ignore | ||
import SyncRpc, { stop } from "../sync-rpc/index.js"; | ||
|
||
export const buildSyncClient = (type: "thread" | "process"): SyncClient => { | ||
return type == "process" ? new ProcessSyncClient() : new ThreadSyncClient(); | ||
}; | ||
|
||
export interface SyncClient { | ||
launchWorker(params: { knexConfig: ReturnType<typeof getKnexConfig> }): void; | ||
execSQL(params: { sql: string; bindings: readonly any[]; logLevel?: LogLevel }): any; | ||
stopWorker(): void; | ||
} | ||
|
||
export class ProcessSyncClient implements SyncClient { | ||
private _rpc: any; | ||
launchWorker(params: { knexConfig: ReturnType<typeof getKnexConfig> }): void { | ||
if (this._rpc) return; | ||
this._rpc = SyncRpc(new URL("../worker.cjs", import.meta.url).pathname, params); | ||
} | ||
execSQL(params: { sql: string; bindings: readonly any[]; logLevel?: LogLevel }): any { | ||
return this._rpc(params); | ||
} | ||
stopWorker() { | ||
stop(); | ||
} | ||
} | ||
|
||
export class ThreadSyncClient implements SyncClient { | ||
private _rpc: ReturnType<typeof sync.launch> | undefined; | ||
|
||
launchWorker(params: { knexConfig: ReturnType<typeof getKnexConfig> }): void { | ||
if (this._rpc) return; | ||
this._rpc = sync.launch(); | ||
this._rpc.actions.init(params); | ||
} | ||
execSQL(params: { sql: string; bindings: readonly any[]; logLevel?: LogLevel }): any { | ||
return this._rpc?.actions?.execSQL(params); | ||
} | ||
stopWorker() { | ||
this._rpc?.worker.terminate(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* eslint-disable */ | ||
import fs from "fs"; | ||
import Knex from "knex"; | ||
import { defineSyncWorker } from "sync-actions"; | ||
|
||
const log = (data: object) => { | ||
fs.appendFile("query.log", JSON.stringify(data, null, 2) + "\n", (err) => {}); | ||
}; | ||
|
||
let knex: Knex.Knex; | ||
let knexConfig: any; | ||
|
||
function init(connection: { knexConfig: any }) { | ||
knexConfig = connection.knexConfig; | ||
knex = Knex(knexConfig); | ||
} | ||
|
||
function execSQL(query: { sql: string; bindings: readonly any[] }) { | ||
const { sql, bindings } = query; | ||
return knexConfig.client === "pg" | ||
? postgresPromise(knex, sql, bindings) | ||
: knex.raw(sql, bindings); | ||
} | ||
|
||
function postgresPromise(knex: Knex.Knex, sql: string, bindings: readonly any[]) { | ||
return new Promise(async (resolve, error) => { | ||
try { | ||
const { command, rowCount, rows } = await knex.raw(sql, bindings); | ||
return resolve({ command, rows, rowCount }); | ||
} catch (err) { | ||
return error(err); | ||
} | ||
}); | ||
} | ||
|
||
export const sync = defineSyncWorker(import.meta.filename, { | ||
init, | ||
execSQL, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,10 @@ | ||
import { Model, initAccelRecord } from "accel-record"; | ||
import { initAccelRecord } from "accel-record"; | ||
import { dbConfig } from "../vitest.setup"; | ||
|
||
describe("initAccelRecord", () => { | ||
test("should not throw error even if called multiple times", async () => { | ||
const subject = () => initAccelRecord(dbConfig()); | ||
// for afterEach | ||
const restartTx = () => Model.startTransaction(); | ||
|
||
expect(async () => await subject()).not.toThrow(); | ||
restartTx(); | ||
expect(async () => await subject()).not.toThrow(); | ||
restartTx(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters