Skip to content

Commit ddb3ff1

Browse files
committed
changes from june-14
I made and never commited these changes so no idea what they're about
1 parent 22264f8 commit ddb3ff1

File tree

1 file changed

+44
-19
lines changed

1 file changed

+44
-19
lines changed

env.d.ts

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -739,16 +739,19 @@ type MongoTypeString = keyof MongoTypeStringsToTypes
739739
type MongoTypeNumber = -1 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 16 | 17 | 18 | 19 | 127
740740
type MongoId = Exclude<MongoPrimitive, null> | MongoObject
741741
type MongoDocument = MongoObject & { _id?: MongoId }
742-
type MongoQuery = MongoObject & Record<string, { $type: MongoTypeString }>
742+
//type MongoQueryValue = MongoPrimitive | MongoQueryValue[] | MongoQuery
743+
//type MongoQuery = { [k: string]: MongoQueryValue }
743744

744-
type MongoQueryType<TQuery extends MongoQuery> = {
745+
type MongoQueryType<TQuery extends MongoObject> = { [k: string]: MongoValue } & {
745746
[K in keyof TQuery]:
746-
TQuery[K] extends infer V
747-
? V extends MongoPrimitive ? V
748-
: V extends { $type: infer InferredTypeString } ? MongoTypeStringsToTypes[InferredTypeString]
749-
: V extends { [`$${string}`]: any } ? never
750-
: V extends MongoObject ? MongoQueryType<V>
751-
: never
747+
TQuery[K] extends MongoPrimitive ?
748+
TQuery[K]
749+
: TQuery[K] extends { $type: keyof MongoTypeStringsToTypes } ?
750+
MongoTypeStringsToTypes[TQuery[K]["$type"]]
751+
: { [k: `$${string}`]: any } extends TQuery[K] ?
752+
never
753+
: TQuery[K] extends MongoObject ?
754+
MongoQueryType<TQuery[K]>
752755
: never
753756
}
754757

@@ -767,7 +770,7 @@ type MongoQuerySelector<T extends MongoValue> = Partial<
767770
>
768771

769772
type Query<T extends MongoObject> = { [K in keyof T]?: T[K] | MongoQuerySelector<T[K]> } & { _id?: MongoId }
770-
type Projection<T extends MongoObject> = { [K in keyof T]?: boolean | 0 | 1 }
773+
type Projection = Record<string, boolean | 0 | 1>
771774

772775
type MongoUpdateOperators<T extends MongoObject> = Partial<{
773776
/* Universal operators */
@@ -815,35 +818,35 @@ type MongoUpdateCommand<Schema extends MongoObject> = MongoUpdateOperators<Schem
815818

816819
type SortOrder = { [key: string]: 1 | -1 | SortOrder }
817820

818-
type Cursor<TDocument extends MongoDocument> = {
819-
/** Returns the first document that satisfies the query. */ first: () => TDocument | null
820-
/** Returns an array of documents that satisfy the query. */ array: () => TDocument[]
821+
type Cursor<T> = {
822+
/** Returns the first document that satisfies the query. */ first: () => T | null
823+
/** Returns an array of documents that satisfy the query. */ array: () => T[]
821824
/** Returns the number of documents that match the query. */ count: () => number
822825

823826
/** Returns the first document that satisfies the query. Also makes cursor unusable. */
824-
first_and_close: () => TDocument
827+
first_and_close: () => T
825828

826829
/** Returns an array of documents that satisfy the query. Also makes cursor unusable. */
827-
array_and_close: () => TDocument[]
830+
array_and_close: () => T[]
828831

829832
/** Returns the number of documents that match the query. Also makes cursor unusable. */
830833
count_and_close: () => number
831834

832835
/** Run `callback` on each document that satisfied the query. */
833-
each: (callback: (document: TDocument) => void) => null
836+
each: (callback: (document: T) => void) => null
834837

835838
/** Returns a new cursor with documents sorted as specified.
836839
* A value of 1 sorts the property ascending, and -1 descending.
837840
* @param order The way the documents are to be sorted. */
838-
sort: (order?: SortOrder) => Cursor<TDocument>
841+
sort: (order?: SortOrder) => Cursor<T>
839842

840843
/** Returns a new cursor without the first number of documents.
841844
* @param count Number of documents to skip. */
842-
skip: (count: number) => Cursor<TDocument>
845+
skip: (count: number) => Cursor<T>
843846

844847
/** Returns a new cursor limited to a number of documents as specified.
845848
* @param count Number of documents. */
846-
limit: (count: number) => Cursor<TDocument>
849+
limit: (count: number) => Cursor<T>
847850

848851
/** @param key The key of the documents. */ distinct: { (key: string): MongoValue[], (key: "_id"): MongoId[] }
849852
/** Make cursor unusable. */ close: () => null
@@ -919,6 +922,20 @@ declare const $s: Nullsec
919922

920923
type ObjectId = { $oid: string }
921924

925+
type MongoProject<TDocument, TProjection> = TDocument
926+
// can't use keyof because it evaluates to `string | number`
927+
/*{
928+
[K in keyof TDocument | keyof TProjection]:
929+
keyof TDocument
930+
//K extends keyof TProjection ?
931+
//"A"
932+
//TProjection[K] extends false | 0 ?
933+
// undefined
934+
//: TDocument[K]
935+
//: TDocument[K]
936+
//: keyof TProjection
937+
}*/
938+
922939
declare const $db: {
923940
/** Insert a document or documents into a collection.
924941
* @param documents A document or array of documents to insert into the collection. */
@@ -932,7 +949,15 @@ declare const $db: {
932949
/** Find documents in a collection or view and returns a cursor to the selected documents.
933950
* @param query Specifies deletion criteria using query operators.
934951
* @param projection Specifies the fields to return in the documents that match the query filter. */
935-
f: <T extends MongoDocument>(query?: Query<T>, projection?: Projection<T>) => Cursor<T>
952+
f: <
953+
const TQuery extends MongoObject & { _id?: MongoId },
954+
const TProjection extends { [k: string]: boolean | 0 | 1 } & { [K in keyof TQuery]?: boolean | 0 | 1 }
955+
>(query: TQuery, projection?: TProjection) =>
956+
Cursor<MongoProject<
957+
MongoQueryType<TQuery> &
958+
(TQuery extends { _id: any } ? {} : { _id?: MongoId }),
959+
TProjection
960+
>>
936961

937962
/** Update existing documents in a collection.
938963
* @param query Specifies deletion criteria using query operators.

0 commit comments

Comments
 (0)