-
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathClimbSchema.ts
More file actions
142 lines (130 loc) · 4.12 KB
/
ClimbSchema.ts
File metadata and controls
142 lines (130 loc) · 4.12 KB
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
import mongoose from 'mongoose'
import muuid from 'uuid-mongodb'
import { Point } from '@turf/helpers'
import { GradeScalesTypes } from '@openbeta/sandbag'
import { ClimbType, IClimbMetadata, IClimbContent, SafetyType, ClimbEditOperationType } from './ClimbTypes.js'
import { GradeContexts } from '../GradeUtils.js'
import { ChangeRecordMetadataType } from './ChangeLogType.js'
const { Schema } = mongoose
const ChangeRecordMetadata = new Schema<ChangeRecordMetadataType>({
user: {
type: 'object',
value: { type: 'Buffer' },
required: true
},
historyId: { type: Schema.Types.ObjectId, ref: 'change_logs' },
prevHistoryId: { type: Schema.Types.ObjectId, ref: 'change_logs' },
operation: {
type: Schema.Types.Mixed,
enum: Object.values(ClimbEditOperationType),
required: true
},
seq: { type: Number, required: true, default: 0 }
}, { _id: false, timestamps: false })
export const PointSchema = new mongoose.Schema<Point>({
type: {
type: String,
enum: ['Point'],
required: true
},
coordinates: {
type: [Number],
required: true
}
}, { _id: false })
const ContentSchema = new Schema<IClimbContent>({
description: { type: Schema.Types.String, required: false },
protection: { type: Schema.Types.String, required: false },
location: { type: Schema.Types.String, required: false }
}, { _id: false })
const MetadataSchema = new Schema<IClimbMetadata>({
lnglat: {
type: PointSchema,
index: '2dsphere'
},
left_right_index: { type: Number, required: true, default: -1 },
mp_id: { type: String, required: false },
mp_crag_id: { type: String, required: true },
wikidata_id: { type: String, required: false },
areaRef: {
type: Schema.Types.Mixed,
value: { type: 'Buffer' },
required: true,
ref: 'areas'
}
}, { _id: false })
const GradeTypeSchema = new Schema<GradeScalesTypes>({
vscale: Schema.Types.String,
yds: { type: Schema.Types.String, required: false },
ewbank: { type: Schema.Types.String, required: false },
brazilian_crux: { type: Schema.Types.String, required: false },
french: { type: Schema.Types.String, required: false },
font: { type: Schema.Types.String, required: false },
UIAA: { type: Schema.Types.String, required: false },
wi: { type: Schema.Types.String, required: false }
}, { _id: false })
const PitchSchema = new mongoose.Schema({
_id: {
type: 'object',
value: { type: 'Buffer' },
default: () => muuid.v4()
},
parentId: {
type: Schema.Types.Mixed,
ref: 'climbs',
required: true
},
pitchNumber: { type: Number, required: true },
grades: { type: GradeTypeSchema },
type: { type: mongoose.Schema.Types.Mixed },
length: { type: Number },
boltsCount: { type: Number },
description: { type: String }
}, {
_id: false,
timestamps: true
})
export const ClimbSchema = new Schema<ClimbType>({
_id: {
type: 'object',
value: { type: 'Buffer' },
default: () => muuid.v4()
},
name: { type: Schema.Types.String, required: true, index: true },
yds: { type: Schema.Types.String, required: true },
grades: GradeTypeSchema,
gradeContext: { type: String, enum: Object.values(GradeContexts), required: false },
fa: { type: Schema.Types.String, required: false },
type: { type: Schema.Types.Mixed },
safety: {
type: Schema.Types.String,
enum: Object.values(SafetyType),
required: true
},
boltsCount: { type: Schema.Types.Number, required: false },
pitches: { type: [PitchSchema], default: undefined, required: false },
metadata: MetadataSchema,
content: ContentSchema,
_deleting: { type: Date },
updatedBy: {
type: 'object',
value: { type: 'Buffer' }
},
createdBy: {
type: 'object',
value: { type: 'Buffer' }
},
_change: ChangeRecordMetadata
}, {
_id: false,
timestamps: true
})
ClimbSchema.index({ _deleting: 1 }, { expireAfterSeconds: 0 })
ClimbSchema.pre('validate', function (next) {
if (this.safety as string === '') { this.safety = SafetyType.UNSPECIFIED }
if (this.yds === '') { this.yds = 'UNKNOWN' }
next()
})
export const getClimbModel = (name: string = 'climbs'): mongoose.Model<ClimbType> => {
return mongoose.model(name, ClimbSchema)
}