Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Type distinct() #54

Merged
merged 4 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/colection.distinct.assert.ts
Original file line number Diff line number Diff line change
@@ -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<ta.Equal<Awaited<ReturnType<typeof distinctString>>, string[]>>()
ta.assert<ta.Equal<Awaited<ReturnType<typeof distinctId>>, ObjectId[]>>()
ta.assert<ta.Equal<Awaited<ReturnType<typeof distinctNested>>, number[]>>()
ta.assert<ta.Equal<Awaited<ReturnType<typeof distinctReadOnly>>, boolean[]>>()

// @ts-expect-error
const _distinctIncorrect = (c: TestCollection) => c.distinct('no', {})

// @ts-expect-error
const _distinctWriteOnly = (c: TestCollection) => c.distinct('writeOnly', {})
17 changes: 12 additions & 5 deletions src/types/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
DropCollectionOptions,
DropIndexesOptions,
EstimatedDocumentCountOptions,
Flatten,
IndexInformationOptions,
InsertManyResult,
InsertOneOptions,
Expand All @@ -27,6 +28,7 @@ import {
RenameOptions,
ReplaceOptions,
UnorderedBulkOperation,
WithId,
WithoutId,
} from 'mongodb'
import { TsAggregationCursor, TsPipeline } from './aggregation'
Expand All @@ -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,
Expand Down Expand Up @@ -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<TKey extends FlattenProjectionPaths<TReturnSchema>>(
key: TKey,
filter: TsFilter<TFilterSchema>,
options?: DistinctOptions
): Promise<any[]>
): Promise<FlattenProjectionType<TReturnSchema, TKey>[]>
/**
* Find a document and delete it in one atomic operation. Requires a write lock for the duration of the operation.
*
Expand Down