-
Notifications
You must be signed in to change notification settings - Fork 122
/
index.d.ts
165 lines (156 loc) · 3.4 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
declare module "moleculer-db-adapter-mongoose" {
import { Service, ServiceBroker } from "moleculer";
import {
ConnectOptions ,
Document,
Query as DocumentQuery, //reff: https://github.com/Automattic/mongoose/issues/10036#issuecomment-803144616
Model,
Schema,
} from "mongoose";
import { Db } from "mongodb";
type HasModelOrSchema<T extends Document> =
| {
model: Model<T>;
}
| {
schema: Schema;
modelName: string;
};
/**
* Parameters to {@link MongooseDbAdapter.count}
*/
interface CountFilters {
query?: any;
search?: string;
searchFields?: string[]; // never used?
}
/**
* Parameters to {@link MongooseDbAdapter.createCursor}
*/
interface FindFilters {
query?: any;
search?: string;
searchFields?: string[]; // never used???
sort?: string | string[];
offset?: number;
limit?: number;
}
class MongooseDbAdapter<TDocument extends Document> {
uri: string;
opts?: ConnectOptions;
broker: ServiceBroker;
service: Service;
model: Model<TDocument>;
schema?: Schema;
modelName?: string;
db: Db;
/**
* Creates an instance of MongooseDbAdapter.
*/
constructor(uri: string, opts?: ConnectOptions);
/**
* Initialize adapter
*/
init(
broker: ServiceBroker,
service: Service & HasModelOrSchema<TDocument>
): void;
/**
* Connect to database
*/
connect(): Promise<void>;
/**
* Disconnect from database
*/
disconnect(): Promise<void>;
/**
* Find all entities by filters.
*
* Available filter props:
* - limit
* - offset
* - sort
* - search
* - searchFields
* - query
*/
find(filters: FindFilters): Promise<TDocument[]>;
/**
* Find an entity by query
*/
findOne(query: any): Promise<TDocument | null>;
/**
* Find an entities by ID
*/
findById(_id: any): Promise<TDocument | null>;
/**
* Find any entities by IDs
*/
findByIds(idList: any[]): Promise<TDocument[]>;
/**
* Get count of filtered entites
*
* Available filter props:
* - search
* - searchFields
* - query
*/
count(filters?: CountFilters): Promise<number>;
/**
* Insert an entity
*/
insert(entity: any): Promise<TDocument>;
/**
* Insert many entities
*/
insertMany(entities: any[]): Promise<TDocument[]>;
/**
* Update many entities by `query` and `update`
*/
updateMany(query: any, update: any): Promise<number>;
/**
* Update an entity by ID and `update`
*/
updateById(
_id: any,
update: any
): DocumentQuery<TDocument | null, TDocument>;
/**
* Remove entities which are matched by `query`
*/
removeMany(query: any): Promise<number>;
/**
* Remove an entity by ID
*/
removeById(_id: any): DocumentQuery<TDocument | null, TDocument>;
/**
* Clear all entities from collection
*/
clear(): Promise<number>;
/**
* Convert DB entity to JSON object
*/
entityToObject(entity: any): any;
/**
* Create a filtered query
* Available filters in `params`:
* - search
* - sort
* - limit
* - offset
* - query
*/
createCursor(
params: FindFilters
): DocumentQuery<TDocument[], TDocument>;
/**
* Transforms 'idField' into MongoDB's '_id'
*/
beforeSaveTransformID(entity: object, idField: string): object;
/**
* Transforms MongoDB's '_id' into user defined 'idField'
*/
afterRetrieveTransformID(entity: object, idField: string): object;
}
export = MongooseDbAdapter;
}