From c8319d16d04e9fe4d0bbce6832f58f6fb3fa8c69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cau=C3=A3=20Rinaldi?= Date: Mon, 28 Oct 2024 14:24:42 -0300 Subject: [PATCH 1/4] Feat: Type distinct() --- src/types/collection.assert.ts | 32 ++++++++++++++++++++++++++++++++ src/types/collection.ts | 17 ++++++++++++----- 2 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 src/types/collection.assert.ts diff --git a/src/types/collection.assert.ts b/src/types/collection.assert.ts new file mode 100644 index 0000000..1cfe9e1 --- /dev/null +++ b/src/types/collection.assert.ts @@ -0,0 +1,32 @@ +import * as ta from 'type-assertions' +import { TsReadWriteCollection } from '../collection' +import { ObjectId } from 'mongodb' + +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. * From c617c13b313fedc00a99501367495e1122c4f7af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cau=C3=A3=20Rinaldi?= Date: Mon, 28 Oct 2024 15:08:40 -0300 Subject: [PATCH 2/4] Move tests --- src/collection.assert.ts | 35 +++++++++++++++++++++++++++++++++- src/types/collection.assert.ts | 32 ------------------------------- 2 files changed, 34 insertions(+), 33 deletions(-) delete mode 100644 src/types/collection.assert.ts diff --git a/src/collection.assert.ts b/src/collection.assert.ts index 20b0b9d..580df34 100644 --- a/src/collection.assert.ts +++ b/src/collection.assert.ts @@ -7,7 +7,11 @@ import type { WithId, } from 'mongodb' import * as ta from 'type-assertions' -import type { TsCollection, TsReadCollection } from './collection' +import { + TsCollection, + TsReadCollection, + TsReadWriteCollection, +} from './collection' import type { TsFilter, TsFindOneAndDeleteOptions } from './types' type TSchema = { a: string; _id: ObjectId } @@ -83,3 +87,32 @@ ta.assert< > > >() + +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.assert.ts b/src/types/collection.assert.ts deleted file mode 100644 index 1cfe9e1..0000000 --- a/src/types/collection.assert.ts +++ /dev/null @@ -1,32 +0,0 @@ -import * as ta from 'type-assertions' -import { TsReadWriteCollection } from '../collection' -import { ObjectId } from 'mongodb' - -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', {}) From 71f4c51f8ab90ee1dea9779740ff28d60f8ad32e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cau=C3=A3=20Rinaldi?= Date: Mon, 28 Oct 2024 15:26:51 -0300 Subject: [PATCH 3/4] Move tests to separate file --- src/colection.distinct.assert.ts | 32 +++++++++++++++++++++++++++++ src/collection.assert.ts | 35 +------------------------------- 2 files changed, 33 insertions(+), 34 deletions(-) create mode 100644 src/colection.distinct.assert.ts 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/collection.assert.ts b/src/collection.assert.ts index 580df34..d0d2402 100644 --- a/src/collection.assert.ts +++ b/src/collection.assert.ts @@ -7,11 +7,7 @@ import type { WithId, } from 'mongodb' import * as ta from 'type-assertions' -import { - TsCollection, - TsReadCollection, - TsReadWriteCollection, -} from './collection' +import { TsCollection, TsReadCollection } from './collection' import type { TsFilter, TsFindOneAndDeleteOptions } from './types' type TSchema = { a: string; _id: ObjectId } @@ -87,32 +83,3 @@ ta.assert< > > >() - -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', {}) From f7a5246350687ef3063daa3ae73ded95371c06a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cau=C3=A3=20Rinaldi?= Date: Mon, 28 Oct 2024 15:40:05 -0300 Subject: [PATCH 4/4] Back type --- src/collection.assert.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/collection.assert.ts b/src/collection.assert.ts index d0d2402..20b0b9d 100644 --- a/src/collection.assert.ts +++ b/src/collection.assert.ts @@ -7,7 +7,7 @@ import type { WithId, } from 'mongodb' import * as ta from 'type-assertions' -import { TsCollection, TsReadCollection } from './collection' +import type { TsCollection, TsReadCollection } from './collection' import type { TsFilter, TsFindOneAndDeleteOptions } from './types' type TSchema = { a: string; _id: ObjectId }