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/virtuals.js CHANGED
@@ -1,250 +1,245 @@
1
- module.exports = function(monastery, opendb) {
1
+ const monastery = require('../lib/index.js')
2
2
 
3
- test('virtuals', async () => {
4
- // Setup
5
- let db = (await opendb(null)).db
6
- // Test model setup
7
- let bird = db.model('bird', {
8
- fields: {
9
- age: { type: 'number', virtual: true, default: 55 },
10
- birdsOwner: { model: 'user' },
11
- name: { type: 'string' },
12
- }
13
- })
14
- let user = db.model('user', {
15
- fields: {
16
- myBird: { model: 'bird', virtual: true },
17
- myBirds: [{ model: 'bird', virtual: true }],
18
- name: { type: 'string' },
19
- }
20
- })
21
- expect(user.fields.name.virtual).toEqual(undefined)
22
- expect(user.fields.myBirds[0].virtual).toEqual(true)
23
- expect(user.fields.myBirds.schema.virtual).toEqual(true)
3
+ let db
4
+ beforeAll(async () => { db = monastery('127.0.0.1/monastery', { timestamps: false }) })
5
+ afterAll(async () => { db.close() })
24
6
 
25
- // Test insert/update/validate
26
- let user1 = await user.insert({
27
- data: {
28
- myBird: db.id(), // shouldnt be added
29
- myBirds: [db.id()], // shouldnt be added
30
- name: 'Bruce',
31
- }
32
- })
33
- let bird1 = await bird.insert({
34
- data: {
35
- age: 12, // shouldnt be inserted
36
- birdsOwner: user1._id,
37
- name: 'Ponyo',
38
- }
39
- })
40
- let updatedBird1 = await bird.update({
41
- query: bird1._id,
42
- data: {
43
- age: 12, // shouldnt be added
44
- birdsOwner: user1._id,
45
- name: 'Ponyo2',
46
- }
47
- })
48
- let validatedBird1 = await bird.validate({
49
- age: 12, // shouldnt be included
7
+ test('virtuals', async () => {
8
+ // Test model setup
9
+ let bird = db.model('bird', {
10
+ fields: {
11
+ age: { type: 'number', virtual: true, default: 55 },
12
+ birdsOwner: { model: 'user' },
13
+ name: { type: 'string' },
14
+ },
15
+ })
16
+ let user = db.model('user', {
17
+ fields: {
18
+ myBird: { model: 'bird', virtual: true },
19
+ myBirds: [{ model: 'bird', virtual: true }],
20
+ name: { type: 'string' },
21
+ },
22
+ })
23
+ expect(user.fields.name.virtual).toEqual(undefined)
24
+ expect(user.fields.myBirds[0].virtual).toEqual(true)
25
+ expect(user.fields.myBirds.schema.virtual).toEqual(true)
26
+
27
+ // Test insert/update/validate
28
+ let user1 = await user.insert({
29
+ data: {
30
+ myBird: db.id(), // shouldnt be added
31
+ myBirds: [db.id()], // shouldnt be added
32
+ name: 'Bruce',
33
+ },
34
+ })
35
+ let bird1 = await bird.insert({
36
+ data: {
37
+ age: 12, // shouldnt be inserted
50
38
  birdsOwner: user1._id,
51
39
  name: 'Ponyo',
52
- })
53
- expect(user1).toEqual({
54
- _id: user1._id,
55
- name: 'Bruce'
56
- })
57
- expect(bird1).toEqual({
58
- _id: bird1._id,
59
- birdsOwner: user1._id,
60
- name: 'Ponyo'
61
- })
62
- expect(updatedBird1).toEqual({
63
- birdsOwner: user1._id,
64
- name: 'Ponyo2'
65
- })
66
- expect(validatedBird1).toEqual({
40
+ },
41
+ })
42
+ let updatedBird1 = await bird.update({
43
+ query: bird1._id,
44
+ data: {
45
+ age: 12, // shouldnt be added
67
46
  birdsOwner: user1._id,
68
- name: 'Ponyo'
69
- })
47
+ name: 'Ponyo2',
48
+ },
49
+ })
50
+ let validatedBird1 = await bird.validate({
51
+ age: 12, // shouldnt be included
52
+ birdsOwner: user1._id,
53
+ name: 'Ponyo',
54
+ })
55
+ expect(user1).toEqual({
56
+ _id: user1._id,
57
+ name: 'Bruce',
58
+ })
59
+ expect(bird1).toEqual({
60
+ _id: bird1._id,
61
+ birdsOwner: user1._id,
62
+ name: 'Ponyo',
63
+ })
64
+ expect(updatedBird1).toEqual({
65
+ birdsOwner: user1._id,
66
+ name: 'Ponyo2',
67
+ })
68
+ expect(validatedBird1).toEqual({
69
+ birdsOwner: user1._id,
70
+ name: 'Ponyo',
71
+ })
70
72
 
71
- // Test find
72
- let find1 = await user.findOne({
73
- query: user1._id,
74
- populate: [{
75
- as: 'myBirds',
76
- from: 'bird',
77
- let: { id: '$_id' },
78
- pipeline: [{
79
- $match: {
80
- $expr: {
81
- $eq: ['$birdsOwner', '$$id']
82
- }
83
- }
84
- }]
85
- }]
86
- })
87
- expect(find1).toEqual({
88
- _id: user1._id,
89
- myBirds: [{
90
- _id: bird1._id,
91
- age: 55,
92
- birdsOwner: user1._id,
93
- name: 'Ponyo2'
73
+ // Test find
74
+ let find1 = await user.findOne({
75
+ query: user1._id,
76
+ populate: [{
77
+ as: 'myBirds',
78
+ from: 'bird',
79
+ let: { id: '$_id' },
80
+ pipeline: [{
81
+ $match: {
82
+ $expr: {
83
+ $eq: ['$birdsOwner', '$$id'],
84
+ },
85
+ },
94
86
  }],
95
- name: 'Bruce'
96
- })
87
+ }],
88
+ })
89
+ expect(find1).toEqual({
90
+ _id: user1._id,
91
+ myBirds: [{
92
+ _id: bird1._id,
93
+ age: 55,
94
+ birdsOwner: user1._id,
95
+ name: 'Ponyo2',
96
+ }],
97
+ name: 'Bruce',
98
+ })
97
99
 
98
- // Test find is processing models for incorrect data structures
99
- let find2 = await user.findOne({
100
- query: user1._id,
101
- populate: [{
102
- // should be an object but the $lookup returns an array
103
- as: 'myBird',
104
- from: 'bird',
105
- let: { id: '$_id' },
106
- pipeline: [{
107
- $match: {
108
- $expr: {
109
- $eq: ['$birdsOwner', '$$id']
110
- }
111
- }
112
- }]
113
- }, {
114
- // should be an array but the query returns an object
115
- as: 'myBirds',
116
- from: 'bird',
117
- let: { id: '$_id' },
118
- pipeline: [{
119
- $match: {
120
- $expr: {
121
- $eq: ['$birdsOwner', '$$id']
122
- }
123
- }
124
- }]
100
+ // Test find is processing models for incorrect data structures
101
+ let find2 = await user.findOne({
102
+ query: user1._id,
103
+ populate: [{
104
+ // should be an object but the $lookup returns an array
105
+ as: 'myBird',
106
+ from: 'bird',
107
+ let: { id: '$_id' },
108
+ pipeline: [{
109
+ $match: {
110
+ $expr: {
111
+ $eq: ['$birdsOwner', '$$id'],
112
+ },
113
+ },
125
114
  }],
126
- addFields: {
127
- myBirds: { $arrayElemAt: ['$myBirds', 0] }
128
- }
129
- })
130
- expect(find2).toEqual({
131
- _id: user1._id,
132
- myBird: [{
133
- _id: bird1._id,
134
- age: 55, // means the model was processed
135
- birdsOwner: user1._id,
136
- name: 'Ponyo2'
115
+ }, {
116
+ // should be an array but the query returns an object
117
+ as: 'myBirds',
118
+ from: 'bird',
119
+ let: { id: '$_id' },
120
+ pipeline: [{
121
+ $match: {
122
+ $expr: {
123
+ $eq: ['$birdsOwner', '$$id'],
124
+ },
125
+ },
137
126
  }],
138
- myBirds: {
139
- _id: bird1._id,
140
- age: 55, // means the model was processed
141
- birdsOwner: user1._id,
142
- name: 'Ponyo2'
143
- },
144
- name: 'Bruce'
145
- })
146
-
147
- db.close()
127
+ }],
128
+ addFields: {
129
+ myBirds: { $arrayElemAt: ['$myBirds', 0] },
130
+ },
131
+ })
132
+ expect(find2).toEqual({
133
+ _id: user1._id,
134
+ myBird: [{
135
+ _id: bird1._id,
136
+ age: 55, // means the model was processed
137
+ birdsOwner: user1._id,
138
+ name: 'Ponyo2',
139
+ }],
140
+ myBirds: {
141
+ _id: bird1._id,
142
+ age: 55, // means the model was processed
143
+ birdsOwner: user1._id,
144
+ name: 'Ponyo2',
145
+ },
146
+ name: 'Bruce',
148
147
  })
148
+ })
149
149
 
150
- test('insert update virtuals (validate)', async () => {
151
- // Setup
152
- let db = (await opendb(null)).db
153
- let user = db.model('user', {
154
- fields: {
155
- list: [{ type: 'number' }],
156
- dog: { type: 'string' },
157
- pet: { type: 'string' },
158
- pets: [{ name: { type: 'string'}, age: { type: 'number'} }],
159
- animals: {
160
- cat: { type: 'string' },
161
- dog: { type: 'string' }
162
- },
163
- hiddenPets: [{
164
- name: { type: 'string'}
165
- }],
166
- hiddenList: [{ type: 'number'}],
167
- deep: {
168
- deep2: {
169
- deep3: {
170
- deep4: { type: 'string' }
171
- }
172
- }
173
- }
174
- },
175
- insertBL: [
176
- 'dog',
177
- 'animals.cat',
178
- 'pets.age',
179
- 'hiddenPets',
180
- 'hiddenList',
181
- 'deep.deep2.deep3'
182
- ]
183
- })
184
- let doc1 = {
185
- list: [44, 54],
186
- dog: 'Bruce',
187
- pet: 'Freddy',
188
- pets: [{ name: 'Pluto', age: 5 }, { name: 'Milo', age: 4 }],
150
+ test('insert update virtuals (validate)', async () => {
151
+ let user = db.model('user', {
152
+ fields: {
153
+ list: [{ type: 'number' }],
154
+ dog: { type: 'string' },
155
+ pet: { type: 'string' },
156
+ pets: [{ name: { type: 'string'}, age: { type: 'number'} }],
189
157
  animals: {
190
- cat: 'Ginger',
191
- dog: 'Max'
158
+ cat: { type: 'string' },
159
+ dog: { type: 'string' },
192
160
  },
193
161
  hiddenPets: [{
194
- name: 'secretPet'
162
+ name: { type: 'string'},
195
163
  }],
196
- hiddenList: [12, 23],
164
+ hiddenList: [{ type: 'number'}],
197
165
  deep: {
198
166
  deep2: {
199
167
  deep3: {
200
- deep4: 'hideme'
201
- }
202
- }
203
- }
204
- }
205
-
206
- // Default insert validation
207
- let user1 = await user.validate(doc1)
208
- expect(user1).toEqual({
209
- list: [44, 54],
210
- pet: 'Freddy',
211
- pets: [{ name: 'Pluto' }, { name: 'Milo' }],
212
- animals: {
213
- dog: 'Max'
168
+ deep4: { type: 'string' },
169
+ },
170
+ },
214
171
  },
215
- deep: {
216
- deep2: {}
217
- }
218
- })
219
-
220
- // Custom blacklist (remove and add to the current schema blacklist)
221
- let user2 = await user.validate(doc1, {
222
- blacklist: [
223
- '-dog',
224
- '-animals.dog', // wrong property
225
- 'pets.name',
226
- '-hiddenList',
227
- '-deep' // blacklist a parent
228
- ],
229
- })
230
- expect(user2).toEqual({
231
- list: [44, 54],
232
- dog: 'Bruce',
233
- pet: 'Freddy',
234
- pets: [ {}, {} ],
235
- animals: {
236
- dog: 'Max'
172
+ },
173
+ insertBL: [
174
+ 'dog',
175
+ 'animals.cat',
176
+ 'pets.age',
177
+ 'hiddenPets',
178
+ 'hiddenList',
179
+ 'deep.deep2.deep3',
180
+ ],
181
+ })
182
+ let doc1 = {
183
+ list: [44, 54],
184
+ dog: 'Bruce',
185
+ pet: 'Freddy',
186
+ pets: [{ name: 'Pluto', age: 5 }, { name: 'Milo', age: 4 }],
187
+ animals: {
188
+ cat: 'Ginger',
189
+ dog: 'Max',
190
+ },
191
+ hiddenPets: [{
192
+ name: 'secretPet',
193
+ }],
194
+ hiddenList: [12, 23],
195
+ deep: {
196
+ deep2: {
197
+ deep3: {
198
+ deep4: 'hideme',
199
+ },
237
200
  },
238
- hiddenList: [12, 23],
239
- deep: {
240
- deep2: {
241
- deep3: {
242
- deep4: 'hideme'
243
- }
244
- }
245
- }
246
- })
247
- db.close()
201
+ },
202
+ }
203
+
204
+ // Default insert validation
205
+ let user1 = await user.validate(doc1)
206
+ expect(user1).toEqual({
207
+ list: [44, 54],
208
+ pet: 'Freddy',
209
+ pets: [{ name: 'Pluto' }, { name: 'Milo' }],
210
+ animals: {
211
+ dog: 'Max',
212
+ },
213
+ deep: {
214
+ deep2: {},
215
+ },
248
216
  })
249
217
 
250
- }
218
+ // Custom blacklist (remove and add to the current schema blacklist)
219
+ let user2 = await user.validate(doc1, {
220
+ blacklist: [
221
+ '-dog',
222
+ '-animals.dog', // wrong property
223
+ 'pets.name',
224
+ '-hiddenList',
225
+ '-deep', // blacklist a parent
226
+ ],
227
+ })
228
+ expect(user2).toEqual({
229
+ list: [44, 54],
230
+ dog: 'Bruce',
231
+ pet: 'Freddy',
232
+ pets: [ {}, {} ],
233
+ animals: {
234
+ dog: 'Max',
235
+ },
236
+ hiddenList: [12, 23],
237
+ deep: {
238
+ deep2: {
239
+ deep3: {
240
+ deep4: 'hideme',
241
+ },
242
+ },
243
+ },
244
+ })
245
+ })
@@ -1,90 +0,0 @@
1
- let MongoClient = require('mongodb').MongoClient
2
- let STATE = {
3
- CLOSED: 'closed',
4
- OPENING: 'opening',
5
- OPEN: 'open'
6
- }
7
-
8
- module.exports.open = function(uri, opts, fn) {
9
- /*
10
- * Monkey patch to remove db event listener warnings
11
- * @todo remove when monk is removed
12
- * @see https://www.mongodb.com/community/forums/t/node-44612-deprecationwarning-listening-to-events-on-
13
- the-db-class-has-been-deprecated-and-will-be-removed-in-the-next-major-version/15849/4
14
- */
15
- MongoClient.connect(uri, opts, function (err, client) {
16
- // this = Manager
17
- if (err) {
18
- this._state = STATE.CLOSED
19
- this.emit('error-opening', err)
20
- } else {
21
- this._state = STATE.OPEN
22
-
23
- this._client = client
24
- this._db = client.db()
25
-
26
- // set up events
27
- var self = this
28
- ;['authenticated', 'close', 'error', 'parseError', 'timeout'].forEach(function (eventName) {
29
- self._client.on(eventName, function (e) {
30
- self.emit(eventName, e)
31
- })
32
- })
33
-
34
- this.emit('open', this._db)
35
- }
36
- if (fn) {
37
- fn(err, this)
38
- }
39
- }.bind(this))
40
- }
41
-
42
- module.exports.then = function (fn) {
43
- /*
44
- * Unfortuantly db.then doesn't run after the promise has been fulfilled, this fixes this issue.
45
- * @see https://github.com/Automattic/monk/blob/master/lib/manager.js#L194
46
- */
47
- return new Promise(function (resolve, reject) {
48
- if (this._state == STATE.OPEN) {
49
- resolve()
50
- } else if (this._state == STATE.CLOSED) {
51
- reject()
52
- } else {
53
- this.once('open', resolve)
54
- this.once('error-opening', reject)
55
- }
56
- }.bind(this)).then(fn.bind(null, this))
57
- }
58
-
59
- module.exports.findOneAndUpdate = function(query, update, opts, fn) {
60
- /*
61
- * Monkey patch to use returnDocument
62
- * @todo remove when monk is removed
63
- * @see https://github.com/Automattic/monk/blob/master/lib/collection.js#L265
64
- */
65
- // this = model
66
- if (typeof opts === 'function') {
67
- fn = opts
68
- opts = {}
69
- }
70
- return this._dispatch(function findOneAndUpdate(args) {
71
- var method = 'findOneAndUpdate'
72
- if (typeof (args.options || {}).returnDocument === 'undefined') {
73
- args.options.returnDocument = 'after'
74
- }
75
- if (args.options.replaceOne | args.options.replace) {
76
- method = 'findOneAndReplace'
77
- }
78
- return args.col[method](args.query, args.update, args.options)
79
- .then(function (doc) {
80
- if (doc && typeof doc.value !== 'undefined') {
81
- return doc.value
82
- }
83
- if (doc.ok && doc.lastErrorObject && doc.lastErrorObject.n === 0) {
84
- return null
85
- }
86
- return doc
87
- })
88
- })({options: opts, query: query, update: update, callback: fn}, 'findOneAndUpdate')
89
- }
90
-
package/test/monk.js DELETED
@@ -1,40 +0,0 @@
1
- module.exports = function(monastery, opendb) {
2
-
3
- test('Monk conflicts', async () => {
4
- // Setup
5
- let db = (await opendb(false)).db
6
- let monkdb = require('monk')(':badconnection', () => {})
7
- db.model('user', { fields: {} })
8
- let modelNamedConnected = db.model('connected', { fields: {} })
9
-
10
- // Any of our monastery properties already exist on the manager?
11
- for (let name of ['connected', 'debug', 'log', 'model', 'models']) {
12
- expect(monkdb).not.toHaveProperty(name)
13
- }
14
-
15
- // Model without a name conflict is added to manager[model]
16
- expect(db.user).toEqual(expect.any(Object))
17
-
18
- // Model with a name conflict is only added to manager.model[name]
19
- expect(db.connected).toEqual(db.connected)
20
- expect(db.model.connected).toEqual(modelNamedConnected)
21
-
22
- // Close test since monk(uri) awaits upoin a connection (Update, done not needed in latest jest)
23
- // done()
24
- })
25
-
26
- test('Monastery connect with promise', (done) => {
27
- let db = monastery('localhost/monastery', { serverSelectionTimeoutMS: 2000 })
28
-
29
- db.then(db => {
30
- expect(db).toEqual(expect.any(Object))
31
- db.close()
32
- done()
33
-
34
- }).catch(err => {
35
- console.error(err)
36
- done()
37
- })
38
- })
39
-
40
- }
package/test/test.js DELETED
@@ -1,38 +0,0 @@
1
- /**
2
- * Todo:
3
- * - Test custom model rules
4
- * - Test custom messages
5
- * - Test for index and unique schema fields
6
- * - Test blacklisting
7
- * - Test deep/array population
8
- * - Test default limit
9
- * Notes:
10
- * - expect().toEqual: strict deep match
11
- * - expect().toMatchObject: received object can have random properties
12
- * - expect.objectContaining:
13
- */
14
-
15
- global.oid = require('mongodb').ObjectID
16
-
17
- let monastery = require('../lib/index.js')
18
- let opendb = async function(uri, opts) {
19
- let db = monastery(
20
- uri === false? false : (uri || 'localhost/monastery'),
21
- opts || { timestamps: false, serverSelectionTimeoutMS: 2000 }
22
- )
23
- // Wait until mongo is open
24
- if (db.then) await db.then(() => {})
25
- // Returning an object instead of promise
26
- return { db: db }
27
- }
28
-
29
- /* Run tests sequentially */
30
- require('./util.js')(monastery, opendb)
31
- require('./monk.js')(monastery, opendb)
32
- require('./model.js')(monastery, opendb)
33
- require('./crud.js')(monastery, opendb)
34
- require('./blacklisting.js')(monastery, opendb)
35
- require('./populate.js')(monastery, opendb)
36
- require('./validate.js')(monastery, opendb)
37
- require('./plugin-images.js')(monastery, opendb)
38
- require('./virtuals.js')(monastery, opendb)