@datastax/astra-mongoose
Version:
Astra's NodeJS Mongoose compatibility client
180 lines (179 loc) • 7.27 kB
TypeScript
import { Collection, MongooseCollectionOptions } from './collection';
import { AstraDbAdmin, CollectionDescriptor, CreateAstraKeyspaceOptions, CreateCollectionOptions, CreateDataAPIKeyspaceOptions, CreateTableDefinition, CreateTableOptions, DataAPIDbAdmin, DropCollectionOptions, DropTableOptions, ListCollectionsOptions, ListTablesOptions, LoggingEvent, RawDataAPIResponse, TableDescriptor, WithTimeout } from '@datastax/astra-db-ts';
import { CollectionsDb, TablesDb } from './db';
import { default as MongooseConnection } from 'mongoose/lib/connection';
import type { ConnectOptions, Mongoose, Model } from 'mongoose';
import { DataAPIClient } from '@datastax/astra-db-ts';
interface ConnectOptionsInternal extends ConnectOptions {
isTable?: boolean;
isAstra?: boolean;
_fireAndForget?: boolean;
username?: string;
password?: string;
autoIndex?: boolean;
autoCreate?: boolean;
sanitizeFilter?: boolean;
bufferCommands?: boolean;
debug?: boolean | ((name: string, fn: string, ...args: unknown[]) => void) | null;
logging?: LoggingEvent;
}
/**
* Extends Mongoose's Connection class to provide compatibility with Data API. Responsible for maintaining the
* connection to Data API.
*/
export declare class Connection extends MongooseConnection {
debugType: string;
initialConnection: Promise<Connection> | null;
client: DataAPIClient | null;
admin: AstraDbAdmin | DataAPIDbAdmin | null;
db: CollectionsDb | TablesDb | null;
keyspaceName: string | null;
config?: ConnectOptionsInternal;
baseUrl: string | null;
baseApiPath: string | null;
models: Record<string, Model<unknown>>;
_debug?: boolean | ((name: string, fn: string, ...args: unknown[]) => void) | null;
constructor(base: Mongoose);
/**
* Helper borrowed from Mongoose to wait for the connection to finish connecting. Because Mongoose
* supports creating a new connection, registering some models, and connecting to the database later.
* This method is private and should not be called by clients.
*
* #### Example:
* const conn = mongoose.createConnection();
* // This may call `createCollection()` internally depending on `autoCreate` option, even though
* // this connection hasn't connected to the database yet.
* conn.model('Test', mongoose.Schema({ name: String }));
* await conn.openUri(uri);
*
* @ignore
*/
_waitForClient(): Promise<void>;
/**
* Get a collection by name. Cached in `this.collections`.
* @param name
* @param options
*/
collection<DocType extends Record<string, unknown> = Record<string, unknown>>(name: string, options?: MongooseCollectionOptions): Collection<DocType>;
/**
* Create a new collection in the database
* @param name The name of the collection to create
* @param options
*/
createCollection<DocType extends Record<string, unknown> = Record<string, unknown>>(name: string, options?: CreateCollectionOptions<DocType>): Promise<import("@datastax/astra-db-ts").Collection<DocType, import("@datastax/astra-db-ts").FoundDoc<DocType>>>;
/**
* Get current debug setting, accounting for potential changes to global debug config (`mongoose.set('debug', true | false)`)
*/
get debug(): boolean | ((name: string, fn: string, ...args: unknown[]) => void) | null | undefined;
/**
* Create a new table in the database
* @param name
* @param definition
*/
createTable<DocType extends Record<string, unknown> = Record<string, unknown>>(name: string, definition: CreateTableDefinition, options?: Omit<CreateTableOptions, 'definition'>): Promise<import("@datastax/astra-db-ts").Table<DocType, Partial<import("@datastax/astra-db-ts").FoundRow<DocType>>, import("@datastax/astra-db-ts").FoundRow<DocType>>>;
/**
* Drop a collection from the database
* @param name
*/
dropCollection(name: string, options?: DropCollectionOptions): Promise<void>;
/**
* Drop a table from the database
* @param name The name of the table to drop
*/
dropTable(name: string, options?: DropTableOptions): Promise<void>;
/**
* Create a new keyspace.
*
* @param name The name of the keyspace to create
*/
createKeyspace(name: string, options?: CreateAstraKeyspaceOptions & CreateDataAPIKeyspaceOptions): Promise<void>;
/**
* Not implemented.
*
* @ignore
*/
dropDatabase(): Promise<void>;
/**
* List all collections in the database
*/
listCollections(options: ListCollectionsOptions & {
nameOnly: true;
}): Promise<string[]>;
listCollections(options?: ListCollectionsOptions & {
nameOnly?: false;
}): Promise<CollectionDescriptor[]>;
/**
* List all tables in the database
*/
listTables(options: ListTablesOptions & {
nameOnly: true;
}): Promise<string[]>;
listTables(options?: ListTablesOptions & {
nameOnly?: false;
}): Promise<TableDescriptor[]>;
/**
* Run an arbitrary Data API command on the database
* @param command The command to run
*/
runCommand(command: Record<string, unknown>): Promise<RawDataAPIResponse>;
/**
* List all keyspaces. Called "listDatabases" for Mongoose compatibility
*/
listDatabases(options?: WithTimeout<'keyspaceAdminTimeoutMs'>): Promise<{
databases: {
name: string;
}[];
}>;
/**
* Logic for creating a connection to Data API. Mongoose calls `openUri()` internally when the
* user calls `mongoose.create()` or `mongoose.createConnection(uri)`
*
* @param uri the connection string
* @param options
*/
openUri(uri: string, options?: ConnectOptionsInternal): Promise<this>;
/**
* Create an astra-db-ts client and corresponding objects: client, db, admin.
* @param uri the connection string
* @param options
*/
createClient(uri: string, options?: ConnectOptionsInternal): Promise<this>;
/**
* Not supported
*
* @param _client
* @ignore
*/
setClient(): void;
/**
* For consistency with Mongoose's API. `mongoose.createConnection(uri)` returns the connection, **not** a promise,
* so the Mongoose pattern to call `createConnection()` and wait for connection to succeed is
* `await createConnection(uri).asPromise()`
*/
asPromise(): Promise<Connection> | null;
/**
* Not supported
*
* @ignore
*/
startSession(): void;
/**
* Mongoose calls `doClose()` to close the connection when the user calls `mongoose.disconnect()` or `conn.close()`.
* Handles closing the astra-db-ts client.
* This method is private and should not be called by clients directly. Mongoose will call this method internally when
* the user calls `mongoose.disconnect()` or `conn.close()`.
*
* @returns Client
* @ignore
*/
doClose(): this;
}
interface ParsedUri {
baseUrl: string;
baseApiPath: string;
keyspaceName: string;
applicationToken?: string;
authHeaderName?: string;
}
export declare const parseUri: (uri: string) => ParsedUri;
export {};