monastery 1.41.2 → 1.42.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/changelog.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ## [1.42.0](https://github.com/boycce/monastery/compare/1.41.2...1.42.0) (2023-10-09)
6
+
5
7
  ### [1.41.2](https://github.com/boycce/monastery/compare/1.41.1...1.41.2) (2023-10-04)
6
8
 
7
9
  ### [1.41.1](https://github.com/boycce/monastery/compare/1.41.0...1.41.1) (2023-04-17)
package/lib/model-crud.js CHANGED
@@ -25,9 +25,13 @@ module.exports = {
25
25
  try {
26
26
  opts = await this._queryObject(opts, 'insert')
27
27
 
28
+ // console.log(11, opts.data )
29
+
28
30
  // Validate
29
31
  let data = await this.validate(opts.data || {}, opts) // was { ...opts }
30
32
 
33
+ // console.log(22, data)
34
+
31
35
  // Insert
32
36
  await util.runSeries(this.beforeInsert.map(f => f.bind(opts, data)))
33
37
  let response = await this._insert(data, util.omit(opts, this._queryOptions))
@@ -108,6 +108,8 @@ module.exports = {
108
108
  let data2 = util.isArray(fields)? [] : {}
109
109
  let timestamps = util.isDefined(opts.timestamps)? opts.timestamps : this.manager.timestamps
110
110
 
111
+ // console.log(8888, fields)
112
+
111
113
  util.forEach(util.forceArray(data), function(data, i) {
112
114
  util.forEach(fields, function(field, fieldName) {
113
115
  let verrors = []
@@ -131,8 +133,11 @@ module.exports = {
131
133
  }
132
134
  }
133
135
 
136
+ // console.log(333,path3)
137
+
134
138
  // Ignore blacklisted
135
139
  if (this._pathBlacklisted(path3, opts.projectionValidate) && !schema.defaultOverride) return
140
+ // console.log(444,path3)
136
141
  // Ignore insert only
137
142
  if (opts.update && schema.insertOnly) return
138
143
  // Ignore virtual fields
@@ -141,6 +146,7 @@ module.exports = {
141
146
  if (isTypeRule && util.isFunction(isTypeRule.tryParse)) {
142
147
  value = isTypeRule.tryParse.call(dataRoot, value, fieldName, this)
143
148
  }
149
+ // console.log(555,path3)
144
150
 
145
151
  // Schema field (ignore object/array schemas)
146
152
  if (util.isSchema(field) && fieldName !== 'schema') {
package/lib/model.js CHANGED
@@ -39,14 +39,14 @@ let Model = module.exports = function(name, opts, manager) {
39
39
  error: manager.error,
40
40
  info: manager.info,
41
41
  warn: manager.warn,
42
- insertBL: opts.insertBL || [],
42
+ insertBL: opts.insertBL || ['_id'],
43
43
  fields: { ...(util.deepCopy(opts.fields) || {}) },
44
44
  findBL: opts.findBL || ['password'],
45
45
  manager: manager,
46
46
  messages: opts.messages || {},
47
47
  name: name,
48
48
  rules: { ...(opts.rules || {}) },
49
- updateBL: opts.updateBL || [],
49
+ updateBL: opts.updateBL || ['_id'],
50
50
  })
51
51
 
52
52
  // Run before model hooks
@@ -71,7 +71,7 @@ let Model = module.exports = function(name, opts, manager) {
71
71
  }, this)
72
72
 
73
73
  // Extend default fields with passed in fields and check for invalid fields
74
- this._setupFields(this.fields = Object.assign({}, this._timestampFields, this.fields))
74
+ this._setupFields(this.fields = Object.assign({}, this._defaultFields, this.fields))
75
75
  this.fieldsFlattened = this._getFieldsFlattened(this.fields, '') // test output?
76
76
 
77
77
  // Extend model with monk collection queries
@@ -321,7 +321,11 @@ Model.prototype._setupIndexes = function(fields, opts={}) {
321
321
  }
322
322
  }
323
323
 
324
- Model.prototype._timestampFields = {
324
+ Model.prototype._defaultFields = {
325
+ _id: {
326
+ insertOnly: true,
327
+ type: 'id'
328
+ },
325
329
  createdAt: {
326
330
  default: function(fieldName, model) {
327
331
  return model.manager.useMilliseconds? Date.now() : Math.floor(Date.now() / 1000)
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "monastery",
3
3
  "description": "⛪ A straight forward MongoDB ODM built around Monk",
4
4
  "author": "Ricky Boyce",
5
- "version": "1.41.2",
5
+ "version": "1.42.0",
6
6
  "license": "MIT",
7
7
  "repository": "github:boycce/monastery",
8
8
  "homepage": "https://boycce.github.io/monastery/",
@@ -339,7 +339,7 @@ module.exports = function(monastery, opendb) {
339
339
  db.close()
340
340
  })
341
341
 
342
- test('insert update blacklisting (validate)', async () => {
342
+ test('insert blacklisting (validate)', async () => {
343
343
  // Setup
344
344
  let db = (await opendb(null)).db
345
345
  let user = db.model('user', {
@@ -365,15 +365,21 @@ module.exports = function(monastery, opendb) {
365
365
  }
366
366
  },
367
367
  insertBL: [
368
+ '_id', // default
368
369
  'dog',
369
370
  'animals.cat',
370
371
  'pets.age',
371
372
  'hiddenPets',
372
373
  'hiddenList',
373
374
  'deep.deep2.deep3'
374
- ]
375
+ ],
376
+ updateBL: [
377
+ '_id' // default
378
+ ],
375
379
  })
380
+ let doc1Id = db.id()
376
381
  let doc1 = {
382
+ _id: doc1Id,
377
383
  list: [44, 54],
378
384
  dog: 'Bruce',
379
385
  pet: 'Freddy',
@@ -409,9 +415,10 @@ module.exports = function(monastery, opendb) {
409
415
  }
410
416
  })
411
417
 
412
- // Custom blacklist (remove and add to the current schema blacklist)
418
+ // Custom insert blacklist (remove and add to the current schema blacklist)
413
419
  let user2 = await user.validate(doc1, {
414
420
  blacklist: [
421
+ '-_id',
415
422
  '-dog',
416
423
  '-animals.dog', // wrong property
417
424
  'pets.name',
@@ -421,6 +428,7 @@ module.exports = function(monastery, opendb) {
421
428
  })
422
429
  let customBlacklist
423
430
  expect(user2).toEqual((customBlacklist = {
431
+ _id: doc1Id,
424
432
  list: [44, 54],
425
433
  dog: 'Bruce',
426
434
  pet: 'Freddy',
@@ -440,7 +448,7 @@ module.exports = function(monastery, opendb) {
440
448
 
441
449
  // Blacklist string
442
450
  let user3 = await user.validate(doc1, {
443
- blacklist: '-dog -animals.dog pets.name -hiddenList -deep'
451
+ blacklist: '-_id -dog -animals.dog pets.name -hiddenList -deep'
444
452
  })
445
453
  expect(user3).toEqual(customBlacklist)
446
454
 
@@ -467,6 +475,14 @@ module.exports = function(monastery, opendb) {
467
475
  }
468
476
  }
469
477
  })
478
+
479
+ // double check that _id.insertOnly is working
480
+ let user6 = await user.validate(doc1, { update: true, blacklist: false })
481
+ expect(user6).toEqual({
482
+ ...doc1,
483
+ _id: undefined,
484
+ })
485
+
470
486
  db.close()
471
487
  })
472
488
 
package/test/model.js CHANGED
@@ -14,6 +14,11 @@ module.exports = function(monastery, opendb) {
14
14
 
15
15
  // no fields defined
16
16
  expect(db.model('user2', { fields: {} }).fields).toEqual({
17
+ _id: {
18
+ insertOnly: true,
19
+ isId: true,
20
+ type: 'id',
21
+ },
17
22
  createdAt: {
18
23
  default: expect.any(Function),
19
24
  insertOnly: true,
@@ -74,6 +79,11 @@ module.exports = function(monastery, opendb) {
74
79
 
75
80
  // Default fields
76
81
  expect(db.model('user2', { fields: {} }).fields).toEqual({
82
+ _id: {
83
+ insertOnly: true,
84
+ isId: true,
85
+ type: 'id',
86
+ },
77
87
  createdAt: {
78
88
  default: expect.any(Function),
79
89
  insertOnly: true,
package/test/validate.js CHANGED
@@ -765,6 +765,11 @@ module.exports = function(monastery, opendb) {
765
765
  // Subdocument -> array -> subdocument data (empty)
766
766
  await expect(user.validate({ animals: { dogs: [{}] }}))
767
767
  .resolves.toEqual({ animals: { dogs: [{}] }})
768
+
769
+ // _id is blacklisted by default
770
+ let id = db.id()
771
+ await expect(user.validate({ _id: id })).resolves.toEqual({})
772
+ await expect(user.validate({ _id: id }, { update: true })).resolves.toEqual({})
768
773
  })
769
774
 
770
775
  test('schema options', async () => {