UNPKG

durable-objects-nosql

Version:

MongoDB-style NoSQL interface for Cloudflare Worker Durable Objects

149 lines (146 loc) 3.88 kB
/** * Types for the Durable Objects NoSQL package */ /** * MongoDB-style query operators */ type QueryOperator = '$eq' | '$gt' | '$gte' | '$lt' | '$lte' | '$ne' | '$in' | '$nin' | '$and' | '$or' | '$not'; /** * MongoDB-style update operators */ type UpdateOperator = '$set' | '$unset' | '$inc' | '$push' | '$pull'; /** * MongoDB-style query object */ type Query = { [key: string]: any | { [key in QueryOperator]?: any; }; }; /** * MongoDB-style update object */ type Update = { [key in UpdateOperator]?: { [field: string]: any; }; }; /** * Result of an insert operation */ interface InsertOneResult { id: string; } /** * Result of a bulk insert operation */ interface InsertManyResult { ids: string[]; } /** * Result of an update operation */ interface UpdateResult { matchedCount: number; modifiedCount: number; } /** * Result of a delete operation */ interface DeleteResult { deletedCount: number; } /** * Options for find operations */ interface FindOptions { limit?: number; skip?: number; sort?: { [key: string]: 1 | -1; }; } /** * Cursor for iterating over query results */ interface Cursor<T = any> { toArray(): Promise<T[]>; first(): Promise<T | null>; count(): Promise<number>; limit(n: number): Cursor<T>; skip(n: number): Cursor<T>; sort(sortSpec: { [key: string]: 1 | -1; }): Cursor<T>; } /** * Collection interface for MongoDB-style operations */ interface Collection<T = any> { find(query?: Query, options?: FindOptions): Cursor<T>; findOne(query?: Query): Promise<T | null>; insertOne(document: T): Promise<InsertOneResult>; insertMany(documents: T[]): Promise<InsertManyResult>; updateOne(query: Query, update: Update): Promise<UpdateResult>; updateMany(query: Query, update: Update): Promise<UpdateResult>; deleteOne(query: Query): Promise<DeleteResult>; deleteMany(query: Query): Promise<DeleteResult>; } /** * Transaction interface for Durable Object Storage */ interface DurableObjectTransaction { get<T = unknown>(key: string): Promise<T | undefined>; get<T = unknown>(keys: string[]): Promise<Map<string, T>>; put<T>(key: string, value: T): Promise<void>; put<T>(entries: Record<string, T>): Promise<void>; delete(key: string): Promise<boolean>; delete(keys: string[]): Promise<number>; } /** * Cloudflare Worker DurableObjectStorage interface (partial) */ interface DurableObjectStorage { sql: { exec<T>(query: string, ...bindings: any[]): { next(): { done?: boolean; value?: T; }; }; }; transaction<T>(closure: (txn: DurableObjectTransaction) => Promise<T>): Promise<T>; get<T = unknown>(key: string): Promise<T | undefined>; get<T = unknown>(keys: string[]): Promise<Map<string, T>>; put<T>(key: string, value: T): Promise<void>; put<T>(entries: Record<string, T>): Promise<void>; delete(key: string): Promise<boolean>; delete(keys: string[]): Promise<number>; } /** * Main class for MongoDB-style NoSQL interface for Durable Objects */ declare class DurableObjectsNoSQL { private storage; private collections; constructor(storage: DurableObjectStorage); /** * Get a collection by name */ collection<T = any>(name: string): Collection<T>; /** * Initialize the database schema */ private initializeDatabase; } /** * Proxy handler for collection access */ declare const handler: { get(target: DurableObjectsNoSQL, prop: string): any; }; /** * Create a new DurableObjectsNoSQL instance with collection proxy */ declare function createNoSQLClient(storage: DurableObjectStorage): DurableObjectsNoSQL; export { DurableObjectsNoSQL, createNoSQLClient, createNoSQLClient as default, handler };