deno.land / x / mongoose@6.7.5 / test / docs / defaults.test.js

defaults.test.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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
'use strict';
const assert = require('assert');const mongoose = require('../../');const start = require('../common');
describe('defaults docs', function() { let db; const Schema = mongoose.Schema;
before(function() { db = mongoose.createConnection(start.uri); });
after(async function() { await db.close(); });
afterEach(function() { db.models = {}; });
/** * Your schemas can define default values for certain paths. If you create * a new document without that path set, the default will kick in. * * Note: Mongoose only applies a default if the value of the path is * strictly `undefined`. */ it('Declaring defaults in your schema', async function() { const schema = new Schema({ name: String, role: { type: String, default: 'guitarist' } });
const Person = db.model('Person', schema);
const axl = new Person({ name: 'Axl Rose', role: 'singer' }); assert.equal(axl.role, 'singer');
const slash = new Person({ name: 'Slash' }); assert.equal(slash.role, 'guitarist');
const izzy = new Person({ name: 'Izzy', role: undefined }); assert.equal(izzy.role, 'guitarist');
// Defaults do **not** run on `null`, `''`, or value other than `undefined`. const foo = new Person({ name: 'Bar', role: null }); assert.strictEqual(foo.role, null);
await Person.create(axl, slash); const docs = await Person.find({ role: 'guitarist' }); assert.equal(docs.length, 1); assert.equal(docs[0].name, 'Slash'); });
/** * You can also set the `default` schema option to a function. Mongoose will * execute that function and use the return value as the default. */ it('Default functions', function() { const schema = new Schema({ title: String, date: { type: Date, // `Date.now()` returns the current unix timestamp as a number default: Date.now } });
const BlogPost = db.model('BlogPost', schema);
const post = new BlogPost({ title: '5 Best Arnold Schwarzenegger Movies' });
// The post has a default Date set to now assert.ok(post.date.getTime() >= Date.now() - 1000); assert.ok(post.date.getTime() <= Date.now()); });
/** * Mongoose also applies defaults when you upsert a document. * Mongoose only applies defaults on upsert if a new document is * inserted, not if the upsert updated an existing document. * */ it('The `setDefaultsOnInsert` option', async function() { const schema = new Schema({ title: String, genre: { type: String, default: 'Action' } });
const Movie = db.model('Movie', schema); // acquit:ignore:start await Movie.deleteMany({}); // acquit:ignore:end
const query = {}; const update = { title: 'The Terminator' }; const options = { // Return the document after updates are applied new: true, // Create a document if one isn't found. upsert: true };
let doc = await Movie.findOneAndUpdate(query, update, options).lean(); doc.genre; // 'Action', Mongoose set a default value. // acquit:ignore:start assert.equal(doc.title, 'The Terminator'); assert.equal(doc.genre, 'Action'); // acquit:ignore:end
await Movie.deleteMany({});
doc = await Movie.findOneAndUpdate(query, update, { new: true, upsert: true, setDefaultsOnInsert: false }).lean(); doc.genre; // undefined, Mongoose did not set a default value // acquit:ignore:start assert.equal(doc.title, 'The Terminator'); assert.equal(doc.genre, void 0); // acquit:ignore:end });
/** * Unless it is running on a query with `setDefaultsOnInsert`, a default * function's `this` refers to the document. */ it('Default functions and `this`', function() { const schema = new Schema({ title: String, released: Boolean, releaseDate: { type: Date, default: function() { if (this.released) { return Date.now(); } return null; } } });
const Movie = db.model('Movie', schema);
const movie1 = new Movie({ title: 'The Terminator', released: true });
// The post has a default Date set to now assert.ok(movie1.releaseDate.getTime() >= Date.now() - 1000); assert.ok(movie1.releaseDate.getTime() <= Date.now());
const movie2 = new Movie({ title: 'The Legend of Conan', released: false });
// Since `released` is false, the default function will return null assert.strictEqual(movie2.releaseDate, null); });});
mongoose

Version Info

Tagged at
a year ago