Files
low-code-react-app-b/packages/spark-tools/dist/lib/db/db.d.ts
2026-01-17 22:16:42 +00:00

65 lines
2.5 KiB
TypeScript

import { z } from 'zod';
import { KVClient } from '../kv';
/**
* Document type that combines a schema type with an _id field
*/
export type Document<T> = T & {
_id: string;
};
/**
* DBClient provides methods to interact with Spark's document database.
*/
export declare class DBClient {
private kv;
constructor(kvClient?: KVClient);
/**
* Generate a unique document ID using ULID
* @returns A unique document ID
*/
generateDocId(): string;
/**
* Get all documents in a collection using the DB API
* @param collectionName The name of the collection
* @returns Array of all documents in the collection with id field
*/
getAll<T>(collectionName: string): Promise<Document<T>[]>;
/**
* Insert a document into a collection with schema validation
* @param collectionName The name of the collection
* @param schema The Zod schema for validation
* @param data The document data to insert
* @returns The inserted document with generated id
*/
insert<T extends z.ZodType>(collectionName: string, schema: T, data: z.infer<T>): Promise<Document<z.infer<T>>>;
/**
* Get a document by ID from a collection
* @param collectionName The name of the collection
* @param id The document ID
* @returns The document with id field or null if not found
*/
get<T>(collectionName: string, id: string): Promise<Document<T> | null>;
/**
* Update a document with partial data and schema validation
* @param collectionName The name of the collection
* @param id The document ID
* @param schema The Zod schema for validation
* @param data Partial data to update
* @returns The updated document or null if not found
*/
update<T extends z.ZodType>(collectionName: string, id: string, schema: T, data: Partial<z.infer<T>>): Promise<Document<z.infer<T>> | null>;
/**
* Delete a document from a collection
* @param collectionName The name of the collection
* @param id The document ID to delete
* @returns true if document was deleted, false if not found
*/
delete(collectionName: string, id: string): Promise<boolean>;
/**
* Query documents with filtering
* @param collectionName The name of the collection
* @param filterFn Function to filter documents
* @returns Array of filtered documents
*/
query<T>(collectionName: string, filterFn: (doc: Document<T>) => boolean): Promise<Document<T>[]>;
}