-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathmongoose.js
65 lines (62 loc) · 1.27 KB
/
mongoose.js
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
import { URL } from 'url';
import { Decimal128 } from 'mongodb';
import mongoose from 'mongoose';
import { COLLECTIONS } from './setup.js';
function isValidUrl(value) {
try {
// native url package will throw a TypeError on invalid urls
// eslint-disable-next-line no-new
new URL(value);
return true;
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (err) {
return false;
}
}
const sampleSchema = new mongoose.Schema(
{
age: Number,
binary: Buffer,
city: String,
firstName: {
required: true,
type: String,
},
lastName: {
required: true,
type: String,
},
localization: Object,
reference: {
required: true,
type: mongoose.Schema.Types.ObjectId,
},
reviews: [
{
_id: false,
score: Number,
},
],
salary: Decimal128,
scores: [Number],
source: {
default: 'mongoosetests',
enum: COLLECTIONS,
required: true,
type: String,
},
url: {
required: true,
type: String,
validate: {
message: '{VALUE} is not valid!',
validator: isValidUrl,
},
},
zip: Number,
},
{
timestamps: true,
}
);
export default mongoose.model('mongoosetests', sampleSchema);