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

transactions.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
'use strict';
const assert = require('assert');const start = require('../common');
const mongoose = start.mongoose;const Schema = mongoose.Schema;
describe('transactions', function() { let db; let _skipped = false; this.timeout(10000);
before(async function() { if (!process.env.REPLICA_SET) { _skipped = true; this.skip(); } db = start({ replicaSet: process.env.REPLICA_SET }); try { await db.asPromise();
// Skip if not a repl set if (db.client.topology.constructor.name !== 'ReplSet' && !db.client.topology.s.description.type.includes('ReplicaSet')) { _skipped = true; this.skip();
throw new Error('skip'); }
const version = await start.mongodVersion();
if (version[0] < 4) { _skipped = true; this.skip(); } } catch (err) { _skipped = true; this.skip(); } });
it('basic example', function() { const Customer = db.model('Customer', new Schema({ name: String }));
let session = null; return Customer.createCollection(). then(() => db.startSession()). then(_session => { session = _session; // Start a transaction session.startTransaction(); // This `create()` is part of the transaction because of the `session` // option. return Customer.create([{ name: 'Test' }], { session: session }); }). // Transactions execute in isolation, so unless you pass a `session` // to `findOne()` you won't see the document until the transaction // is committed. then(() => Customer.findOne({ name: 'Test' })). then(doc => assert.ok(!doc)). // This `findOne()` will return the doc, because passing the `session` // means this `findOne()` will run as part of the transaction. then(() => Customer.findOne({ name: 'Test' }).session(session)). then(doc => assert.ok(doc)). // Once the transaction is committed, the write operation becomes // visible outside of the transaction. then(() => session.commitTransaction()). then(() => Customer.findOne({ name: 'Test' })). then(doc => assert.ok(doc)). then(() => session.endSession()); });
it('withTransaction', function() { // acquit:ignore:start const Customer = db.model('Customer_withTrans', new Schema({ name: String })); // acquit:ignore:end
let session = null; return Customer.createCollection(). then(() => Customer.startSession()). // The `withTransaction()` function's first parameter is a function // that returns a promise. then(_session => { session = _session; return session.withTransaction(() => { return Customer.create([{ name: 'Test' }], { session: session }); }); }). then(() => Customer.countDocuments()). then(count => assert.strictEqual(count, 1)). then(() => session.endSession()); });
it('abort', function() { // acquit:ignore:start const Customer = db.model('Customer0', new Schema({ name: String })); // acquit:ignore:end let session = null; return Customer.createCollection(). then(() => Customer.startSession()). then(_session => { session = _session; session.startTransaction(); return Customer.create([{ name: 'Test' }], { session: session }); }). then(() => Customer.create([{ name: 'Test2' }], { session: session })). then(() => session.abortTransaction()). then(() => Customer.countDocuments()). then(count => assert.strictEqual(count, 0)). then(() => session.endSession()); });
it('save', function() { const User = db.model('User', new Schema({ name: String }));
let session = null; return User.createCollection(). then(() => db.startSession()). then(_session => { session = _session; return User.create({ name: 'foo' }); }). then(() => { session.startTransaction(); return User.findOne({ name: 'foo' }).session(session); }). then(user => { // Getter/setter for the session associated with this document. assert.ok(user.$session()); user.name = 'bar'; // By default, `save()` uses the associated session return user.save(); }). then(() => User.findOne({ name: 'bar' })). // Won't find the doc because `save()` is part of an uncommitted transaction then(doc => assert.ok(!doc)). then(() => session.commitTransaction()). then(() => session.endSession()). then(() => User.findOne({ name: 'bar' })). then(doc => assert.ok(doc)); });
it('create (gh-6909)', function() { const User = db.model('gh6909_User', new Schema({ name: String }));
let session = null; return User.createCollection(). then(() => db.startSession()). then(_session => { session = _session; session.startTransaction(); return User.create([{ name: 'foo' }], { session: session }); }). then(users => { users[0].name = 'bar'; return users[0].save(); }). then(() => User.findOne({ name: 'bar' })). then(user => { // Not in transaction, shouldn't find it assert.ok(!user);
session.commitTransaction(); }); });
it('aggregate', function() { const Event = db.model('Event', new Schema({ createdAt: Date }), 'Event');
let session = null; return Event.createCollection(). then(() => db.startSession()). then(_session => { session = _session; session.startTransaction(); return Event.insertMany([ { createdAt: new Date('2018-06-01') }, { createdAt: new Date('2018-06-02') }, { createdAt: new Date('2017-06-01') }, { createdAt: new Date('2017-05-31') } ], { session: session }); }). then(() => Event.aggregate([ { $group: { _id: { month: { $month: '$createdAt' }, year: { $year: '$createdAt' } }, count: { $sum: 1 } } }, { $sort: { count: -1, '_id.year': -1, '_id.month': -1 } } ]).session(session)). then(res => assert.deepEqual(res, [ { _id: { month: 6, year: 2018 }, count: 2 }, { _id: { month: 6, year: 2017 }, count: 1 }, { _id: { month: 5, year: 2017 }, count: 1 } ])). then(() => session.commitTransaction()). then(() => session.endSession()); });
describe('populate (gh-6754)', function() { let Author; let Article; let session;
before(function() { if (_skipped) { this.skip(); return; // https://github.com/mochajs/mocha/issues/2546 }
Author = db.model('Author', new Schema({ name: String }), 'Author'); Article = db.model('Article', new Schema({ author: { type: mongoose.Schema.Types.ObjectId, ref: 'Author' } }), 'Article');
return Author.createCollection(). then(() => Article.createCollection()); });
beforeEach(function() { return Author.deleteMany({}). then(() => Article.deleteMany({})). then(() => db.startSession()). then(_session => { session = _session; return session.startTransaction(); }); });
afterEach(async function() { await session.commitTransaction(); return session.endSession(); });
it('`populate()` uses the querys session', function() { return Author.create([{ name: 'Val' }], { session: session }). then(authors => Article.create([{ author: authors[0]._id }], { session: session })). then(articles => { return Article. findById(articles[0]._id). session(session). populate('author'); }). then(article => assert.equal(article.author.name, 'Val')); });
it('can override `populate()` session', function() { return Author.create([{ name: 'Val' }], { session: session }). // Article created _outside_ the transaction then(authors => Article.create([{ author: authors[0]._id }])). then(articles => { return Article. findById(articles[0]._id). populate({ path: 'author', options: { session: session } }); }). then(article => assert.equal(article.author.name, 'Val')); });
it('`Document#populate()` uses the documents `$session()` by default', function() { return Author.create([{ name: 'Val' }], { session: session }). then(authors => Article.create([{ author: authors[0]._id }], { session: session })). // By default, the populate query should use the associated `$session()` then(articles => Article.findById(articles[0]._id).session(session)). then(article => { assert.ok(article.$session()); return article.populate('author'); }). then(article => assert.equal(article.author.name, 'Val')); });
it('`Document#populate()` supports overwriting the session', function() { return Author.create([{ name: 'Val' }], { session: session }). then(authors => Article.create([{ author: authors[0]._id }], { session: session })). then(() => Article.findOne().session(session)). then(article => { return article. populate({ path: 'author', options: { session: null } }); }). then(article => assert.ok(!article.author)); }); });
it('deleteOne and deleteMany (gh-7857)(gh-6805)', function() { const Character = db.model('Character', new Schema({ name: String }), 'Character');
let session = null; return Character.createCollection(). then(() => db.startSession()). then(_session => { session = _session; session.startTransaction(); return Character.insertMany([ { name: 'Tyrion Lannister' }, { name: 'Cersei Lannister' }, { name: 'Jon Snow' }, { name: 'Daenerys Targaryen' } ], { session: session }); }). then(() => Character.deleteMany({ name: /Lannister/ }, { session: session })). then(() => Character.deleteOne({ name: 'Jon Snow' }, { session: session })). then(() => Character.find({}).session(session)). then(res => assert.equal(res.length, 1)). then(() => session.commitTransaction()). then(() => session.endSession()); });
it('remove, update, updateOne (gh-7455)', async function() { const Character = db.model('gh7455_Character', new Schema({ name: String, title: String }, { versionKey: false }));
await Character.create({ name: 'Tyrion Lannister' }); const session = await db.startSession();
session.startTransaction();
const tyrion = await Character.findOne().session(session);
await tyrion.updateOne({ title: 'Hand of the King' });
// Session isn't committed assert.equal(await Character.countDocuments({ title: /hand/i }), 0);
await tyrion.remove();
// Undo both update and delete since doc should pull from `$session()` await session.abortTransaction(); session.endSession();
const fromDb = await Character.findOne().then(doc => doc.toObject()); delete fromDb._id; assert.deepEqual(fromDb, { name: 'Tyrion Lannister' }); });
it('save() with no changes (gh-8571)', async function() { const Test = db.model('Test', Schema({ name: String }));
await Test.createCollection(); const session = await db.startSession(); await session.withTransaction(async () => { const test = await Test.create([{}], { session }).then(res => res[0]); await test.save(); // throws DocumentNotFoundError }); await session.endSession(); });});
mongoose

Version Info

Tagged at
a year ago