UNPKG

3.6 kBTypeScriptView Raw
1
2declare module 'mongoose' {
3 import mongodb = require('mongodb');
4
5 class NativeBuffer extends Buffer {}
6
7 namespace Types {
8 class Array<T> extends global.Array<T> {
9 /** Pops the array atomically at most one time per document `save()`. */
10 $pop(): T;
11
12 /** Atomically shifts the array at most one time per document `save()`. */
13 $shift(): T;
14
15 /** Adds values to the array if not already present. */
16 addToSet(...args: any[]): any[];
17
18 isMongooseArray: true;
19
20 /** Pushes items to the array non-atomically. */
21 nonAtomicPush(...args: any[]): number;
22
23 /** Wraps [`Array#push`](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/push) with proper change tracking. */
24 push(...args: any[]): number;
25
26 /**
27 * Pulls items from the array atomically. Equality is determined by casting
28 * the provided value to an embedded document and comparing using
29 * [the `Document.equals()` function.](./api.html#document_Document-equals)
30 */
31 pull(...args: any[]): this;
32
33 /**
34 * Alias of [pull](#mongoosearray_MongooseArray-pull)
35 */
36 remove(...args: any[]): this;
37
38 /** Sets the casted `val` at index `i` and marks the array modified. */
39 set(index: number, val: T): this;
40
41 /** Atomically shifts the array at most one time per document `save()`. */
42 shift(): T;
43
44 /** Returns a native js Array. */
45 toObject(options?: ToObjectOptions): any;
46 toObject<T>(options?: ToObjectOptions): T;
47
48 /** Wraps [`Array#unshift`](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/unshift) with proper change tracking. */
49 unshift(...args: any[]): number;
50 }
51
52 class Buffer extends NativeBuffer {
53 /** Sets the subtype option and marks the buffer modified. */
54 subtype(subtype: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 128 | ToObjectOptions): void;
55
56 /** Converts this buffer to its Binary type representation. */
57 toObject(subtype?: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 128): mongodb.Binary;
58 }
59
60 class Decimal128 extends mongodb.Decimal128 { }
61
62 class DocumentArray<T> extends Types.Array<T extends Types.Subdocument ? T : Types.Subdocument<InferId<T>> & T> {
63 /** DocumentArray constructor */
64 constructor(values: any[]);
65
66 isMongooseDocumentArray: true;
67
68 /** Creates a subdocument casted to this schema. */
69 create(obj: any): T extends Types.Subdocument ? T : Types.Subdocument<InferId<T>> & T;
70
71 /** Searches array items for the first document with a matching _id. */
72 id(id: any): (T extends Types.Subdocument ? T : Types.Subdocument<InferId<T>> & T) | null;
73
74 push(...args: (AnyKeys<T> & AnyObject)[]): number;
75 }
76
77 class Map<V> extends global.Map<string, V> {
78 /** Converts a Mongoose map into a vanilla JavaScript map. */
79 toObject(options?: ToObjectOptions & { flattenMaps?: boolean }): any;
80 }
81
82 class ObjectId extends mongodb.ObjectId {
83 _id: this;
84 }
85
86 class Subdocument<IdType = any> extends Document<IdType> {
87 $isSingleNested: true;
88
89 /** Returns the top level document of this sub-document. */
90 ownerDocument(): Document;
91
92 /** Returns this sub-documents parent document. */
93 parent(): Document;
94
95 /** Returns this sub-documents parent document. */
96 $parent(): Document;
97 }
98
99 class ArraySubdocument<IdType = any> extends Subdocument<IdType> {
100 /** Returns this sub-documents parent array. */
101 parentArray(): Types.DocumentArray<unknown>;
102 }
103 }
104}