Skip to content

Commit 3988b0d

Browse files
Longboyysamualtnorman
authored andcommitted
Added MongoSchema type to fix broken type issues
1 parent 38278e7 commit 3988b0d

File tree

1 file changed

+27
-15
lines changed

1 file changed

+27
-15
lines changed

env.d.ts

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -720,9 +720,13 @@ type MongoTypeString = "minKey" | "double" | "string" | "object" | "array" | "bi
720720
"bool" | "date" | "null" | "regex" | "dbPointer" | "javascript" | "symbol" | "int" | "timestamp" | "long" | "decimal" | "maxKey";
721721
type MongoTypeNumber = -1 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 16 | 17 | 18 | 19 | 127;
722722

723-
type MongoValue = string | number | boolean | Date | MongoValue[] | Record<string, MongoValue> | null
723+
type MongoValue = string | number | boolean | Date | MongoValue[] | { [key: string]: MongoValue } | null
724724

725-
type MongoCommandValue = string | number | boolean | Date | MongoCommandValue[] | Record<string, MongoCommandValue> | null | undefined;
725+
type MongoCommandValue = string | number | boolean | Date | MongoCommandValue[] | { [key: string]: MongoCommandValue } | null | undefined;
726+
727+
type MongoSchema = {
728+
[key: string]: MongoValue
729+
}
726730

727731
/**
728732
* Currently unused
@@ -760,21 +764,27 @@ type MongoQuerySelector<T extends MongoValue = MongoValue> = Partial<T extends M
760764
(MongoArraySelectors<T> & MongoElementSelectors & MongoComparisonSelectors<T>) :
761765
(MongoElementSelectors & MongoComparisonSelectors<T>)>
762766

763-
type Query<Schema extends object> = Partial<{[key in keyof Schema]: MongoValue | MongoQuerySelector}> & {
767+
type Query<Schema extends MongoSchema> = Partial<{[key in keyof Schema]: MongoValue | MongoQuerySelector<Schema[key]>}> & {
764768
_id?: Id
765769
};
766770

767-
type Projection<Schema extends object> = Partial<{
771+
type Projection<Schema extends MongoSchema> = Partial<{
768772
[key in keyof Schema]: boolean | 0 | 1
769773
}>
770774

775+
/*
776+
rename
777+
addToSet
778+
*/
771779

