@bedrockio/model 0.17.0 → 0.18.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
@@ -1,3 +1,7 @@
1
+ ## 0.18.0
2
+
3
+ - Added `reload`.
4
+
1
5
  ## 0.17.0
2
6
 
3
7
  - Added `time` validation.
package/README.md CHANGED
@@ -23,6 +23,7 @@ Bedrock utilities for model creation.
23
23
  - [Access Control](#access-control)
24
24
  - [Assign](#assign)
25
25
  - [Upsert](#upsert)
26
+ - [Reload](#reload)
26
27
  - [Clone](#clone)
27
28
  - [Slugs](#slugs)
28
29
  - [Testing](#testing)
@@ -1752,6 +1753,32 @@ if (!shop) {
1752
1753
  }
1753
1754
  ```
1754
1755
 
1756
+ ### Reload
1757
+
1758
+ Adds a single `reload` method that reloads a document in place. This is useful
1759
+ for testing purposes, etc:
1760
+
1761
+ ```js
1762
+ const shop = await Shop.create({
1763
+ name: 'My Shop',
1764
+ });
1765
+
1766
+ await Shop.updateOne(
1767
+ {
1768
+ _id: shop._id,
1769
+ },
1770
+ {
1771
+ $set: {
1772
+ name: 'My New Shop',
1773
+ },
1774
+ },
1775
+ );
1776
+
1777
+ await shop.reload();
1778
+
1779
+ shop.name; // Now "My New Shop"
1780
+ ```
1781
+
1755
1782
  ### Clone
1756
1783
 
1757
1784
  Adds a single `clone` method on documents. This is an async method mostly for
@@ -360,7 +360,7 @@ function resolvePaths(schema, str) {
360
360
  source = source.replaceAll('\\*', '[^.]+');
361
361
  source = `^${source}$`;
362
362
  const reg = RegExp(source);
363
- paths = getSchemaPaths(schema).filter(path => {
363
+ paths = (0, _utils.getSchemaPaths)(schema).filter(path => {
364
364
  return reg.test(path);
365
365
  });
366
366
  } else {
@@ -369,17 +369,4 @@ function resolvePaths(schema, str) {
369
369
  return paths.map(path => {
370
370
  return [path, schema.pathType(path)];
371
371
  });
372
- }
373
- function getSchemaPaths(schema) {
374
- return Object.entries(schema.paths || {}).flatMap(([key, schema]) => {
375
- if (key.startsWith('_')) {
376
- return [];
377
- } else if (schema.schema) {
378
- return getSchemaPaths(schema.schema).map(path => {
379
- return [key, path].join('.');
380
- });
381
- } else {
382
- return [key];
383
- }
384
- });
385
372
  }
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.applyReload = applyReload;
7
+ var _mongoose = _interopRequireDefault(require("mongoose"));
8
+ var _utils = require("./utils");
9
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
10
+ function applyReload(schema) {
11
+ schema.method('reload', async function reload() {
12
+ const paths = getPopulatedPaths(this);
13
+ const doc = await this.constructor.findById(this.id).include(paths);
14
+ if (!doc) {
15
+ throw new Error('Document deleted');
16
+ }
17
+ this.overwrite(doc);
18
+ });
19
+ }
20
+ function getPopulatedPaths(doc) {
21
+ return getReferencePaths(doc.constructor.schema).filter(path => {
22
+ const value = doc.get(path);
23
+ return value && !_mongoose.default.isObjectIdOrHexString(value);
24
+ });
25
+ }
26
+ function getReferencePaths(schema) {
27
+ return (0, _utils.getSchemaPaths)(schema).filter(path => {
28
+ return (0, _utils.isReferenceField)(schema, path);
29
+ });
30
+ }
@@ -14,6 +14,7 @@ var _deleteHooks = require("./delete-hooks");
14
14
  var _disallowed = require("./disallowed");
15
15
  var _hydrate = require("./hydrate");
16
16
  var _include = require("./include");
17
+ var _reload = require("./reload");
17
18
  var _search = require("./search");
18
19
  var _serialization = require("./serialization");
19
20
  var _slug = require("./slug");
@@ -58,6 +59,7 @@ function createSchema(definition, options = {}) {
58
59
  (0, _search.applySearch)(schema, definition);
59
60
  (0, _cache.applyCache)(schema, definition);
60
61
  (0, _clone.applyClone)(schema);
62
+ (0, _reload.applyReload)(schema);
61
63
  (0, _disallowed.applyDisallowed)(schema);
62
64
  (0, _include.applyInclude)(schema);
63
65
  (0, _hydrate.applyHydrate)(schema);
package/dist/cjs/utils.js CHANGED
@@ -5,6 +5,7 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.getField = getField;
7
7
  exports.getInnerField = getInnerField;
8
+ exports.getSchemaPaths = getSchemaPaths;
8
9
  exports.isArrayField = isArrayField;
9
10
  exports.isDateField = isDateField;
10
11
  exports.isEqual = isEqual;
@@ -149,4 +150,17 @@ function resolveInnerField(field) {
149
150
  field = field.obj;
150
151
  }
151
152
  return field;
153
+ }
154
+ function getSchemaPaths(schema) {
155
+ return Object.entries(schema.paths || {}).flatMap(([key, schema]) => {
156
+ if (key.startsWith('_')) {
157
+ return [];
158
+ } else if (schema.schema) {
159
+ return getSchemaPaths(schema.schema).map(path => {
160
+ return [key, path].join('.');
161
+ });
162
+ } else {
163
+ return [key];
164
+ }
165
+ });
152
166
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bedrockio/model",
3
- "version": "0.17.0",
3
+ "version": "0.18.0",
4
4
  "description": "Bedrock utilities for model creation.",
5
5
  "type": "module",
6
6
  "scripts": {
@@ -45,7 +45,7 @@
45
45
  "jest": "^30.2.0",
46
46
  "jest-environment-node": "^30.2.0",
47
47
  "mongodb": "^6.20.0",
48
- "mongoose": "^8.18.2",
48
+ "mongoose": "^8.19.2",
49
49
  "prettier": "^3.6.2",
50
50
  "typescript": "^5.9.2"
51
51
  },
package/src/include.js CHANGED
@@ -2,6 +2,7 @@ import { escapeRegExp } from 'lodash';
2
2
  import mongoose from 'mongoose';
3
3
 
4
4
  import { POPULATE_MAX_DEPTH } from './const';
5
+ import { getSchemaPaths } from './utils';
5
6
  import { getInnerField, isSchemaTypedef } from './utils';
6
7
 
7
8
  // @ts-ignore
@@ -363,17 +364,3 @@ function resolvePaths(schema, str) {
363
364
  return [path, schema.pathType(path)];
364
365
  });
365
366
  }
366
-
367
- function getSchemaPaths(schema) {
368
- return Object.entries(schema.paths || {}).flatMap(([key, schema]) => {
369
- if (key.startsWith('_')) {
370
- return [];
371
- } else if (schema.schema) {
372
- return getSchemaPaths(schema.schema).map((path) => {
373
- return [key, path].join('.');
374
- });
375
- } else {
376
- return [key];
377
- }
378
- });
379
- }
package/src/reload.js ADDED
@@ -0,0 +1,30 @@
1
+ import mongoose from 'mongoose';
2
+
3
+ import { getSchemaPaths } from './utils';
4
+ import { isReferenceField } from './utils';
5
+
6
+ export function applyReload(schema) {
7
+ schema.method('reload', async function reload() {
8
+ const paths = getPopulatedPaths(this);
9
+ const doc = await this.constructor.findById(this.id).include(paths);
10
+
11
+ if (!doc) {
12
+ throw new Error('Document deleted');
13
+ }
14
+
15
+ this.overwrite(doc);
16
+ });
17
+ }
18
+
19
+ function getPopulatedPaths(doc) {
20
+ return getReferencePaths(doc.constructor.schema).filter((path) => {
21
+ const value = doc.get(path);
22
+ return value && !mongoose.isObjectIdOrHexString(value);
23
+ });
24
+ }
25
+
26
+ function getReferencePaths(schema) {
27
+ return getSchemaPaths(schema).filter((path) => {
28
+ return isReferenceField(schema, path);
29
+ });
30
+ }
package/src/schema.js CHANGED
@@ -8,6 +8,7 @@ import { applyDeleteHooks } from './delete-hooks';
8
8
  import { applyDisallowed } from './disallowed';
9
9
  import { applyHydrate } from './hydrate';
10
10
  import { applyInclude } from './include';
11
+ import { applyReload } from './reload';
11
12
  import { applySearch } from './search';
12
13
  import { serializeOptions } from './serialization';
13
14
  import { applySlug } from './slug';
@@ -61,6 +62,7 @@ export function createSchema(definition, options = {}) {
61
62
  applySearch(schema, definition);
62
63
  applyCache(schema, definition);
63
64
  applyClone(schema);
65
+ applyReload(schema);
64
66
  applyDisallowed(schema);
65
67
  applyInclude(schema);
66
68
  applyHydrate(schema);
package/src/utils.js CHANGED
@@ -139,3 +139,17 @@ function resolveInnerField(field) {
139
139
  }
140
140
  return field;
141
141
  }
142
+
143
+ export function getSchemaPaths(schema) {
144
+ return Object.entries(schema.paths || {}).flatMap(([key, schema]) => {
145
+ if (key.startsWith('_')) {
146
+ return [];
147
+ } else if (schema.schema) {
148
+ return getSchemaPaths(schema.schema).map((path) => {
149
+ return [key, path].join('.');
150
+ });
151
+ } else {
152
+ return [key];
153
+ }
154
+ });
155
+ }
@@ -1 +1 @@
1
- {"version":3,"file":"include.d.ts","sourceRoot":"","sources":["../src/include.js"],"names":[],"mappings":"AAiBA,gDAuEC;AAMD,uDA4BC;AAGD,yDAIC;AAaD,yEAUC"}
1
+ {"version":3,"file":"include.d.ts","sourceRoot":"","sources":["../src/include.js"],"names":[],"mappings":"AAkBA,gDAuEC;AAMD,uDA4BC;AAGD,yDAIC;AAaD,yEAUC"}
@@ -0,0 +1,2 @@
1
+ export function applyReload(schema: any): void;
2
+ //# sourceMappingURL=reload.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reload.d.ts","sourceRoot":"","sources":["../src/reload.js"],"names":[],"mappings":"AAKA,+CAWC"}
package/types/schema.d.ts CHANGED
@@ -19,6 +19,7 @@ export function createSchema(definition: object, options?: mongoose.SchemaOption
19
19
  };
20
20
  collation?: mongoose.mongo.CollationOptions;
21
21
  collectionOptions?: mongoose.mongo.CreateCollectionOptions;
22
+ lean?: boolean | mongoose.LeanOptions;
22
23
  timeseries?: mongoose.mongo.TimeSeriesCollectionOptions;
23
24
  expireAfterSeconds?: number;
24
25
  expires?: number | string;
@@ -1 +1 @@
1
- {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/schema.js"],"names":[],"mappings":"AAuBA;;;;;;;GAOG;AACH,yCAJW,MAAM,YACN,QAAQ,CAAC,aAAa;;;;;;;YAgDJ,CAAC;WAC1B,CAAF;mBAAqB,CAAC;;;;;;;;;;;;;;;;;;;;;;;SA2HlB,CAAC;gBACL,CAAC;SAAW,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;aAlId;AAED,iEAsBC;qBA9FoB,UAAU"}
1
+ {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/schema.js"],"names":[],"mappings":"AAwBA;;;;;;;GAOG;AACH,yCAJW,MAAM,YACN,QAAQ,CAAC,aAAa;;;;;;;YA+CX,CAAC;WAAa,CAAC;mBAEnC,CAAC;;;;;;;;;;;;;;;;;;;;;;;;SA4HmB,CAAC;gBACL,CAAC;SAAW,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;aAlI9B;AAED,iEAsBC;qBAhGoB,UAAU"}
package/types/utils.d.ts CHANGED
@@ -13,5 +13,6 @@ export function resolveRefPath(schema: any, path: any): {
13
13
  foreign: any;
14
14
  };
15
15
  export function getInnerField(obj: any, path: any): any;
16
+ export function getSchemaPaths(schema: any): any;
16
17
  import mongoose from 'mongoose';
17
18
  //# sourceMappingURL=utils.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.js"],"names":[],"mappings":"AAQA,iDAcC;AAED,gHAEC;AAED,+DAEC;AAED,0DAEC;AAED,4DAEC;AAED,4DAEC;AAED,2DAGC;AAOD,mDAGC;AAuBD,mDAgBC;AAKD;;;;EAoBC;AAKD,wDAEC;qBAhIoB,UAAU"}
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.js"],"names":[],"mappings":"AAQA,iDAcC;AAED,gHAEC;AAED,+DAEC;AAED,0DAEC;AAED,4DAEC;AAED,4DAEC;AAED,2DAGC;AAOD,mDAGC;AAuBD,mDAgBC;AAKD;;;;EAoBC;AAKD,wDAEC;AAcD,iDAYC;qBA1JoB,UAAU"}