monastery 2.2.2 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/test/populate.js CHANGED
@@ -1,295 +1,282 @@
1
- module.exports = function(monastery, opendb) {
1
+ const monastery = require('../lib/index.js')
2
2
 
3
- test('model populate', async () => {
4
- // Setup
5
- let db = (await opendb(null)).db
6
- let bird = db.model('bird', { fields: {
7
- name: { type: 'string' }
8
- }})
9
- let user = db.model('user', { fields: {
10
- name: { type: 'string' },
11
- myBird: { model: 'bird' },
12
- pets: { myBird: { model: 'bird' } }
13
- }})
14
- let bird1 = await bird.insert({ data: { name: 'ponyo' }})
15
- let user1 = await user.insert({ data: {
16
- name: 'Martin Luther',
17
- myBird: bird1._id,
18
- pets: { myBird: bird1._id }
19
- }})
20
- let user2 = await user.insert({ data: {
21
- name: 'Martin Luther2',
22
- myBird: bird1._id,
23
- pets: { myBird: bird1._id }
24
- }})
3
+ let db
4
+ beforeAll(async () => { db = monastery('127.0.0.1/monastery', { timestamps: false }) })
5
+ afterAll(async () => { db.close() })
25
6
 
26
- // Basic populate
27
- let find1 = await user.findOne({ query: user1._id, populate: ['myBird'] })
28
- expect(find1).toEqual({
29
- _id: user1._id,
30
- name: 'Martin Luther',
31
- myBird: {
32
- _id: bird1._id,
33
- name: 'ponyo'
34
- },
35
- pets: {
36
- myBird: bird1._id
37
- }
38
- })
7
+ test('model populate', async () => {
8
+ let bird = db.model('bird', { fields: {
9
+ name: { type: 'string' },
10
+ }})
11
+ let user = db.model('user', { fields: {
12
+ name: { type: 'string' },
13
+ myBird: { model: 'bird' },
14
+ pets: { myBird: { model: 'bird' } },
15
+ }})
16
+ let bird1 = await bird.insert({ data: { name: 'ponyo' }})
17
+ let user1 = await user.insert({ data: {
18
+ name: 'Martin Luther',
19
+ myBird: bird1._id,
20
+ pets: { myBird: bird1._id },
21
+ }})
22
+ let user2 = await user.insert({ data: {
23
+ name: 'Martin Luther2',
24
+ myBird: bird1._id,
25
+ pets: { myBird: bird1._id },
26
+ }})
39
27
 
40
- // Deep populate
41
- let find2 = await user.findOne({ query: user1._id, populate: ['pets.myBird'] })
42
- expect(find2).toEqual({
43
- _id: user1._id,
44
- name: 'Martin Luther',
28
+ // Basic populate
29
+ let find1 = await user.findOne({ query: user1._id, populate: ['myBird'] })
30
+ expect(find1).toEqual({
31
+ _id: user1._id,
32
+ name: 'Martin Luther',
33
+ myBird: {
34
+ _id: bird1._id,
35
+ name: 'ponyo',
36
+ },
37
+ pets: {
45
38
  myBird: bird1._id,
46
- pets: {
47
- myBird: {
48
- _id: bird1._id,
49
- name: 'ponyo'
50
- },
51
- }
52
- })
53
-
54
- // Populate mulitple documents
55
- let find3 = await user.find({
56
- query: { _id: { $in: [user1._id, user2._id] }},
57
- populate: ['myBird']
58
- })
39
+ },
40
+ })
59
41
 
60
- expect(find3).toEqual([{
61
- _id: user1._id,
62
- name: 'Martin Luther',
63
- myBird: {
64
- _id: bird1._id,
65
- name: 'ponyo'
66
- },
67
- pets: {
68
- myBird: bird1._id
69
- }
70
- },{
71
- _id: user2._id,
72
- name: 'Martin Luther2',
42
+ // Deep populate
43
+ let find2 = await user.findOne({ query: user1._id, populate: ['pets.myBird'] })
44
+ expect(find2).toEqual({
45
+ _id: user1._id,
46
+ name: 'Martin Luther',
47
+ myBird: bird1._id,
48
+ pets: {
73
49
  myBird: {
74
50
  _id: bird1._id,
75
- name: 'ponyo'
51
+ name: 'ponyo',
76
52
  },
77
- pets: {
78
- myBird: bird1._id
79
- }
80
- }])
53
+ },
54
+ })
81
55
 
82
- db.close()
56
+ // Populate mulitple documents
57
+ let find3 = await user.find({
58
+ query: { _id: { $in: [user1._id, user2._id] }},
59
+ populate: ['myBird'],
83
60
  })
84
61
 
85
- test('model populate array', async () => {
86
- // Setup
87
- let db = (await opendb(null)).db
88
- let bird = db.model('bird', { fields: {
89
- name: { type: 'string' }
90
- }})
91
- let user = db.model('user', { fields: {
92
- birds: [{ model: 'bird' }],
93
- animal: { birds: [{ model: 'bird' }] },
94
- animals: [{ bird: { model: 'bird' }, num: { type: 'number' } }],
95
- }})
96
- let bird1 = await bird.insert({ data: { name: 'ponyo' }})
97
- let bird2 = await bird.insert({ data: { name: 'jack' }})
98
- let bird3 = await bird.insert({ data: { name: 'sophie' }})
99
- let user1 = await user.insert({ data: {
62
+ expect(find3).toEqual([{
63
+ _id: user1._id,
64
+ name: 'Martin Luther',
65
+ myBird: {
66
+ _id: bird1._id,
67
+ name: 'ponyo',
68
+ },
69
+ pets: {
70
+ myBird: bird1._id,
71
+ },
72
+ },{
73
+ _id: user2._id,
74
+ name: 'Martin Luther2',
75
+ myBird: {
76
+ _id: bird1._id,
77
+ name: 'ponyo',
78
+ },
79
+ pets: {
80
+ myBird: bird1._id,
81
+ },
82
+ }])
83
+ })
84
+
85
+ test('model populate array', async () => {
86
+ let bird = db.model('bird', { fields: {
87
+ name: { type: 'string' },
88
+ }})
89
+ let user = db.model('user', { fields: {
90
+ birds: [{ model: 'bird' }],
91
+ animal: { birds: [{ model: 'bird' }] },
92
+ animals: [{ bird: { model: 'bird' }, num: { type: 'number' } }],
93
+ }})
94
+ let bird1 = await bird.insert({ data: { name: 'ponyo' }})
95
+ let bird2 = await bird.insert({ data: { name: 'jack' }})
96
+ let bird3 = await bird.insert({ data: { name: 'sophie' }})
97
+ let user1 = await user.insert({ data: {
98
+ birds: [bird1._id, bird2._id],
99
+ animal: { birds: [bird1._id, bird2._id] },
100
+ animals: [{ bird: bird1._id, num: 1 }, { bird: bird3._id, num: 2 }],
101
+ }})
102
+
103
+ // Array
104
+ let find1 = await user.findOne({ query: user1._id, populate: ['birds'] })
105
+ expect(find1).toEqual({
106
+ _id: user1._id,
107
+ birds: [
108
+ { _id: bird1._id, name: 'ponyo' },
109
+ { _id: bird2._id, name: 'jack' },
110
+ ],
111
+ animal: {
100
112
  birds: [bird1._id, bird2._id],
101
- animal: { birds: [bird1._id, bird2._id] },
102
- animals: [{ bird: bird1._id, num: 1 }, { bird: bird3._id, num: 2 }],
103
- }})
113
+ },
114
+ animals: [
115
+ { bird: bird1._id, num: 1 },
116
+ { bird: bird3._id, num: 2 },
117
+ ],
118
+ })
104
119
 
105
- // Array
106
- let find1 = await user.findOne({ query: user1._id, populate: ['birds'] })
107
- expect(find1).toEqual({
108
- _id: user1._id,
120
+ // Nested array
121
+ let find2 = await user.findOne({ query: user1._id, populate: ['animal.birds'] })
122
+ expect(find2).toEqual({
123
+ _id: user1._id,
124
+ birds: [bird1._id, bird2._id],
125
+ animal: {
109
126
  birds: [
110
127
  { _id: bird1._id, name: 'ponyo' },
111
128
  { _id: bird2._id, name: 'jack' },
112
129
  ],
113
- animal: {
114
- birds: [bird1._id, bird2._id],
115
- },
116
- animals: [
117
- { bird: bird1._id, num: 1 },
118
- { bird: bird3._id, num: 2 },
119
- ],
120
- })
130
+ },
131
+ animals: [
132
+ { bird: bird1._id, num: 1 },
133
+ { bird: bird3._id, num: 2 },
134
+ ],
135
+ })
121
136
 
122
- // Nested array
123
- let find2 = await user.findOne({ query: user1._id, populate: ['animal.birds'] })
124
- expect(find2).toEqual({
125
- _id: user1._id,
126
- birds: [bird1._id, bird2._id],
127
- animal: {
128
- birds: [
129
- { _id: bird1._id, name: 'ponyo' },
130
- { _id: bird2._id, name: 'jack' },
131
- ],
132
- },
133
- animals: [
134
- { bird: bird1._id, num: 1 },
135
- { bird: bird3._id, num: 2 },
137
+ // modelId within an array of subdocuments (won't populate, but show a debug error)
138
+ user.error = (e) => {} // hide debug error
139
+ let find3 = await user.findOne({ query: user1._id, populate: ['animals.bird'] })
140
+ expect(find3).toEqual({
141
+ _id: user1._id,
142
+ birds: [bird1._id, bird2._id],
143
+ animal: {
144
+ birds: [
145
+ bird1._id,
146
+ bird2._id,
136
147
  ],
137
- })
148
+ },
149
+ animals: [
150
+ { bird: bird1._id, num: 1 },
151
+ { bird: bird3._id, num: 2 },
152
+ ],
153
+ })
154
+ })
138
155
 
139
- // modelId within an array of subdocuments (won't populate, but show a debug error)
140
- user.error = (e) => {/*console.log(e)*/} // hide debug error
141
- let find3 = await user.findOne({ query: user1._id, populate: ['animals.bird'] })
142
- expect(find3).toEqual({
143
- _id: user1._id,
144
- birds: [bird1._id, bird2._id],
145
- animal: {
146
- birds: [
147
- bird1._id,
148
- bird2._id
149
- ],
156
+ test('model populate type=any', async () => {
157
+ db.model('company', { fields: {
158
+ address: { type: 'any' },
159
+ }})
160
+ db.model('user', {
161
+ fields: {
162
+ company: { model: 'company' },
163
+ cards: { type: 'any' },
164
+ cards2: {
165
+ white: { type: 'number' },
166
+ yellow: { type: 'number' },
150
167
  },
151
- animals: [
152
- { bird: bird1._id, num: 1 },
153
- { bird: bird3._id, num: 2 },
154
- ],
155
- })
156
-
157
- db.close()
168
+ cards3: {
169
+ white: { type: 'number' },
170
+ },
171
+ },
172
+ findBL: ['cards2.white', 'cards3.white'],
158
173
  })
174
+ let company = await db.company.insert({ data: {
175
+ address: {
176
+ number: 1234,
177
+ city: 'Auckland',
178
+ },
179
+ }})
180
+ let user = await db.user.insert({ data: {
181
+ company: company._id,
182
+ cards: { black: 1234 },
183
+ cards2: { white: 1234, yellow: 1234 },
184
+ cards3: { white: 1234 },
185
+ }})
159
186
 
160
- test('model populate type=any', async () => {
161
- let db = (await opendb(null)).db
162
- db.model('company', { fields: {
163
- address: { type: 'any' }
164
- }})
165
- db.model('user', {
166
- fields: {
167
- company: { model: 'company' },
168
- cards: { type: 'any' },
169
- cards2: {
170
- white: { type: 'number' },
171
- yellow: { type: 'number' }
172
- },
173
- cards3: {
174
- white: { type: 'number' },
175
- }
176
- },
177
- findBL: ['cards2.white', 'cards3.white']
178
- })
179
- let company = await db.company.insert({ data: {
187
+ let foundUser = await db.user.find({ query: user._id, populate: ['company'] })
188
+ expect(foundUser).toEqual({
189
+ _id: user._id,
190
+ company: {
191
+ _id: company._id,
180
192
  address: {
181
193
  number: 1234,
182
- city: 'Auckland'
183
- }
184
- }})
185
- let user = await db.user.insert({ data: {
186
- company: company._id,
187
- cards: { black: 1234 },
188
- cards2: { white: 1234, yellow: 1234 },
189
- cards3: { white: 1234 }
190
- }})
191
-
192
- let foundUser = await db.user.find({ query: user._id, populate: ['company'] })
193
- expect(foundUser).toEqual({
194
- _id: user._id,
195
- company: {
196
- _id: company._id,
197
- address: {
198
- number: 1234,
199
- city: 'Auckland'
200
- }
194
+ city: 'Auckland',
201
195
  },
202
- cards: { black: 1234 },
203
- cards2: { yellow: 1234 },
204
- cards3: {}
205
- })
206
-
207
- db.close()
196
+ },
197
+ cards: { black: 1234 },
198
+ cards2: { yellow: 1234 },
199
+ cards3: {},
208
200
  })
201
+ })
209
202
 
210
- test('model populate or blacklisting via $lookup', async () => {
211
- // Setup
212
- let db = (await opendb(null)).db
213
- let user = db.model('user', {
214
- fields: {
215
- birds: [{ model: 'bird' }],
216
- anyModel: [{ type: 'any' }]
217
- },
218
- findBL: [
219
- 'birds.name',
220
- 'anyModel.name'
221
- ]
222
- })
223
- let bird = db.model('bird', {
224
- fields: {
225
- name: { type: 'string' },
226
- color: { type: 'string' },
227
- owner: { model: 'user' }
228
- },
229
- findBL: [
230
- 'color'
231
- ]
232
- })
233
-
234
- let user1 = await user.insert({ data: {} })
235
- let id = user1._id.toString()
236
- let bird1 = await bird.insert({ data: { color: 'red', name: 'ponyo', owner: id, anyModel: id }})
237
- let bird2 = await bird.insert({ data: { color: 'blue', name: 'carla', owner: id, anyModel: id }})
238
-
239
- // Multiple $lookup
240
- let find1 = await user.findOne({
241
- query: user1._id,
242
- populate: [{
243
- 'as': 'birds',
244
- 'from': 'bird',
245
- 'let': { id: '$_id' },
246
- 'pipeline': [
247
- { $match: { $expr: { $eq: ['$owner', '$$id'] }}}
248
- ]
249
- }]
250
- })
203
+ test('model populate or blacklisting via $lookup', async () => {
204
+ let user = db.model('user', {
205
+ fields: {
206
+ birds: [{ model: 'bird' }],
207
+ anyModel: [{ type: 'any' }],
208
+ },
209
+ findBL: [
210
+ 'birds.name',
211
+ 'anyModel.name',
212
+ ],
213
+ })
214
+ let bird = db.model('bird', {
215
+ fields: {
216
+ name: { type: 'string' },
217
+ color: { type: 'string' },
218
+ owner: { model: 'user' },
219
+ },
220
+ findBL: [
221
+ 'color',
222
+ ],
223
+ })
251
224
 
252
- expect(find1).toEqual({
253
- _id: user1._id,
254
- birds: [
255
- {
256
- _id: bird1._id,
257
- owner: user1._id
258
- }, {
259
- _id: bird2._id,
260
- owner: user1._id
261
- }
262
- ]
263
- })
225
+ let user1 = await user.insert({ data: {} })
226
+ let id = user1._id.toString()
227
+ let bird1 = await bird.insert({ data: { color: 'red', name: 'ponyo', owner: id, anyModel: id }})
228
+ let bird2 = await bird.insert({ data: { color: 'blue', name: 'carla', owner: id, anyModel: id }})
264
229
 
265
- // $lookup on type:any
266
- let find2 = await user.findOne({
267
- query: user1._id,
268
- populate: [{
269
- 'as': 'anyModel',
270
- 'from': 'bird',
271
- 'let': { id: '$_id' },
272
- 'pipeline': [
273
- { $match: { $expr: { $eq: ['$owner', '$$id'] }}}
274
- ]
275
- }]
276
- })
277
- expect(find2).toEqual({
278
- _id: user1._id,
279
- anyModel: [
280
- {
281
- _id: bird1._id,
282
- color: 'red',
283
- owner: user1._id
284
- }, {
285
- _id: bird2._id,
286
- color: 'blue',
287
- owner: user1._id
288
- }
289
- ]
290
- })
230
+ // Multiple $lookup
231
+ let find1 = await user.findOne({
232
+ query: user1._id,
233
+ populate: [{
234
+ 'as': 'birds',
235
+ 'from': 'bird',
236
+ 'let': { id: '$_id' },
237
+ 'pipeline': [
238
+ { $match: { $expr: { $eq: ['$owner', '$$id'] }}},
239
+ ],
240
+ }],
241
+ })
291
242
 
292
- db.close()
243
+ expect(find1).toEqual({
244
+ _id: user1._id,
245
+ birds: [
246
+ {
247
+ _id: bird1._id,
248
+ owner: user1._id,
249
+ }, {
250
+ _id: bird2._id,
251
+ owner: user1._id,
252
+ },
253
+ ],
293
254
  })
294
255
 
295
- }
256
+ // $lookup on type:any
257
+ let find2 = await user.findOne({
258
+ query: user1._id,
259
+ populate: [{
260
+ 'as': 'anyModel',
261
+ 'from': 'bird',
262
+ 'let': { id: '$_id' },
263
+ 'pipeline': [
264
+ { $match: { $expr: { $eq: ['$owner', '$$id'] }}},
265
+ ],
266
+ }],
267
+ })
268
+ expect(find2).toEqual({
269
+ _id: user1._id,
270
+ anyModel: [
271
+ {
272
+ _id: bird1._id,
273
+ color: 'red',
274
+ owner: user1._id,
275
+ }, {
276
+ _id: bird2._id,
277
+ color: 'blue',
278
+ owner: user1._id,
279
+ },
280
+ ],
281
+ })
282
+ })