UNPKG

@dataql/node

Version:

DataQL core SDK for unified data management with MongoDB and GraphQL - Production Multi-Cloud Ready

228 lines (227 loc) 7.1 kB
import { CustomRequestConnection } from "./types.js"; import { DocumentScope } from "./DocumentScope.js"; import { Plugin, PluginType, DataQLHook, HookData } from "./plugins/index.js"; import { GlobalDataQLConfig } from "./config.js"; export type DataOptions = { dbName?: string; env?: "dev" | "prod"; devPrefix?: string; appToken?: string; /** Database connection string - for users who want to provide their own MongoDB URI instead of using DataQL's Database-as-a-Service */ connectionString?: string; customConnection?: CustomRequestConnection; globalConfig?: GlobalDataQLConfig; }; export interface DataCollection { name: string; schema: any; options?: { indexes?: any[]; }; get $type(): any; /** * Document-scoped API: Call with a filter to get a DocumentScope for subdocument operations * Example: users({ id: userId }).addresses.create(addressData) */ (filter: any): DocumentScope; /** * Create one or many documents. Pass an object for single, or array for many. */ create(doc: any | any[]): Promise<{ insertedId?: any; insertedIds?: any[]; result?: any; } | any[]>; /** * Create a unique document. Returns existing document if found, creates new one if not. * Comparison excludes ID fields and subdocument fields. */ createUnique(doc: any): Promise<{ insertedId?: any; result?: any; isExisting?: boolean; }>; /** * Insert one or many documents (alias for create). Pass an object for single, or array for many. */ insert(doc: any | any[]): Promise<{ insertedId?: any; insertedIds?: any[]; result?: any; } | any[]>; /** * Find documents. Pass a filter object or array of filters. */ find(filter?: any | any[]): Promise<any[]>; /** * Update one or many documents. Pass an object for single, or array for many. */ update(filter: any | any[], update?: any, upsert?: boolean): Promise<any>; /** * Upsert (insert or update) one or many documents. Pass an object for single, or array for many. */ upsert(doc: any | any[]): Promise<any>; /** * Delete one or many documents. Pass an object for single, or array for many. */ delete(filter: any | any[]): Promise<any>; } export declare class Data { private appToken; private env; private devPrefix; private dbName?; private connectionString?; private collections; private schemaHashCache; private deviceId?; private customConnection?; private pluginManager; private globalConfig; private bestEndpointCache?; constructor(options?: DataOptions); private getDeviceId; private getBestEndpoint; private fetchWithCustomConnection; collection(name: string, schema: any, options?: { indexes?: any[]; }): DataCollection; transaction<T = void>(fn: (tx: { [K in keyof typeof this.collections]: { create: (doc: any) => Promise<void>; update: (filter: any, update: any) => Promise<void>; upsert: (doc: any) => Promise<void>; delete: (filter: any) => Promise<void>; }; }) => Promise<T>): Promise<T>; /** * Introspect a database and generate DataQL schemas automatically * Routes through DataQL's infrastructure: SDK → Worker → Lambda → Database */ introspect(databaseUrl: string, options?: { databaseName?: string; sampleSize?: number; maxDepth?: number; includeIndexes?: boolean; excludeCollections?: string[]; includeCollections?: string[]; }): Promise<{ success: boolean; data?: any; error?: string; schemas?: Record<string, any>; }>; /** * Migrate entire database to DataQL cloud infrastructure in one click * Routes through DataQL's infrastructure: SDK → Worker → Lambda → Database */ migrateToCloud(databaseUrl: string, options?: { sampleSize?: number; maxDepth?: number; includeIndexes?: boolean; excludeCollections?: string[]; includeCollections?: string[]; batchSize?: number; validateData?: boolean; preserveIds?: boolean; createBackup?: boolean; }): Promise<{ success: boolean; migrationId?: string; cloudDatabase?: { name: string; connectionString: string; region: string; created: string; }; migration?: { collections: number; documents: number; duration: number; status: "completed" | "failed" | "partial"; errors?: string[]; }; schemas?: Record<string, any>; rollback?: { supported: boolean; instructions?: string; }; error?: string; }>; /** * Plugin System Methods */ /** * Register a plugin with DataQL * * @param plugin - The plugin to register * @param config - Plugin configuration * * @example * ```typescript * // Register an analytics plugin * await data.registerPlugin(analyticsPlugin, { * apiKey: "your-api-key", * endpoint: "https://analytics.example.com" * }); * * // Register a database adapter plugin * await data.registerPlugin(postgresAdapter); * ``` */ registerPlugin(plugin: Plugin, config?: Record<string, any>): Promise<void>; /** * Unregister a plugin * * @param pluginId - ID of the plugin to unregister */ unregisterPlugin(pluginId: string): Promise<void>; /** * Get a registered plugin * * @param pluginId - ID of the plugin to get */ getPlugin(pluginId: string): Plugin | undefined; /** * Get all registered plugins */ getPlugins(): Plugin[]; /** * Get plugins by type * * @param type - Plugin type to filter by */ getPluginsByType(type: PluginType): Plugin[]; /** * Check if a plugin is registered * * @param pluginId - ID of the plugin to check */ hasPlugin(pluginId: string): boolean; /** * Initialize all registered plugins */ initializePlugins(): Promise<void>; /** * Get plugin system statistics */ getPluginStats(): Record<string, any>; /** * Execute a hook with all registered handlers * * @param hook - The hook to execute * @param data - Data to pass to hook handlers */ executeHook<T extends DataQLHook>(hook: T, data: HookData[T]): Promise<HookData[T]>; /** * Get extension methods from plugins */ getExtensions(): Record<string, any>; /** * Extend the Data class with plugin methods dynamically * * This method allows plugins to add new methods to the Data class * instance, enabling seamless integration of plugin functionality. */ applyExtensions(): Promise<void>; }