-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathRelease.ts
More file actions
130 lines (108 loc) · 3.21 KB
/
Copy pathRelease.ts
File metadata and controls
130 lines (108 loc) · 3.21 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
import { HydratedDocument, model, ObjectId, Schema, Types } from 'mongoose'
import { semverObjectToString, semverStringToObject } from '../utils/semver.js'
import { SoftDeleteDocument, softDeletionPlugin } from './plugins/softDeletePlugin.js'
// This interface stores information about the properties on the base object.
// It should be used for plain object representations, e.g. for sending to the
// client.
export interface ReleaseInterface {
modelId: string
modelCardVersion: number
semver: SemverObject
notes: string
minor: boolean
draft: boolean
fileIds: Array<string>
images: Array<ImageTagRef>
deleted: boolean
createdBy: string
createdAt: Date
updatedAt: Date
}
export interface ReleaseVirtuals {
semverString: string
}
export interface ImageNameRef {
repository: string
name: string
}
export interface ImageTagRef extends ImageNameRef {
tag: string
}
export interface ImageDigestRef extends ImageNameRef {
digest: string
}
export type ImageRef = ImageTagRef | ImageDigestRef
// The doc type includes all values in the plain interface, as well as all the
// properties and functions that Mongoose provides. If a function takes in an
// object from Mongoose it should use this interface
export type ReleaseDoc = HydratedDocument<
Omit<ReleaseInterface, 'images'> & {
images: Array<HydratedDocument<ImageTagRef>>
},
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
{},
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
{},
ReleaseVirtuals
> &
SoftDeleteDocument
export interface SemverObject {
major: number
minor: number
patch: number
metadata?: string
}
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
const ReleaseSchema = new Schema<ReleaseDoc, {}, {}, ReleaseVirtuals>(
{
modelId: { type: String, required: true },
modelCardVersion: { type: Number, required: true },
semver: {
type: Schema.Types.Mixed,
required: true,
},
notes: { type: String, required: true },
minor: { type: Boolean, required: true },
draft: { type: Boolean, required: true },
fileIds: [
{
type: Schema.Types.ObjectId,
set: function (stringId: string) {
return new Types.ObjectId(stringId)
},
get: function (objectId: ObjectId) {
return objectId.toString()
},
},
],
images: [
{
repository: { type: String },
name: { type: String },
tag: { type: String },
},
],
createdBy: { type: String, required: true },
},
{
timestamps: true,
collection: 'v2_releases',
toJSON: { getters: true },
toObject: { getters: true },
},
)
ReleaseSchema.virtual('semverString').get(function () {
return semverObjectToString(this.semver)
})
ReleaseSchema.virtual('semverString').set(function (semver: SemverObject | string) {
if (typeof semver === 'string') {
this.semver = semverStringToObject(semver)
} else {
this.semver = semver
}
})
ReleaseSchema.plugin(softDeletionPlugin)
ReleaseSchema.index({ modelId: 1, semver: 1 }, { unique: true })
ReleaseSchema.index({ modelId: 1 })
const ReleaseModel = model<ReleaseDoc>('v2_Release', ReleaseSchema)
export default ReleaseModel