/
collection.ts
334 lines (274 loc) · 12.3 KB
/
collection.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
/* eslint-disable @typescript-eslint/naming-convention */
/* eslint-disable no-underscore-dangle */
import {
AggregateResult,
Aggregation,
BaseCollection,
Caller,
DataSource,
Filter,
PaginatedFilter,
Projection,
RecordData,
ValidationError,
} from '@forestadmin/datasource-toolkit';
import { Error, Model, PipelineStage } from 'mongoose';
import MongooseSchema from './mongoose/schema';
import { Stack } from './types';
import addNullValues from './utils/add-null-values';
import {
buildSubdocumentPatch,
compareIds,
escape,
groupIdsByPath,
replaceMongoTypes,
splitId,
unflattenRecord,
} from './utils/helpers';
import FilterGenerator from './utils/pipeline/filter';
import GroupGenerator from './utils/pipeline/group';
import LookupGenerator from './utils/pipeline/lookup';
import ProjectionGenerator from './utils/pipeline/projection';
import ReparentGenerator from './utils/pipeline/reparent';
import VirtualFieldsGenerator from './utils/pipeline/virtual-fields';
import FieldsGenerator from './utils/schema/fields';
export default class MongooseCollection extends BaseCollection {
model: Model<unknown>;
stack: Stack;
constructor(dataSource: DataSource, model: Model<unknown>, stack: Stack) {
const { prefix } = stack[stack.length - 1];
const name = escape(prefix ? `${model.modelName}.${prefix}` : model.modelName);
super(name, dataSource, { mongoose: model.base, model });
this.model = model;
this.stack = stack;
this.enableCount();
this.addFields(FieldsGenerator.buildFieldsSchema(model, stack));
}
async list(
caller: Caller,
filter: PaginatedFilter,
projection: Projection,
): Promise<RecordData[]> {
return this._list(filter, projection);
}
private async _list(filter: PaginatedFilter, projection: Projection): Promise<RecordData[]> {
const lookupProjection = projection.union(
filter.conditionTree?.projection,
filter.sort?.projection,
);
const pipeline = [
...this.buildBasePipeline(filter, lookupProjection),
...ProjectionGenerator.project(projection),
];
return addNullValues(replaceMongoTypes(await this.model.aggregate(pipeline)), projection);
}
async aggregate(
caller: Caller,
filter: Filter,
aggregation: Aggregation,
limit?: number,
): Promise<AggregateResult[]> {
const lookupProjection = aggregation.projection.union(filter.conditionTree?.projection);
const rows = await this.model.aggregate([
...this.buildBasePipeline(filter, lookupProjection),
...GroupGenerator.group(aggregation),
{ $sort: { value: -1 as const } },
...(limit ? [{ $limit: limit }] : []),
]);
return replaceMongoTypes(rows);
}
async create(caller: Caller, data: RecordData[]): Promise<RecordData[]> {
return this.handleValidationError(() => this._create(caller, data));
}
async update(caller: Caller, filter: Filter, patch: RecordData): Promise<void> {
return this.handleValidationError(() => this._update(caller, filter, patch));
}
async delete(caller: Caller, filter: Filter): Promise<void> {
return this.handleValidationError(() => this._delete(caller, filter));
}
private async _create(caller: Caller, flatData: RecordData[]): Promise<RecordData[]> {
const { asFields } = this.stack[this.stack.length - 1];
const data = flatData.map(fd => unflattenRecord(fd, asFields));
// For root models, we can simply insert the records.
if (this.stack.length < 2) {
const { insertedIds } = await this.model.insertMany(data, { rawResult: true });
return flatData.map((flatRecord, index) => ({
_id: replaceMongoTypes(insertedIds[index]),
...flatRecord,
}));
}
// Only array fields can create subdocuments (the others should use update)
const schema = MongooseSchema.fromModel(this.model).applyStack(this.stack);
if (schema.isArray) {
return this.createForArraySubfield(data, flatData, schema);
}
return this.createForObjectSubfield(data, flatData);
}
private computeSubFieldName() {
const lastStackStep = this.stack[this.stack.length - 1];
return this.stack.length > 2
? lastStackStep.prefix.substring(this.stack[this.stack.length - 2].prefix.length + 1)
: lastStackStep.prefix;
}
private async createForArraySubfield(
data: RecordData[],
flatData: RecordData[],
schema: MongooseSchema,
) {
const fieldName = this.computeSubFieldName();
const updates: Record<string, { rootId: unknown; path: string; records: unknown[] }> = {};
const results = [];
for (const [index, record] of data.entries()) {
const flatRecord = flatData[index];
const { parentId, ...rest } = record;
if (!parentId) throw new ValidationError('Trying to create a subrecord with no parent');
const [rootId, path] = splitId(`${parentId}.${fieldName}`);
const rootIdString = String(rootId);
if (!updates[rootIdString]) updates[rootIdString] = { rootId, path, records: [] };
// unwrap 'content' on leafs
updates[rootIdString].records.push(schema.isLeaf ? rest.content : rest);
results.push({
_id: `${rootId}.${path}.${updates[rootIdString].records.length - 1}`,
...flatRecord,
});
}
// Apply the modifications to the root document.
const promises = Object.values(updates).map(({ rootId, path, records }) =>
this.model.updateOne(
{ _id: rootId },
{ $push: { [path]: { $position: 0, $each: records } } },
),
);
await Promise.all(promises);
return results;
}
private async createForObjectSubfield(data: RecordData[], flatData: RecordData[]) {
if (data.length > 1) throw new ValidationError('Trying to create multiple subrecords at once');
if (data.length === 0) throw new ValidationError('Trying to create without data');
const fieldName = this.computeSubFieldName();
const { parentId, ...rest } = data[0];
if (!parentId) throw new ValidationError('Trying to create a subrecord with no parent');
const [rootId, path] = splitId(`${parentId}.${fieldName}`);
await this.model.updateOne({ _id: rootId }, { $set: { [path]: rest } });
return [{ _id: `${rootId}.${path}`, ...flatData[0] }];
}
private async _update(caller: Caller, filter: Filter, flatPatch: RecordData): Promise<void> {
const { asFields } = this.stack[this.stack.length - 1];
const patch = unflattenRecord(flatPatch, asFields, true);
// Fetch the ids of the documents OR subdocuments that will be updated.
// We need to do that regardless of `this.prefix` because the filter may contain conditions on
// relationships.
const records = await this._list(filter, new Projection('_id'));
const ids = records.map(record => record._id);
if (this.stack.length < 2) {
// We are updating a real document, we can delegate the work to mongoose directly.
await (ids.length > 1
? this.model.updateMany({ _id: ids }, patch, { rawResult: true })
: this.model.updateOne({ _id: ids }, patch, { rawResult: true }));
} else if (patch.parentId && ids.some(id => !id.startsWith(patch.parentId))) {
// When we update subdocuments, we need to make sure that the new parent is the same as the
// old one: reparenting is not supported.
throw new ValidationError(`'${this.name}' is virtual: records cannot be reparented.`);
} else {
// We are updating a subdocument (this.prefix is set).
// This method can be called from customer code, so we need to handle the case where we are
// updating many documents at once (the GUI only allows to update documents one by one).
// This is trivial when using the flattener on simple objects, but becomes more convoluted
// when using arrays: we need to update the right element of the array in each document.
// For performance reasons we group the ids by path (which contain the indexes of the
// potentially nested arrays) instead of performing one update per doc.
// Also note, that when we are using a single field as a model, an extra level of nesting is
// added (this is common when using the flattener to create many to many relationships)
const { isLeaf } = MongooseSchema.fromModel(this.model).applyStack(this.stack);
// `idsByPath` contains one entry if using the flattener in object-mode, but potentially many
// if using the flattener in array-mode.
const idsByPath = groupIdsByPath(ids);
// Perform the updates.
const promises = Object.entries(idsByPath).map(([path, rootIds]) => {
// When using object-mode flattener, path == this.prefix.
// When using array-mode flattener, path == this.prefix + '.0' (or '.1', etc).
// (Both can be used at the same time as this is a recursive process).
const subdocPatch = buildSubdocumentPatch(path, patch, isLeaf);
if (!Object.keys(subdocPatch).length) return null;
return ids.length > 1
? this.model.updateMany({ _id: rootIds }, subdocPatch, { rawResult: true })
: this.model.updateOne({ _id: rootIds }, subdocPatch, { rawResult: true });
});
await Promise.all(promises);
}
}
private async _delete(caller: Caller, filter: Filter): Promise<void> {
const records = await this._list(filter, new Projection('_id'));
const ids = records.map(record => record._id);
if (this.stack.length < 2) {
await this.model.deleteMany({ _id: ids }, { rawResult: true });
return;
}
const schema = MongooseSchema.fromModel(this.model).applyStack(this.stack);
const idsByPath = groupIdsByPath(ids);
if (schema.isArray) {
// Iterate paths in reverse order so that when we delete elements from arrays, the indexes
// of the next elements that we'll touch don't change.
for (const path of Object.keys(idsByPath).sort(compareIds).reverse()) {
const arrayPath = path.substring(0, path.lastIndexOf('.'));
const index = Number(path.substring(path.lastIndexOf('.') + 1));
// There is no update operator to pop items out of arrays at known positions
// => we use an aggregation pipeline in the update operation
// @see https://jira.mongodb.org/browse/SERVER-1014?focusedCommentId=2305681#comment-2305681
const newArrayValue = [
{ $slice: [`$${arrayPath}`, index] },
{ $slice: [`$${arrayPath}`, index + 1, { $size: `$${arrayPath}` }] },
];
// When updating arrays, indexes will change with each request so we need to perform the
// request sequentially.
// eslint-disable-next-line no-await-in-loop
await this.model.collection.updateMany(
{ _id: { $in: idsByPath[path] } } as unknown,
[{ $set: { [arrayPath]: { $concatArrays: newArrayValue } } }],
{},
);
}
} else {
const promises = Object.entries(idsByPath).map(([path, pathIds]) =>
this.model.collection.updateMany(
{ _id: { $in: pathIds } } as unknown,
{ $unset: { [path]: '' } },
{},
),
);
await Promise.all(promises);
}
}
private buildBasePipeline(
filter: PaginatedFilter,
lookupProjection: Projection,
): PipelineStage[] {
const fieldsUsedInFilters = FilterGenerator.listRelationsUsedInFilter(filter);
return [
...ReparentGenerator.reparent(this.model, this.stack),
...VirtualFieldsGenerator.addVirtual(this.model, this.stack, lookupProjection),
// For performance reasons, we want to only include the relationships that are used in filters
// before applying the filters
...LookupGenerator.lookup(this.model, this.stack, lookupProjection, {
include: fieldsUsedInFilters,
}),
...FilterGenerator.filter(this.model, this.stack, filter),
// Here are the remaining relationships that are not used in filters. For performance reasons
// they are computed after the filters.
...LookupGenerator.lookup(this.model, this.stack, lookupProjection, {
exclude: fieldsUsedInFilters,
}),
];
}
private async handleValidationError<T>(callback: () => Promise<T>): Promise<T> {
try {
// Do not remove the await here, it's important!
return await callback();
} catch (error) {
if (error instanceof Error.ValidationError) {
throw new ValidationError(error.message);
}
throw error;
}
}
}