UNPKG

react-native-sst-storage-db

Version:

A powerful file-based MongoDB-like database for React Native. Single JSON file storage with collections, advanced querying, indexing, and atomic operations. No AsyncStorage dependency.

178 lines (150 loc) 4.77 kB
/** * TypeScript definitions for react-native-sst-storage-db */ export interface StorageOptions { databaseName?: string; databasePath?: string; autoSave?: boolean; enableIndexing?: boolean; enableLogging?: boolean; maxCollections?: number; maxDocuments?: number; backupCount?: number; encryptData?: boolean; compression?: boolean; } export interface StorageInfo { initialized: boolean; collections: { count: number; names: string[]; totalDocuments: number; }; keyValuePairs: number; options: StorageOptions; databasePath: string; } export interface Document { _id: string; createdAt: string; updatedAt: string; [key: string]: any; } export interface QueryOptions { sort?: Record<string, 1 | -1>; limit?: number; skip?: number; projection?: Record<string, 0 | 1>; } export interface UpdateOptions { multi?: boolean; multiple?: boolean; } export interface UpdateResult { modifiedCount: number; } export interface DeleteResult { deletedCount: number; deletedDoc?: Document; } export interface QueryOperators { $eq?: any; $ne?: any; $lt?: number | string | Date; $lte?: number | string | Date; $gt?: number | string | Date; $gte?: number | string | Date; $in?: any[]; $nin?: any[]; $exists?: boolean; $regex?: string; $size?: number; } export type Query = Record<string, any | QueryOperators>; export interface UpdateOperators { $set?: Record<string, any>; $unset?: Record<string, any>; $inc?: Record<string, number>; $push?: Record<string, any>; $pull?: Record<string, any>; } export type UpdateData = Record<string, any> | UpdateOperators; export interface ExportData { timestamp: string; version: string; metadata: DatabaseMetadata; collections: Record<string, Document[]>; keyValueStore: Record<string, any>; indexes: Record<string, Record<string, Record<string, string[]>>>; } export interface DatabaseMetadata { version: string; created: string; lastModified: string; collections: string[]; schema: Record<string, any>; } export class Collection { constructor(name: string, storage: SSTStorage); // Create operations insert(document: Record<string, any>): Promise<Document>; insertMany(documents: Record<string, any>[]): Promise<Document[]>; // Read operations find(query?: Query, options?: QueryOptions): Promise<Document[]>; findOne(query?: Query, options?: QueryOptions): Promise<Document | null>; findById(id: string): Promise<Document | null>; count(query?: Query): Promise<number>; // Update operations update(query: Query, updateData: UpdateData, options?: UpdateOptions): Promise<UpdateResult>; updateOne(query: Query, updateData: UpdateData): Promise<UpdateResult>; updateMany(query: Query, updateData: UpdateData): Promise<UpdateResult>; updateById(id: string, updateData: UpdateData): Promise<Document | null>; // Delete operations delete(query: Query): Promise<DeleteResult>; deleteOne(query: Query): Promise<DeleteResult>; deleteById(id: string): Promise<Document | null>; clear(): Promise<DeleteResult>; // Aggregation operations aggregate(pipeline: Record<string, any>[]): Promise<Document[]>; distinct(field: string): Promise<any[]>; groupBy(field: string): Promise<Record<string, Document[]>>; // Indexing operations createIndex(field: string): Promise<boolean>; dropIndex(field: string): Promise<boolean>; getIndexStats(): Record<string, { keyCount: number; totalDocuments: number }>; rebuildIndexes(): Promise<boolean>; } export class SSTStorage { constructor(options?: StorageOptions); // Initialization initialize(): Promise<boolean>; isInitialized(): boolean; // Key-value operations set(key: string, value: any): Promise<boolean>; get(key: string): Promise<any>; remove(key: string): Promise<boolean>; getAllKeys(): Promise<string[]>; getAll(): Promise<Record<string, any>>; has(key: string): Promise<boolean>; // Collection operations collection(name: string): Collection; getCollectionNames(): string[]; dropCollection(name: string): Promise<boolean>; // Utility methods getInfo(): Promise<StorageInfo>; clear(): Promise<boolean>; exportData(): Promise<ExportData>; importData(data: ExportData): Promise<boolean>; } // Default export export default SSTStorage; // Named exports export { SSTStorage }; export { Collection }; declare module 'react-native-sst-storage-db' { export = { SSTStorage: typeof SSTStorage; Collection: typeof Collection; default: typeof SSTStorage; }; }