@aneuhold/be-ts-db-lib 4.0.1 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +23 -1
- package/lib/index.d.ts +2 -1
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +2 -1
- package/lib/index.js.map +1 -1
- package/lib/index.ts +2 -0
- package/lib/repositories/BaseRepository.d.ts +33 -16
- package/lib/repositories/BaseRepository.d.ts.map +1 -1
- package/lib/repositories/BaseRepository.js +51 -13
- package/lib/repositories/BaseRepository.js.map +1 -1
- package/lib/repositories/BaseRepository.ts +88 -38
- package/lib/repositories/dashboard/DashboardBaseRepository.d.ts +12 -1
- package/lib/repositories/dashboard/DashboardBaseRepository.d.ts.map +1 -1
- package/lib/repositories/dashboard/DashboardBaseRepository.js +30 -1
- package/lib/repositories/dashboard/DashboardBaseRepository.js.map +1 -1
- package/lib/repositories/dashboard/DashboardBaseRepository.ts +52 -2
- package/lib/repositories/dashboard/DashboardBaseWithUserIdRepository.d.ts +25 -0
- package/lib/repositories/dashboard/DashboardBaseWithUserIdRepository.d.ts.map +1 -0
- package/lib/repositories/dashboard/DashboardBaseWithUserIdRepository.js +70 -0
- package/lib/repositories/dashboard/DashboardBaseWithUserIdRepository.js.map +1 -0
- package/lib/repositories/dashboard/DashboardBaseWithUserIdRepository.ts +101 -0
- package/lib/repositories/dashboard/DashboardNonogramKatanaItemRepository.d.ts +2 -9
- package/lib/repositories/dashboard/DashboardNonogramKatanaItemRepository.d.ts.map +1 -1
- package/lib/repositories/dashboard/DashboardNonogramKatanaItemRepository.js +9 -19
- package/lib/repositories/dashboard/DashboardNonogramKatanaItemRepository.js.map +1 -1
- package/lib/repositories/dashboard/DashboardNonogramKatanaItemRepository.ts +9 -25
- package/lib/repositories/dashboard/DashboardNonogramKatanaUpgradeRepository.d.ts +2 -9
- package/lib/repositories/dashboard/DashboardNonogramKatanaUpgradeRepository.d.ts.map +1 -1
- package/lib/repositories/dashboard/DashboardNonogramKatanaUpgradeRepository.js +9 -19
- package/lib/repositories/dashboard/DashboardNonogramKatanaUpgradeRepository.js.map +1 -1
- package/lib/repositories/dashboard/DashboardNonogramKatanaUpgradeRepository.ts +9 -25
- package/lib/repositories/dashboard/DashboardTaskRepository.d.ts +12 -5
- package/lib/repositories/dashboard/DashboardTaskRepository.d.ts.map +1 -1
- package/lib/repositories/dashboard/DashboardTaskRepository.js +77 -8
- package/lib/repositories/dashboard/DashboardTaskRepository.js.map +1 -1
- package/lib/repositories/dashboard/DashboardTaskRepository.ts +104 -10
- package/lib/repositories/dashboard/DashboardUserConfigRepository.d.ts +11 -6
- package/lib/repositories/dashboard/DashboardUserConfigRepository.d.ts.map +1 -1
- package/lib/repositories/dashboard/DashboardUserConfigRepository.js +24 -16
- package/lib/repositories/dashboard/DashboardUserConfigRepository.js.map +1 -1
- package/lib/repositories/dashboard/DashboardUserConfigRepository.ts +39 -16
- package/lib/services/RepoSubscriptionService.d.ts +7 -6
- package/lib/services/RepoSubscriptionService.d.ts.map +1 -1
- package/lib/services/RepoSubscriptionService.js.map +1 -1
- package/lib/services/RepoSubscriptionService.ts +13 -6
- package/lib/util/DbOperationMetaData.d.ts +47 -0
- package/lib/util/DbOperationMetaData.d.ts.map +1 -0
- package/lib/util/DbOperationMetaData.js +58 -0
- package/lib/util/DbOperationMetaData.js.map +1 -0
- package/lib/util/DbOperationMetaData.ts +65 -0
- package/package.json +11 -10
|
@@ -11,6 +11,7 @@ import type {
|
|
|
11
11
|
} from 'mongodb';
|
|
12
12
|
import type { RepoListeners, RepoSubscribers } from '../services/RepoSubscriptionService.js';
|
|
13
13
|
import RepoSubscriptionService from '../services/RepoSubscriptionService.js';
|
|
14
|
+
import type DbOperationMetaData from '../util/DbOperationMetaData.js';
|
|
14
15
|
import DocumentCleaner from '../util/DocumentCleaner.js';
|
|
15
16
|
import DocumentDb from '../util/DocumentDb.js';
|
|
16
17
|
import type IValidator from '../validators/BaseValidator.js';
|
|
@@ -21,15 +22,15 @@ import type IValidator from '../validators/BaseValidator.js';
|
|
|
21
22
|
* Implementation note: I have tried to do the types correctly here, but kept struggling with
|
|
22
23
|
* MongoDB's types around Filter<T> and _id fields.
|
|
23
24
|
*
|
|
24
|
-
* @template
|
|
25
|
+
* @template TBaseType - The type of the documents in the collection.
|
|
25
26
|
*/
|
|
26
|
-
export default abstract class BaseRepository<
|
|
27
|
+
export default abstract class BaseRepository<TBaseType extends BaseDocument> {
|
|
27
28
|
protected collectionName: string;
|
|
28
29
|
|
|
29
|
-
private collection?: Collection<
|
|
30
|
+
private collection?: Collection<TBaseType>;
|
|
30
31
|
|
|
31
|
-
protected subscribers: RepoSubscribers<
|
|
32
|
-
RepoSubscriptionService.getDefaultSubscribers<
|
|
32
|
+
protected subscribers: RepoSubscribers<TBaseType> =
|
|
33
|
+
RepoSubscriptionService.getDefaultSubscribers<TBaseType>();
|
|
33
34
|
|
|
34
35
|
/**
|
|
35
36
|
* Constructs a new base repository.
|
|
@@ -41,9 +42,9 @@ export default abstract class BaseRepository<TBasetype extends BaseDocument> {
|
|
|
41
42
|
*/
|
|
42
43
|
constructor(
|
|
43
44
|
collectionName: string,
|
|
44
|
-
private validator: IValidator<
|
|
45
|
-
private defaultFilter?: Partial<
|
|
46
|
-
private defaultUpdateCleaner?: (doc: Partial<
|
|
45
|
+
private validator: IValidator<TBaseType>,
|
|
46
|
+
private defaultFilter?: Partial<TBaseType>,
|
|
47
|
+
private defaultUpdateCleaner?: (doc: Partial<TBaseType>) => Partial<TBaseType>
|
|
47
48
|
) {
|
|
48
49
|
this.collectionName = collectionName;
|
|
49
50
|
}
|
|
@@ -53,7 +54,7 @@ export default abstract class BaseRepository<TBasetype extends BaseDocument> {
|
|
|
53
54
|
*
|
|
54
55
|
* @returns The collection.
|
|
55
56
|
*/
|
|
56
|
-
protected async getCollection(): Promise<Collection<
|
|
57
|
+
protected async getCollection(): Promise<Collection<TBaseType>> {
|
|
57
58
|
if (!this.collection) {
|
|
58
59
|
this.collection = await DocumentDb.getCollection(this.collectionName);
|
|
59
60
|
this.setupSubscribers();
|
|
@@ -69,7 +70,7 @@ export default abstract class BaseRepository<TBasetype extends BaseDocument> {
|
|
|
69
70
|
*
|
|
70
71
|
* @param listeners - The listeners to register.
|
|
71
72
|
*/
|
|
72
|
-
subscribeToChanges(listeners: RepoListeners<
|
|
73
|
+
subscribeToChanges(listeners: RepoListeners<TBaseType>) {
|
|
73
74
|
const { insertNew, updateOne, updateMany, deleteOne, deleteList } = listeners;
|
|
74
75
|
if (insertNew) {
|
|
75
76
|
this.subscribers.insertNew.push(insertNew);
|
|
@@ -92,16 +93,17 @@ export default abstract class BaseRepository<TBasetype extends BaseDocument> {
|
|
|
92
93
|
* Inserts a new document into the collection.
|
|
93
94
|
*
|
|
94
95
|
* @param newDoc - The new document to insert.
|
|
96
|
+
* @param meta - Tracks database operation metadata for a single request.
|
|
95
97
|
* @returns The inserted document or null if the insertion failed.
|
|
96
98
|
*/
|
|
97
|
-
async insertNew(newDoc:
|
|
99
|
+
async insertNew(newDoc: TBaseType, meta?: DbOperationMetaData): Promise<TBaseType | null> {
|
|
98
100
|
const collection = await this.getCollection();
|
|
99
101
|
await this.validator.validateNewObject(newDoc);
|
|
100
|
-
const insertResult = await collection.insertOne(newDoc as OptionalUnlessRequiredId<
|
|
102
|
+
const insertResult = await collection.insertOne(newDoc as OptionalUnlessRequiredId<TBaseType>);
|
|
101
103
|
if (!insertResult.acknowledged) {
|
|
102
104
|
return null;
|
|
103
105
|
}
|
|
104
|
-
await Promise.all(this.subscribers.insertNew.map((subscriber) => subscriber(newDoc)));
|
|
106
|
+
await Promise.all(this.subscribers.insertNew.map((subscriber) => subscriber(newDoc, meta)));
|
|
105
107
|
return newDoc;
|
|
106
108
|
}
|
|
107
109
|
|
|
@@ -109,18 +111,19 @@ export default abstract class BaseRepository<TBasetype extends BaseDocument> {
|
|
|
109
111
|
* Inserts multiple new documents into the collection.
|
|
110
112
|
*
|
|
111
113
|
* @param newDocs - The new documents to insert.
|
|
114
|
+
* @param meta - Tracks database operation metadata for a single request.
|
|
112
115
|
* @returns The inserted documents or an empty array if the insertion failed.
|
|
113
116
|
*/
|
|
114
|
-
async insertMany(newDocs:
|
|
117
|
+
async insertMany(newDocs: TBaseType[], meta?: DbOperationMetaData): Promise<TBaseType[]> {
|
|
115
118
|
const collection = await this.getCollection();
|
|
116
119
|
await Promise.all(newDocs.map((doc) => this.validator.validateNewObject(doc)));
|
|
117
120
|
const insertResult = await collection.insertMany(
|
|
118
|
-
newDocs as OptionalUnlessRequiredId<
|
|
121
|
+
newDocs as OptionalUnlessRequiredId<TBaseType>[]
|
|
119
122
|
);
|
|
120
123
|
if (!insertResult.acknowledged) {
|
|
121
124
|
return [];
|
|
122
125
|
}
|
|
123
|
-
await Promise.all(this.subscribers.insertMany.map((subscriber) => subscriber(newDocs)));
|
|
126
|
+
await Promise.all(this.subscribers.insertMany.map((subscriber) => subscriber(newDocs, meta)));
|
|
124
127
|
return newDocs;
|
|
125
128
|
}
|
|
126
129
|
|
|
@@ -130,10 +133,10 @@ export default abstract class BaseRepository<TBasetype extends BaseDocument> {
|
|
|
130
133
|
* @param filter - The filter to apply.
|
|
131
134
|
* @returns The matching document or null if no document was found.
|
|
132
135
|
*/
|
|
133
|
-
async get(filter: Partial<
|
|
136
|
+
async get(filter: Partial<TBaseType>): Promise<TBaseType | null> {
|
|
134
137
|
const collection = await this.getCollection();
|
|
135
|
-
const result = await collection.findOne(this.getFilterWithDefault(filter as Filter<
|
|
136
|
-
return result as
|
|
138
|
+
const result = await collection.findOne(this.getFilterWithDefault(filter as Filter<TBaseType>));
|
|
139
|
+
return result as TBaseType | null;
|
|
137
140
|
}
|
|
138
141
|
|
|
139
142
|
/**
|
|
@@ -141,11 +144,11 @@ export default abstract class BaseRepository<TBasetype extends BaseDocument> {
|
|
|
141
144
|
*
|
|
142
145
|
* @returns An array of all documents in the collection.
|
|
143
146
|
*/
|
|
144
|
-
async getAll(): Promise<
|
|
147
|
+
async getAll(): Promise<TBaseType[]> {
|
|
145
148
|
const collection = await this.getCollection();
|
|
146
149
|
const result = await collection.find(this.getFilterWithDefault()).toArray();
|
|
147
150
|
// Set to unknown first because of some weird type things.
|
|
148
|
-
return result as unknown as
|
|
151
|
+
return result as unknown as TBaseType[];
|
|
149
152
|
}
|
|
150
153
|
|
|
151
154
|
/**
|
|
@@ -167,38 +170,40 @@ export default abstract class BaseRepository<TBasetype extends BaseDocument> {
|
|
|
167
170
|
* @param docIds - The IDs of the documents to retrieve.
|
|
168
171
|
* @returns An array of matching documents.
|
|
169
172
|
*/
|
|
170
|
-
async getList(docIds: UUID[]): Promise<
|
|
173
|
+
async getList(docIds: UUID[]): Promise<TBaseType[]> {
|
|
171
174
|
const collection = await this.getCollection();
|
|
172
175
|
const result = await collection
|
|
173
|
-
.find(this.getFilterWithDefault({ _id: { $in: docIds } } as Filter<
|
|
176
|
+
.find(this.getFilterWithDefault({ _id: { $in: docIds } } as Filter<TBaseType>))
|
|
174
177
|
.toArray();
|
|
175
|
-
return result as
|
|
178
|
+
return result as TBaseType[];
|
|
176
179
|
}
|
|
177
180
|
|
|
178
181
|
/**
|
|
179
182
|
* Deletes a document by its ID.
|
|
180
183
|
*
|
|
181
184
|
* @param docId - The ID of the document to delete.
|
|
185
|
+
* @param meta - Tracks database operation metadata for a single request.
|
|
182
186
|
* @returns The result of the delete operation.
|
|
183
187
|
*/
|
|
184
|
-
async delete(docId: UUID): Promise<DeleteResult> {
|
|
188
|
+
async delete(docId: UUID, meta?: DbOperationMetaData): Promise<DeleteResult> {
|
|
185
189
|
const collection = await this.getCollection();
|
|
186
|
-
await Promise.all(this.subscribers.deleteOne.map((subscriber) => subscriber(docId)));
|
|
187
|
-
return collection.deleteOne({ _id: docId } as Filter<
|
|
190
|
+
await Promise.all(this.subscribers.deleteOne.map((subscriber) => subscriber(docId, meta)));
|
|
191
|
+
return collection.deleteOne({ _id: docId } as Filter<TBaseType>);
|
|
188
192
|
}
|
|
189
193
|
|
|
190
194
|
/**
|
|
191
195
|
* Deletes multiple documents by their IDs.
|
|
192
196
|
*
|
|
193
197
|
* @param docIds - The IDs of the documents to delete.
|
|
198
|
+
* @param meta - Tracks database operation metadata for a single request.
|
|
194
199
|
* @returns The result of the delete operation.
|
|
195
200
|
*/
|
|
196
|
-
async deleteList(docIds: UUID[]): Promise<DeleteResult> {
|
|
201
|
+
async deleteList(docIds: UUID[], meta?: DbOperationMetaData): Promise<DeleteResult> {
|
|
197
202
|
const collection = await this.getCollection();
|
|
198
203
|
const deleteResult = collection.deleteMany({
|
|
199
204
|
_id: { $in: docIds }
|
|
200
|
-
} as Filter<
|
|
201
|
-
await Promise.all(this.subscribers.deleteList.map((subscriber) => subscriber(docIds)));
|
|
205
|
+
} as Filter<TBaseType>);
|
|
206
|
+
await Promise.all(this.subscribers.deleteList.map((subscriber) => subscriber(docIds, meta)));
|
|
202
207
|
return deleteResult;
|
|
203
208
|
}
|
|
204
209
|
|
|
@@ -218,9 +223,10 @@ export default abstract class BaseRepository<TBasetype extends BaseDocument> {
|
|
|
218
223
|
* This base method strips the `_id` before updating.
|
|
219
224
|
*
|
|
220
225
|
* @param updatedDoc - The document to update.
|
|
226
|
+
* @param meta - Tracks database operation metadata for a single request.
|
|
221
227
|
* @returns The result of the update operation.
|
|
222
228
|
*/
|
|
223
|
-
async update(updatedDoc: Partial<
|
|
229
|
+
async update(updatedDoc: Partial<TBaseType>, meta?: DbOperationMetaData): Promise<UpdateResult> {
|
|
224
230
|
const collection = await this.getCollection();
|
|
225
231
|
await this.validator.validateUpdateObject(updatedDoc);
|
|
226
232
|
|
|
@@ -228,10 +234,10 @@ export default abstract class BaseRepository<TBasetype extends BaseDocument> {
|
|
|
228
234
|
|
|
229
235
|
const cleanedDoc = this.cleanUpdateObject(updatedDoc);
|
|
230
236
|
|
|
231
|
-
const result = collection.updateOne({ _id: docId } as Filter<
|
|
237
|
+
const result = collection.updateOne({ _id: docId } as Filter<TBaseType>, {
|
|
232
238
|
$set: cleanedDoc
|
|
233
239
|
});
|
|
234
|
-
await Promise.all(this.subscribers.updateOne.map((subscriber) => subscriber(updatedDoc)));
|
|
240
|
+
await Promise.all(this.subscribers.updateOne.map((subscriber) => subscriber(updatedDoc, meta)));
|
|
235
241
|
return result;
|
|
236
242
|
}
|
|
237
243
|
|
|
@@ -241,9 +247,13 @@ export default abstract class BaseRepository<TBasetype extends BaseDocument> {
|
|
|
241
247
|
* This base method strips the `_id` before updating.
|
|
242
248
|
*
|
|
243
249
|
* @param updatedDocs - The documents to update.
|
|
250
|
+
* @param meta - Tracks database operation metadata for a single request.
|
|
244
251
|
* @returns The result of the bulk update operation.
|
|
245
252
|
*/
|
|
246
|
-
async updateMany(
|
|
253
|
+
async updateMany(
|
|
254
|
+
updatedDocs: Array<Partial<TBaseType>>,
|
|
255
|
+
meta?: DbOperationMetaData
|
|
256
|
+
): Promise<BulkWriteResult> {
|
|
247
257
|
const collection = await this.getCollection();
|
|
248
258
|
await Promise.all(updatedDocs.map((doc) => this.validator.validateUpdateObject(doc)));
|
|
249
259
|
|
|
@@ -256,20 +266,60 @@ export default abstract class BaseRepository<TBasetype extends BaseDocument> {
|
|
|
256
266
|
update: { $set: cleanedDoc }
|
|
257
267
|
}
|
|
258
268
|
};
|
|
259
|
-
}) as AnyBulkWriteOperation<
|
|
269
|
+
}) as AnyBulkWriteOperation<TBaseType>[];
|
|
260
270
|
|
|
261
|
-
await Promise.all(
|
|
271
|
+
await Promise.all(
|
|
272
|
+
this.subscribers.updateMany.map((subscriber) => subscriber(updatedDocs, meta))
|
|
273
|
+
);
|
|
262
274
|
|
|
263
275
|
return collection.bulkWrite(bulkOps);
|
|
264
276
|
}
|
|
265
277
|
|
|
278
|
+
/**
|
|
279
|
+
* Fetches and caches multiple documents, returning only those not already cached.
|
|
280
|
+
* This method checks the cache in the provided metadata object first, then fetches
|
|
281
|
+
* any missing documents from the database and caches them.
|
|
282
|
+
*
|
|
283
|
+
* @param docIds - The IDs of the documents to fetch.
|
|
284
|
+
* @param meta - The metadata object containing the cache.
|
|
285
|
+
* @returns An array of all documents (from cache and newly fetched).
|
|
286
|
+
*/
|
|
287
|
+
protected async fetchAndCacheDocsForMeta(
|
|
288
|
+
docIds: UUID[],
|
|
289
|
+
meta: DbOperationMetaData
|
|
290
|
+
): Promise<TBaseType[]> {
|
|
291
|
+
const docIdsToFetch: UUID[] = [];
|
|
292
|
+
const cachedDocs: TBaseType[] = [];
|
|
293
|
+
|
|
294
|
+
// Check cache first
|
|
295
|
+
for (const docId of docIds) {
|
|
296
|
+
const cached = meta.getCachedDoc<TBaseType>(docId);
|
|
297
|
+
if (cached) {
|
|
298
|
+
cachedDocs.push(cached);
|
|
299
|
+
} else {
|
|
300
|
+
docIdsToFetch.push(docId);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
// Fetch uncached docs
|
|
305
|
+
if (docIdsToFetch.length > 0) {
|
|
306
|
+
const fetchedDocs = await this.getList(docIdsToFetch);
|
|
307
|
+
fetchedDocs.forEach((doc) => {
|
|
308
|
+
meta.cacheDoc(doc._id, doc);
|
|
309
|
+
cachedDocs.push(doc);
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
return cachedDocs;
|
|
314
|
+
}
|
|
315
|
+
|
|
266
316
|
/**
|
|
267
317
|
* Gets the filter with the default filter applied if there is one.
|
|
268
318
|
*
|
|
269
319
|
* @param filter - The filter to apply.
|
|
270
320
|
* @returns The filter with the default filter applied.
|
|
271
321
|
*/
|
|
272
|
-
protected getFilterWithDefault(filter: Filter<
|
|
322
|
+
protected getFilterWithDefault(filter: Filter<TBaseType> = {}): Filter<TBaseType> {
|
|
273
323
|
if (!this.defaultFilter) {
|
|
274
324
|
return filter;
|
|
275
325
|
}
|
|
@@ -299,7 +349,7 @@ export default abstract class BaseRepository<TBasetype extends BaseDocument> {
|
|
|
299
349
|
* @param updatedDoc - The document to clean.
|
|
300
350
|
* @returns The cleaned document.
|
|
301
351
|
*/
|
|
302
|
-
private cleanUpdateObject(updatedDoc: Partial<
|
|
352
|
+
private cleanUpdateObject(updatedDoc: Partial<TBaseType>): Partial<TBaseType> {
|
|
303
353
|
return this.defaultUpdateCleaner
|
|
304
354
|
? this.defaultUpdateCleaner(DocumentCleaner.id(updatedDoc))
|
|
305
355
|
: DocumentCleaner.id(updatedDoc);
|
|
@@ -1,11 +1,22 @@
|
|
|
1
1
|
import type { BaseDocumentWithType } from '@aneuhold/core-ts-db-lib';
|
|
2
|
-
import
|
|
2
|
+
import type { UUID } from 'crypto';
|
|
3
|
+
import type { BulkWriteResult, DeleteResult, UpdateResult } from 'mongodb';
|
|
4
|
+
import type DbOperationMetaData from '../../util/DbOperationMetaData.js';
|
|
5
|
+
import type IValidator from '../../validators/BaseValidator.js';
|
|
3
6
|
import BaseRepository from '../BaseRepository.js';
|
|
4
7
|
/**
|
|
5
8
|
* A base repository for the `dashboard` collection.
|
|
6
9
|
*/
|
|
7
10
|
export default abstract class DashboardBaseRepository<TBaseType extends BaseDocumentWithType> extends BaseRepository<TBaseType> {
|
|
11
|
+
protected docType: string;
|
|
8
12
|
private static COLLECTION_NAME;
|
|
9
13
|
constructor(docType: string, validator: IValidator<TBaseType>, updateCleaner?: (doc: Partial<TBaseType>) => Partial<TBaseType>);
|
|
14
|
+
insertNew(newDoc: TBaseType, meta?: DbOperationMetaData): Promise<TBaseType | null>;
|
|
15
|
+
insertMany(newDocs: TBaseType[], meta?: DbOperationMetaData): Promise<TBaseType[]>;
|
|
16
|
+
update(updatedDoc: Partial<TBaseType>, meta?: DbOperationMetaData): Promise<UpdateResult>;
|
|
17
|
+
updateMany(updatedDocs: Partial<TBaseType>[], meta?: DbOperationMetaData): Promise<BulkWriteResult>;
|
|
18
|
+
delete(docId: UUID, meta?: DbOperationMetaData): Promise<DeleteResult>;
|
|
19
|
+
deleteList(docIds: UUID[], meta?: DbOperationMetaData): Promise<DeleteResult>;
|
|
20
|
+
deleteAll(meta?: DbOperationMetaData): Promise<DeleteResult>;
|
|
10
21
|
}
|
|
11
22
|
//# sourceMappingURL=DashboardBaseRepository.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DashboardBaseRepository.d.ts","sourceRoot":"","sources":["../../../src/repositories/dashboard/DashboardBaseRepository.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;
|
|
1
|
+
{"version":3,"file":"DashboardBaseRepository.d.ts","sourceRoot":"","sources":["../../../src/repositories/dashboard/DashboardBaseRepository.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAC;AACrE,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AACnC,OAAO,KAAK,EAAE,eAAe,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAC3E,OAAO,KAAK,mBAAmB,MAAM,mCAAmC,CAAC;AAEzE,OAAO,KAAK,UAAU,MAAM,mCAAmC,CAAC;AAChE,OAAO,cAAc,MAAM,sBAAsB,CAAC;AAElD;;GAEG;AACH,MAAM,CAAC,OAAO,CAAC,QAAQ,OAAO,uBAAuB,CACnD,SAAS,SAAS,oBAAoB,CACtC,SAAQ,cAAc,CAAC,SAAS,CAAC;IAI/B,SAAS,CAAC,OAAO,EAAE,MAAM;IAH3B,OAAO,CAAC,MAAM,CAAC,eAAe,CAAe;gBAGjC,OAAO,EAAE,MAAM,EACzB,SAAS,EAAE,UAAU,CAAC,SAAS,CAAC,EAChC,aAAa,CAAC,EAAE,CAAC,GAAG,EAAE,OAAO,CAAC,SAAS,CAAC,KAAK,OAAO,CAAC,SAAS,CAAC;IAUlD,SAAS,CACtB,MAAM,EAAE,SAAS,EACjB,IAAI,CAAC,EAAE,mBAAmB,GACzB,OAAO,CAAC,SAAS,GAAG,IAAI,CAAC;IAKb,UAAU,CACvB,OAAO,EAAE,SAAS,EAAE,EACpB,IAAI,CAAC,EAAE,mBAAmB,GACzB,OAAO,CAAC,SAAS,EAAE,CAAC;IAKR,MAAM,CACnB,UAAU,EAAE,OAAO,CAAC,SAAS,CAAC,EAC9B,IAAI,CAAC,EAAE,mBAAmB,GACzB,OAAO,CAAC,YAAY,CAAC;IAKT,UAAU,CACvB,WAAW,EAAE,OAAO,CAAC,SAAS,CAAC,EAAE,EACjC,IAAI,CAAC,EAAE,mBAAmB,GACzB,OAAO,CAAC,eAAe,CAAC;IAKZ,MAAM,CAAC,KAAK,EAAE,IAAI,EAAE,IAAI,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,YAAY,CAAC;IAKtE,UAAU,CAAC,MAAM,EAAE,IAAI,EAAE,EAAE,IAAI,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,YAAY,CAAC;IAK7E,SAAS,CAAC,IAAI,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,YAAY,CAAC;CAI5E"}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import CleanDocument from '../../util/DocumentCleaner.js';
|
|
2
|
-
import IValidator from '../../validators/BaseValidator.js';
|
|
3
2
|
import BaseRepository from '../BaseRepository.js';
|
|
4
3
|
/**
|
|
5
4
|
* A base repository for the `dashboard` collection.
|
|
6
5
|
*/
|
|
7
6
|
export default class DashboardBaseRepository extends BaseRepository {
|
|
7
|
+
docType;
|
|
8
8
|
static COLLECTION_NAME = 'dashboard';
|
|
9
9
|
constructor(docType, validator, updateCleaner) {
|
|
10
10
|
const defaultUpdateCleaner = (updatedDoc) => updateCleaner
|
|
@@ -12,6 +12,35 @@ export default class DashboardBaseRepository extends BaseRepository {
|
|
|
12
12
|
: CleanDocument.docType(updatedDoc);
|
|
13
13
|
const defaultFilter = { docType };
|
|
14
14
|
super(DashboardBaseRepository.COLLECTION_NAME, validator, defaultFilter, defaultUpdateCleaner);
|
|
15
|
+
this.docType = docType;
|
|
16
|
+
}
|
|
17
|
+
async insertNew(newDoc, meta) {
|
|
18
|
+
meta?.recordDocTypeTouched(this.docType);
|
|
19
|
+
return super.insertNew(newDoc, meta);
|
|
20
|
+
}
|
|
21
|
+
async insertMany(newDocs, meta) {
|
|
22
|
+
meta?.recordDocTypeTouched(this.docType);
|
|
23
|
+
return super.insertMany(newDocs, meta);
|
|
24
|
+
}
|
|
25
|
+
async update(updatedDoc, meta) {
|
|
26
|
+
meta?.recordDocTypeTouched(this.docType);
|
|
27
|
+
return super.update(updatedDoc, meta);
|
|
28
|
+
}
|
|
29
|
+
async updateMany(updatedDocs, meta) {
|
|
30
|
+
meta?.recordDocTypeTouched(this.docType);
|
|
31
|
+
return super.updateMany(updatedDocs, meta);
|
|
32
|
+
}
|
|
33
|
+
async delete(docId, meta) {
|
|
34
|
+
meta?.recordDocTypeTouched(this.docType);
|
|
35
|
+
return super.delete(docId, meta);
|
|
36
|
+
}
|
|
37
|
+
async deleteList(docIds, meta) {
|
|
38
|
+
meta?.recordDocTypeTouched(this.docType);
|
|
39
|
+
return super.deleteList(docIds, meta);
|
|
40
|
+
}
|
|
41
|
+
async deleteAll(meta) {
|
|
42
|
+
meta?.recordDocTypeTouched(this.docType);
|
|
43
|
+
return super.deleteAll();
|
|
15
44
|
}
|
|
16
45
|
}
|
|
17
46
|
//# sourceMappingURL=DashboardBaseRepository.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DashboardBaseRepository.js","sourceRoot":"","sources":["../../../src/repositories/dashboard/DashboardBaseRepository.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"DashboardBaseRepository.js","sourceRoot":"","sources":["../../../src/repositories/dashboard/DashboardBaseRepository.ts"],"names":[],"mappings":"AAIA,OAAO,aAAa,MAAM,+BAA+B,CAAC;AAE1D,OAAO,cAAc,MAAM,sBAAsB,CAAC;AAElD;;GAEG;AACH,MAAM,CAAC,OAAO,OAAgB,uBAE5B,SAAQ,cAAyB;IAIrB;IAHJ,MAAM,CAAC,eAAe,GAAG,W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MAAM,CAAC,KAAW,EAAE,IAA0B;QAC3D,IAAI,EAAE,oBAAoB,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACzC,OAAO,KAAK,CAAC,MAAM,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;IACnC,CAAC;IAEQ,KAAK,CAAC,UAAU,CAAC,MAAc,EAAE,IAA0B;QAClE,IAAI,EAAE,oBAAoB,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACzC,OAAO,KAAK,CAAC,UAAU,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;IACxC,CAAC;IAEQ,KAAK,CAAC,SAAS,CAAC,IAA0B;QACjD,IAAI,EAAE,oBAAoB,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACzC,OAAO,KAAK,CAAC,SAAS,EAAE,CAAC;IAC3B,CAAC"}
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import type { BaseDocumentWithType } from '@aneuhold/core-ts-db-lib';
|
|
2
|
+
import type { UUID } from 'crypto';
|
|
3
|
+
import type { BulkWriteResult, DeleteResult, UpdateResult } from 'mongodb';
|
|
4
|
+
import type DbOperationMetaData from '../../util/DbOperationMetaData.js';
|
|
2
5
|
import CleanDocument from '../../util/DocumentCleaner.js';
|
|
3
|
-
import IValidator from '../../validators/BaseValidator.js';
|
|
6
|
+
import type IValidator from '../../validators/BaseValidator.js';
|
|
4
7
|
import BaseRepository from '../BaseRepository.js';
|
|
5
8
|
|
|
6
9
|
/**
|
|
@@ -12,7 +15,7 @@ export default abstract class DashboardBaseRepository<
|
|
|
12
15
|
private static COLLECTION_NAME = 'dashboard';
|
|
13
16
|
|
|
14
17
|
constructor(
|
|
15
|
-
docType: string,
|
|
18
|
+
protected docType: string,
|
|
16
19
|
validator: IValidator<TBaseType>,
|
|
17
20
|
updateCleaner?: (doc: Partial<TBaseType>) => Partial<TBaseType>
|
|
18
21
|
) {
|
|
@@ -23,4 +26,51 @@ export default abstract class DashboardBaseRepository<
|
|
|
23
26
|
const defaultFilter = { docType } as Partial<TBaseType>;
|
|
24
27
|
super(DashboardBaseRepository.COLLECTION_NAME, validator, defaultFilter, defaultUpdateCleaner);
|
|
25
28
|
}
|
|
29
|
+
|
|
30
|
+
override async insertNew(
|
|
31
|
+
newDoc: TBaseType,
|
|
32
|
+
meta?: DbOperationMetaData
|
|
33
|
+
): Promise<TBaseType | null> {
|
|
34
|
+
meta?.recordDocTypeTouched(this.docType);
|
|
35
|
+
return super.insertNew(newDoc, meta);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
override async insertMany(
|
|
39
|
+
newDocs: TBaseType[],
|
|
40
|
+
meta?: DbOperationMetaData
|
|
41
|
+
): Promise<TBaseType[]> {
|
|
42
|
+
meta?.recordDocTypeTouched(this.docType);
|
|
43
|
+
return super.insertMany(newDocs, meta);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
override async update(
|
|
47
|
+
updatedDoc: Partial<TBaseType>,
|
|
48
|
+
meta?: DbOperationMetaData
|
|
49
|
+
): Promise<UpdateResult> {
|
|
50
|
+
meta?.recordDocTypeTouched(this.docType);
|
|
51
|
+
return super.update(updatedDoc, meta);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
override async updateMany(
|
|
55
|
+
updatedDocs: Partial<TBaseType>[],
|
|
56
|
+
meta?: DbOperationMetaData
|
|
57
|
+
): Promise<BulkWriteResult> {
|
|
58
|
+
meta?.recordDocTypeTouched(this.docType);
|
|
59
|
+
return super.updateMany(updatedDocs, meta);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
override async delete(docId: UUID, meta?: DbOperationMetaData): Promise<DeleteResult> {
|
|
63
|
+
meta?.recordDocTypeTouched(this.docType);
|
|
64
|
+
return super.delete(docId, meta);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
override async deleteList(docIds: UUID[], meta?: DbOperationMetaData): Promise<DeleteResult> {
|
|
68
|
+
meta?.recordDocTypeTouched(this.docType);
|
|
69
|
+
return super.deleteList(docIds, meta);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
override async deleteAll(meta?: DbOperationMetaData): Promise<DeleteResult> {
|
|
73
|
+
meta?.recordDocTypeTouched(this.docType);
|
|
74
|
+
return super.deleteAll();
|
|
75
|
+
}
|
|
26
76
|
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { BaseDocumentWithType, RequiredUserId } from '@aneuhold/core-ts-db-lib';
|
|
2
|
+
import type { UUID } from 'crypto';
|
|
3
|
+
import type { BulkWriteResult, DeleteResult, UpdateResult } from 'mongodb';
|
|
4
|
+
import type DbOperationMetaData from '../../util/DbOperationMetaData.js';
|
|
5
|
+
import type IValidator from '../../validators/BaseValidator.js';
|
|
6
|
+
import DashboardBaseRepository from './DashboardBaseRepository.js';
|
|
7
|
+
/**
|
|
8
|
+
* A base repository for the `dashboard` collection that requires a `userId`.
|
|
9
|
+
*/
|
|
10
|
+
export default abstract class DashboardBaseWithUserIdRepository<TBaseType extends BaseDocumentWithType & RequiredUserId> extends DashboardBaseRepository<TBaseType> {
|
|
11
|
+
constructor(docType: string, validator: IValidator<TBaseType>, updateCleaner?: (doc: Partial<TBaseType>) => Partial<TBaseType>);
|
|
12
|
+
/**
|
|
13
|
+
* Gets all items for a given user.
|
|
14
|
+
*
|
|
15
|
+
* @param userId The ID of the user to get items for.
|
|
16
|
+
*/
|
|
17
|
+
getAllForUser(userId: UUID): Promise<TBaseType[]>;
|
|
18
|
+
insertNew(newDoc: TBaseType, meta?: DbOperationMetaData): Promise<TBaseType | null>;
|
|
19
|
+
insertMany(newDocs: TBaseType[], meta?: DbOperationMetaData): Promise<TBaseType[]>;
|
|
20
|
+
update(updatedDoc: Partial<TBaseType>, meta?: DbOperationMetaData): Promise<UpdateResult>;
|
|
21
|
+
updateMany(updatedDocs: Partial<TBaseType>[], meta?: DbOperationMetaData): Promise<BulkWriteResult>;
|
|
22
|
+
delete(docId: UUID, meta?: DbOperationMetaData): Promise<DeleteResult>;
|
|
23
|
+
deleteList(docIds: UUID[], meta?: DbOperationMetaData): Promise<DeleteResult>;
|
|
24
|
+
}
|
|
25
|
+
//# sourceMappingURL=DashboardBaseWithUserIdRepository.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DashboardBaseWithUserIdRepository.d.ts","sourceRoot":"","sources":["../../../src/repositories/dashboard/DashboardBaseWithUserIdRepository.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AACrF,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AACnC,OAAO,KAAK,EAAE,eAAe,EAAE,YAAY,EAAU,YAAY,EAAE,MAAM,SAAS,CAAC;AACnF,OAAO,KAAK,mBAAmB,MAAM,mCAAmC,CAAC;AAEzE,OAAO,KAAK,UAAU,MAAM,mCAAmC,CAAC;AAChE,OAAO,uBAAuB,MAAM,8BAA8B,CAAC;AAEnE;;GAEG;AACH,MAAM,CAAC,OAAO,CAAC,QAAQ,OAAO,iCAAiC,CAC7D,SAAS,SAAS,oBAAoB,GAAG,cAAc,CACvD,SAAQ,uBAAuB,CAAC,SAAS,CAAC;gBAExC,OAAO,EAAE,MAAM,EACf,SAAS,EAAE,UAAU,CAAC,SAAS,CAAC,EAChC,aAAa,CAAC,EAAE,CAAC,GAAG,EAAE,OAAO,CAAC,SAAS,CAAC,KAAK,OAAO,CAAC,SAAS,CAAC;IAOjE;;;;OAIG;IACG,aAAa,CAAC,MAAM,EAAE,IAAI,GAAG,OAAO,CAAC,SAAS,EAAE,CAAC;IASxC,SAAS,CACtB,MAAM,EAAE,SAAS,EACjB,IAAI,CAAC,EAAE,mBAAmB,GACzB,OAAO,CAAC,SAAS,GAAG,IAAI,CAAC;IAQb,UAAU,CACvB,OAAO,EAAE,SAAS,EAAE,EACpB,IAAI,CAAC,EAAE,mBAAmB,GACzB,OAAO,CAAC,SAAS,EAAE,CAAC;IAMR,MAAM,CACnB,UAAU,EAAE,OAAO,CAAC,SAAS,CAAC,EAC9B,IAAI,CAAC,EAAE,mBAAmB,GACzB,OAAO,CAAC,YAAY,CAAC;IAUT,UAAU,CACvB,WAAW,EAAE,OAAO,CAAC,SAAS,CAAC,EAAE,EACjC,IAAI,CAAC,EAAE,mBAAmB,GACzB,OAAO,CAAC,eAAe,CAAC;IASZ,MAAM,CAAC,KAAK,EAAE,IAAI,EAAE,IAAI,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,YAAY,CAAC;IAUtE,UAAU,CAAC,MAAM,EAAE,IAAI,EAAE,EAAE,IAAI,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,YAAY,CAAC;CAO7F"}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import CleanDocument from '../../util/DocumentCleaner.js';
|
|
2
|
+
import DashboardBaseRepository from './DashboardBaseRepository.js';
|
|
3
|
+
/**
|
|
4
|
+
* A base repository for the `dashboard` collection that requires a `userId`.
|
|
5
|
+
*/
|
|
6
|
+
export default class DashboardBaseWithUserIdRepository extends DashboardBaseRepository {
|
|
7
|
+
constructor(docType, validator, updateCleaner) {
|
|
8
|
+
const defaultUpdateCleaner = (doc) => updateCleaner ? updateCleaner(CleanDocument.userId(doc)) : CleanDocument.userId(doc);
|
|
9
|
+
super(docType, validator, defaultUpdateCleaner);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Gets all items for a given user.
|
|
13
|
+
*
|
|
14
|
+
* @param userId The ID of the user to get items for.
|
|
15
|
+
*/
|
|
16
|
+
async getAllForUser(userId) {
|
|
17
|
+
const collection = await this.getCollection();
|
|
18
|
+
const filter = {
|
|
19
|
+
$and: [this.getFilterWithDefault(), { userId }]
|
|
20
|
+
};
|
|
21
|
+
const result = await collection.find(filter).toArray();
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
async insertNew(newDoc, meta) {
|
|
25
|
+
const result = await super.insertNew(newDoc, meta);
|
|
26
|
+
if (result) {
|
|
27
|
+
meta?.addAffectedUserIds([result.userId]);
|
|
28
|
+
}
|
|
29
|
+
return result;
|
|
30
|
+
}
|
|
31
|
+
async insertMany(newDocs, meta) {
|
|
32
|
+
const result = await super.insertMany(newDocs, meta);
|
|
33
|
+
meta?.addAffectedUserIds(result.map((d) => d.userId));
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
36
|
+
async update(updatedDoc, meta) {
|
|
37
|
+
if (meta && updatedDoc._id) {
|
|
38
|
+
const docs = await this.fetchAndCacheDocsForMeta([updatedDoc._id], meta);
|
|
39
|
+
if (docs.length > 0) {
|
|
40
|
+
meta.addAffectedUserIds([docs[0].userId]);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return super.update(updatedDoc, meta);
|
|
44
|
+
}
|
|
45
|
+
async updateMany(updatedDocs, meta) {
|
|
46
|
+
if (meta && updatedDocs.length > 0) {
|
|
47
|
+
const docIds = updatedDocs.map((doc) => doc._id).filter((id) => id !== undefined);
|
|
48
|
+
const cachedDocs = await this.fetchAndCacheDocsForMeta(docIds, meta);
|
|
49
|
+
meta.addAffectedUserIds(cachedDocs.map((doc) => doc.userId));
|
|
50
|
+
}
|
|
51
|
+
return super.updateMany(updatedDocs, meta);
|
|
52
|
+
}
|
|
53
|
+
async delete(docId, meta) {
|
|
54
|
+
if (meta) {
|
|
55
|
+
const docs = await this.fetchAndCacheDocsForMeta([docId], meta);
|
|
56
|
+
if (docs.length > 0) {
|
|
57
|
+
meta.addAffectedUserIds([docs[0].userId]);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
return super.delete(docId, meta);
|
|
61
|
+
}
|
|
62
|
+
async deleteList(docIds, meta) {
|
|
63
|
+
if (meta && docIds.length > 0) {
|
|
64
|
+
const cachedDocs = await this.fetchAndCacheDocsForMeta(docIds, meta);
|
|
65
|
+
meta.addAffectedUserIds(cachedDocs.map((doc) => doc.userId));
|
|
66
|
+
}
|
|
67
|
+
return super.deleteList(docIds, meta);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
//# sourceMappingURL=DashboardBaseWithUserIdRepository.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"DashboardBaseWithUserIdRepository.js","sourceRoot":"","sources":["../../../src/repositories/dashboard/DashboardBaseWithUserIdRepository.ts"],"names":[],"mappings":"AAIA,OAAO,aAAa,MAAM,+BAA+B,CAAC;AAE1D,OAAO,uBAAuB,MAAM,8BAA8B,CAAC;AAEnE;;GAEG;AACH,MAAM,CAAC,OAAO,OAAgB,iCAE5B,SAAQ,uBAAkC;IAC1C,YACE,OAAe,EACf,SAAgC,EAChC,aAA+D;QAE/D,MAAM,oBAAoB,GAAG,CAAC,GAAuB,EAAE,EAAE,CACvD,aAAa,CAAC,CAAC,CAAC,aAAa,CAAC,aAAa,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;QACvF,KAAK,CAAC,OAAO,EAAE,SAAS,EAAE,oBAAoB,CAAC,CAAC;IAClD,CAAC;IAED;;;;OAIG;IACH,KAAK,CAAC,aAAa,CAAC,MAAY;QAC9B,MAAM,UAAU,GAAG,MAAM,IAAI,CAAC,aAAa,EAAE,CAAC;QAC9C,MAAM,MAAM,GAAG;YACb,IAAI,EAAE,CAAC,IAAI,CAAC,oBAAoB,EAAE,EAAE,EAAE,MAAM,EAAE,CAAC;SAC3B,CAAC;QACvB,MAAM,MAAM,GAAG,MAAM,UAAU,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,OAAO,EAAE,CAAC;QACvD,OAAO,MAAqB,CAAC;IAC/B,CAAC;IAEQ,KAAK,CAAC,SAAS,CACtB,MAAiB,EACjB,IAA0B;QAE1B,MAAM,MAAM,GAAG,MAAM,KAAK,CAAC,SAAS,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;QACnD,IAAI,MAAM,EAAE,CAAC;YACX,IAAI,EAAE,kBAAk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DeleteResult, Filter, UpdateResult } from 'mongodb';
|
|
4
|
+
import type DbOperationMetaData from '../../util/DbOperationMetaData.js';
|
|
5
|
+
import CleanDocument from '../../util/DocumentCleaner.js';
|
|
6
|
+
import type IValidator from '../../validators/BaseValidator.js';
|
|
7
|
+
import DashboardBaseRepository from './DashboardBaseRepository.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* A base repository for the `dashboard` collection that requires a `userId`.
|
|
11
|
+
*/
|
|
12
|
+
export default abstract class DashboardBaseWithUserIdRepository<
|
|
13
|
+
TBaseType extends BaseDocumentWithType & RequiredUserId
|
|
14
|
+
> extends DashboardBaseRepository<TBaseType> {
|
|
15
|
+
constructor(
|
|
16
|
+
docType: string,
|
|
17
|
+
validator: IValidator<TBaseType>,
|
|
18
|
+
updateCleaner?: (doc: Partial<TBaseType>) => Partial<TBaseType>
|
|
19
|
+
) {
|
|
20
|
+
const defaultUpdateCleaner = (doc: Partial<TBaseType>) =>
|
|
21
|
+
updateCleaner ? updateCleaner(CleanDocument.userId(doc)) : CleanDocument.userId(doc);
|
|
22
|
+
super(docType, validator, defaultUpdateCleaner);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Gets all items for a given user.
|
|
27
|
+
*
|
|
28
|
+
* @param userId The ID of the user to get items for.
|
|
29
|
+
*/
|
|
30
|
+
async getAllForUser(userId: UUID): Promise<TBaseType[]> {
|
|
31
|
+
const collection = await this.getCollection();
|
|
32
|
+
const filter = {
|
|
33
|
+
$and: [this.getFilterWithDefault(), { userId }]
|
|
34
|
+
} as Filter<TBaseType>;
|
|
35
|
+
const result = await collection.find(filter).toArray();
|
|
36
|
+
return result as TBaseType[];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
override async insertNew(
|
|
40
|
+
newDoc: TBaseType,
|
|
41
|
+
meta?: DbOperationMetaData
|
|
42
|
+
): Promise<TBaseType | null> {
|
|
43
|
+
const result = await super.insertNew(newDoc, meta);
|
|
44
|
+
if (result) {
|
|
45
|
+
meta?.addAffectedUserIds([result.userId]);
|
|
46
|
+
}
|
|
47
|
+
return result;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
override async insertMany(
|
|
51
|
+
newDocs: TBaseType[],
|
|
52
|
+
meta?: DbOperationMetaData
|
|
53
|
+
): Promise<TBaseType[]> {
|
|
54
|
+
const result = await super.insertMany(newDocs, meta);
|
|
55
|
+
meta?.addAffectedUserIds(result.map((d) => d.userId));
|
|
56
|
+
return result;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
override async update(
|
|
60
|
+
updatedDoc: Partial<TBaseType>,
|
|
61
|
+
meta?: DbOperationMetaData
|
|
62
|
+
): Promise<UpdateResult> {
|
|
63
|
+
if (meta && updatedDoc._id) {
|
|
64
|
+
const docs = await this.fetchAndCacheDocsForMeta([updatedDoc._id], meta);
|
|
65
|
+
if (docs.length > 0) {
|
|
66
|
+
meta.addAffectedUserIds([docs[0].userId]);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return super.update(updatedDoc, meta);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
override async updateMany(
|
|
73
|
+
updatedDocs: Partial<TBaseType>[],
|
|
74
|
+
meta?: DbOperationMetaData
|
|
75
|
+
): Promise<BulkWriteResult> {
|
|
76
|
+
if (meta && updatedDocs.length > 0) {
|
|
77
|
+
const docIds = updatedDocs.map((doc) => doc._id).filter((id): id is UUID => id !== undefined);
|
|
78
|
+
const cachedDocs = await this.fetchAndCacheDocsForMeta(docIds, meta);
|
|
79
|
+
meta.addAffectedUserIds(cachedDocs.map((doc) => doc.userId));
|
|
80
|
+
}
|
|
81
|
+
return super.updateMany(updatedDocs, meta);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
override async delete(docId: UUID, meta?: DbOperationMetaData): Promise<DeleteResult> {
|
|
85
|
+
if (meta) {
|
|
86
|
+
const docs = await this.fetchAndCacheDocsForMeta([docId], meta);
|
|
87
|
+
if (docs.length > 0) {
|
|
88
|
+
meta.addAffectedUserIds([docs[0].userId]);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return super.delete(docId, meta);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
override async deleteList(docIds: UUID[], meta?: DbOperationMetaData): Promise<DeleteResult> {
|
|
95
|
+
if (meta && docIds.length > 0) {
|
|
96
|
+
const cachedDocs = await this.fetchAndCacheDocsForMeta(docIds, meta);
|
|
97
|
+
meta.addAffectedUserIds(cachedDocs.map((doc) => doc.userId));
|
|
98
|
+
}
|
|
99
|
+
return super.deleteList(docIds, meta);
|
|
100
|
+
}
|
|
101
|
+
}
|