@nic-jennings/sql-datasource
Version:
SQL DataSource with Batching and Caching Support
43 lines (42 loc) • 1.46 kB
TypeScript
import DataLoader from "dataloader";
import { Knex } from "knex";
import { Knex as KnexOriginal } from "knex";
import { KeyValueCache } from "@apollo/utils.keyvaluecache";
declare module "knex" {
namespace Knex {
type KnexConfig = {
client: string;
connection: string | undefined;
};
type BatchCallback = (query: Knex.QueryBuilder, keys: readonly any[]) => Promise<any[]>;
interface QueryBuilder {
cache<TRecord extends {}, TResult>(value: number): KnexOriginal.QueryBuilder<TRecord, TResult>;
batch(callback: BatchCallback): BatchedLoader;
}
}
}
export interface DataSourceKnex extends Knex {
cache?: Knex.QueryBuilder["cache"];
batch?: Knex.QueryBuilder["batch"];
}
export interface BatchedLoader<T = unknown, K = any> extends DataLoader<T, K> {
}
export declare type BatchedSQLDataSourceProps<Context = any> = {
knexConfig: Knex.KnexConfig | DataSourceKnex;
writeKnexConfig?: Knex.KnexConfig | DataSourceKnex;
cache?: KeyValueCache;
context?: Context;
};
export declare class BatchedSQLDataSource {
cache: BatchedSQLDataSourceProps["cache"];
context: BatchedSQLDataSourceProps["context"];
db: {
query: DataSourceKnex;
write: DataSourceKnex;
};
constructor(config: BatchedSQLDataSourceProps);
private _extendKnex;
private _connectToDatabase;
private _batchQuery;
private _cacheQuery;
}