UNPKG

1.87 kBTypeScriptView Raw
1/// <reference types="mongoose" />
2
3declare module "mongoose" {
4 interface ListOptions {
5 filter?: object;
6 lean?: boolean;
7 limit?: number;
8 offset?: number;
9 populate?: string | [string];
10 select?: string | [string];
11 sort?: string | [string];
12 group?: string | [string];
13 }
14
15 interface Model<T extends Document>
16 extends NodeJS.EventEmitter,
17 ModelProperties {
18 /**
19 * Get one document by id.
20 *
21 * @param id id of document
22 * @param populate which fields want to populate
23 */
24 get(id: string, populate?: string): Promise<T>;
25
26 /**
27 * Update or Create a document with given id
28 *
29 * @param id given id
30 * @param update document content
31 */
32 upsert(id: string, update: T): Promise<T>;
33
34 /**
35 * List documents with limit and offset
36 *
37 * @param opt list options
38 */
39 list(opt?: ListOptions): Promise<Array<T>>;
40
41 /**
42 * Count documents
43 *
44 * @param filter mongo filter
45 */
46 count(filter?: object): Promise<number>;
47
48 /**
49 * Soft delete document by id
50 *
51 * @param id id of document
52 */
53 softDelete(id: string): Promise<T>;
54 }
55
56 interface Document
57 extends MongooseDocument,
58 NodeJS.EventEmitter,
59 ModelProperties {
60 /**
61 * Soft delete itself.
62 * It will set the deleted flag.
63 */
64 softDelete(): Promise<this>;
65 /**
66 * Restore itself.
67 * It will unset the deleted flag.
68 */
69 restore(): Promise<this>;
70 }
71}
72
73declare module "@36node/mongoose-helper" {
74 import mongoose = require("mongoose");
75
76 /**
77 * Mongoose helper for 36node team.
78 */
79 export function helper(schema: mongoose.Schema, options?: object): void;
80 export const defaultOptions = {
81 timestamps: true,
82 toJSON: {
83 virtuals: true,
84 },
85 toObject: {
86 virtuals: true,
87 },
88 };
89}