@datastax/astra-mongoose
Version:
Astra's NodeJS Mongoose compatibility client
253 lines (252 loc) • 12.2 kB
TypeScript
import { default as MongooseCollection } from 'mongoose/lib/collection';
import type { Connection } from './connection';
import { Collection as AstraCollection, CollectionCountDocumentsOptions, CollectionDeleteManyOptions, CollectionDeleteOneOptions, CollectionEstimatedDocumentCountOptions, CollectionFindAndRerankOptions, CollectionFindOptions, CollectionFindOneAndDeleteOptions, CollectionFindOneAndReplaceOptions, CollectionFindOneAndUpdateOptions, CollectionFindOneOptions, CollectionInsertManyOptions, CollectionInsertOneOptions, CollectionOptions, CollectionReplaceOneOptions, CollectionUpdateFilter, CollectionUpdateManyOptions, CollectionUpdateOneOptions, CreateTableDefinition, CreateTableOptions, Filter, RunCommandOptions, StrictCreateTableColumnDefinition, Table as AstraTable, TableDeleteManyOptions, TableDeleteOneOptions, TableDropIndexOptions, TableFindOneOptions, TableFindOptions, TableInsertManyOptions, TableInsertOneOptions, TableOptions, TableIndexOptions, TableTextIndexOptions, TableUpdateFilter, TableUpdateOneOptions, TableVectorIndexOptions } from '@datastax/astra-db-ts';
import { SchemaOptions } from 'mongoose';
import { IndexSpecification } from 'mongodb';
export type MongooseSortOption = Record<string, 1 | -1 | {
$meta: Array<number>;
} | {
$meta: string;
}>;
type FindOptions = (Omit<CollectionFindOptions, 'sort'> | Omit<TableFindOptions, 'sort'>) & {
sort?: MongooseSortOption;
};
type FindOneOptions = (Omit<CollectionFindOneOptions, 'sort'> | Omit<TableFindOneOptions, 'sort'>) & {
sort?: MongooseSortOption;
};
type FindOneAndUpdateOptions = Omit<CollectionFindOneAndUpdateOptions, 'sort'> & {
sort?: MongooseSortOption;
includeResultMetadata?: boolean;
};
type FindOneAndDeleteOptions = Omit<CollectionFindOneAndDeleteOptions, 'sort'> & {
sort?: MongooseSortOption;
includeResultMetadata?: boolean;
};
type FindOneAndReplaceOptions = Omit<CollectionFindOneAndReplaceOptions, 'sort'> & {
sort?: MongooseSortOption;
includeResultMetadata?: boolean;
};
type DeleteOneOptions = (Omit<CollectionDeleteOneOptions, 'sort'> | Omit<TableDeleteOneOptions, 'sort'>) & {
sort?: MongooseSortOption;
};
type ReplaceOneOptions = Omit<CollectionReplaceOneOptions, 'sort'> & {
sort?: MongooseSortOption;
};
type UpdateOneOptions = (Omit<CollectionUpdateOneOptions, 'sort'> | Omit<TableUpdateOneOptions, 'sort'>) & {
sort?: MongooseSortOption;
};
interface AstraMongooseIndexDescription {
name: string;
definition: {
column: string | ({
[key: string]: '$keys' | '$values';
});
options?: TableIndexOptions | TableVectorIndexOptions;
};
key: Record<string, 1 | -1 | '$keys' | '$values'>;
}
export interface MongooseCollectionOptions {
schemaUserProvidedOptions?: SchemaOptions;
capped?: boolean;
modelName?: string;
autoCreate?: boolean;
}
/**
* Collection operations supported by the driver. This class is called "Collection" for consistency with Mongoose, because
* in Mongoose a Collection is the interface that Models and Queries use to communicate with the database. However, from
* an Astra perspective, this class can be a wrapper around a Collection **or** a Table depending on the corresponding db's
* `isTable` option. Needs to be a separate class because Mongoose only supports one collection class.
*/
export declare class Collection<DocType extends Record<string, unknown> = Record<string, unknown>> extends MongooseCollection {
debugType: string;
_collection?: AstraCollection<DocType> | AstraTable<DocType>;
_closed: boolean;
connection: Connection;
options?: (TableOptions | CollectionOptions) & MongooseCollectionOptions;
name: string;
constructor(name: string, conn: Connection, options?: (TableOptions | CollectionOptions) & MongooseCollectionOptions);
get collection(): AstraCollection | AstraTable<DocType>;
get isTable(): boolean | undefined;
/**
* Count documents in the collection that match the given filter.
* @param filter
*/
countDocuments(filter: Filter, options?: CollectionCountDocumentsOptions): Promise<number>;
/**
* Find documents in the collection that match the given filter.
* @param filter
* @param options
* @param callback
*/
find(filter: Filter, options: FindOptions): import("@datastax/astra-db-ts").TableFindCursor<DocType, import("@datastax/astra-db-ts").WithSim<import("@datastax/astra-db-ts").FoundDoc<import("@datastax/astra-db-ts").SomeDoc>>> | import("@datastax/astra-db-ts").CollectionFindCursor<DocType, import("@datastax/astra-db-ts").WithSim<import("@datastax/astra-db-ts").FoundDoc<import("@datastax/astra-db-ts").SomeDoc>>>;
/**
* Find a single document in the collection that matches the given filter.
* @param filter
* @param options
*/
findOne(filter: Filter, options?: FindOneOptions): Promise<DocType | null>;
/**
* Insert a single document into the collection.
* @param doc
*/
insertOne(doc: Record<string, unknown>, options?: CollectionInsertOneOptions | TableInsertOneOptions): Promise<import("@datastax/astra-db-ts").CollectionInsertOneResult<import("@datastax/astra-db-ts").FoundDoc<import("@datastax/astra-db-ts").SomeDoc>> | import("@datastax/astra-db-ts").TableInsertOneResult<Partial<import("@datastax/astra-db-ts").FoundRow<DocType>>>>;
/**
* Insert multiple documents into the collection.
* @param documents
* @param options
*/
insertMany(documents: Record<string, unknown>[], options?: CollectionInsertManyOptions | TableInsertManyOptions): Promise<import("@datastax/astra-db-ts").CollectionInsertManyResult<import("@datastax/astra-db-ts").FoundDoc<import("@datastax/astra-db-ts").SomeDoc>> | import("@datastax/astra-db-ts").TableInsertManyResult<Partial<import("@datastax/astra-db-ts").FoundRow<DocType>>>>;
/**
* Update a single document in a collection.
* @param filter
* @param update
* @param options
*/
findOneAndUpdate(filter: Filter, update: CollectionUpdateFilter<DocType>, options: FindOneAndUpdateOptions): Promise<DocType | {
value: DocType | null;
} | null>;
/**
* Find a single document in the collection and delete it.
* @param filter
* @param options
*/
findOneAndDelete(filter: Filter, options: FindOneAndDeleteOptions): Promise<DocType | {
value: DocType | null;
} | null>;
/**
* Find a single document in the collection and replace it.
* @param filter
* @param newDoc
* @param options
*/
findOneAndReplace(filter: Filter, newDoc: Record<string, unknown>, options: FindOneAndReplaceOptions): Promise<DocType | {
value: DocType | null;
} | null>;
/**
* Delete one or more documents in a collection that match the given filter.
* @param filter
*/
deleteMany(filter: Filter, options?: CollectionDeleteManyOptions | TableDeleteManyOptions): Promise<void | import("@datastax/astra-db-ts").GenericDeleteManyResult>;
/**
* Delete a single document in a collection that matches the given filter.
* @param filter
* @param options
* @param callback
*/
deleteOne(filter: Filter, options: DeleteOneOptions): Promise<void | import("@datastax/astra-db-ts").CollectionDeleteOneResult>;
/**
* Update a single document in a collection that matches the given filter, replacing it with `replacement`.
* Converted to a `findOneAndReplace()` under the hood.
* @param filter
* @param replacement
* @param options
*/
replaceOne(filter: Filter, replacement: Record<string, unknown>, options: ReplaceOneOptions): Promise<import("@datastax/astra-db-ts").CollectionReplaceOneResult<import("@datastax/astra-db-ts").FoundDoc<import("@datastax/astra-db-ts").SomeDoc>>>;
/**
* Update a single document in a collection that matches the given filter.
* @param filter
* @param update
* @param options
*/
updateOne(filter: Filter, update: CollectionUpdateFilter<DocType> | TableUpdateFilter<DocType>, options: UpdateOneOptions): Promise<{}>;
/**
* Update multiple documents in a collection that match the given filter.
* @param filter
* @param update
* @param options
*/
updateMany(filter: Filter, update: CollectionUpdateFilter<DocType>, options: CollectionUpdateManyOptions): Promise<import("@datastax/astra-db-ts").CollectionUpdateManyResult<import("@datastax/astra-db-ts").FoundDoc<import("@datastax/astra-db-ts").SomeDoc>>>;
/**
* Get the estimated number of documents in a collection based on collection metadata
*/
estimatedDocumentCount(options?: CollectionEstimatedDocumentCountOptions): Promise<number>;
/**
* Sync the underlying table schema with the specified definition: creates a new
* table if one doesn't exist, or alters the existing table to match the definition
* by adding or dropping columns as necessary.
*
* Note that modifying an existing column is NOT supported and will throw an error.
*
* @param definition new table definition (strict only)
* @param options passed to createTable if the table doesn't exist
* @returns void
*/
syncTable<DocType extends Record<string, unknown> = Record<string, unknown>>(definition: Pick<CreateTableDefinition, 'primaryKey'> & {
columns: Record<string, StrictCreateTableColumnDefinition>;
}, options?: CreateTableOptions): Promise<void>;
/**
* Alter the underlying table with the specified name and operation - can add or drop columns
* @param operation add/drop
*/
alterTable(operation: {
add: {
columns: Record<string, StrictCreateTableColumnDefinition>;
};
} | {
drop: {
columns: string[];
};
}): Promise<import("@datastax/astra-db-ts").RawDataAPIResponse>;
/**
* Run an arbitrary command against this collection
* @param command
*/
runCommand(command: Record<string, unknown>, options?: Omit<RunCommandOptions, 'table' | 'collection' | 'keyspace'>): Promise<import("@datastax/astra-db-ts").RawDataAPIResponse>;
/**
* Bulk write not supported.
* @param ops
* @param options
*/
bulkWrite(): void;
/**
* Aggregate not supported.
* @param pipeline
* @param options
*/
aggregate(): void;
/**
* Returns a list of all indexes on the collection. Returns a pseudo-cursor for Mongoose compatibility.
* Only works in tables mode, throws an error in collections mode.
*/
listIndexes(): {
toArray: () => Promise<AstraMongooseIndexDescription[]>;
};
/**
* Create a new index. Only works in tables mode, throws an error in collections mode.
*
* @param indexSpec MongoDB-style index spec for Mongoose compatibility
* @param options
*/
createIndex(indexSpec: Record<string, boolean | 1 | -1 | '$keys' | '$values' | 'text'> | IndexSpecification, options?: (TableTextIndexOptions | TableIndexOptions | TableVectorIndexOptions) & {
name?: string;
vector?: boolean;
}): Promise<string>;
/**
* Drop an existing index by name. Only works in tables mode, throws an error in collections mode.
*
* @param name
*/
dropIndex(name: string, options?: TableDropIndexOptions): Promise<void>;
/**
* Finds documents that match the filter and reranks them based on the provided options.
* @param filter
* @param options
*/
findAndRerank(filter: Filter, options?: CollectionFindAndRerankOptions): Promise<import("@datastax/astra-db-ts").CollectionFindAndRerankCursor<{
document: Record<string, unknown> | null;
scores: Record<string, number>;
}, import("@datastax/astra-db-ts").FoundDoc<import("@datastax/astra-db-ts").SomeDoc>>>;
/**
* Watch operation not supported.
*
* @ignore
*/
watch(): void;
/**
* Distinct operation not supported.
*
* @ignore
*/
distinct(): void;
}
export {};