diff --git a/src/colection.distinct.assert.ts b/src/colection.distinct.assert.ts new file mode 100644 index 0000000..a78aa0e --- /dev/null +++ b/src/colection.distinct.assert.ts @@ -0,0 +1,32 @@ +import type { ObjectId } from 'mongodb' +import { TsReadWriteCollection } from './collection' +import * as ta from 'type-assertions' + +type DistinctSampleType = { + _id: ObjectId + str: string + nested: { + field: number + } +} + +type TestCollection = TsReadWriteCollection< + DistinctSampleType & { writeOnly: boolean }, + DistinctSampleType & { readOnly: boolean } +> + +const distinctString = (c: TestCollection) => c.distinct('str', {}) +const distinctId = (c: TestCollection) => c.distinct('_id', {}) +const distinctNested = (c: TestCollection) => c.distinct('nested.field', {}) +const distinctReadOnly = (c: TestCollection) => c.distinct('readOnly', {}) + +ta.assert>, string[]>>() +ta.assert>, ObjectId[]>>() +ta.assert>, number[]>>() +ta.assert>, boolean[]>>() + +// @ts-expect-error +const _distinctIncorrect = (c: TestCollection) => c.distinct('no', {}) + +// @ts-expect-error +const _distinctWriteOnly = (c: TestCollection) => c.distinct('writeOnly', {}) diff --git a/src/types/collection.ts b/src/types/collection.ts index b0a83b5..58a1f98 100644 --- a/src/types/collection.ts +++ b/src/types/collection.ts @@ -16,6 +16,7 @@ import { DropCollectionOptions, DropIndexesOptions, EstimatedDocumentCountOptions, + Flatten, IndexInformationOptions, InsertManyResult, InsertOneOptions, @@ -27,6 +28,7 @@ import { RenameOptions, ReplaceOptions, UnorderedBulkOperation, + WithId, WithoutId, } from 'mongodb' import { TsAggregationCursor, TsPipeline } from './aggregation' @@ -39,8 +41,14 @@ import { TsFindOptions, } from './find' import { IndexSpecification } from './mongo-index' -import { TsUpdate, TsUpdateOptions, TsUpdateResult } from './update' +import { + SelectFlattenUpdatePaths, + TsUpdate, + TsUpdateOptions, + TsUpdateResult, +} from './update' import { Doc, DocumentWithId } from './util' +import { FlattenProjectionPaths, FlattenProjectionType } from './flatten' export declare class SafeCollection< TInsertSchema extends Document, @@ -369,13 +377,12 @@ export declare class SafeCollection< * @param key - Field of the document to find distinct values for * @param filter - The filter for filtering the set of documents to which we apply the distinct filter. * @param options - Optional settings for the command - * @param callback - An optional callback, a Promise will be returned if none is provided */ - distinct( - key: string, + distinct>( + key: TKey, filter: TsFilter, options?: DistinctOptions - ): Promise + ): Promise[]> /** * Find a document and delete it in one atomic operation. Requires a write lock for the duration of the operation. *