deno.land / x / mongoose@6.7.5 / test / helpers / populate.getVirtual.test.js

populate.getVirtual.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
'use strict';
const Schema = require('../../lib/schema');const assert = require('assert');const getVirtual = require('../../lib/helpers/populate/getVirtual');
describe('getVirtual', function() { it('handles embedded discriminators (gh-6411)', function() { // Generate Embedded Discriminators const eventSchema = new Schema( { message: String }, { discriminatorKey: 'kind' } );
const batchSchema = new Schema({ nested: { events: [eventSchema] } });
const docArray = batchSchema.path('nested.events');
// First embedded discriminator has a virtual const clickedSchema = new Schema({ element: { type: String }, users: [Number] });
clickedSchema.virtual('users_$', { ref: 'Users', localField: 'users', foreignField: 'employeeId' });
docArray.discriminator('Clicked', clickedSchema);
// Second embedded discriminator does not have a virtual docArray.discriminator('Purchased', new Schema({ product: { type: String } }));
assert.equal( getVirtual(batchSchema, 'nested.events.users_$').virtual.options.ref, 'Users' ); });
it('handles embedded discriminators under single nested (gh-6488)', function() { // Generate Embedded Discriminators const eventSchema = new Schema({ message: String }, { discriminatorKey: 'kind' });
const batchSchema = new Schema({ nested: { events: [eventSchema] } });
const docArray = batchSchema.path('nested.events');
// *** Adding Nested Layer and adding virtual to schema of nestedLayer const nestedLayerSchema = new Schema({ users: [Number] }, { toJSON: { virtuals: true }, toObject: { virtuals: true } });
nestedLayerSchema.virtual('users_$', { ref: 'Users', localField: 'users', foreignField: 'employeeId' });
// First embedded discriminator schema const clickedSchema = new Schema({ element: { type: String }, nestedLayer: nestedLayerSchema }, { toJSON: { virtuals: true }, toObject: { virtuals: true } });
docArray.discriminator('Clicked', clickedSchema);
// Second embedded discriminator docArray.discriminator('Purchased', new Schema({ product: String }));
const res = getVirtual(batchSchema, 'nested.events.nestedLayer.users_$'); assert.equal(res.virtual.options.ref, 'Users'); });
it('handles multiple calls with discriminator under doc array (gh-6644)', function() { const eventSchema = new Schema({ message: String }, { discriminatorKey: 'kind' });
const batchSchema = new Schema({ events: [eventSchema] });
const docArray = batchSchema.path('events');
const clickedSchema = new Schema({ element: { type: String }, users: [{}] }, { toJSON: { virtuals: true }, toObject: { virtuals: true } });
clickedSchema.virtual('users_$', { ref: 'Users', localField: 'users', foreignField: 'employeeId' });
docArray.discriminator('Clicked', clickedSchema);
let res = getVirtual(batchSchema, 'events.users_$'); assert.equal(res.virtual.options.ref, 'Users'); assert.equal(res.nestedSchemaPath, 'events');
res = getVirtual(batchSchema, 'events.users_$'); assert.equal(res.virtual.options.ref, 'Users'); assert.equal(res.nestedSchemaPath, 'events'); });});
mongoose

Version Info

Tagged at
a year ago