Skip to content

Commit 12a66eb

Browse files
committed
improve typings
1 parent 646a419 commit 12a66eb

File tree

1 file changed

+75
-173
lines changed

1 file changed

+75
-173
lines changed

env.d.ts

Lines changed: 75 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -717,153 +717,112 @@ type Nullsec = Lowsec & PlayerNullsec & {
717717
}
718718

719719
type MongoTypeString = "minKey" | "double" | "string" | "object" | "array" | "binData" | "undefined" | "objectId" |
720-
"bool" | "date" | "null" | "regex" | "dbPointer" | "javascript" | "symbol" | "int" | "timestamp" | "long" | "decimal" | "maxKey";
721-
type MongoTypeNumber = -1 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 16 | 17 | 18 | 19 | 127;
720+
"bool" | "date" | "null" | "regex" | "dbPointer" | "javascript" | "symbol" | "int" | "timestamp" | "long" |
721+
"decimal" | "maxKey"
722722

723-
type MongoValue = string | number | boolean | Date | MongoValue[] | { [key: string]: MongoValue } | null
723+
type MongoTypeNumber = -1 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 16 | 17 | 18 | 19 | 127
724+
type MongoPrimitive = null | boolean | number | Date | string
725+
type MongoValue = MongoPrimitive | MongoValue[] | MongoObject
726+
type MongoObject = { [k: string]: MongoValue }
727+
type MongoCommandValue = MongoPrimitive | MongoCommandValue[] | { [k: string]: MongoCommandValue }
728+
type MongoArraySelectors<T extends MongoValue[] = MongoValue[]> = { $all: T, $elemMatch: T, $size: number }
724729

725-
type MongoCommandValue = string | number | boolean | Date | MongoCommandValue[] | { [key: string]: MongoCommandValue } | null | undefined;
730+
type MongoComparisonSelectors<T extends MongoValue = MongoValue> =
731+
{ $eq: T, $gt: T, $gte: T, $in: T[], $lt: T, $lte: T, $ne: T, $nin: T[] }
726732

727-
type MongoSchema = {
728-
[key: string]: MongoValue
729-
}
733+
type MongoElementSelectors = { $exists: boolean, $type: MongoTypeNumber | MongoTypeString }
730734

731-
/**
732-
* Currently unused
733-
*/
734-
type MongoLogicalSelectors<T extends MongoValue = MongoValue> = {
735-
$not: T | MongoComparisonSelectors<T> | MongoLogicalSelectors<T>
736-
$nor: T[]
737-
$or: T[]
738-
$and: T[]
739-
}
735+
type MongoQuerySelector<T extends MongoValue> = Partial<
736+
T extends []
737+
? MongoArraySelectors<T> & MongoElementSelectors & MongoComparisonSelectors<T>
738+
: MongoElementSelectors & MongoComparisonSelectors<T>
739+
>
740740

741-
type MongoArraySelectors<T extends Array<MongoValue> = Array<MongoValue>> = {
742-
$all: T
743-
$elemMatch: T
744-
$size: number
745-
}
741+
type Query<T extends MongoObject> = { [K in keyof T]?: T[K] | MongoQuerySelector<T[K]> } & { _id?: MongoId }
742+
type Projection<T extends MongoObject> = { [K in keyof T]?: boolean | 0 | 1 }
746743

