Skip to content

Commit 8bb1b3f

Browse files
Longboyysamualtnorman
authored andcommitted
Refine Mongo types
1 parent 2115948 commit 8bb1b3f

File tree

1 file changed

+38
-16
lines changed

1 file changed

+38
-16
lines changed

env.d.ts

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -768,22 +768,44 @@ type Projection<Schema extends object> = Partial<{
768768
[key in keyof Schema]: boolean | 0 | 1
769769
}>
770770

771-
type MongoCommand<Schema extends object> = Partial<{
772-
$set: Partial<Record<string, MongoCommandValue> & {
773-
[K in keyof Schema]: Schema[K]
774-
}>,
775-
$push: Partial<Record<string, MongoCommandValue> & {
776-
[K in keyof Schema]: Schema[K]
777-
}>,
778-
$unset: Partial<Record<string, ""> & {
779-
[K in keyof Schema]: ""
780-
}>
771+
772+
type MongoUpdateOperators<Schema extends object> = Partial<{
773+
$set: Partial<Record<string, MongoCommandValue> & Schema>;
774+
$setOnInsert: Partial<Record<string, MongoCommandValue> & Schema>;
775+
$unset: Partial<Record<string, ""> & Schema>;
776+
$rename: Partial<Record<string, string> & Schema>;
777+
$inc: Partial<Record<string, number> & {
778+
[key in keyof Schema as Schema[key] extends number | Date ? key : never]: Schema[key] extends number ? number : Date;
779+
}>;
780+
$mul: Partial<Record<string, number> & {
781+
[key in keyof Schema as Schema[key] extends number ? key : never]: number;
782+
}>;
783+
$min: Partial<Record<string, number> & {
784+
[key in keyof Schema as Schema[key] extends number ? key : never]: number;
785+
}>;
786+
$max: Partial<Record<string, number> & {
787+
[key in keyof Schema as Schema[key] extends number ? key : never]: number;
788+
}>;
781789
}>
782790

791+
/*
792+
type MongoUpdateArrayOperators<T extends Array> = {
793+
$pop: -1 | 1,
794+
$pull: Query<T>,
795+
}
796+
*/
797+
798+
type MongoUpdateArrayOperatorModifiers<Schema extends Array> = {
799+
$each: Schema,
800+
$position: number,
801+
$slice: number,
802+
$sort: SortOrder | 1 | -1
803+
}
804+
805+
type MongoUpdateCommand<Schema extends object> = MongoUpdateOperators<Schema>
806+
783807
type Id = string | number | boolean | Date | Record<string, MongoValue>
784-
type MongoDocument<Schema extends object = object> = {
785-
[K in keyof Schema]: Schema[K]
786-
} & { _id: Id }
808+
type MongoDocument<Schema extends object = object> = Schema & { _id: Id }
787809
type SortOrder = { [key: string]: 1 | -1 | SortOrder }
788810

789811
type Cursor<Doc extends MongoDocument = MongoDocument> = {
@@ -929,7 +951,7 @@ declare const $db: {
929951
* @param query Specifies deletion criteria using query operators.
930952
* @param command The modifications to apply.
931953
* {@link https://docs.mongodb.com/manual/reference/method/db.collection.update/#parameters} */
932-
u: <T extends object = object>(query: Query<T> | Query<T>[], command: MongoCommand<MongoDocument<T>>) => {
954+
u: <T extends object = object>(query: Query<T> | Query<T>[], command: MongoUpdateCommand<MongoDocument<T>>) => {
933955
ok: 0 | 1
934956
nModified: number
935957
n: number
@@ -946,7 +968,7 @@ declare const $db: {
946968
* @param query Specifies deletion criteria using query operators.
947969
* @param command The modifications to apply.
948970
* {@link https://docs.mongodb.com/manual/reference/method/db.collection.update/#parameters} */
949-
u1: <T extends object = object>(query: Query<T> | Query<T>[], command: MongoCommand<MongoDocument<T>>) => {
971+
u1: <T extends object = object>(query: Query<T> | Query<T>[], command: MongoUpdateCommand<MongoDocument<T>>) => {
950972
ok: 0 | 1
951973
nModified: number
952974
n: number
@@ -972,7 +994,7 @@ declare const $db: {
972994
* @param query Specifies deletion criteria using query operators.
973995
* @param command The modifications to apply.
974996
* {@link https://docs.mongodb.com/manual/reference/method/db.collection.update/#parameters} */
975-
us: <T extends object = object>(query: Query<T> | Query<T>[], command: MongoCommand<MongoDocument<T>>) => {
997+
us: <T extends object = object>(query: Query<T> | Query<T>[], command: MongoUpdateCommand<MongoDocument<T>>) => {
976998
ok: 0 | 1
977999
nModified: number
9781000
n: number

0 commit comments

Comments
 (0)