772-
type MongoUpdateOperators<Schema extends object> = Partial<{
780+
type MongoUpdateOperators<Schema extends MongoSchema> = Partial<{
773781
/* Universal operators */
774782
$set: Partial<Record<string, MongoCommandValue> & Schema>
775783
$setOnInsert: Partial<Record<string, MongoCommandValue> & Schema>
776784
$unset: Partial<Record<string, ""> & Schema>
777-
$rename: Partial<Record<string, string> & Schema>
785+
$rename: Partial<Record<string, string> & {
786+
[key in keyof Schema]: string
787+
}>
778788
/* Date & number operators */
779789
$inc: Partial<Record<string, number> & {
780790
[key in keyof Schema as Schema[key] extends number | Date ? key : never]: Schema[key] extends number ? number : Date
@@ -793,13 +803,15 @@ type MongoUpdateOperators<Schema extends object> = Partial<{
793803
[key in keyof Schema as Schema[key] extends Array<infer U> ? key : never]: -1 | 1
794804
}>
795805
$push: Partial<Record<string, MongoCommandValue> & {
796-
[key in keyof Schema as Schema[key] extends Array<infer U> ? key : never]: Schema[key] | MongoUpdateArrayOperatorModifiers<Schema[key]>
806+
[key in keyof Schema as Schema[key] extends Array<infer U> ? key : never]: (Schema[key] extends (infer U)[] ? U : never)
807+
| MongoUpdateArrayOperatorModifiers<Schema[key]>
797808
}>
798809
$addToSet: Partial<Record<string, MongoCommandValue> & {
799810
[key in keyof Schema as Schema[key] extends Array<infer U> ? key : never]: Schema[key] | MongoUpdateArrayOperatorUniversalModifiers<Schema[key]>
800811
}>
801812
$pull: Partial<Record<string, MongoCommandValue> & {
802-
[key in keyof Schema as Schema[key] extends Array<infer U> ? key : never]: Schema[key] extends (infer U)[] ? U : Schema[key] | MongoQuerySelector<Schema[key]>
813+
[key in keyof Schema as Schema[key] extends Array<infer U> ? key : never]: (Schema[key] extends (infer U)[] ? U : never)
814+
| MongoQuerySelector<Schema[key]>
803815
}>
804816
$pullAll: Partial<Record<string, MongoCommandValue> & {
805817
[key in keyof Schema as Schema[key] extends Array<infer U> ? key : never]: Schema[key]
@@ -816,7 +828,7 @@ type MongoUpdateArrayOperatorModifiers<T> = MongoUpdateArrayOperatorUniversalMod
816828
$sort: 1 | -1
817829
}>
818830

819-
type MongoUpdateCommand<Schema extends object> = MongoUpdateOperators<Schema>
831+
type MongoUpdateCommand<Schema extends MongoSchema> = MongoUpdateOperators<Schema>
820832

821833
type Id = string | number | boolean | Date | Record<string, MongoValue>
822834
type MongoDocument<Schema extends object = object> = Schema & { _id: Id }
@@ -930,7 +942,7 @@ type ObjectId = { $oid: string }
930942
declare const $db: {
931943
/** Insert a document or documents into a collection.
932944
* @param documents A document or array of documents to insert into the collection. */
933-
i: <T extends object = object>(documents: (T & { _id?: Id }) | (T & { _id?: Id })[]) => {
945+
i: <T extends MongoSchema = MongoSchema>(documents: (T & { _id?: Id }) | (T & { _id?: Id })[]) => {
934946
ok: 1
935947
n: number
936948
opTime: { ts: "Undefined Conversion", t: number }
@@ -944,7 +956,7 @@ declare const $db: {
944956

945957
/** Remove documents from a collection.
946958
* @param query Specifies deletion criteria using query operators. */
947-
r: <T extends object = object>(query: Query<T>) => {
959+
r: <T extends MongoSchema = MongoSchema>(query: Query<T>) => {
948960
ok: 0 | 1
949961
n: number
950962
opTime: { ts: "Undefined Conversion", t: number }
@@ -959,13 +971,13 @@ declare const $db: {
959971
/** Find documents in a collection or view and returns a cursor to the selected documents.
960972
* @param query Specifies deletion criteria using query operators.
961973
* @param projection Specifies the fields to return in the documents that match the query filter. */
962-
f: <T extends object = object>(query?: Query<T>, projection?: Projection<T>) => Cursor<MongoDocument<T>>
974+
f: <T extends MongoSchema = MongoSchema>(query?: Query<T>, projection?: Projection<T>) => Cursor<MongoDocument<T>>
963975

964976
/** Update existing documents in a collection.
965977
* @param query Specifies deletion criteria using query operators.
966978
* @param command The modifications to apply.
967979
* {@link https://docs.mongodb.com/manual/reference/method/db.collection.update/#parameters} */
968-
u: <T extends object = object>(query: Query<T> | Query<T>[], command: MongoUpdateCommand<MongoDocument<T>>) => {
980+
u: <T extends MongoSchema = MongoSchema>(query: Query<T> | Query<T>[], command: MongoUpdateCommand<MongoDocument<T>>) => {
969981
ok: 0 | 1
970982
nModified: number
971983
n: number
@@ -982,7 +994,7 @@ declare const $db: {
982994
* @param query Specifies deletion criteria using query operators.
983995
* @param command The modifications to apply.
984996
* {@link https://docs.mongodb.com/manual/reference/method/db.collection.update/#parameters} */
985-
u1: <T extends object = object>(query: Query<T> | Query<T>[], command: MongoUpdateCommand<MongoDocument<T>>) => {
997+
u1: <T extends MongoSchema = MongoSchema>(query: Query<T> | Query<T>[], command: MongoUpdateCommand<MongoDocument<T>>) => {
986998
ok: 0 | 1
987999
nModified: number
9881000
n: number
@@ -1008,7 +1020,7 @@ declare const $db: {
10081020
* @param query Specifies deletion criteria using query operators.
10091021
* @param command The modifications to apply.
10101022
* {@link https://docs.mongodb.com/manual/reference/method/db.collection.update/#parameters} */
1011-
us: <T extends object = object>(query: Query<T> | Query<T>[], command: MongoUpdateCommand<MongoDocument<T>>) => {
1023+
us: <T extends MongoSchema = MongoSchema>(query: Query<T> | Query<T>[], command: MongoUpdateCommand<MongoDocument<T>>) => {
10121024
ok: 0 | 1
10131025
nModified: number
10141026
n: number

0 commit comments

Comments
 (0)