UNPKG

@datastax/astra-mongoose

Version:

Astra's NodeJS Mongoose compatibility client

159 lines (158 loc) 8 kB
import { AlterTypeOptions, Collection, Collection as AstraCollection, CollectionDescriptor, CollectionOptions, CreateCollectionOptions, CreateTableDefinition, CreateTableOptions, CreateTypeDefinition, Db as AstraDb, DropCollectionOptions, DropTableOptions, DropTypeOptions, ListCollectionsOptions, ListTablesOptions, RawDataAPIResponse, SomeRow, Table as AstraTable, TableDescriptor, TableOptions, TypeDescriptor } from '@datastax/astra-db-ts'; /** * Defines the base database class for interacting with Astra DB. Responsible for creating collections and tables. * This class abstracts the operations for both collections mode and tables mode. There is a separate TablesDb class * for tables and CollectionsDb class for collections. */ export declare abstract class BaseDb { astraDb: AstraDb; /** * Whether we're using "tables mode" or "collections mode". If tables mode, then `collection()` returns * a Table instance, **not** a Collection instance. Also, if tables mode, `createCollection()` throws an * error for Mongoose `syncIndexes()` compatibility reasons. */ isTable: boolean; name: string; constructor(astraDb: AstraDb, keyspaceName: string, isTable?: boolean); /** * Get a collection by name. * @param name The name of the collection. */ abstract collection<DocType extends Record<string, unknown> = Record<string, unknown>>(name: string, options: Record<string, unknown>): AstraCollection<DocType> | AstraTable<DocType>; /** * Create a new collection with the specified name and options. * @param name The name of the collection to be created. * @param options Additional options for creating the collection. */ abstract createCollection<DocType extends Record<string, unknown> = Record<string, unknown>>(name: string, options?: CreateCollectionOptions<DocType>): Promise<Collection<DocType>>; /** * Create a new table with the specified name and definition * @param name * @param definition */ createTable<DocType extends Record<string, unknown> = Record<string, unknown>>(name: string, definition: CreateTableDefinition, options?: Omit<CreateTableOptions, 'definition'>): Promise<AstraTable<DocType, Partial<import("@datastax/astra-db-ts").FoundRow<DocType>>, import("@datastax/astra-db-ts").FoundRow<DocType>>>; /** * Drop a collection by name. * @param name The name of the collection to be dropped. */ dropCollection(name: string, options?: DropCollectionOptions): Promise<void>; /** * Drop a table by name. This function does **not** throw an error if the table does not exist. * @param name */ dropTable(name: string, options?: DropTableOptions): Promise<void>; /** * List all collections in the database. * @param options Additional options for listing collections. */ 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[]>; /** * List all user-defined types (UDTs) in the database. * @returns An array of type descriptors. */ listTypes(options: { nameOnly: true; }): Promise<string[]>; listTypes(options?: { nameOnly?: false; }): Promise<TypeDescriptor[]>; /** * Create a new user-defined type (UDT) with the specified name and fields definition. * @param name The name of the type to create. * @param definition The definition of the fields for the type. * @returns The result of the createType command. */ createType(name: string, definition: CreateTypeDefinition): Promise<void>; /** * Drop (delete) a user-defined type (UDT) by name. * @param name The name of the type to drop. * @returns The result of the dropType command. */ dropType(name: string, options?: DropTypeOptions): Promise<void>; /** * Alter a user-defined type (UDT) by renaming or adding fields. * @param name The name of the type to alter. * @param update The alterations to be made: renaming or adding fields. * @returns The result of the alterType command. */ alterType<UDTSchema extends SomeRow = SomeRow>(name: string, update: AlterTypeOptions<UDTSchema>): Promise<void>; /** * Synchronizes the set of user-defined types (UDTs) in the database. It makes existing types in the database * match the list provided by `types`. New types that are missing are created, and types that exist in the database * but are not in the input list are dropped. If a type is present in both, we add all the new type's fields to the existing type. * * @param types An array of objects each specifying the name and CreateTypeDefinition for a UDT to synchronize. * @returns An object describing which types were created, updated, or dropped. * @throws {AstraMongooseError} If an error occurs during type synchronization, with partial progress information in the error. */ syncTypes(types: { name: string; definition: CreateTypeDefinition; }[]): Promise<{ created: string[]; updated: string[]; dropped: string[]; }>; /** * Execute a command against the database. * @param command The command to be executed. */ command(command: Record<string, unknown>): Promise<RawDataAPIResponse>; } /** * Db instance that creates and manages collections. * @extends BaseDb */ export declare class CollectionsDb extends BaseDb { /** * Creates an instance of CollectionsDb. Do not instantiate this class directly. * @param astraDb The AstraDb instance to interact with the database. * @param keyspaceName The name of the keyspace to use. */ constructor(astraDb: AstraDb, keyspaceName: string); /** * Get a collection by name. * @param name The name of the collection. */ collection<DocType extends Record<string, unknown> = Record<string, unknown>>(name: string, options: CollectionOptions): Collection<DocType, import("@datastax/astra-db-ts").FoundDoc<DocType>>; /** * Send a CreateCollection command to Data API. */ createCollection<DocType extends Record<string, unknown> = Record<string, unknown>>(name: string, options?: CreateCollectionOptions<DocType>): Promise<Collection<DocType, import("@datastax/astra-db-ts").FoundDoc<DocType>>>; } /** * Db instance that creates and manages tables. * @extends BaseDb */ export declare class TablesDb extends BaseDb { /** * Creates an instance of TablesDb. Do not instantiate this class directly. * @param astraDb The AstraDb instance to interact with the database. * @param keyspaceName The name of the keyspace to use. */ constructor(astraDb: AstraDb, keyspaceName: string); /** * Get a table by name. This method is called `collection()` for compatibility with Mongoose, which calls * this method for getting a Mongoose Collection instance, which may map to a table in Astra DB when using tables mode. * @param name The name of the table. */ collection<DocType extends Record<string, unknown> = Record<string, unknown>>(name: string, options: TableOptions): AstraTable<DocType, Partial<import("@datastax/astra-db-ts").FoundRow<DocType>>, import("@datastax/astra-db-ts").FoundRow<DocType>>; /** * Throws an error, astra-mongoose does not support creating collections in tables mode. */ createCollection<DocType extends Record<string, unknown> = Record<string, unknown>>(name: string, options?: CreateCollectionOptions<DocType>): Promise<Collection<DocType>>; }