monastery 1.41.0 → 1.41.2

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,10 @@
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.41.2](https://github.com/boycce/monastery/compare/1.41.1...1.41.2) (2023-10-04)
6
+
7
+ ### [1.41.1](https://github.com/boycce/monastery/compare/1.41.0...1.41.1) (2023-04-17)
8
+
5
9
  ## [1.41.0](https://github.com/boycce/monastery/compare/1.40.5...1.41.0) (2023-04-16)
6
10
 
7
11
  ### [1.40.5](https://github.com/boycce/monastery/compare/1.40.4...1.40.5) (2023-02-22)
@@ -43,15 +43,20 @@ Model definition object.
43
43
  pets: [{ // array of embedded documents
44
44
  name: { type: 'string' },
45
45
  type: { type: 'string' },
46
- }]
47
- // You can add a rule on the embedded document or array using the following structure
48
- pets: {
49
- type: [{
46
+ }],
47
+ // You can add a rule on an embedded-document/array using the following structure
48
+ address: {
49
+ name: { type: 'string' },
50
+ type: { type: 'string' },
51
+ schema: { minLength: 1 },
52
+ },
53
+ pets: db.arrayWithSchema(
54
+ [{
50
55
  name: { type: 'string' },
51
56
  type: { type: 'string' },
52
57
  }],
53
- minLength: 1,
54
- }
58
+ { minLength: 1 },
59
+ )
55
60
  }
56
61
  }
57
62
  ```
@@ -30,15 +30,18 @@ A monk manager instance with additional Monastery methods, i.e. `model` `models`
30
30
  ### Example
31
31
 
32
32
  ```js
33
- const db = require('monastery')('localhost/mydb', options)
33
+ import monastery from 'monastery'
34
+ const db = monastery('localhost/mydb', options)
34
35
  ```
35
36
 
36
37
  ```js
37
- const db = require('monastery')('localhost/mydb,192.168.1.1') // replica set
38
+ import monastery from 'monastery'
39
+ const db = monastery('localhost/mydb,192.168.1.1') // replica set
38
40
  ```
39
41
 
40
42
  ```js
