mirror of
https://github.com/fluxerapp/fluxer
synced 2026-10-17 13:05:32 +09:00
refactor(api): collapse single-impl interfaces and facades (#3382)
This commit is contained in:
1 parent
e7b13fd19f
commit
c16d414d93
426 files changed
+2085
-6445
No files matched your search
@@ -1,23 +0,0 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import type {types} from 'cassandra-driver';
|
||||
|
||||
type CassandraValue =
|
||||
| string
|
||||
| number
|
||||
| bigint
|
||||
| boolean
|
||||
| Buffer
|
||||
| Date
|
||||
| types.LocalDate
|
||||
| Set<unknown>
|
||||
| Map<unknown, unknown>
|
||||
| Array<unknown>
|
||||
| Record<string, unknown>
|
||||
| null;
|
||||
export type CassandraParams = Record<string, CassandraValue>;
|
||||
|
||||
export interface PreparedQuery<P extends CassandraParams = CassandraParams> {
|
||||
cql: string;
|
||||
params: P;
|
||||
}
|
||||
@@ -1,7 +1,5 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import type {CassandraParams, PreparedQuery} from '@pkgs/cassandra/src/CassandraTypes';
|
||||
import {type Logger, NoopLogger} from '@pkgs/cassandra/src/Logger';
|
||||
import cassandra from 'cassandra-driver';
|
||||
|
||||
const distance = cassandra.types.distance;
|
||||
@@ -22,45 +20,19 @@ interface CassandraConfig {
|
||||
readTimeoutMs?: number | undefined;
|
||||
}
|
||||
|
||||
interface CassandraClientOptions {
|
||||
logger?: Logger | undefined;
|
||||
}
|
||||
|
||||
interface CassandraExecuteOptions {
|
||||
prepare?: boolean | undefined;
|
||||
}
|
||||
|
||||
interface CassandraBatchOptions {
|
||||
prepare?: boolean | undefined;
|
||||
}
|
||||
|
||||
interface ICassandraClient {
|
||||
connect(): Promise<void>;
|
||||
shutdown(): Promise<void>;
|
||||
execute<P extends CassandraParams>(
|
||||
query: PreparedQuery<P>,
|
||||
options?: CassandraExecuteOptions,
|
||||
): Promise<cassandra.types.ResultSet>;
|
||||
batch(queries: Array<PreparedQuery>, options?: CassandraBatchOptions): Promise<void>;
|
||||
getNativeClient(): cassandra.Client;
|
||||
}
|
||||
|
||||
interface DefaultClientState {
|
||||
client: CassandraClient | null;
|
||||
logger: Logger;
|
||||
}
|
||||
|
||||
const defaultClientState: DefaultClientState = {
|
||||
client: null,
|
||||
logger: NoopLogger,
|
||||
};
|
||||
|
||||
class CassandraClient implements ICassandraClient {
|
||||
class CassandraClient {
|
||||
private readonly config: CassandraConfig;
|
||||
private logger: Logger;
|
||||
private client: cassandra.Client | null;
|
||||
|
||||
public constructor(config: CassandraConfig, options: CassandraClientOptions = {}) {
|
||||
public constructor(config: CassandraConfig) {
|
||||
this.config = {
|
||||
hosts: [...config.hosts],
|
||||
port: config.port,
|
||||
@@ -70,7 +42,6 @@ class CassandraClient implements ICassandraClient {
|
||||
password: config.password,
|
||||
readTimeoutMs: config.readTimeoutMs,
|
||||
};
|
||||
this.logger = options.logger ?? NoopLogger;
|
||||
this.client = null;
|
||||
}
|
||||
|
||||
@@ -109,14 +80,6 @@ class CassandraClient implements ICassandraClient {
|
||||
});
|
||||
await client.connect();
|
||||
this.client = client;
|
||||
this.logger.info(
|
||||
{
|
||||
hosts: this.config.hosts,
|
||||
keyspace: this.config.keyspace,
|
||||
local_dc: this.config.localDc,
|
||||
},
|
||||
'Connected to Cassandra',
|
||||
);
|
||||
}
|
||||
|
||||
public async shutdown(): Promise<void> {
|
||||
@@ -126,26 +89,6 @@ class CassandraClient implements ICassandraClient {
|
||||
const activeClient = this.client;
|
||||
this.client = null;
|
||||
await activeClient.shutdown();
|
||||
this.logger.info({}, 'Cassandra connection closed');
|
||||
}
|
||||
|
||||
public async execute<P extends CassandraParams>(
|
||||
query: PreparedQuery<P>,
|
||||
options: CassandraExecuteOptions = {},
|
||||
): Promise<cassandra.types.ResultSet> {
|
||||
return this.getNativeClient().execute(query.cql, query.params, {
|
||||
prepare: options.prepare ?? true,
|
||||
});
|
||||
}
|
||||
|
||||
public async batch(queries: Array<PreparedQuery>, options: CassandraBatchOptions = {}): Promise<void> {
|
||||
if (queries.length === 0) {
|
||||
return;
|
||||
}
|
||||
const batch = queries.map((query) => ({query: query.cql, params: query.params}));
|
||||
await this.getNativeClient().batch(batch, {
|
||||
prepare: options.prepare ?? true,
|
||||
});
|
||||
}
|
||||
|
||||
public getNativeClient(): cassandra.Client {
|
||||
@@ -160,7 +103,7 @@ export async function initCassandra(config: CassandraConfig): Promise<void> {
|
||||
if (defaultClientState.client !== null) {
|
||||
await defaultClientState.client.shutdown();
|
||||
}
|
||||
const client = new CassandraClient(config, {logger: defaultClientState.logger});
|
||||
const client = new CassandraClient(config);
|
||||
await client.connect();
|
||||
defaultClientState.client = client;
|
||||
}
|
||||
@@ -173,7 +116,7 @@ export async function shutdownCassandra(): Promise<void> {
|
||||
defaultClientState.client = null;
|
||||
}
|
||||
|
||||
function getDefaultCassandraClient(): ICassandraClient {
|
||||
function getDefaultCassandraClient(): CassandraClient {
|
||||
if (defaultClientState.client === null) {
|
||||
throw new Error('Cassandra client is not initialized. Call initCassandra() first.');
|
||||
}
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
export interface Logger {
|
||||
info(context: Record<string, unknown>, message: string): void;
|
||||
debug(context: Record<string, unknown>, message: string): void;
|
||||
error(context: Record<string, unknown>, message: string): void;
|
||||
}
|
||||
|
||||
export const NoopLogger: Logger = {
|
||||
info(): void {},
|
||||
debug(): void {},
|
||||
error(): void {},
|
||||
};
|
||||
Reference in new issue
Block a user