Skip to content

Commit 79ce0f2

Browse files
committed
okay $db.f() is in a pretty good place I think. there's still more to add but what's there is now working properly
1 parent ddb3ff1 commit 79ce0f2

File tree

1 file changed

+44
-50
lines changed

1 file changed

+44
-50
lines changed

env.d.ts

Lines changed: 44 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -710,47 +710,42 @@ type Nullsec = Lowsec & PlayerNullsec & {
710710
// database
711711
type MongoPrimitive = null | boolean | number | Date | string
712712
type MongoValue = MongoPrimitive | MongoValue[] | MongoObject
713-
type MongoObject = { [k: string]: MongoValue }
713+
type MongoObject = { [k: string]: MongoValue, [k: `$${string}`]: never }
714+
type MongoQueryValue = MongoPrimitive | MongoQueryValue[] | MongoQueryObject
715+
716+
type MongoQueryObject =
717+
{ [k: string]: MongoQueryValue, [k: `$${string}`]: MongoValue, $type?: keyof MongoTypeStringsToTypes | (string & {}) }
714718

715719
type MongoTypeStringsToTypes = {
716-
minKey: unknown
717-
double: unknown
720+
double: number
718721
string: string
719722
object: MongoObject
720723
array: MongoValue[]
721-
binData: unknown
722-
undefined: unknown
723-
objectId: unknown
724+
objectId: ObjectId
724725
bool: boolean
725726
date: Date
726727
null: null
727-
regex: unknown
728-
dbPointer: unknown
729-
javascript: unknown
730-
symbol: unknown
731-
int: unknown
732-
timestamp: unknown
733-
long: unknown
734-
decimal: unknown
735-
maxKey: unknown
728+
int: number
729+
long: number
736730
}
737731

738732
type MongoTypeString = keyof MongoTypeStringsToTypes
739733
type MongoTypeNumber = -1 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 16 | 17 | 18 | 19 | 127
740734
type MongoId = Exclude<MongoPrimitive, null> | MongoObject
735+
type MongoQueryId = Exclude<MongoPrimitive, null> | MongoQueryObject
741736
type MongoDocument = MongoObject & { _id?: MongoId }
742-
//type MongoQueryValue = MongoPrimitive | MongoQueryValue[] | MongoQuery
743-
//type MongoQuery = { [k: string]: MongoQueryValue }
744737

745-
type MongoQueryType<TQuery extends MongoObject> = { [k: string]: MongoValue } & {
746-
[K in keyof TQuery]:
738+
type MongoQueryType<TQuery extends MongoQueryObject> = {
739+
-readonly [K in keyof TQuery]:
747740
TQuery[K] extends MongoPrimitive ?
748741
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 ?
742+
: TQuery[K] extends { $type: infer TType } ?
743+
TType extends keyof MongoTypeStringsToTypes ? MongoTypeStringsToTypes[TType] : unknown
744+
: TQuery[K] extends { $in: (infer TIn)[] } ?
745+
TIn
746+
: keyof TQuery[K] extends `$${string}` ?
747+
unknown
748+
: TQuery[K] extends { [k: string]: any } ?
754749
MongoQueryType<TQuery[K]>
755750
: never
756751
}
@@ -769,7 +764,7 @@ type MongoQuerySelector<T extends MongoValue> = Partial<
769764
: MongoElementSelectors & MongoComparisonSelectors<T>
770765
>
771766

772-
type Query<T extends MongoObject> = { [K in keyof T]?: T[K] | MongoQuerySelector<T[K]> } & { _id?: MongoId }
767+
type MongoQuery<T extends MongoObject> = { [K in keyof T]?: T[K] | MongoQuerySelector<T[K]> } & { _id?: MongoId }
773768
type Projection = Record<string, boolean | 0 | 1>
774769

775770
type MongoUpdateOperators<T extends MongoObject> = Partial<{
@@ -922,19 +917,23 @@ declare const $s: Nullsec
922917

923918
type ObjectId = { $oid: string }
924919

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-
}*/
920+
// _id is always returned unless _id: false is passed
921+
// when anyField: true is given, other fields (except _id) are omitted
922+
923+
type MongoProject<TDocument, TProjection> =
924+
true extends (1 extends TProjection[keyof TProjection] ? true : TProjection[keyof TProjection]) ?
925+
(TProjection extends { _id: false | 0 } ? {} : { _id: TDocument extends { _id: infer TId } ? TId : MongoId }) &
926+
{
927+
[K in
928+
keyof TDocument as K extends keyof TProjection ? TProjection[K] extends true | 1 ? K : never : never
929+
]: TDocument[K]
930+
} &
931+
{
932+
-readonly [K in
933+
keyof TProjection as TProjection[K] extends true | 1 ? K extends keyof TDocument ? never : K : never
934+
]?: MongoValue
935+
}
936+
: { [k: string]: MongoValue } & { [K in keyof TDocument as K extends keyof TProjection ? never : K]: TDocument[K] }
938937

939938
declare const $db: {
940939
/** Insert a document or documents into a collection.
@@ -944,33 +943,28 @@ declare const $db: {
944943

945944
/** Remove documents from a collection.
946945
* @param query Specifies deletion criteria using query operators. */
947-
r: <T extends MongoDocument>(query: Query<T>) => { n: number, opTime: { t: number }, ok: 0 | 1 }[]
946+
r: <T extends MongoDocument>(query: MongoQuery<T>) => { n: number, opTime: { t: number }, ok: 0 | 1 }[]
948947

949948
/** Find documents in a collection or view and returns a cursor to the selected documents.
950949
* @param query Specifies deletion criteria using query operators.
951950
* @param projection Specifies the fields to return in the documents that match the query filter. */
952951
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-
>>
952+
const TQuery extends MongoQueryObject & { _id?: MongoQueryId },
953+
const TProjection extends { [k: string]: boolean | 0 | 1 } = {}
954+
>(query: TQuery, projection?: TProjection) => Cursor<MongoProject<MongoQueryType<TQuery>, TProjection>>
961955

962956
/** Update existing documents in a collection.
963957
* @param query Specifies deletion criteria using query operators.
964958
* @param command The modifications to apply.
965959
* {@link https://docs.mongodb.com/manual/reference/method/db.collection.update/#parameters} */
966-
u: <T extends MongoDocument>(query: Query<T> | Query<T>[], command: MongoUpdateCommand<T>) =>
960+
u: <T extends MongoDocument>(query: MongoQuery<T> | MongoQuery<T>[], command: MongoUpdateCommand<T>) =>
967961
{ n: number, opTime: { t: number }, ok: 0 | 1, nModified: number }[]
968962

969963
/** Updates one document within the collection based on the filter.
970964
* @param query Specifies deletion criteria using query operators.
971965
* @param command The modifications to apply.
972966
* {@link https://docs.mongodb.com/manual/reference/method/db.collection.update/#parameters} */
973-
u1: <T extends MongoDocument>(query: Query<T> | Query<T>[], command: MongoUpdateCommand<T>) =>
967+
u1: <T extends MongoDocument>(query: MongoQuery<T> | MongoQuery<T>[], command: MongoUpdateCommand<T>) =>
974968
{ n: number, ok: 0 | 1, opTime: { t: number }, nModified: number }[]
975969

976970
/** Update or insert document.
@@ -980,7 +974,7 @@ declare const $db: {
980974
* @param query Specifies deletion criteria using query operators.
981975
* @param command The modifications to apply.
982976
* {@link https://docs.mongodb.com/manual/reference/method/db.collection.update/#parameters} */
983-
us: <T extends MongoDocument>(query: Query<T> | Query<T>[], command: MongoUpdateCommand<T>) =>
977+
us: <T extends MongoDocument>(query: MongoQuery<T> | MongoQuery<T>[], command: MongoUpdateCommand<T>) =>
984978
{ n: number, ok: 0 | 1, opTime: { t: number }, nModified: number }[]
985979

986980
ObjectId: () => ObjectId

0 commit comments

Comments
 (0)