import { AxiosInstance, AxiosResponse } from 'axios'; interface CouchbaseResponseError { code: number; msg: string; reason?: { scope: string; collection?: string; }; } interface CouchbaseResponse { requestID: string; status: string; metrics: { elapsedTime: string; executionTime: string; resultCount: number; resultSize: number; errorCount?: number; warningCount?: number; }; errors?: Array; warnings?: Array<{ code: number; msg: string; }>; results?: Array; } interface CouchbaseUtilsClientConstructorType { connStr: string; username: string; password: string; bucketName: string; scopeName: string; axiosInstance: AxiosInstance; } declare class CouchbaseUtilsClient { private readonly connStr; private readonly username; private readonly password; private readonly bucketName; private readonly scopeName; private readonly axiosInstance; constructor(data: CouchbaseUtilsClientConstructorType); /** * Processes the Axios response from a Couchbase query. * Throws an error if the Couchbase response contains errors. * @param axiosResponse The Axios response containing a CouchbaseResponse. * @returns The results from the CouchbaseResponse. * @throws Error if the CouchbaseResponse contains errors. */ protected getCouchbaseResponse(axiosResponse: AxiosResponse): any[] | undefined; /** * Sends a request to the Couchbase query service. * @param statement The N1QL query statement to execute. * @returns The results from the CouchbaseResponse. * @throws Error if the request fails or the CouchbaseResponse contains errors. */ protected sendRequest(statement: string): Promise; /** * Executes a raw N1QL query statement. * @param statement - The raw N1QL query statement to execute. * @returns The results from the CouchbaseResponse. * @throws Error if the request fails or the CouchbaseResponse contains errors. */ query(statement: string): Promise; /** * Creates a scope by sending the N1QL query in the POST body. * @param scopeName The name of the scope to be created within the bucket (e.g., "`user_config`"). * @returns The result of the create scope operation. * @throws Error if the operation fails. */ createScope(scopeName: string): Promise; /** * Creates a collection by sending the N1QL query in the POST body. * @param collectionName The fully qualified collection name (e.g., "`user_config`.`111`.`i18n`"). * @returns The result of the create collection operation. * @throws Error if the operation fails. */ createCollection(collectionName: string): Promise; /** * Inserts or updates a document in the specified collection. * @param collectionName The name of the collection (e.g., "`user_config`.`111`.`i18n`"). * @param documentKey The document key to use for the upsert operation. * @param data The document data to upsert. * @returns The result of the upsert operation. * @throws Error if the document key is missing or the upsert operation fails. */ upsert(collectionName: string, documentKey: string, data: any): Promise; /** * Updates a nested field inside a document in Couchbase using N1QL. * * @param {string} collectionName - The name of the collection containing the document. * @param {string} documentId - The unique ID of the document to update. * @param {string} fieldPath - The dot-separated path to the nested field (e.g., "user.profile.age"). * @param {any} newValue - The new value to set for the specified nested field. * @returns {Promise} - Resolves when the update is successful. * @throws {Error} - Throws an error if the document ID is missing or if the query execution fails. * * @example * await couchbaseClient.updateNestedValue( * 'users', * 'user123', * 'profile.age', * 30 * ); * // This updates the document with ID "user123", * // setting the "profile.age" field to 30. */ updateNestedValue(collectionName: string, documentId: string, fieldPath: string, // example: watch_lists.`دیدبان ۱` newValue: any): Promise; /** * Retrieves all documents from the specified collection. * @param collectionName - The name of the collection to retrieve documents from. * @returns An array of documents from the specified collection. * @throws Error if the retrieval operation fails. */ getAll(collectionName: string): Promise; /** * Retrieves a document by its ID from the specified collection. * @param collectionName - The fully qualified collection name (e.g., "`user_config`.`111`.`i18n`"). * @param documentId - The ID of the document to retrieve. * @returns The retrieved document. * @throws Error if the retrieval operation fails. */ get(collectionName: string, documentId: string): Promise; /** * Retrieves a nested field directly from Couchbase. * @param collectionName - The name of the collection (e.g., "dashboardConfig"). * @param documentId - The document key/ID. * @param fieldPath - The dot-separated nested field path (e.g., "b.c"). * @returns The value of the nested field. * @throws Error if the document or field is not found or the query fails. */ getNestedValue(collectionName: string, documentId: string, fieldPath: string): Promise; /** * Deletes a document from the specified collection by its ID. * @param collectionName - The name of the collection from which to delete the document. * @param documentId - The ID of the document to delete. * @returns A Promise that resolves with the result of the deletion operation. * @throws Error if the deletion operation fails, such as if the document does not exist. */ delete(collectionName: string, documentId: string): Promise; /** * Removes a nested field from a document in Couchbase using N1QL UNSET. * * @param {string} collectionName - The name of the collection containing the document. * @param {string} documentId - The unique ID of the document to update. * @param {string} fieldPath - The dot-separated path to the nested field (e.g., "user.profile.age"). * @returns {Promise} - Resolves when the field is removed successfully. * @throws {Error} - Throws an error if the document ID is missing or if the query execution fails. * * @example * await couchbaseClient.removeNestedField( * 'userConfig', * '5', * 'watch_lists.`تست1`' * ); * // This removes the key "تست1" from "watch_lists". */ removeNestedField(collectionName: string, documentId: string, fieldPath: string): Promise; /** * Inspects Couchbase response errors and determines how they should be handled. * * This method acts as an error classifier and lightweight recovery trigger. * It analyzes the `errors` array in the Couchbase response and performs * specific auto-healing actions when possible. * * Supported error handling: * - Authentication errors (code 2120): * Treated as fatal. No retry is allowed. * * - Scope errors (code 12021): * Automatically attempts to create the missing scope. * * - Collection errors (code 12003): * Automatically attempts to create the missing collection. * * Important behavior: * - This method does NOT perform retry control logic beyond indicating * whether a retry is allowed. * - It may perform side effects (such as creating missing scopes or collections). * - It is the caller's responsibility to decide whether to retry based on * the returned `retry` flag. * * @param errorData The Couchbase response object containing an optional `errors` * array with detailed error information returned from the query service. * * @returns A promise that resolves to an object indicating whether the * operation is safe to retry (`retry: true`) or should be considered final (`retry: false`). */ checkError(errorData: CouchbaseResponse): Promise<{ retry: boolean; }>; /** * Handles the error related to a scope creation failure by extracting and validating the scope name, * then attempting to create the scope if necessary. * @param scopeError The error response from Couchbase, typically containing information about the scope failure. * @returns void * @throws Error if the scope format is invalid or if the error does not contain necessary information. */ handleScopeError(scopeError: CouchbaseResponseError): Promise; /** * Handles errors related to missing collections by extracting the collection name * from the error message and attempting to create the collection. * * This method parses the error message to extract the collection name. If the * message format is invalid or the collection name is missing, an error is thrown. * If the collection creation fails, the error is rethrown after logging the issue. * * @param collectionError An object containing the error `code` and `msg` * from the Couchbase error response. * @throws Error if the error message is malformed or if the collection * creation fails. * @returns A promise that resolves when the collection is successfully created, * or an error is thrown if creation fails. */ handleCollectionError(collectionError: { code: number; msg: string; }): Promise; } export { CouchbaseUtilsClient, CouchbaseUtilsClient as default }; export type { CouchbaseResponse, CouchbaseResponseError, CouchbaseUtilsClientConstructorType };