41
- require('monastery')('localhost/mydb,192.168.1.1').then((db) => {
43
+ import monastery from 'monastery'
44
+ monastery('localhost/mydb,192.168.1.1').then((db) => {
42
45
  // db is the connected instance of the Manager
43
46
  }).catch((err) => {
44
47
  // error connecting to the database
@@ -9,7 +9,7 @@ Setup model definitions from a folder location
9
9
 
10
10
  ### Arguments
11
11
 
12
- `path` *(string)*: path to model definitions, the filenames are used as the corresponding model name.
12
+ `path` *(string)*: path to model definitions, the filenames are used as the corresponding model name. Make sure the model definition is exported as the default
13
13
 
14
14
  ### Returns
15
15
 
@@ -23,7 +23,7 @@ db.model.{model-name}
23
23
 
24
24
  ```js
25
25
  // ./models/user.js
26
- export default {
26
+ export default { // Make sure the model definition is exported as the default
27
27
  fields: {
28
28
  name: { type: 'string', required: true },
29
29
  email: { type: 'email', required: true, index: 'unique' }
package/docs/readme.md CHANGED
@@ -24,9 +24,11 @@ $ npm install --save monastery
24
24
  ## Usage
25
25
 
26
26
  ```javascript
27
+ import monastery from 'monastery'
28
+
27
29
  // Initialise a monastery manager
28
- const db = require('monastery')('localhost/mydb')
29
- // const db = require('monastery')('user:pass@localhost:port/mydb')
30
+ const db = monastery('localhost/mydb')
31
+ // const db = monastery('user:pass@localhost:port/mydb')
30
32
 
31
33
  // Define a model
32
34
  db.model('user', {
@@ -101,6 +103,7 @@ Coming soon...
101
103
  - Split away from Monk (unless updated)
102
104
  - Add a warning if an invalid model is referenced in jthe schema
103
105
  - Remove leading forward slashes from custom image paths (AWS adds this as a seperate folder)
106
+ - double check await db.model.remove({ query: idfromparam }) doesnt cause issues for null, undefined or '', but continue to allow {}
104
107
 
105
108
  ## Versions
106
109
 
package/lib/index.js CHANGED
@@ -93,9 +93,17 @@ let models = async function(pathname, waitForIndexes) {
93
93
  }
94
94
  let filenames = fs.readdirSync(pathname)
95
95
  for (let filename of filenames) {
96
- let filepath = path.join(pathname, filename)
97
- let definition = (await import(filepath)).default
96
+ // Ignore folders
97
+ if (!filename.match(/\.[cm]?js$/)) continue
98
98
  let name = filename.replace('.js', '')
99
+ let filepath = path.join(pathname, filename)
100
+ // We can't use require() here since we need to be able to import both CJS and ES6 modules
101
+ let definition = (await import(filepath))?.default
102
+ // When a commonJS project uses babel (e.g. `nodemon -r @babel/register`, similar to `-r esm`), import()
103
+ // will return ES6 model definitions in another nested `default` object
104
+ if (definition?.default) definition = definition.default
105
+ if (!definition) throw new Error(`The model definition for '${name}' must export a default object`)
106
+ // Wait for indexes to be created?
99
107
  if (waitForIndexes) out[name] = await this.model(name, { ...definition, waitForIndexes })
100
108
  else out[name] = this.model(name, definition)
101
109
  }
package/lib/model.js CHANGED
@@ -14,16 +14,18 @@ let Model = module.exports = function(name, opts, manager) {
14
14
  */
15
15
  if (!(this instanceof Model)) {
16
16
  return new Model(name, opts, this)
17
-
18
17
  } else if (!name) {
19
18
  throw 'No model name defined'
20
-
21
19
  } else if (name.match(/^_/)) {
22
20
  throw 'Model names cannot start with an underscore'
21
+ } else if (!opts) {
22
+ throw `No model definition passed for "${name}"`
23
+ } else if (!opts.fields) {
24
+ throw `We couldn't find ${name}.fields in the model definition, the model maybe setup `
25
+ + `or exported incorrectly:\n${JSON.stringify(opts, null, 2)}`
23
26
  }
24
27
 
25
28
  // Add schema options
26
- opts = opts || {}
27
29
  Object.assign(this, {
28
30
  ...(opts.methods || {}),
29
31
  afterFind: opts.afterFind || [],
@@ -131,25 +133,6 @@ Model.prototype._setupFields = function(fields) {
131
133
  * @param {object|array} fields - subsdocument or array
132
134
  */
133
135
  util.forEach(fields, function(field, fieldName) {
134
- // Preprocess type objects
135
- if (util.isSchema(field) && typeof field.type == 'object') {
136
- if (field.schema) {
137
- this.error(`Field "${fieldName}.schema" on model "${this.name}" is ignored when using `
138
- + 'object types, simply define the schema fields alongside field.type.')
139
- delete fields[fieldName]
140
- return
141
- }
142
- if (util.isSchema(field.type)) {
143
- this.error(`"${fieldName}.type" on model "${this.name}" is a schema object, it must be either a `
144
- + 'string, subdocument or array')
145
- delete fields[fieldName]
146
- return
147
- }
148
- let fieldCache = field
149
- field = fields[fieldName] = field.type
150
- field.schema = util.omit(fieldCache, ['type'])
151
- }
152
-
153
136
  // Schema field
154
137
  if (util.isSchema(field)) {
155
138
  // No image schema pre-processing done yet by a plugin
package/lib/util.js CHANGED
@@ -109,6 +109,8 @@ module.exports = {
109
109
 
110
110
  isSubdocument: function(value) {
111
111
  /**
112
+ * "are all fields objects/arrays?"
113
+ *
112
114
  * Is the value a subdocument which contains more fields? Or a just a field definition?
113
115
  * @param {object} value - object to check
114
116
  * E.g. isSubdocument = {
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.0",
5
+ "version": "1.41.2",
6
6
  "license": "MIT",
7
7
  "repository": "github:boycce/monastery",
8
8
  "homepage": "https://boycce.github.io/monastery/",
package/test/model.js CHANGED
@@ -13,7 +13,7 @@ module.exports = function(monastery, opendb) {
13
13
  }})
14
14
 
15
15
  // no fields defined
16
- expect(db.model('user2').fields).toEqual({
16
+ expect(db.model('user2', { fields: {} }).fields).toEqual({
17
17
  createdAt: {
18
18
  default: expect.any(Function),
19
19
  insertOnly: true,
@@ -68,64 +68,12 @@ module.exports = function(monastery, opendb) {
68
68
  ))
69
69
  })
70
70
 
71
- test('model setup with type object', async () => {
72
- // Setup
73
- let db = (await opendb(false)).db
74
- let user = db.model('user', { fields: {
75
- name: {
76
- type: { cat: { type: 'string' }}, // subdocument
77
- minLength: 4,
78
- },
79
- name2: {
80
- type: [{ type: 'string' }], // array
81
- minLength: 4,
82
- },
83
- }})
84
-
85
- // subdocument object type
86
- expect(user.fields.name).toEqual({
87
- cat: { isString: true, type: 'string' },
88
- schema: { isObject: true, type: 'object', minLength: 4 }
89
- })
90
- // array object type
91
- expect(JSON.stringify(user.fields.name2)).toEqual(JSON.stringify(
92
- [{ type: 'string', isString: true }]
93
- ))
94
- expect(user.fields.name2.schema).toEqual({
95
- type: 'array',
96
- isArray: true,
97
- minLength: 4,
98
- })
99
- })
100
-
101
- test('model setup with invalid schema type object', async () => {
102
- // Setup
103
- let db = (await opendb(false, { hideErrors: true })).db // hide debug error
104
- let user = db.model('user', { fields: {
105
- // valid subdocument
106
- name: {
107
- type: { type: 'string' },
108
- },
109
- // schema with invlaid object type
110
- name2: {
111
- type: { type: 'string' },
112
- minLength: 10,
113
- },
114
- }})
115
-
116
- expect(user.fields.name).toEqual({
117
- type: { isString: true, type: 'string' },
118
- schema: { isObject: true, type: 'object' },
119
- })
120
- expect(user.fields.name2).toEqual(undefined)
121
- })
122
-
123
71
  test('model setup with default fields', async () => {
124
72
  // Setup
125
73
  let db = (await opendb(false, { defaultObjects: true })).db
126
74
 
127
75
  // Default fields
128
- expect(db.model('user2').fields).toEqual({
76
+ expect(db.model('user2', { fields: {} }).fields).toEqual({
129
77
  createdAt: {
130
78
  default: expect.any(Function),
131
79
  insertOnly: true,
@@ -167,6 +115,52 @@ module.exports = function(monastery, opendb) {
167
115
  })
168
116
  })
169
117
 
118
+ test('model setup with schema', async () => {
119
+ // Setup
120
+ let db = (await opendb(false)).db
121
+ let objectSchemaTypeRef = { name: { type: 'string', minLength: 5 } }
122
+ let user = db.model('user', {
123
+ fields: {
124
+ pet: { ...objectSchemaTypeRef, schema: { virtual: true }},
125
+ pets: db.arrayWithSchema(
126
+ [objectSchemaTypeRef],
127
+ { virtual: true },
128
+ ),
129
+ }
130
+ })
131
+ // Object with schema
132
+ expect(user.fields.pet).toEqual({
133
+ name: {
134
+ type: 'string',
135
+ isString: true,
136
+ minLength: 5,
137
+ },
138
+ schema: {
139
+ type: 'object',
140
+ isObject: true,
141
+ virtual: true,
142
+ },
143
+ })
144
+ // Array with schema
145
+ expect(user.fields.pets[0]).toEqual({
146
+ name: {
147
+ type: 'string',
148
+ isString: true,
149
+ minLength: 5,
150
+ },
151
+ schema: {
152
+ type: 'object',
153
+ isObject: true,
154
+ },
155
+ })
156
+ expect(user.fields.pets.schema).toEqual({
157
+ type: 'array',
158
+ isArray: true,
159
+ virtual: true,
160
+ })
161
+ })
162
+
163
+
170
164
  test('model reserved rules', async () => {
171
165
  // Setup
172
166
  let db = (await opendb(false, { hideErrors: true })).db // hide debug error
@@ -201,7 +195,7 @@ module.exports = function(monastery, opendb) {
201
195
  }
202
196
 
203
197
  // Unique & text index (after model initialisation, in serial)
204
- let userIndexRawModel = db.model('userIndexRaw', {})
198
+ let userIndexRawModel = db.model('userIndexRaw', {fields: {}})
205
199
  await userIndexRawModel._setupIndexes({
206
200
  email: { type: 'string', index: 'unique' },
207
201
  })
package/test/monk.js CHANGED
@@ -4,8 +4,8 @@ module.exports = function(monastery, opendb) {
4
4
  // Setup
5
5
  let db = (await opendb(false)).db
6
6
  let monkdb = require('monk')(':badconnection', () => {})
7
- db.model('user', {})
8
- let modelNamedConnected = db.model('connected', {})
7
+ db.model('user', { fields: {} })
8
+ let modelNamedConnected = db.model('connected', { fields: {} })
9
9
 
10
10
  // Any of our monastery properties already exist on the manager?
11
11
  for (let name of ['connected', 'debug', 'log', 'model', 'models']) {