@hot-updater/plugin-core
Version:
React Native OTA solution for self-hosted
76 lines (75 loc) • 2.5 kB
text/typescript
import { DatabasePlugin, DatabasePluginHooks, PaginationInfo } from "./types/index.cjs";
import { Bundle } from "@hot-updater/core";
//#region src/createDatabasePlugin.d.ts
interface AbstractDatabasePlugin {
getBundleById: (bundleId: string) => Promise<Bundle | null>;
getBundles: (options: {
where?: {
channel?: string;
platform?: string;
};
limit: number;
offset: number;
}) => Promise<{
data: Bundle[];
pagination: PaginationInfo;
}>;
getChannels: () => Promise<string[]>;
onUnmount?: () => Promise<void>;
commitBundle: (params: {
changedSets: {
operation: "insert" | "update" | "delete";
data: Bundle;
}[];
}) => Promise<void>;
}
/**
* Database plugin methods without name
*/
type DatabasePluginMethods = Omit<AbstractDatabasePlugin, never>;
/**
* Factory function that creates database plugin methods
*/
type DatabasePluginFactory<TConfig> = (config: TConfig) => DatabasePluginMethods;
/**
* Configuration options for creating a database plugin
*/
interface CreateDatabasePluginOptions<TConfig> {
/**
* The name of the database plugin (e.g., "postgres", "d1Database")
*/
name: string;
/**
* Function that creates the database plugin methods
*/
factory: DatabasePluginFactory<TConfig>;
}
/**
* Creates a database plugin with lazy initialization and automatic hook execution.
*
* This factory function abstracts the double currying pattern used by all database plugins,
* ensuring consistent lazy initialization behavior across different database providers.
* Hooks are automatically executed at appropriate times without requiring manual invocation.
*
* @param options - Configuration options for the database plugin
* @returns A double-curried function that lazily initializes the database plugin
*
* @example
* ```typescript
* export const postgres = createDatabasePlugin<PostgresConfig>({
* name: "postgres",
* factory: (config) => {
* const db = new Kysely(config);
* return {
* async getBundleById(bundleId) { ... },
* async getBundles(options) { ... },
* async getChannels() { ... },
* async commitBundle({ changedSets }) { ... }
* };
* }
* });
* ```
*/
declare function createDatabasePlugin<TConfig>(options: CreateDatabasePluginOptions<TConfig>): (config: TConfig, hooks?: DatabasePluginHooks) => (() => DatabasePlugin);
//#endregion
export { AbstractDatabasePlugin, CreateDatabasePluginOptions, createDatabasePlugin };