-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapter.ts
389 lines (338 loc) · 9.85 KB
/
adapter.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
// deno-lint-ignore-file no-explicit-any
import {
_,
commons,
AdapterParams,
AdapterQuery,
AdapterServiceOptions,
CountOptions,
DeleteOptions,
FindOptions,
Id,
InsertOptions,
NotFound,
NullableId,
ObjectId,
PaginationOptions,
UpdateOptions,
Filter
} from "./deps.ts";
import type { Collection } from "./deps.ts";
import { errorHandler } from "./errorHandler.ts";
const {
AdapterBase,
select
} = commons;
export interface Paginated<T> {
total: number;
limit: number;
skip: number;
data: T[];
}
interface MongoAdapterOptions<Item = any> extends AdapterServiceOptions {
Model: Collection<Item> | Promise<Collection<Item>>;
disableObjectify?: boolean;
useEstimatedDocumentCount?: boolean;
}
export interface MongoAdapterParams<Item = any, Query = AdapterQuery>
extends Omit<AdapterParams<Query, Partial<MongoAdapterOptions>>, "query"> {
mongo?:
| FindOptions
| InsertOptions
| DeleteOptions
| CountOptions
| UpdateOptions;
query?: Filter<Item> & AdapterQuery
}
export class MongoAdapter<
Item,
Body = Partial<Item>,
Params extends MongoAdapterParams<any> = MongoAdapterParams<Item>,
> extends AdapterBase<Item, Body, Params, MongoAdapterOptions> {
constructor(options: MongoAdapterOptions) {
if (!options) {
throw new Error("Mongo options have to be provided");
}
super({
id: "_id",
...options,
});
}
getObjectId(id: Id | ObjectId) {
if (this.options.disableObjectify) {
return id;
}
if (this.id === "_id" && ObjectId.isValid(id)) {
id = new ObjectId(id.toString());
}
return id;
}
optionsFilter(id: NullableId, params: Params) {
const { $select, $sort, $limit, $skip, ...query } =
(params.query || {}) as AdapterQuery;
if (id !== null) {
query.$and = (query.$and || []).concat({
[this.id]: this.getObjectId(id),
});
}
if (query[this.id]) {
query[this.id] = this.getObjectId(query[this.id]);
}
return {
options: {
select: $select,
sort: $sort,
limit: $limit,
skip: $skip,
},
filter: query,
};
}
getSelect(select: string[] | Record<string, number>) {
if (Array.isArray(select)) {
return select.reduce<Record<string, number>>(
(value, name) => ({
...value,
[name]: 1,
}),
{},
);
}
}
async $findOrGet(
id: NullableId,
params: Params,
): Promise<Item | Item[] | Paginated<Item>> {
return id === null ? await this.$find(params) : await this.$get(id, params);
}
normalizeId(id: NullableId, data: Partial<Body>): Partial<Body> {
if (this.id === "_id") {
// Default Mongo IDs cannot be updated. The Mongo library handles
// this automatically.
return _.omit(data, this.id);
} else if (id !== null) {
// If not using the default Mongo _id field set the ID to its
// previous value. This prevents orphaned documents.
return {
...data,
[this.id]: id,
};
} else {
return data;
}
}
$get(id: Id, params: Params = {} as Params): Promise<Item> {
const { Model } = this.getOptions(params);
const {
filter,
options: { select },
} = this.optionsFilter(id, params);
const projection = select
? {
projection: {
...this.getSelect(select),
[this.id]: 1,
},
}
: {};
const findOptions: FindOptions = {
...params.mongo,
...projection,
};
return Promise.resolve(Model)
.then((model) => model.findOne(filter, findOptions))
.then((data) => {
if (data == null) {
throw new NotFound(`No record found for id '${id}`);
}
return data;
})
.catch(errorHandler);
}
async $find(
params?: Params & { paginate?: PaginationOptions },
): Promise<Paginated<Item>>;
async $find(params?: Params & { paginate: false }): Promise<Item[]>;
async $find(params?: Params): Promise<Paginated<Item> | Item[]>;
async $find(
params: Params = {} as Params,
): Promise<Paginated<Item> | Item[]> {
const { options, filter } = this.optionsFilter(null, params);
const { paginate, Model, useEstimatedDocumentCount } = this.getOptions(
params,
);
const findOptions = { ...params.mongo } as FindOptions;
const model = await Promise.resolve(Model);
if (options.select !== undefined) {
// findOptions.projection = this.getSelect(options.select);
}
const cursor = model.find(filter, findOptions);
if (options.sort !== undefined) {
cursor.sort(options.sort);
}
if (options.limit !== undefined) {
cursor.limit(options.limit);
}
if (options.skip !== undefined) {
cursor.skip(options.skip);
}
const runQuery = async (total: number) => ({
total,
limit: options.limit as number,
skip: options.skip || 0,
data: options.limit === 0 ? [] : ((await cursor.toArray()) as Item[]),
});
if (paginate && paginate.default) {
if (!options.limit) options.limit = paginate.default;
if (paginate.max && options.limit > paginate.max) {
options.limit = paginate.max;
}
if (useEstimatedDocumentCount) {
return model.estimatedDocumentCount().then(runQuery);
}
return model.countDocuments(filter, findOptions).then(runQuery);
}
return runQuery(0).then((page) => page.data);
}
async $create(data: Partial<Body>, params?: Params): Promise<Item>;
async $create(data: Partial<Body>[], params?: Params): Promise<Item[]>;
async $create(
data: Partial<Body> | Partial<Body>[],
_params?: Params,
): Promise<Item | Item[]>;
async $create(
data: Partial<Body> | Partial<Body>[],
params: Params = {} as Params,
): Promise<Item | Item[]> {
const writeOptions = { ...params.mongo };
const { Model } = this.getOptions(params);
const model = await Promise.resolve(Model);
const setId = (item: any) => {
const entry = Object.assign({}, item);
// Generate an ObjectId if we use a custom id
if (this.id !== "_id" && typeof entry[this.id] === "undefined") {
return {
...entry,
[this.id]: new ObjectId().toHexString(),
};
}
return entry;
};
const promise = Array.isArray(data)
? model
.insertMany(data.map(setId), writeOptions as InsertOptions)
.then((result) => {
return model
.find({
_id: {
$in: result.insertedIds
}
}, params.mongo as FindOptions)
.toArray()
})
: model
.insertOne(setId(data), writeOptions as InsertOptions)
.then(id => model.findOne({ _id: id }, params.mongo as FindOptions))
return promise
.then(select(params, this.id))
.catch(errorHandler);
}
async $patch(id: null, data: Partial<Body>, params?: Params): Promise<Item[]>;
async $patch(id: Id, data: Partial<Body>, params?: Params): Promise<Item>;
async $patch(
id: NullableId,
data: Partial<Body>,
_params?: Params,
): Promise<Item | Item[]>;
async $patch(
id: NullableId,
_data: Partial<Body>,
params: Params = {} as Params,
): Promise<Item | Item[]> {
const data = this.normalizeId(id, _data);
const { Model } = this.getOptions(params);
const model = await Promise.resolve(Model);
const {
filter,
options: { select },
} = this.optionsFilter(id, params);
const updateOptions = { ...params.mongo } as UpdateOptions;
const modifier = Object.keys(data).reduce((current, key) => {
const value = data[key as keyof typeof data];
if (key.charAt(0) !== "$") {
current.$set = {
...current.$set,
[key]: value,
};
} else {
current[key] = value;
}
return current;
}, {} as any);
const originalIds = await this.$findOrGet(id, {
...params,
filter: {
...filter,
$select: [this.id],
},
paginate: false,
}) as Item;
const items = Array.isArray(originalIds) ? originalIds : [originalIds];
const idList = items.map((item: any) => item[this.id]) as ObjectId[];
const findParams: Params = {
...params,
paginate: false,
filter: {
[this.id]: {
$in: idList,
},
$select: select,
},
};
await model.updateMany(filter, modifier, updateOptions);
return (this.$findOrGet(id, findParams) as Promise<Item | Item[]>)
.catch(errorHandler);
}
async $update(
id: Id,
data: Body,
params: Params = {} as Params,
): Promise<Item> {
const { Model } = this.getOptions(params);
const model = await Promise.resolve(Model);
const { filter } = this.optionsFilter(id, params);
const replaceOptions = { ...params.mongo } as UpdateOptions;
await model.replaceOne(filter, this.normalizeId(id, data), replaceOptions);
return (this.$findOrGet(id, params) as Promise<Item>)
.catch(errorHandler);
}
async $remove(id: null, params?: Params): Promise<Item[]>;
async $remove(id: Id, params?: Params): Promise<Item>;
async $remove(id: NullableId, _params?: Params): Promise<Item | Item[]>;
async $remove(
id: NullableId,
params: Params = {} as Params,
): Promise<Item | Item[]> {
const { Model } = this.getOptions(params);
const model = await Promise.resolve(Model);
const {
filter,
options: { select },
} = this.optionsFilter(id, params);
const deleteOptions = { ...params.mongo } as DeleteOptions;
const findParams = {
...params,
paginate: false,
filter: {
...filter,
$select: select,
},
} as Params;
return (this.$findOrGet(id, findParams) as Promise<Item>)
.then(async (items) => {
await model.deleteMany(filter, deleteOptions);
return items;
})
.catch(errorHandler);
}
}