747-
type MongoComparisonSelectors<T extends MongoValue = MongoValue> = {
748-
$eq: T
749-
$gt: T
750-
$gte: T
751-
$in: T[]
752-
$lt: T
753-
$lte: T
754-
$ne: T
755-
$nin: T[]
756-
}
744+
type MongoUpdateOperators<T extends MongoObject> = Partial<{
745+
/* Universal operators */
746+
$set: Partial<Record<string, MongoCommandValue> & T>
747+
$setOnInsert: Partial<Record<string, MongoCommandValue> & T>
748+
$unset: Partial<Record<string, ""> & T>
757749

758-
type MongoElementSelectors = {
759-
$exists: boolean
760-
$type: MongoTypeNumber | MongoTypeString
761-
}
750+
$rename: Partial<Record<string, string> & { [key in keyof T]: string }>
762751

763-
type MongoQuerySelector<T extends MongoValue = MongoValue> = Partial<T extends MongoValue[] ?
764-
(MongoArraySelectors<T> & MongoElementSelectors & MongoComparisonSelectors<T>) :
765-
(MongoElementSelectors & MongoComparisonSelectors<T>)>
752+
/* Date & number operators */
753+
$inc: Record<string, number> &
754+
{ [K in keyof T as T[K] extends number | Date ? K : never]?: T[K] extends number ? number : Date }
766755

767-
type Query<Schema extends MongoSchema> = Partial<{[key in keyof Schema]: MongoValue | MongoQuerySelector<Schema[key]>}> & {
768-
_id?: Id
769-
};
756+
$mul: Record<string, number> & { [K in keyof T as T[K] extends number ? K : never]?: number }
757+
$min: Record<string, number> & { [K in keyof T as T[K] extends number ? K : never]?: number }
758+
$max: Record<string, number> & { [K in keyof T as T[K] extends number ? K : never]?: number }
770759

771-
type Projection<Schema extends MongoSchema> = Partial<{
772-
[key in keyof Schema]: boolean | 0 | 1
773-
}>
760+
/* Array operators */
761+
$pop: Record<string, -1 | 1> & { [K in keyof T as T[K] extends [] ? K : never]?: -1 | 1 }
774762

763+
$push: Record<string, MongoCommandValue> & {
764+
[K in keyof T as T[K] extends [] ? K : never]?: (T[K] extends (infer U)[] ? U : never)
765+
| MongoUpdateArrayOperatorModifiers<T[K]>
766+
}
775767

776-
type MongoUpdateOperators<Schema extends MongoSchema> = Partial<{
777-
/* Universal operators */
778-
$set: Partial<Record<string, MongoCommandValue> & Schema>
779-
$setOnInsert: Partial<Record<string, MongoCommandValue> & Schema>
780-
$unset: Partial<Record<string, ""> & Schema>
781-
$rename: Partial<Record<string, string> & {
782-
[key in keyof Schema]: string
783-
}>
784-
/* Date & number operators */
785-
$inc: Partial<Record<string, number> & {
786-
[key in keyof Schema as Schema[key] extends number | Date ? key : never]: Schema[key] extends number ? number : Date
787-
}>
788-
$mul: Partial<Record<string, number> & {
789-
[key in keyof Schema as Schema[key] extends number ? key : never]: number
790-
}>
791-
$min: Partial<Record<string, number> & {
792-
[key in keyof Schema as Schema[key] extends number ? key : never]: number
793-
}>
794-
$max: Partial<Record<string, number> & {
795-
[key in keyof Schema as Schema[key] extends number ? key : never]: number
796-
}>
797-
/* Array operators */
798-
$pop: Partial<Record<string, -1 | 1> & {
799-
[key in keyof Schema as Schema[key] extends Array<infer U> ? key : never]: -1 | 1
800-
}>
801-
$push: Partial<Record<string, MongoCommandValue> & {
802-
[key in keyof Schema as Schema[key] extends Array<infer U> ? key : never]: (Schema[key] extends (infer U)[] ? U : never)
803-
| MongoUpdateArrayOperatorModifiers<Schema[key]>
804-
}>
805768
$addToSet: Partial<Record<string, MongoCommandValue> & {
806-
[key in keyof Schema as Schema[key] extends Array<infer U> ? key : never]: (Schema[key] extends (infer U)[] ? U : never)
807-
| MongoUpdateArrayOperatorUniversalModifiers<Schema[key]>
769+
[K in keyof T as T[K] extends [] ? K : never]: (T[K] extends (infer U)[] ? U : never)
770+
| MongoUpdateArrayOperatorUniversalModifiers<T[K]>
808771
}>
772+
809773
$pull: Partial<Record<string, MongoCommandValue> & {
810-
[key in keyof Schema as Schema[key] extends Array<infer U> ? key : never]: (Schema[key] extends (infer U)[] ? U : never)
811-
| MongoQuerySelector<Schema[key]>
812-
}>
813-
$pullAll: Partial<Record<string, MongoCommandValue> & {
814-
[key in keyof Schema as Schema[key] extends Array<infer U> ? key : never]: Schema[key]
774+
[K in keyof T as T[K] extends [] ? K : never]: (T[K] extends (infer U)[] ? U : never)
775+
| MongoQuerySelector<T[K]>
815776
}>
816-
}>
817777

818-
type MongoUpdateArrayOperatorUniversalModifiers<T> = Partial<{
819-
$each: T extends Array<infer U> ? T : T[]
778+
$pullAll: Record<string, MongoCommandValue> & { [K in keyof T as T[K] extends [] ? K : never]?: T[K] }
820779
}>
821780

822-
type MongoUpdateArrayOperatorModifiers<T> = MongoUpdateArrayOperatorUniversalModifiers<T> & Partial<{
823-
$position: number
824-
$slice: number
825-
$sort: 1 | -1
826-
}>
781+
type MongoUpdateArrayOperatorUniversalModifiers<T> = { $each?: T extends [] ? T : T[] }
782+
783+
type MongoUpdateArrayOperatorModifiers<T> = MongoUpdateArrayOperatorUniversalModifiers<T> &
784+
{ $position?: number, $slice?: number, $sort?: 1 | -1 }
827785

828-
type MongoUpdateCommand<Schema extends MongoSchema> = MongoUpdateOperators<Schema>
786+
type MongoUpdateCommand<Schema extends MongoObject> = MongoUpdateOperators<Schema>
829787

830-
type Id = string | number | boolean | Date | Record<string, MongoValue>
831-
type MongoDocument<Schema extends object = object> = Schema & { _id: Id }
788+
type MongoId = Exclude<MongoPrimitive, null> | MongoObject
789+
type MongoDocument<T extends MongoObject> = T & { _id?: MongoId }
832790
type SortOrder = { [key: string]: 1 | -1 | SortOrder }
833791

834-
type Cursor<Doc extends MongoDocument = MongoDocument> = {
835-
/** Returns the first document that satisfies the query. */ first: () => Doc | null
836-
/** Returns an array of documents that satisfy the query. */ array: () => Doc[]
792+
type Cursor<T extends MongoObject> = {
793+
/** Returns the first document that satisfies the query. */ first: () => MongoDocument<T> | null
794+
/** Returns an array of documents that satisfy the query. */ array: () => MongoDocument<T>[]
837795
/** Returns the number of documents that match the query. */ count: () => number
838796

839797
/** Returns the first document that satisfies the query. Also makes cursor unusable. */
840-
first_and_close: () => Doc
798+
first_and_close: () => MongoDocument<T>
841799

842800
/** Returns an array of documents that satisfy the query. Also makes cursor unusable. */
843-
array_and_close: () => Doc[]
801+
array_and_close: () => MongoDocument<T>[]
844802

845803
/** Returns the number of documents that match the query. Also makes cursor unusable. */
846804
count_and_close: () => number
847805

848806
/** Run `callback` on each document that satisfied the query. */
849-
each: (callback: (document: Doc) => void) => null
807+
each: (callback: (document: MongoDocument<T>) => void) => null
850808

851809
/** Returns a new cursor with documents sorted as specified.
852810
* A value of 1 sorts the property ascending, and -1 descending.
853811
* @param order The way the documents are to be sorted. */
854-
sort: (order?: SortOrder) => Cursor<Doc>
812+
sort: (order?: SortOrder) => Cursor<T>
855813

856814
/** Returns a new cursor without the first number of documents.
857815
* @param count Number of documents to skip. */
858-
skip: (count: number) => Cursor<Doc>
816+
skip: (count: number) => Cursor<T>
859817

860818
/** Returns a new cursor limited to a number of documents as specified.
861819
* @param count Number of documents. */
862-
limit: (count: number) => Cursor<Doc>
820+
limit: (count: number) => Cursor<T>
863821

864-
/** @param key The key of the documents. */ distinct: ((key: string) => MongoValue[]) & ((key: "_id") => Id[])
822+
/** @param key The key of the documents. */ distinct: { (key: string): MongoValue[], (key: "_id"): MongoId[] }
865823
/** Make cursor unusable. */ close: () => null
866824
NumberLong: (number: number) => number
825+
// TODO what actually is the type here?
867826
ObjectId: () => any
868827
}
869828

@@ -874,7 +833,9 @@ type CliContext = {
874833
/** The number of rows in the caller’s terminal. */ rows: number
875834

876835
/** The name of the script that directly called this script, or null if called on the command line or as a
877-
* scriptor. */ calling_script: null
836+
* scriptor. */
837+
calling_script: null
838+
878839
is_scriptor?: undefined
879840
is_brain?: undefined
880841
}
@@ -885,12 +846,8 @@ type SubscriptContext = Replace<CliContext, {
885846
calling_script: string
886847
}>
887848

888-
type ScriptorContext =
889-
Replace<CliContext, { /** Whether the script is being run as a scriptor. */ is_scriptor: true }>
890-
891-
type BrainContext =
892-
Replace<CliContext, { /** Whether the script is being run via a bot brain. */ is_brain: true }>
893-
849+
type ScriptorContext = Replace<CliContext, { /** Whether the script is being run as a scriptor. */ is_scriptor: true }>
850+
type BrainContext = Replace<CliContext, { /** Whether the script is being run via a bot brain. */ is_brain: true }>
894851
type Context = CliContext | SubscriptContext | ScriptorContext | BrainContext
895852

896853
/** Subscript space that can call FULLSEC scripts. */ declare const $fs: Fullsec
@@ -939,76 +896,31 @@ type ObjectId = { $oid: string }
939896
declare const $db: {
940897
/** Insert a document or documents into a collection.
941898
* @param documents A document or array of documents to insert into the collection. */
942-
i: <T extends MongoSchema = MongoSchema>(documents: (T & { _id?: Id }) | (T & { _id?: Id })[]) => {
943-
ok: 1
944-
n: number
945-
opTime: { ts: "Undefined Conversion", t: number }
946-
electionId: "Undefined Conversion"
947-
operationTime: "Undefined Conversion"
948-
$clusterTime: {
949-
clusterTime: "Undefined Conversion"
950-
signature: { hash: "Undefined Conversion", keyId: "Undefined Conversion" }
951-
}
952-
}
899+
i: <T extends MongoObject>(documents: (T & { _id?: MongoId }) | (T & { _id?: MongoId })[]) =>
900+
{ n: number, opTime: { t: number }, ok: 0 | 1 }[]
953901

954902
/** Remove documents from a collection.
955903
* @param query Specifies deletion criteria using query operators. */
956-
r: <T extends MongoSchema = MongoSchema>(query: Query<T>) => {
957-
ok: 0 | 1
958-
n: number
959-
opTime: { ts: "Undefined Conversion", t: number }
960-
electionId: "Undefined Conversion"
961-
operationTime: "Undefined Conversion"
962-
$clusterTime: {
963-
clusterTime: "Undefined Conversion"
964-
signature: { hash: "Undefined Conversion", keyId: "Undefined Conversion" }
965-
}
966-
}
904+
r: <T extends MongoObject>(query: Query<T>) => { n: number, opTime: { t: number }, ok: 0 | 1 }[]
967905

968906
/** Find documents in a collection or view and returns a cursor to the selected documents.
969907
* @param query Specifies deletion criteria using query operators.
970908
* @param projection Specifies the fields to return in the documents that match the query filter. */
971-
f: <T extends MongoSchema = MongoSchema>(query?: Query<T>, projection?: Projection<T>) => Cursor<MongoDocument<T>>
909+
f: <T extends MongoObject>(query?: Query<T>, projection?: Projection<T>) => Cursor<T>
972910

973911
/** Update existing documents in a collection.
974912
* @param query Specifies deletion criteria using query operators.
975913
* @param command The modifications to apply.
976914
* {@link https://docs.mongodb.com/manual/reference/method/db.collection.update/#parameters} */
977-
u: <T extends MongoSchema = MongoSchema>(query: Query<T> | Query<T>[], command: MongoUpdateCommand<MongoDocument<T>>) => {
978-
ok: 0 | 1
979-
nModified: number
980-
n: number
981-
opTime: { ts: "Undefined Conversion", t: number }
982-
electionId: "Undefined Conversion"
983-
operationTime: "Undefined Conversion"
984-
$clusterTime: {
985-
clusterTime: "Undefined Conversion"
986-
signature: { hash: "Undefined Conversion", keyId: "Undefined Conversion" }
987-
}
988-
}
915+
u: <T extends MongoObject>(query: Query<T> | Query<T>[], command: MongoUpdateCommand<MongoDocument<T>>) =>
916+
{ n: number, opTime: { t: number }, ok: 0 | 1, nModified: number }[]
989917

990918
/** Updates one document within the collection based on the filter.
991919
* @param query Specifies deletion criteria using query operators.
992920
* @param command The modifications to apply.
993921
* {@link https://docs.mongodb.com/manual/reference/method/db.collection.update/#parameters} */
994-
u1: <T extends MongoSchema = MongoSchema>(query: Query<T> | Query<T>[], command: MongoUpdateCommand<MongoDocument<T>>) => {
995-
ok: 0 | 1
996-
nModified: number
997-
n: number
998-
opTime: {
999-
ts: "Undefined Conversion"
1000-
t: number
1001-
}
1002-
electionId: "Undefined Conversion"
1003-
operationTime: "Undefined Conversion"
1004-
$clusterTime: {
1005-
clusterTime: "Undefined Conversion"
1006-
signature: {
1007-
hash: "Undefined Conversion"
1008-
keyId: "Undefined Conversion"
1009-
}
1010-
}
1011-
}
922+
u1: <T extends MongoObject>(query: Query<T> | Query<T>[], command: MongoUpdateCommand<MongoDocument<T>>) =>
923+
{ n: number, ok: 0 | 1, opTime: { t: number }, nModified: number }[]
1012924

1013925
/** Update or insert document.
1014926
* Same as Update, but if no documents match the query, one document will be inserted based on the properties in
@@ -1017,18 +929,8 @@ declare const $db: {
1017929
* @param query Specifies deletion criteria using query operators.
1018930
* @param command The modifications to apply.
1019931
* {@link https://docs.mongodb.com/manual/reference/method/db.collection.update/#parameters} */
1020-
us: <T extends MongoSchema = MongoSchema>(query: Query<T> | Query<T>[], command: MongoUpdateCommand<MongoDocument<T>>) => {
1021-
ok: 0 | 1
1022-
nModified: number
1023-
n: number
1024-
opTime: { ts: "Undefined Conversion", t: number }
1025-
electionId: "Undefined Conversion"
1026-
operationTime: "Undefined Conversion"
1027-
$clusterTime: {
1028-
clusterTime: "Undefined Conversion"
1029-
signature: { hash: "Undefined Conversion", keyId: "Undefined Conversion" }
1030-
}
1031-
}
932+
us: <T extends MongoObject>(query: Query<T> | Query<T>[], command: MongoUpdateCommand<MongoDocument<T>>) =>
933+
{ n: number, ok: 0 | 1, opTime: { t: number }, nModified: number }[]
1032934

1033935
ObjectId: () => ObjectId
1034936
}

0 commit comments

Comments
 (0)