universo-platform-3d

Форк
0
/
mongo-object-id-helpers.ts 
64 строки · 1.7 Кб
1
import { PipelineStage } from 'mongoose'
2
export type MongoObjectIdString = string
3

4
// The intent for these is to increase static typing usage for more safety, e.g. to ensure a spaceId isn't inputted where a userId should be.
5
export type SpaceId = string
6
export type SpaceObjectId = string
7
export type SpaceVersionId = string
8
export type MaterialInstanceId = string
9
export type AssetId = string
10
export type UserId = string
11
export type UserGroupId = string
12
export type UserEntityActionId = string
13
export type TagId = string
14
export type UserFeedbackItemId = string
15
export type ZoneId = string
16
export type MirrorDBRecordId = string
17
export type PurchaseOptionId = string
18
/**
19
 * @description SCALER UUID
20
 */
21
export type ScalerContainerUuid = string
22
/**
23
 * @description alias for ZoneId, but to be clear that it's a Mongo ObjectId and NOT a UUID
24
 */
25
export type MongoZoneId = string
26
export type CustomDataId = string
27
export type SpaceVariablesDataId = string
28
export type BlockId = string
29

30
export type EntityId =
31
  | SpaceId
32
  | SpaceObjectId
33
  | AssetId
34
  | UserId
35
  | TagId
36
  | UserFeedbackItemId
37
  | UserGroupId
38
  | ZoneId
39
  | CustomDataId
40
  | BlockId
41

42
/**
43
 * @description After hours of debugging, this is how you have to $match query for an id that could be either an ObjectId or a String. We need to standardize this at some point. Mongoose automatically casts strings<>ObjectIds, but an aggregation pipeline does not.
44
 * @date 2023-05-04 00:36
45
 */
46
export function aggregationMatchId(
47
  idToMatch: string,
48
  key = '$_id'
49
): PipelineStage {
50
  return {
51
    $match: {
52
      $expr: {
53
        $or: [
54
          {
55
            $eq: [key, { $toObjectId: idToMatch }]
56
          },
57
          {
58
            $eq: [key, idToMatch]
59
          }
60
        ]
61
      }
62
    }
63
  }
64
}
65

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.