deno.land / x / mongoose@6.7.5 / test / docs / lean.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
'use strict';
const assert = require('assert');const v8Serialize = require('v8').serialize;const start = require('../common');
// This file is in `es-next` because it uses async/await for convenience
describe('Lean Tutorial', function() { let MyModel; const mongoose = new start.mongoose.Mongoose();
before(function() { const schema = new mongoose.Schema({ name: String }); MyModel = mongoose.model('Test1', schema);
return mongoose.connect(start.uri); });
beforeEach(function() { mongoose.deleteModel(/Person/); mongoose.deleteModel(/Group/); });
it('compare sizes lean vs not lean', async function() { const schema = new mongoose.Schema({ name: String }); const MyModel = mongoose.model('Test', schema);
await MyModel.create({ name: 'test' });
const normalDoc = await MyModel.findOne(); // To enable the `lean` option for a query, use the `lean()` function. const leanDoc = await MyModel.findOne().lean();
v8Serialize(normalDoc).length; // approximately 180 v8Serialize(leanDoc).length; // 32, about 5x smaller!
// In case you were wondering, the JSON form of a Mongoose doc is the same // as the POJO. This additional memory only affects how much memory your // Node.js process uses, not how much data is sent over the network. JSON.stringify(normalDoc).length === JSON.stringify(leanDoc).length; // true // acquit:ignore:start assert.ok(v8Serialize(normalDoc).length >= 150 && v8Serialize(normalDoc).length <= 200, v8Serialize(normalDoc).length); assert.equal(v8Serialize(leanDoc).length, 32); assert.equal(JSON.stringify(normalDoc).length, JSON.stringify(leanDoc).length); // acquit:ignore:end });
it('compare types', async function() { // acquit:ignore:start await MyModel.create({ name: 'test' }); // acquit:ignore:end const normalDoc = await MyModel.findOne(); const leanDoc = await MyModel.findOne().lean();
normalDoc instanceof mongoose.Document; // true normalDoc.constructor.name; // 'model'
leanDoc instanceof mongoose.Document; // false leanDoc.constructor.name; // 'Object' // acquit:ignore:start assert.ok(normalDoc instanceof mongoose.Document); assert.equal(normalDoc.constructor.name, 'model'); assert.ok(!(leanDoc instanceof mongoose.Document)); assert.equal(leanDoc.constructor.name, 'Object'); // acquit:ignore:end });
it('getters and virtuals', async function() { // Define a `Person` model. Schema has 2 custom getters and a `fullName` // virtual. Neither the getters nor the virtuals will run if lean is enabled. const personSchema = new mongoose.Schema({ firstName: { type: String, get: capitalizeFirstLetter }, lastName: { type: String, get: capitalizeFirstLetter } }); personSchema.virtual('fullName').get(function() { return `${this.firstName} ${this.lastName}`; }); function capitalizeFirstLetter(v) { // Convert 'bob' -> 'Bob' return v.charAt(0).toUpperCase() + v.substring(1); } const Person = mongoose.model('Person', personSchema); // acquit:ignore:start await Person.deleteMany({}); // acquit:ignore:end
// Create a doc and load it as a lean doc await Person.create({ firstName: 'benjamin', lastName: 'sisko' }); const normalDoc = await Person.findOne(); const leanDoc = await Person.findOne().lean();
normalDoc.fullName; // 'Benjamin Sisko' normalDoc.firstName; // 'Benjamin', because of `capitalizeFirstLetter()` normalDoc.lastName; // 'Sisko', because of `capitalizeFirstLetter()`
leanDoc.fullName; // undefined leanDoc.firstName; // 'benjamin', custom getter doesn't run leanDoc.lastName; // 'sisko', custom getter doesn't run // acquit:ignore:start assert.equal(normalDoc.fullName, 'Benjamin Sisko'); assert.equal(normalDoc.firstName, 'Benjamin'); assert.equal(normalDoc.lastName, 'Sisko'); assert.equal(leanDoc.fullName, void 0); assert.equal(leanDoc.firstName, 'benjamin'); assert.equal(leanDoc.lastName, 'sisko'); // acquit:ignore:end });
it('conventional populate', async function() { // Create models const Group = mongoose.model('Group', new mongoose.Schema({ name: String, members: [{ type: mongoose.ObjectId, ref: 'Person' }] })); const Person = mongoose.model('Person', new mongoose.Schema({ name: String })); // acquit:ignore:start await Group.deleteMany({}); await Person.deleteMany({}); // acquit:ignore:end
// Initialize data const people = await Person.create([ { name: 'Benjamin Sisko' }, { name: 'Kira Nerys' } ]); await Group.create({ name: 'Star Trek: Deep Space Nine Characters', members: people.map(p => p._id) });
// Execute a lean query const group = await Group.findOne().lean().populate('members'); group.members[0].name; // 'Benjamin Sisko' group.members[1].name; // 'Kira Nerys'
// Both the `group` and the populated `members` are lean. group instanceof mongoose.Document; // false group.members[0] instanceof mongoose.Document; // false group.members[1] instanceof mongoose.Document; // false // acquit:ignore:start assert.equal(group.members[0].name, 'Benjamin Sisko'); assert.equal(group.members[1].name, 'Kira Nerys'); // acquit:ignore:end });
it('virtual populate', async function() { // Create models const groupSchema = new mongoose.Schema({ name: String }); groupSchema.virtual('members', { ref: 'Person', localField: '_id', foreignField: 'groupId' }); const Group = mongoose.model('Group', groupSchema); const Person = mongoose.model('Person', new mongoose.Schema({ name: String, groupId: mongoose.ObjectId })); // acquit:ignore:start await Group.deleteMany({}); await Person.deleteMany({}); // acquit:ignore:end
// Initialize data const g = await Group.create({ name: 'DS9 Characters' }); const people = await Person.create([ { name: 'Benjamin Sisko', groupId: g._id }, { name: 'Kira Nerys', groupId: g._id } ]);
// Execute a lean query const group = await Group.findOne().lean().populate({ path: 'members', options: { sort: { name: 1 } } }); group.members[0].name; // 'Benjamin Sisko' group.members[1].name; // 'Kira Nerys'
// Both the `group` and the populated `members` are lean. group instanceof mongoose.Document; // false group.members[0] instanceof mongoose.Document; // false group.members[1] instanceof mongoose.Document; // false // acquit:ignore:start assert.equal(group.members[0].name, 'Benjamin Sisko'); assert.equal(group.members[1].name, 'Kira Nerys'); // acquit:ignore:end });});
mongoose

Version Info

Tagged at
a year ago