Skip to main content

@getriebe/persistence

Connect the storage adapters and their transaction interfaces.

Source revision 49560371f6cd34d30383df5619df46969c594ae4.

export declare function checkRecordKey(value: string): void;
export declare function decodeRecordValue(value: unknown): JsonValue;
export declare function decodeStoredRecords(rows: readonly Readonly<Record<string, unknown>>[]): readonly StoredRecord[];
export declare function encodeRecordValue(value: JsonValue): string;
export type JsonValue = null | boolean | number | string | readonly JsonValue[] | {
readonly [key: string]: JsonValue;
};
export declare function openPostgres(options: PostgresOptions): Promise<Persistence>;
export declare function openSqlite(options?: SqliteOptions): Promise<Persistence>;
export interface Persistence {
readonly capabilities: StorageCapabilities;
transaction<T>(work: (transaction: StorageTransaction) => Promise<T> | T): Promise<T>;
close(): Promise<void>;
}
export declare class PersistenceError extends Error {
readonly code: PersistenceErrorCode;
constructor(code: PersistenceErrorCode, message: string, options?: ErrorOptions);
}
export type PersistenceErrorCode = "conflict" | "unavailable" | "storage" | "commit-unknown" | "schema-incompatible" | "invalid-use";
export interface PostgresOptions {
readonly connectionString: string;
}
export declare function recordKey(namespace: string, id: string): readonly [string, string];
export declare function recordSchemaUpdates(rows: readonly Readonly<Record<string, unknown>>[]): readonly string[];
export declare const recordStoreSql: Readonly<{
createVersionTable: "CREATE TABLE IF NOT EXISTS getriebe_schema (\n singleton INTEGER PRIMARY KEY CHECK (singleton = 1),\n version INTEGER NOT NULL\n )";
readVersion: "SELECT version FROM getriebe_schema";
verifyRecordTable: "SELECT namespace, id, value FROM getriebe_record WHERE 1 = 0";
get: "SELECT value FROM getriebe_record WHERE namespace = $1 AND id = $2";
list: "SELECT id, value FROM getriebe_record WHERE namespace = $1";
put: "INSERT INTO getriebe_record (namespace, id, value) VALUES ($1, $2, $3)\n ON CONFLICT (namespace, id) DO UPDATE SET value = excluded.value";
delete: "DELETE FROM getriebe_record WHERE namespace = $1 AND id = $2";
}>;
export interface SqliteOptions {
readonly path?: string;
}
export interface StorageCapabilities {
readonly adapter: "sqlite" | "postgres";
readonly isolation: "serialized-sqlite-writers" | "serializable";
readonly survivesOrderedRestart: boolean;
}
export interface StorageTransaction {
get(namespace: string, id: string): Promise<JsonValue | undefined>;
list(namespace: string): Promise<readonly StoredRecord[]>;
put(namespace: string, id: string, value: JsonValue): Promise<void>;
delete(namespace: string, id: string): Promise<boolean>;
}
export interface StoredRecord {
readonly id: string;
readonly value: JsonValue;
}