@kikiutils/mongoose 2.1.0 → 2.1.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/options.cjs CHANGED
@@ -9,6 +9,6 @@ const _internals = require('./_internals.cjs');
9
9
  * every time {@link buildMongooseModel} is used. Inside this function, you can modify
10
10
  * the schema, allowing you to apply common settings to all global schemas.
11
11
  */
12
- const setCustomMongooseOptions = (key, value) => (_internals.customMongooseOptions[key] = value);
12
+ const setCustomMongooseOptions = (key, value) => _internals.customMongooseOptions[key] = value;
13
13
 
14
14
  exports.setCustomMongooseOptions = setCustomMongooseOptions;
package/options.mjs CHANGED
@@ -7,6 +7,6 @@ import { customMongooseOptions } from './_internals.mjs';
7
7
  * every time {@link buildMongooseModel} is used. Inside this function, you can modify
8
8
  * the schema, allowing you to apply common settings to all global schemas.
9
9
  */
10
- const setCustomMongooseOptions = (key, value) => (customMongooseOptions[key] = value);
10
+ const setCustomMongooseOptions = (key, value) => customMongooseOptions[key] = value;
11
11
 
12
12
  export { setCustomMongooseOptions };
package/package.json CHANGED
@@ -1,39 +1,13 @@
1
1
  {
2
2
  "name": "@kikiutils/mongoose",
3
- "version": "2.1.0",
3
+ "version": "2.1.2",
4
4
  "description": "A Mongoose plugin for enhanced JSON normalization and common schema creation, with built-in support for pagination and automatic Decimal128 conversion.",
5
- "license": "MIT",
6
5
  "author": "kiki-kanri",
6
+ "license": "MIT",
7
7
  "repository": {
8
8
  "type": "git",
9
9
  "url": "git+https://github.com/kiki-kanri/kikiutils-node-mongoose.git"
10
10
  },
11
- "scripts": {
12
- "build": "ts-project-builder ./src/constants.ts ./src/options.ts ./src/schema-builders/index.ts ./src/utils.ts --clean --preserve-modules",
13
- "bumplog": "changelogen --bump",
14
- "release": "pnpm run build && pnpm run test && changelogen --push --release && sh ./build-and-publish.sh",
15
- "test": "jest --coverage"
16
- },
17
- "dependencies": {
18
- "decimal.js": "^10.4.3",
19
- "lodash": "^4.17.21",
20
- "mongoose": "^8.8.0",
21
- "mongoose-aggregate-paginate-v2": "^1.1.2",
22
- "mongoose-paginate-v2": "^1.8.5",
23
- "type-fest": "^4.26.1"
24
- },
25
- "devDependencies": {
26
- "@kikiutils/changelogen": "^0.7.0",
27
- "@kikiutils/prettierrc": "^1.3.2",
28
- "@kikiutils/tsconfigs": "^3.0.2",
29
- "@types/jest": "^29.5.14",
30
- "@types/lodash": "^4.17.13",
31
- "@types/node": "^22.8.6",
32
- "jest": "^29.7.0",
33
- "ts-jest": "^29.2.5",
34
- "ts-project-builder": "^3.3.1",
35
- "tslib": "^2.8.1"
36
- },
37
11
  "keywords": [
38
12
  "Decimal128",
39
13
  "json",
@@ -44,24 +18,21 @@
44
18
  "plugin",
45
19
  "schema"
46
20
  ],
47
- "engines": {
48
- "node": ">=18.12.1"
49
- },
50
21
  "exports": {
51
22
  "./*": {
23
+ "types": "./*.d.ts",
52
24
  "import": "./*.mjs",
53
- "require": "./*.cjs",
54
- "types": "./*.d.ts"
25
+ "require": "./*.cjs"
55
26
  },
56
27
  "./_internals": {
28
+ "types": null,
57
29
  "import": null,
58
- "require": null,
59
- "types": null
30
+ "require": null
60
31
  },
61
32
  "./schema-builders": {
33
+ "types": "./schema-builders/index.d.ts",
62
34
  "import": "./schema-builders/index.mjs",
63
- "require": "./schema-builders/index.cjs",
64
- "types": "./schema-builders/index.d.ts"
35
+ "require": "./schema-builders/index.cjs"
65
36
  },
66
37
  "./types": {
67
38
  "types": "./types/index.d.ts"
@@ -70,5 +41,35 @@
70
41
  "types": "./types/*.d.ts"
71
42
  }
72
43
  },
73
- "prettier": "@kikiutils/prettierrc"
44
+ "engines": {
45
+ "node": ">=18.12.1"
46
+ },
47
+ "scripts": {
48
+ "build": "ts-project-builder ./src/constants.ts ./src/options.ts ./src/schema-builders/index.ts ./src/utils.ts --clean --preserve-modules",
49
+ "bumplog": "changelogen --bump",
50
+ "lint": "eslint",
51
+ "lint:fix": "eslint --fix",
52
+ "release": "pnpm run lint && pnpm run build && pnpm run test && changelogen --push --release && sh ./build-and-publish.sh",
53
+ "test": "jest --coverage"
54
+ },
55
+ "dependencies": {
56
+ "decimal.js": "^10.4.3",
57
+ "lodash": "^4.17.21",
58
+ "mongoose": "^8.8.4",
59
+ "mongoose-aggregate-paginate-v2": "^1.1.2",
60
+ "mongoose-paginate-v2": "^1.8.5",
61
+ "type-fest": "^4.30.1"
62
+ },
63
+ "devDependencies": {
64
+ "@kikiutils/changelogen": "^0.7.0",
65
+ "@kikiutils/eslint-config": "^0.7.1",
66
+ "@kikiutils/tsconfigs": "^3.0.2",
67
+ "@types/jest": "^29.5.14",
68
+ "@types/lodash": "^4.17.13",
69
+ "@types/node": "^22.10.2",
70
+ "jest": "^29.7.0",
71
+ "ts-jest": "^29.2.5",
72
+ "ts-project-builder": "^3.3.3",
73
+ "tslib": "^2.8.1"
74
+ }
74
75
  }
@@ -8,7 +8,7 @@ const _interopDefaultCompat = e => e && typeof e === 'object' && 'default' in e
8
8
 
9
9
  const lodash__default = /*#__PURE__*/_interopDefaultCompat(lodash);
10
10
 
11
- const { get: lodashGet, set: lodashSet, unset: lodashUnset } = lodash__default;
11
+ const { get: lodashGet, set: lodashSet, unset: lodashUnset, } = lodash__default;
12
12
  /**
13
13
  * Mongoose plugin to normalize the JSON output of documents.
14
14
  *
@@ -23,7 +23,7 @@ const { get: lodashGet, set: lodashSet, unset: lodashUnset } = lodash__default;
23
23
  *
24
24
  * @param schema - The Mongoose schema to apply the plugin to.
25
25
  */
26
- const mongooseNormalizePlugin = (schema) => {
26
+ function mongooseNormalizePlugin(schema) {
27
27
  const toJSON = schema.get('toJSON');
28
28
  const toJSONTransform = toJSON?.transform;
29
29
  schema.set('toJSON', {
@@ -45,9 +45,9 @@ const mongooseNormalizePlugin = (schema) => {
45
45
  if (toJSONTransform && typeof toJSONTransform !== 'boolean')
46
46
  return toJSONTransform(doc, copiedRet, options);
47
47
  return copiedRet;
48
- }
48
+ },
49
49
  });
50
- };
50
+ }
51
51
 
52
52
  exports.default = mongooseNormalizePlugin;
53
53
  exports.mongooseNormalizePlugin = mongooseNormalizePlugin;
@@ -13,5 +13,5 @@ import type { Schema } from 'mongoose';
13
13
  *
14
14
  * @param schema - The Mongoose schema to apply the plugin to.
15
15
  */
16
- export declare const mongooseNormalizePlugin: <S extends Schema>(schema: S) => void;
16
+ export declare function mongooseNormalizePlugin<S extends Schema>(schema: S): void;
17
17
  export default mongooseNormalizePlugin;
@@ -1,6 +1,6 @@
1
1
  import lodash from 'lodash';
2
2
 
3
- const { get: lodashGet, set: lodashSet, unset: lodashUnset } = lodash;
3
+ const { get: lodashGet, set: lodashSet, unset: lodashUnset, } = lodash;
4
4
  /**
5
5
  * Mongoose plugin to normalize the JSON output of documents.
6
6
  *
@@ -15,7 +15,7 @@ const { get: lodashGet, set: lodashSet, unset: lodashUnset } = lodash;
15
15
  *
16
16
  * @param schema - The Mongoose schema to apply the plugin to.
17
17
  */
18
- const mongooseNormalizePlugin = (schema) => {
18
+ function mongooseNormalizePlugin(schema) {
19
19
  const toJSON = schema.get('toJSON');
20
20
  const toJSONTransform = toJSON?.transform;
21
21
  schema.set('toJSON', {
@@ -37,8 +37,8 @@ const mongooseNormalizePlugin = (schema) => {
37
37
  if (toJSONTransform && typeof toJSONTransform !== 'boolean')
38
38
  return toJSONTransform(doc, copiedRet, options);
39
39
  return copiedRet;
40
- }
40
+ },
41
41
  });
42
- };
42
+ }
43
43
 
44
44
  export { mongooseNormalizePlugin as default, mongooseNormalizePlugin };
@@ -1,6 +1,15 @@
1
1
  'use strict';
2
2
 
3
- const createBaseSchemaBuilderFactory = (type) => {
3
+ const isFunctionKeys = new Set([
4
+ 'default',
5
+ 'enum',
6
+ 'index',
7
+ 'max',
8
+ 'maxlength',
9
+ 'min',
10
+ 'minlength',
11
+ ]);
12
+ function createBaseSchemaBuilderFactory(type) {
4
13
  return (schema = {}) => {
5
14
  schema.type = type;
6
15
  return new Proxy(Object.freeze({}), {
@@ -9,26 +18,25 @@ const createBaseSchemaBuilderFactory = (type) => {
9
18
  throw new Error('Cannot use symbol as a schema attribute');
10
19
  if (key in schema)
11
20
  throw new Error(`Duplicate schema attribute: ${key}`);
12
- if (isFunctionKeys.has(key))
13
- return (value) => ((schema[key] = value), receiver);
21
+ if (isFunctionKeys.has(key)) {
22
+ return (value) => {
23
+ schema[key] = value;
24
+ return receiver;
25
+ };
26
+ }
14
27
  if (key === 'nonRequired')
15
28
  return { ...schema };
16
- if (key === 'required')
17
- return { ...schema, required: true };
29
+ if (key === 'required') {
30
+ return {
31
+ ...schema,
32
+ required: true,
33
+ };
34
+ }
18
35
  schema[key] = true;
19
36
  return receiver;
20
- }
37
+ },
21
38
  });
22
39
  };
23
- };
24
- const isFunctionKeys = new Set([
25
- 'default',
26
- 'enum',
27
- 'index',
28
- 'max',
29
- 'maxlength',
30
- 'min',
31
- 'minlength'
32
- ]);
40
+ }
33
41
 
34
42
  exports.createBaseSchemaBuilderFactory = createBaseSchemaBuilderFactory;
@@ -1,2 +1,2 @@
1
1
  import type { Schema } from 'mongoose';
2
- export declare const createBaseSchemaBuilderFactory: <Builder = Readonly<Record<string, any>>>(type: BooleanConstructor | DateConstructor | NumberConstructor | Schema.Types.ObjectId["constructor"] | StringConstructor) => (schema?: Record<string, any>) => Builder;
2
+ export declare function createBaseSchemaBuilderFactory<Builder = Readonly<Record<string, any>>>(type: BooleanConstructor | DateConstructor | NumberConstructor | Schema.Types.ObjectId['constructor'] | StringConstructor): (schema?: Record<string, any>) => Builder;
@@ -1,4 +1,13 @@
1
- const createBaseSchemaBuilderFactory = (type) => {
1
+ const isFunctionKeys = new Set([
2
+ 'default',
3
+ 'enum',
4
+ 'index',
5
+ 'max',
6
+ 'maxlength',
7
+ 'min',
8
+ 'minlength',
9
+ ]);
10
+ function createBaseSchemaBuilderFactory(type) {
2
11
  return (schema = {}) => {
3
12
  schema.type = type;
4
13
  return new Proxy(Object.freeze({}), {
@@ -7,26 +16,25 @@ const createBaseSchemaBuilderFactory = (type) => {
7
16
  throw new Error('Cannot use symbol as a schema attribute');
8
17
  if (key in schema)
9
18
  throw new Error(`Duplicate schema attribute: ${key}`);
10
- if (isFunctionKeys.has(key))
11
- return (value) => ((schema[key] = value), receiver);
19
+ if (isFunctionKeys.has(key)) {
20
+ return (value) => {
21
+ schema[key] = value;
22
+ return receiver;
23
+ };
24
+ }
12
25
  if (key === 'nonRequired')
13
26
  return { ...schema };
14
- if (key === 'required')
15
- return { ...schema, required: true };
27
+ if (key === 'required') {
28
+ return {
29
+ ...schema,
30
+ required: true,
31
+ };
32
+ }
16
33
  schema[key] = true;
17
34
  return receiver;
18
- }
35
+ },
19
36
  });
20
37
  };
21
- };
22
- const isFunctionKeys = new Set([
23
- 'default',
24
- 'enum',
25
- 'index',
26
- 'max',
27
- 'maxlength',
28
- 'min',
29
- 'minlength'
30
- ]);
38
+ }
31
39
 
32
40
  export { createBaseSchemaBuilderFactory };
@@ -1,11 +1,11 @@
1
1
  import type { BooleanSchemaDefinition, DefaultType, IndexDirection, IndexOptions } from 'mongoose';
2
2
  import type { Merge } from 'type-fest';
3
- type BaseProps = {
3
+ export type ExtendBooleanSchemaBuilder<Props extends BaseBooleanSchemaProps, ExtraOmitFields extends string> = Omit<BooleanSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
4
+ export interface BaseBooleanSchemaProps {
4
5
  type: BooleanSchemaDefinition;
5
- };
6
- export type ExtendBooleanSchemaBuilder<Props extends BaseProps, ExtraOmitFields extends string> = Omit<BooleanSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
7
- export interface BooleanSchemaBuilder<Props extends BaseProps = BaseProps, ExtraOmitFields extends string = never> {
8
- default: <T extends DefaultType<D> | ((this: any, doc: any) => DefaultType<D>) | null, D extends boolean>(value: T) => ExtendBooleanSchemaBuilder<Merge<Props, {
6
+ }
7
+ export interface BooleanSchemaBuilder<Props extends BaseBooleanSchemaProps = BaseBooleanSchemaProps, ExtraOmitFields extends string = never> {
8
+ default: <T extends ((this: any, doc: any) => DefaultType<D>) | DefaultType<D> | null, D extends boolean>(value: T) => ExtendBooleanSchemaBuilder<Merge<Props, {
9
9
  default: T;
10
10
  }>, ExtraOmitFields>;
11
11
  index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendBooleanSchemaBuilder<Merge<Props, {
@@ -25,5 +25,4 @@ export interface BooleanSchemaBuilder<Props extends BaseProps = BaseProps, Extra
25
25
  unique: true;
26
26
  }>, ExtraOmitFields>;
27
27
  }
28
- export declare const booleanSchemaBuilder: (schema?: Record<string, any>) => BooleanSchemaBuilder<BaseProps, never>;
29
- export {};
28
+ export declare const booleanSchemaBuilder: (schema?: Record<string, any>) => BooleanSchemaBuilder<BaseBooleanSchemaProps, never>;
@@ -1,23 +1,23 @@
1
- import type { DefaultType, DateSchemaDefinition, IndexDirection, IndexOptions } from 'mongoose';
1
+ import type { DateSchemaDefinition, DefaultType, IndexDirection, IndexOptions } from 'mongoose';
2
2
  import type { Merge } from 'type-fest';
3
3
  import type { Readonlyable } from '../types/utils';
4
- type BaseProps = {
5
- type: DateSchemaDefinition;
6
- };
7
4
  export type ExtendDateSchemaBuilder<Props extends BaseProps, ExtraOmitFields extends string> = Omit<DateSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
5
+ interface BaseProps {
6
+ type: DateSchemaDefinition;
7
+ }
8
8
  export interface DateSchemaBuilder<Props extends {
9
9
  type: DateSchemaDefinition;
10
10
  } = {
11
11
  type: DateSchemaDefinition;
12
12
  }, ExtraOmitFields extends string = never> {
13
- default: <T extends DefaultType<D> | ((this: any, doc: any) => DefaultType<D>) | null, D extends NativeDate>(value: T) => ExtendDateSchemaBuilder<Merge<Props, {
13
+ default: <T extends ((this: any, doc: any) => DefaultType<D>) | DefaultType<D> | null, D extends NativeDate>(value: T) => ExtendDateSchemaBuilder<Merge<Props, {
14
14
  default: T;
15
15
  }>, ExtraOmitFields>;
16
16
  enum: <T extends Readonlyable<Array<D | null>> | {
17
+ [path: string]: D | null;
18
+ } | {
17
19
  message?: M;
18
20
  values: Readonlyable<Array<D | null>>;
19
- } | {
20
- [path: string]: D | null;
21
21
  }, D extends NativeDate, M extends string>(value: T) => ExtendDateSchemaBuilder<Merge<Props, {
22
22
  enum: T;
23
23
  }>, ExtraOmitFields>;
@@ -9,22 +9,24 @@ const _interopDefaultCompat = e => e && typeof e === 'object' && 'default' in e
9
9
  const Decimal__default = /*#__PURE__*/_interopDefaultCompat(Decimal);
10
10
 
11
11
  const baseBuilderFactory = base.createBaseSchemaBuilderFactory(mongoose.Schema.Types.Decimal128);
12
- const decimal128SchemaBuilder = () => {
12
+ function decimal128SchemaBuilder() {
13
13
  const schema = {};
14
14
  const baseBuilder = baseBuilderFactory(schema);
15
15
  return new Proxy(baseBuilder, {
16
16
  get(target, key, receiver) {
17
17
  if (key === 'setRoundAndToFixedSetter') {
18
18
  return (places = 2, rounding = Decimal__default.ROUND_DOWN) => {
19
- schema['set'] = (value) => new Decimal__default(value.toString()).toFixed(places, rounding);
19
+ schema.set = (value) => new Decimal__default(value.toString()).toFixed(places, rounding);
20
20
  return receiver;
21
21
  };
22
22
  }
23
- if (key === 'setToStringGetter')
24
- return (schema['get'] = (value) => value.toString()), receiver;
23
+ if (key === 'setToStringGetter') {
24
+ schema.get = (value) => value.toString();
25
+ return receiver;
26
+ }
25
27
  return Reflect.get(target, key, receiver);
26
- }
28
+ },
27
29
  });
28
- };
30
+ }
29
31
 
30
32
  exports.decimal128SchemaBuilder = decimal128SchemaBuilder;
@@ -3,31 +3,31 @@ import { Schema } from 'mongoose';
3
3
  import type { DefaultType, IndexDirection, IndexOptions, Types } from 'mongoose';
4
4
  import type { Merge } from 'type-fest';
5
5
  import type { Readonlyable } from '../types/utils';
6
- type BaseProps = {
7
- type: Schema.Types.Decimal128;
8
- };
9
6
  export type ExtendDecimal128SchemaBuilder<Props extends BaseProps, ExtraOmitFields extends string> = Omit<Decimal128SchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
10
- type ToStringGetterSchema = {
7
+ interface BaseProps {
8
+ type: Schema.Types.Decimal128;
9
+ }
10
+ interface ToStringGetterSchema {
11
11
  get: (value: Types.Decimal128) => string;
12
- };
13
- type ToStringSetterSchema = {
12
+ }
13
+ interface ToStringSetterSchema {
14
14
  set: (value: {
15
- toString(): string;
15
+ toString: () => string;
16
16
  }) => string;
17
- };
17
+ }
18
18
  export interface Decimal128SchemaBuilder<Props extends {
19
19
  type: Schema.Types.Decimal128;
20
20
  } = {
21
21
  type: Schema.Types.Decimal128;
22
22
  }, ExtraOmitFields extends string = never> {
23
- default: <T extends DefaultType<D> | ((this: any, doc: any) => DefaultType<D>) | null, D extends Types.Decimal128>(value: T) => ExtendDecimal128SchemaBuilder<Merge<Props, {
23
+ default: <T extends ((this: any, doc: any) => DefaultType<D>) | DefaultType<D> | null, D extends Types.Decimal128>(value: T) => ExtendDecimal128SchemaBuilder<Merge<Props, {
24
24
  default: T;
25
25
  }>, ExtraOmitFields>;
26
26
  enum: <T extends Readonlyable<Array<D | null>> | {
27
+ [path: string]: D | null;
28
+ } | {
27
29
  message?: M;
28
30
  values: Readonlyable<Array<D | null>>;
29
- } | {
30
- [path: string]: D | null;
31
31
  }, D extends Types.Decimal128, M extends string>(value: T) => ExtendDecimal128SchemaBuilder<Merge<Props, {
32
32
  enum: T;
33
33
  }>, ExtraOmitFields>;
@@ -51,7 +51,7 @@ export interface Decimal128SchemaBuilder<Props extends {
51
51
  *
52
52
  * @returns A schema builder with the rounding and toFixed behavior applied to the `set` option in Mongoose.
53
53
  */
54
- setRoundAndToFixedSetter: (places?: number, rounding?: Decimal.Rounding) => ExtendDecimal128SchemaBuilder<Merge<Props, ToStringSetterSchema>, ExtraOmitFields | 'setRoundAndToFixedSetter'>;
54
+ setRoundAndToFixedSetter: (places?: number, rounding?: Decimal.Rounding) => ExtendDecimal128SchemaBuilder<Merge<Props, ToStringSetterSchema>, 'setRoundAndToFixedSetter' | ExtraOmitFields>;
55
55
  /**
56
56
  * Sets the toString behavior for Decimal128 fields.
57
57
  * This method defines how the Decimal128 field should be converted to a string when processing or
@@ -61,7 +61,7 @@ export interface Decimal128SchemaBuilder<Props extends {
61
61
  * @returns A schema builder with the custom `get` behavior applied to convert the Decimal128 field to a string
62
62
  * when accessed in the application.
63
63
  */
64
- setToStringGetter: ExtendDecimal128SchemaBuilder<Merge<Props, ToStringGetterSchema>, ExtraOmitFields | 'setToStringGetter'>;
64
+ setToStringGetter: ExtendDecimal128SchemaBuilder<Merge<Props, ToStringGetterSchema>, 'setToStringGetter' | ExtraOmitFields>;
65
65
  sparse: ExtendDecimal128SchemaBuilder<Merge<Props, {
66
66
  sparse: true;
67
67
  }>, ExtraOmitFields>;
@@ -69,5 +69,5 @@ export interface Decimal128SchemaBuilder<Props extends {
69
69
  unique: true;
70
70
  }>, ExtraOmitFields>;
71
71
  }
72
- export declare const decimal128SchemaBuilder: () => Decimal128SchemaBuilder;
72
+ export declare function decimal128SchemaBuilder(): Decimal128SchemaBuilder;
73
73
  export {};
@@ -3,22 +3,24 @@ import { Schema } from 'mongoose';
3
3
  import { createBaseSchemaBuilderFactory } from './base.mjs';
4
4
 
5
5
  const baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.Decimal128);
6
- const decimal128SchemaBuilder = () => {
6
+ function decimal128SchemaBuilder() {
7
7
  const schema = {};
8
8
  const baseBuilder = baseBuilderFactory(schema);
9
9
  return new Proxy(baseBuilder, {
10
10
  get(target, key, receiver) {
11
11
  if (key === 'setRoundAndToFixedSetter') {
12
12
  return (places = 2, rounding = Decimal.ROUND_DOWN) => {
13
- schema['set'] = (value) => new Decimal(value.toString()).toFixed(places, rounding);
13
+ schema.set = (value) => new Decimal(value.toString()).toFixed(places, rounding);
14
14
  return receiver;
15
15
  };
16
16
  }
17
- if (key === 'setToStringGetter')
18
- return (schema['get'] = (value) => value.toString()), receiver;
17
+ if (key === 'setToStringGetter') {
18
+ schema.get = (value) => value.toString();
19
+ return receiver;
20
+ }
19
21
  return Reflect.get(target, key, receiver);
20
- }
22
+ },
21
23
  });
22
- };
24
+ }
23
25
 
24
26
  export { decimal128SchemaBuilder };
@@ -2,27 +2,27 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
+ const boolean = require('./boolean.cjs');
5
6
  const date = require('./date.cjs');
6
7
  const decimal128 = require('./decimal128.cjs');
7
- const boolean = require('./boolean.cjs');
8
8
  const number = require('./number.cjs');
9
9
  const objectId = require('./object-id.cjs');
10
10
  const ref = require('./ref.cjs');
11
11
  const string = require('./string.cjs');
12
12
 
13
13
  const schemaBuilders = {
14
+ boolean: boolean.booleanSchemaBuilder,
14
15
  date: date.dateSchemaBuilder,
15
16
  decimal128: decimal128.decimal128SchemaBuilder,
16
- boolean: boolean.booleanSchemaBuilder,
17
17
  number: number.numberSchemaBuilder,
18
18
  objectId: objectId.objectIdSchemaBuilder,
19
19
  ref: ref.refSchemaBuilder,
20
- string: string.stringSchemaBuilder
20
+ string: string.stringSchemaBuilder,
21
21
  };
22
22
 
23
+ exports.booleanSchemaBuilder = boolean.booleanSchemaBuilder;
23
24
  exports.dateSchemaBuilder = date.dateSchemaBuilder;
24
25
  exports.decimal128SchemaBuilder = decimal128.decimal128SchemaBuilder;
25
- exports.booleanSchemaBuilder = boolean.booleanSchemaBuilder;
26
26
  exports.numberSchemaBuilder = number.numberSchemaBuilder;
27
27
  exports.refSchemaBuilder = ref.refSchemaBuilder;
28
28
  exports.stringSchemaBuilder = string.stringSchemaBuilder;
@@ -1,27 +1,27 @@
1
+ import { decimal128SchemaBuilder } from './decimal128';
2
+ import { stringSchemaBuilder } from './string';
3
+ export * from './boolean';
1
4
  export * from './date';
2
5
  export * from './decimal128';
3
- export * from './boolean';
4
6
  export * from './number';
5
7
  export * from './ref';
6
8
  export * from './string';
7
9
  export declare const schemaBuilders: {
10
+ boolean: (schema?: Record<string, any>) => import("./boolean").BooleanSchemaBuilder<import("./boolean").BaseBooleanSchemaProps, never>;
8
11
  date: (schema?: Record<string, any>) => import("./date").DateSchemaBuilder<{
9
12
  type: import("mongoose").DateSchemaDefinition;
10
13
  }, never>;
11
- decimal128: () => import("./decimal128").Decimal128SchemaBuilder;
12
- boolean: (schema?: Record<string, any>) => import("./boolean").BooleanSchemaBuilder<{
13
- type: import("mongoose").BooleanSchemaDefinition;
14
- }, never>;
14
+ decimal128: typeof decimal128SchemaBuilder;
15
15
  number: (schema?: Record<string, any>) => import("./number").NumberSchemaBuilder<{
16
16
  type: import("mongoose").NumberSchemaDefinition;
17
17
  }, never>;
18
18
  objectId: (schema?: Record<string, any>) => import("./object-id").ObjectIdSchemaBuilder<{
19
19
  type: import("mongoose").ObjectIdSchemaDefinition;
20
20
  }, never>;
21
- ref: <T extends string | import("mongoose").Model<any> | ((this: any, doc: any) => string | import("mongoose").Model<any>)>(ref: T) => import("./ref").RefSchemaBuilder<{
21
+ ref: <T extends ((this: any, doc: any) => import("mongoose").Model<any> | string) | import("mongoose").Model<any> | string>(ref: T) => import("./ref").RefSchemaBuilder<{
22
22
  ref: T;
23
23
  type: import("mongoose").ObjectIdSchemaDefinition;
24
24
  }>;
25
- string: () => import("./string").StringSchemaBuilder;
25
+ string: typeof stringSchemaBuilder;
26
26
  };
27
27
  export default schemaBuilders;
@@ -1,19 +1,19 @@
1
+ import { booleanSchemaBuilder } from './boolean.mjs';
1
2
  import { dateSchemaBuilder } from './date.mjs';
2
3
  import { decimal128SchemaBuilder } from './decimal128.mjs';
3
- import { booleanSchemaBuilder } from './boolean.mjs';
4
4
  import { numberSchemaBuilder } from './number.mjs';
5
5
  import { objectIdSchemaBuilder } from './object-id.mjs';
6
6
  import { refSchemaBuilder } from './ref.mjs';
7
7
  import { stringSchemaBuilder } from './string.mjs';
8
8
 
9
9
  const schemaBuilders = {
10
+ boolean: booleanSchemaBuilder,
10
11
  date: dateSchemaBuilder,
11
12
  decimal128: decimal128SchemaBuilder,
12
- boolean: booleanSchemaBuilder,
13
13
  number: numberSchemaBuilder,
14
14
  objectId: objectIdSchemaBuilder,
15
15
  ref: refSchemaBuilder,
16
- string: stringSchemaBuilder
16
+ string: stringSchemaBuilder,
17
17
  };
18
18
 
19
19
  export { booleanSchemaBuilder, dateSchemaBuilder, decimal128SchemaBuilder, schemaBuilders as default, numberSchemaBuilder, refSchemaBuilder, schemaBuilders, stringSchemaBuilder };
@@ -1,23 +1,23 @@
1
1
  import type { DefaultType, IndexDirection, IndexOptions, NumberSchemaDefinition } from 'mongoose';
2
2
  import type { Merge } from 'type-fest';
3
3
  import type { Readonlyable } from '../types/utils';
4
- type BaseProps = {
5
- type: NumberSchemaDefinition;
6
- };
7
4
  export type ExtendNumberSchemaBuilder<Props extends BaseProps, ExtraOmitFields extends string> = Omit<NumberSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
5
+ interface BaseProps {
6
+ type: NumberSchemaDefinition;
7
+ }
8
8
  export interface NumberSchemaBuilder<Props extends {
9
9
  type: NumberSchemaDefinition;
10
10
  } = {
11
11
  type: NumberSchemaDefinition;
12
12
  }, ExtraOmitFields extends string = never> {
13
- default: <T extends DefaultType<D> | ((this: any, doc: any) => DefaultType<D>) | null, D extends number>(value: T) => ExtendNumberSchemaBuilder<Merge<Props, {
13
+ default: <T extends ((this: any, doc: any) => DefaultType<D>) | DefaultType<D> | null, D extends number>(value: T) => ExtendNumberSchemaBuilder<Merge<Props, {
14
14
  default: T;
15
15
  }>, ExtraOmitFields>;
16
16
  enum: <T extends Readonlyable<Array<N | null>> | {
17
+ [path: string]: N | null;
18
+ } | {
17
19
  message?: M;
18
20
  values: Readonlyable<Array<N | null>>;
19
- } | {
20
- [path: string]: N | null;
21
21
  }, M extends string, N extends number>(value: T) => ExtendNumberSchemaBuilder<Merge<Props, {
22
22
  enum: T;
23
23
  }>, ExtraOmitFields>;
@@ -1,23 +1,23 @@
1
1
  import type { DefaultType, IndexDirection, IndexOptions, ObjectIdSchemaDefinition, Types } from 'mongoose';
2
2
  import type { Merge } from 'type-fest';
3
3
  import type { Readonlyable } from '../types/utils';
4
- type BaseProps = {
5
- type: ObjectIdSchemaDefinition;
6
- };
7
4
  export type ExtendObjectIdSchemaBuilder<Props extends BaseProps, ExtraOmitFields extends string> = Omit<ObjectIdSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
5
+ interface BaseProps {
6
+ type: ObjectIdSchemaDefinition;
7
+ }
8
8
  export interface ObjectIdSchemaBuilder<Props extends {
9
9
  type: ObjectIdSchemaDefinition;
10
10
  } = {
11
11
  type: ObjectIdSchemaDefinition;
12
12
  }, ExtraOmitFields extends string = never> {
13
- default: <T extends DefaultType<D> | ((this: any, doc: any) => DefaultType<D>) | null, D extends Types.ObjectId>(value: T) => ExtendObjectIdSchemaBuilder<Merge<Props, {
13
+ default: <T extends ((this: any, doc: any) => DefaultType<D>) | DefaultType<D> | null, D extends Types.ObjectId>(value: T) => ExtendObjectIdSchemaBuilder<Merge<Props, {
14
14
  default: T;
15
15
  }>, ExtraOmitFields>;
16
- enum: <T extends Readonlyable<Array<O | null>> | {
17
- message?: M;
18
- values: Readonlyable<Array<O | null>>;
16
+ enum: <T extends Readonlyable<Array<null | O>> | {
17
+ [path: string]: null | O;
19
18
  } | {
20
- [path: string]: O | null;
19
+ message?: M;
20
+ values: Readonlyable<Array<null | O>>;
21
21
  }, M extends string, O extends Types.ObjectId>(value: T) => ExtendObjectIdSchemaBuilder<Merge<Props, {
22
22
  enum: T;
23
23
  }>, ExtraOmitFields>;
@@ -1,23 +1,23 @@
1
1
  import type { DefaultType, IndexDirection, IndexOptions, Model, ObjectIdSchemaDefinition, Types } from 'mongoose';
2
2
  import type { Merge } from 'type-fest';
3
3
  import type { Readonlyable } from '../types/utils';
4
- type BaseProps = {
5
- type: ObjectIdSchemaDefinition;
6
- };
7
4
  export type ExtendRefSchemaBuilder<Props extends BaseProps, ExtraOmitFields extends string> = Omit<RefSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
5
+ interface BaseProps {
6
+ type: ObjectIdSchemaDefinition;
7
+ }
8
8
  export interface RefSchemaBuilder<Props extends {
9
9
  type: ObjectIdSchemaDefinition;
10
10
  } = {
11
11
  type: ObjectIdSchemaDefinition;
12
12
  }, ExtraOmitFields extends string = never> {
13
- default: <T extends DefaultType<D> | ((this: any, doc: any) => DefaultType<D>) | null, D extends Types.ObjectId>(value: T) => ExtendRefSchemaBuilder<Merge<Props, {
13
+ default: <T extends ((this: any, doc: any) => DefaultType<D>) | DefaultType<D> | null, D extends Types.ObjectId>(value: T) => ExtendRefSchemaBuilder<Merge<Props, {
14
14
  default: T;
15
15
  }>, ExtraOmitFields>;
16
- enum: <T extends Readonlyable<Array<O | null>> | {
17
- message?: M;
18
- values: Readonlyable<Array<O | null>>;
16
+ enum: <T extends Readonlyable<Array<null | O>> | {
17
+ [path: string]: null | O;
19
18
  } | {
20
- [path: string]: O | null;
19
+ message?: M;
20
+ values: Readonlyable<Array<null | O>>;
21
21
  }, M extends string, O extends Types.ObjectId>(value: T) => ExtendRefSchemaBuilder<Merge<Props, {
22
22
  enum: T;
23
23
  }>, ExtraOmitFields>;
@@ -38,7 +38,7 @@ export interface RefSchemaBuilder<Props extends {
38
38
  unique: true;
39
39
  }>, ExtraOmitFields>;
40
40
  }
41
- export declare const refSchemaBuilder: <T extends string | Model<any> | ((this: any, doc: any) => string | Model<any>)>(ref: T) => RefSchemaBuilder<{
41
+ export declare const refSchemaBuilder: <T extends ((this: any, doc: any) => Model<any> | string) | Model<any> | string>(ref: T) => RefSchemaBuilder<{
42
42
  ref: T;
43
43
  type: ObjectIdSchemaDefinition;
44
44
  }>;
@@ -10,7 +10,7 @@ const net__default = /*#__PURE__*/_interopDefaultCompat(net);
10
10
  const baseBuilderFactory = base.createBaseSchemaBuilderFactory(String);
11
11
  const defaultIPv4ValidateMessage = '`{VALUE}` is not a valid IPv4 address for path `{PATH}`.';
12
12
  const defaultIPv6ValidateMessage = '`{VALUE}` is not a valid IPv6 address for path `{PATH}`.';
13
- const stringSchemaBuilder = () => {
13
+ function stringSchemaBuilder() {
14
14
  const schema = {};
15
15
  const baseBuilder = baseBuilderFactory(schema);
16
16
  return new Proxy(baseBuilder, {
@@ -18,22 +18,32 @@ const stringSchemaBuilder = () => {
18
18
  if (key === 'ipv4') {
19
19
  return (message = defaultIPv4ValidateMessage) => {
20
20
  schema.trim = true;
21
- schema.validate = { message, validator: (value) => net__default.isIPv4(value) };
21
+ schema.validate = {
22
+ message,
23
+ validator: (value) => net__default.isIPv4(value),
24
+ };
22
25
  return receiver;
23
26
  };
24
27
  }
25
28
  if (key === 'ipv6') {
26
29
  return (message = defaultIPv6ValidateMessage) => {
27
30
  schema.trim = true;
28
- schema.validate = { message, validator: (value) => net__default.isIPv6(value) };
31
+ schema.validate = {
32
+ message,
33
+ validator: (value) => net__default.isIPv6(value),
34
+ };
35
+ return receiver;
36
+ };
37
+ }
38
+ if (key === 'length') {
39
+ return (value) => {
40
+ schema.maxlength = schema.minlength = value;
29
41
  return receiver;
30
42
  };
31
43
  }
32
- if (key === 'length')
33
- return (value) => ((schema['maxlength'] = schema['minlength'] = value), receiver);
34
44
  return Reflect.get(target, key, receiver);
35
- }
45
+ },
36
46
  });
37
- };
47
+ }
38
48
 
39
49
  exports.stringSchemaBuilder = stringSchemaBuilder;
@@ -1,30 +1,30 @@
1
1
  import type { DefaultType, IndexDirection, IndexOptions, StringSchemaDefinition } from 'mongoose';
2
2
  import type { Merge } from 'type-fest';
3
3
  import type { Readonlyable } from '../types/utils';
4
- type BaseProps = {
5
- type: StringSchemaDefinition;
6
- };
7
4
  export type ExtendStringSchemaBuilder<Props extends BaseProps, ExtraOmitFields extends string> = Omit<StringSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
8
- type IPSchema<T extends string> = {
5
+ interface BaseProps {
6
+ type: StringSchemaDefinition;
7
+ }
8
+ interface IPSchema<T extends string> {
9
9
  trim: true;
10
10
  validate: {
11
11
  message: T;
12
12
  validator: (value: string) => boolean;
13
13
  };
14
- };
14
+ }
15
15
  export interface StringSchemaBuilder<Props extends {
16
16
  type: StringSchemaDefinition;
17
17
  } = {
18
18
  type: StringSchemaDefinition;
19
19
  }, ExtraOmitFields extends string = never> {
20
- default: <T extends DefaultType<D> | ((this: any, doc: any) => DefaultType<D>) | null, D extends string>(value: T) => ExtendStringSchemaBuilder<Merge<Props, {
20
+ default: <T extends ((this: any, doc: any) => DefaultType<D>) | DefaultType<D> | null, D extends string>(value: T) => ExtendStringSchemaBuilder<Merge<Props, {
21
21
  default: T;
22
22
  }>, ExtraOmitFields>;
23
- enum: <T extends Readonlyable<Array<S | null>> | {
24
- message?: M;
25
- values: Readonlyable<Array<S | null>>;
23
+ enum: <T extends Readonlyable<Array<null | S>> | {
24
+ [path: string]: null | S;
26
25
  } | {
27
- [path: string]: S | null;
26
+ message?: M;
27
+ values: Readonlyable<Array<null | S>>;
28
28
  }, M extends string, S extends string>(value: T) => ExtendStringSchemaBuilder<Merge<Props, {
29
29
  enum: T;
30
30
  }>, ExtraOmitFields>;
@@ -39,7 +39,7 @@ export interface StringSchemaBuilder<Props extends {
39
39
  *
40
40
  * @returns A schema builder with IPv4 validation and the `trim` option enabled.
41
41
  */
42
- ipv4: <T extends string = typeof defaultIPv4ValidateMessage>(message?: T) => ExtendStringSchemaBuilder<Merge<Props, IPSchema<T>>, ExtraOmitFields | 'ipv4' | 'ipv6'>;
42
+ ipv4: <T extends string = typeof defaultIPv4ValidateMessage>(message?: T) => ExtendStringSchemaBuilder<Merge<Props, IPSchema<T>>, 'ipv4' | 'ipv6' | ExtraOmitFields>;
43
43
  /**
44
44
  * Adds IPv6 validation to the string schema.
45
45
  * Ensures the string is a valid IPv6 address and trims the input. The validation message can be customized.
@@ -48,7 +48,7 @@ export interface StringSchemaBuilder<Props extends {
48
48
  *
49
49
  * @returns A schema builder with IPv6 validation and the `trim` option enabled.
50
50
  */
51
- ipv6: <T extends string = typeof defaultIPv6ValidateMessage>(message?: T) => ExtendStringSchemaBuilder<Merge<Props, IPSchema<T>>, ExtraOmitFields | 'ipv4' | 'ipv6'>;
51
+ ipv6: <T extends string = typeof defaultIPv6ValidateMessage>(message?: T) => ExtendStringSchemaBuilder<Merge<Props, IPSchema<T>>, 'ipv4' | 'ipv6' | ExtraOmitFields>;
52
52
  /**
53
53
  * Sets both the maximum and minimum length of the string.
54
54
  * This method ensures the string length is exactly or within a specified range.
@@ -95,5 +95,5 @@ export interface StringSchemaBuilder<Props extends {
95
95
  }
96
96
  declare const defaultIPv4ValidateMessage = "`{VALUE}` is not a valid IPv4 address for path `{PATH}`.";
97
97
  declare const defaultIPv6ValidateMessage = "`{VALUE}` is not a valid IPv6 address for path `{PATH}`.";
98
- export declare const stringSchemaBuilder: () => StringSchemaBuilder;
98
+ export declare function stringSchemaBuilder(): StringSchemaBuilder;
99
99
  export {};
@@ -4,7 +4,7 @@ import { createBaseSchemaBuilderFactory } from './base.mjs';
4
4
  const baseBuilderFactory = createBaseSchemaBuilderFactory(String);
5
5
  const defaultIPv4ValidateMessage = '`{VALUE}` is not a valid IPv4 address for path `{PATH}`.';
6
6
  const defaultIPv6ValidateMessage = '`{VALUE}` is not a valid IPv6 address for path `{PATH}`.';
7
- const stringSchemaBuilder = () => {
7
+ function stringSchemaBuilder() {
8
8
  const schema = {};
9
9
  const baseBuilder = baseBuilderFactory(schema);
10
10
  return new Proxy(baseBuilder, {
@@ -12,22 +12,32 @@ const stringSchemaBuilder = () => {
12
12
  if (key === 'ipv4') {
13
13
  return (message = defaultIPv4ValidateMessage) => {
14
14
  schema.trim = true;
15
- schema.validate = { message, validator: (value) => net.isIPv4(value) };
15
+ schema.validate = {
16
+ message,
17
+ validator: (value) => net.isIPv4(value),
18
+ };
16
19
  return receiver;
17
20
  };
18
21
  }
19
22
  if (key === 'ipv6') {
20
23
  return (message = defaultIPv6ValidateMessage) => {
21
24
  schema.trim = true;
22
- schema.validate = { message, validator: (value) => net.isIPv6(value) };
25
+ schema.validate = {
26
+ message,
27
+ validator: (value) => net.isIPv6(value),
28
+ };
29
+ return receiver;
30
+ };
31
+ }
32
+ if (key === 'length') {
33
+ return (value) => {
34
+ schema.maxlength = schema.minlength = value;
23
35
  return receiver;
24
36
  };
25
37
  }
26
- if (key === 'length')
27
- return (value) => ((schema['maxlength'] = schema['minlength'] = value), receiver);
28
38
  return Reflect.get(target, key, receiver);
29
- }
39
+ },
30
40
  });
31
- };
41
+ }
32
42
 
33
43
  export { stringSchemaBuilder };
@@ -1,59 +1,64 @@
1
1
  import type { AggregatePaginateOptions, Schema } from 'mongoose';
2
2
  declare module 'mongoose' {
3
- interface AggregateCustomLabels<T = string | undefined | boolean> {
4
- totalDocs?: T;
3
+ interface AggregateCustomLabels<T = boolean | string | undefined> {
5
4
  docs?: T;
5
+ hasNextPage?: T;
6
+ hasPrevPage?: T;
6
7
  limit?: T;
7
- page?: T;
8
+ meta?: T;
8
9
  nextPage?: T;
10
+ page?: T;
11
+ pagingCounter?: T;
9
12
  prevPage?: T;
10
- hasNextPage?: T;
11
- hasPrevPage?: T;
13
+ totalDocs?: T;
12
14
  totalPages?: T;
13
- pagingCounter?: T;
14
- meta?: T;
15
15
  }
16
16
  interface AggregatePaginateOptions {
17
- sort?: object | string;
17
+ allowDiskUse?: boolean;
18
+ countQuery?: object;
19
+ customLabels?: AggregateCustomLabels;
20
+ limit?: number;
18
21
  offset?: number;
19
22
  page?: number;
20
- limit?: number;
21
- customLabels?: AggregateCustomLabels;
23
+ /**
24
+ * If pagination is set to `false`, it will return all docs without adding limit condition.
25
+ *
26
+ * @default true
27
+ */
22
28
  pagination?: boolean;
23
- allowDiskUse?: boolean;
24
- countQuery?: object;
29
+ sort?: object | string;
25
30
  useFacet?: boolean;
26
31
  }
27
32
  interface AggregatePaginateQueryPopulateOptions {
28
- /** space delimited path(s) to populate */
29
- path: string;
30
- /** optional fields to select */
31
- select?: any;
32
33
  /** optional query conditions to match */
33
34
  match?: any;
34
35
  /** optional model to use for population */
35
- model?: string | Model<any>;
36
+ model?: Model<any> | string;
36
37
  /** optional query options like sort, limit, etc */
37
38
  options?: any;
39
+ /** space delimited path(s) to populate */
40
+ path: string;
38
41
  /** deep populate */
39
42
  populate?: AggregatePaginateQueryPopulateOptions | AggregatePaginateQueryPopulateOptions[];
43
+ /** optional fields to select */
44
+ select?: any;
40
45
  }
41
46
  interface AggregatePaginateResult<T> {
47
+ [customLabel: string]: boolean | null | number | T[] | undefined;
42
48
  docs: T[];
43
- totalDocs: number;
49
+ hasNextPage: boolean;
50
+ hasPrevPage: boolean;
44
51
  limit: number;
52
+ meta?: any;
53
+ nextPage?: null | number;
45
54
  page?: number;
46
- totalPages: number;
47
- nextPage?: number | null;
48
- prevPage?: number | null;
49
55
  pagingCounter: number;
50
- hasPrevPage: boolean;
51
- hasNextPage: boolean;
52
- meta?: any;
53
- [customLabel: string]: T[] | number | boolean | null | undefined;
56
+ prevPage?: null | number;
57
+ totalDocs: number;
58
+ totalPages: number;
54
59
  }
55
- interface AggregatePaginateModel<RawDocType, QueryHelpers = {}, InstanceMethodsAndOverrides = {}> extends Model<RawDocType, QueryHelpers, InstanceMethodsAndOverrides> {
56
- aggregatePaginate<T>(query?: Aggregate<T[]>, options?: AggregatePaginateOptions, callback?: (err: any, result: AggregatePaginateResult<T>) => void): Promise<AggregatePaginateResult<T>>;
60
+ interface AggregatePaginateModel<RawDocType, QueryHelpers = object, InstanceMethodsAndOverrides = object> extends Model<RawDocType, QueryHelpers, InstanceMethodsAndOverrides> {
61
+ aggregatePaginate: <T>(query?: Aggregate<T[]>, options?: AggregatePaginateOptions, callback?: (err: any, result: AggregatePaginateResult<T>) => void) => Promise<AggregatePaginateResult<T>>;
57
62
  }
58
63
  }
59
64
  declare function mongooseAggregatePaginate(schema: Schema): void;
package/types/data.d.ts CHANGED
@@ -62,7 +62,7 @@ declare global {
62
62
  * @template T - The type from which to omit fields.
63
63
  * @template OT - Additional fields to omit, specified as a union of string, number, or symbol keys.
64
64
  */
65
- type OmitMongooseTimestampAndOtherFields<T, OT extends number | symbol | string = never> = Omit<T, 'createdAt' | 'updatedAt' | OT>;
65
+ type OmitMongooseTimestampAndOtherFields<T, OT extends number | string | symbol = never> = Omit<T, 'createdAt' | 'updatedAt' | OT>;
66
66
  /**
67
67
  * Type definition for including timestamp fields conditionally based on the provided flags.
68
68
  *
package/types/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import type { AggregatePaginateModel, Connection, HydratedDocument, PaginateModel, QueryWithHelpers, Types } from 'mongoose';
2
2
  export type {} from './aggregate-paginate';
3
3
  export type {} from './paginate';
4
- export interface BaseModelStatics<RawDocType, InstanceMethodsAndOverrides = {}, QueryHelpers = {}> {
4
+ export interface BaseModelStatics<RawDocType, InstanceMethodsAndOverrides = object, QueryHelpers = object> {
5
5
  }
6
6
  export interface MongooseConnections {
7
7
  default?: Connection;
@@ -18,11 +18,11 @@ declare global {
18
18
  * @template CreatedAtField - A boolean flag indicating whether the `createdAt` field should be included.
19
19
  * @template UpdatedAtField - A boolean flag indicating whether the `updatedAt` field should be included.
20
20
  */
21
- type BaseMongooseDocType<T, CreatedAtField extends boolean = true, UpdatedAtField extends boolean = true> = Omit<T, 'createdAt' | 'id' | 'updatedAt'> & (CreatedAtField extends true ? {
21
+ type BaseMongooseDocType<T, CreatedAtField extends boolean = true, UpdatedAtField extends boolean = true> = (CreatedAtField extends true ? {
22
22
  createdAt: Date;
23
- } : {}) & (UpdatedAtField extends true ? {
23
+ } : object) & Omit<T, 'createdAt' | 'id' | 'updatedAt'> & (UpdatedAtField extends true ? {
24
24
  updatedAt: Date;
25
- } : {});
25
+ } : object);
26
26
  /**
27
27
  * Type definition for a Mongoose model with pagination and aggregation capabilities.
28
28
  *
@@ -33,7 +33,7 @@ declare global {
33
33
  * @template InstanceMethodsAndOverrides - Optional type parameter for instance methods and overrides.
34
34
  * @template QueryHelpers - Optional type parameter for additional query helper methods.
35
35
  */
36
- type BaseMongoosePaginateModel<RawDocType, InstanceMethodsAndOverrides = {}, QueryHelpers = {}> = AggregatePaginateModel<RawDocType, QueryHelpers, InstanceMethodsAndOverrides> & PaginateModel<RawDocType, QueryHelpers, InstanceMethodsAndOverrides> & BaseModelStatics<RawDocType, InstanceMethodsAndOverrides, QueryHelpers>;
36
+ type BaseMongoosePaginateModel<RawDocType, InstanceMethodsAndOverrides = object, QueryHelpers = object> = AggregatePaginateModel<RawDocType, QueryHelpers, InstanceMethodsAndOverrides> & BaseModelStatics<RawDocType, InstanceMethodsAndOverrides, QueryHelpers> & PaginateModel<RawDocType, QueryHelpers, InstanceMethodsAndOverrides>;
37
37
  /**
38
38
  * Type definition for a Mongoose document or ObjectId.
39
39
  *
@@ -58,7 +58,7 @@ declare global {
58
58
  * @template QueryHelpers - Optional type parameter for additional query helper methods.
59
59
  * @template InstanceMethodsAndOverrides - Optional type parameter for specifying custom instance methods and overrides on the document.
60
60
  */
61
- type MongooseFindOneReturnType<RawDocType, DocType, QueryHelpers = {}, InstanceMethodsAndOverrides = {}> = QueryWithHelpers<DocType | null, DocType, QueryHelpers, RawDocType, 'findOne', InstanceMethodsAndOverrides>;
61
+ type MongooseFindOneReturnType<RawDocType, DocType, QueryHelpers = object, InstanceMethodsAndOverrides = object> = QueryWithHelpers<DocType | null, DocType, QueryHelpers, RawDocType, 'findOne', InstanceMethodsAndOverrides>;
62
62
  /**
63
63
  * Type definition for a hydrated Mongoose document.
64
64
  *
@@ -71,5 +71,5 @@ declare global {
71
71
  * @template InstanceMethodsAndOverrides - Optional type parameter for instance methods and overrides.
72
72
  * @template QueryHelpers - Optional type parameter for additional query helper methods.
73
73
  */
74
- type MongooseHydratedDocument<DocType, InstanceMethodsAndOverrides = {}, QueryHelpers = {}> = HydratedDocument<DocType, InstanceMethodsAndOverrides, QueryHelpers>;
74
+ type MongooseHydratedDocument<DocType, InstanceMethodsAndOverrides = object, QueryHelpers = object> = HydratedDocument<DocType, InstanceMethodsAndOverrides, QueryHelpers>;
75
75
  }
@@ -40,5 +40,5 @@ export interface CustomMongooseOptions {
40
40
  /**
41
41
  * A function that will be executed before the final build when using {@link buildMongooseModel}.
42
42
  */
43
- beforeModelBuild?: <DocType, Model extends BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>, InstanceMethodsAndOverrides = {}, QueryHelpers = {}>(schema: Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>) => void;
43
+ beforeModelBuild?: <DocType, Model extends BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>, InstanceMethodsAndOverrides = object, QueryHelpers = object>(schema: Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>) => void;
44
44
  }
@@ -1,81 +1,90 @@
1
1
  import type { FilterQuery, mongo, PaginateOptions, Schema } from 'mongoose';
2
2
  declare module 'mongoose' {
3
- interface PaginateCustomLabels<T = string | undefined | boolean> {
4
- totalDocs?: T;
3
+ interface PaginateCustomLabels<T = boolean | string | undefined> {
5
4
  docs?: T;
5
+ hasNextPage?: T;
6
+ hasPrevPage?: T;
6
7
  limit?: T;
7
- page?: T;
8
+ meta?: T;
8
9
  nextPage?: T;
10
+ page?: T;
11
+ pagingCounter?: T;
9
12
  prevPage?: T;
10
- hasNextPage?: T;
11
- hasPrevPage?: T;
13
+ totalDocs?: T;
12
14
  totalPages?: T;
13
- pagingCounter?: T;
14
- meta?: T;
15
15
  }
16
16
  interface PaginateReadOptions {
17
17
  pref: string;
18
18
  tags?: any[];
19
19
  }
20
20
  interface PaginateOptions {
21
- select?: object | string;
21
+ allowDiskUse?: boolean;
22
22
  collation?: mongo.CollationOptions;
23
- sort?: object | string;
24
- populate?: PopulateOptions[] | string[] | PopulateOptions | string;
25
- projection?: any;
23
+ customLabels?: PaginateCustomLabels;
24
+ forceCountFn?: boolean;
26
25
  lean?: boolean;
27
26
  leanWithId?: boolean;
27
+ limit?: number;
28
28
  offset?: number;
29
+ options?: QueryOptions;
29
30
  page?: number;
30
- limit?: number;
31
- customLabels?: PaginateCustomLabels;
31
+ /**
32
+ * If pagination is set to `false`, it will return all docs without adding limit condition.
33
+ *
34
+ * @default true
35
+ */
32
36
  pagination?: boolean;
33
- useEstimatedCount?: boolean;
34
- useCustomCountFn?: () => Promise<number>;
35
- forceCountFn?: boolean;
36
- allowDiskUse?: boolean;
37
+ populate?: PopulateOptions | PopulateOptions[] | string | string[];
38
+ projection?: any;
37
39
  read?: PaginateReadOptions;
38
- options?: QueryOptions;
40
+ select?: object | string;
41
+ sort?: object | string;
42
+ useCustomCountFn?: () => Promise<number>;
43
+ useEstimatedCount?: boolean;
39
44
  }
40
45
  interface SubPaginateOptions {
41
- select?: object | string;
42
- populate?: PopulateOptions[] | string[] | PopulateOptions | string;
43
46
  pagination?: boolean;
44
- read?: PaginateReadOptions;
45
47
  pagingOptions: SubDocumentPagingOptions | undefined;
48
+ populate?: PopulateOptions | PopulateOptions[] | string | string[];
49
+ read?: PaginateReadOptions;
50
+ select?: object | string;
46
51
  }
47
52
  interface SubDocumentPagingOptions {
48
- populate?: PopulateOptions[] | string[] | PopulateOptions | string;
49
- page?: number;
50
53
  limit?: number;
54
+ page?: number;
55
+ populate?: PopulateOptions | PopulateOptions[] | string | string[];
51
56
  }
52
57
  interface PaginateResult<T> {
58
+ [customLabel: string]: boolean | null | number | T[] | undefined;
53
59
  docs: T[];
54
- totalDocs: number;
55
- limit: number;
56
- hasPrevPage: boolean;
57
60
  hasNextPage: boolean;
58
- page?: number;
59
- totalPages: number;
61
+ hasPrevPage: boolean;
62
+ limit: number;
63
+ meta?: any;
64
+ nextPage?: null | number;
60
65
  offset: number;
61
- prevPage?: number | null;
62
- nextPage?: number | null;
66
+ page?: number;
63
67
  pagingCounter: number;
64
- meta?: any;
65
- [customLabel: string]: T[] | number | boolean | null | undefined;
68
+ prevPage?: null | number;
69
+ totalDocs: number;
70
+ totalPages: number;
66
71
  }
67
- type PaginateDocument<T, TMethods, TQueryHelpers, O extends PaginateOptions = {}> = O['lean'] extends true ? (O['leanWithId'] extends true ? T & {
72
+ type PaginateDocument<T, TMethods, TQueryHelpers, O extends PaginateOptions = object> = O['lean'] extends true ? (O['leanWithId'] extends true ? T & {
68
73
  id: string;
69
74
  } : T) : HydratedDocument<T, TMethods, TQueryHelpers>;
70
- interface PaginateModel<T, TQueryHelpers = {}, TMethods = {}> extends Model<T, TQueryHelpers, TMethods> {
71
- paginate<O extends PaginateOptions>(query?: FilterQuery<T>, options?: O, callback?: (err: any, result: PaginateResult<PaginateDocument<T, TMethods, TQueryHelpers, O>>) => void): Promise<PaginateResult<PaginateDocument<T, TMethods, TQueryHelpers, O>>>;
72
- paginate<UserType = T, O extends PaginateOptions = PaginateOptions>(query?: FilterQuery<T>, options?: O, callback?: (err: any, result: PaginateResult<PaginateDocument<UserType, TMethods, TQueryHelpers, O>>) => void): Promise<PaginateResult<PaginateDocument<UserType, TMethods, TQueryHelpers, O>>>;
73
- paginate<UserType = T>(query?: FilterQuery<T>, options?: PaginateOptions, callback?: (err: any, result: PaginateResult<PaginateDocument<UserType, TMethods, TQueryHelpers, PaginateOptions>>) => void): Promise<PaginateResult<PaginateDocument<UserType, TMethods, TQueryHelpers, PaginateOptions>>>;
75
+ interface PaginateModel<T, TQueryHelpers = object, TMethods = object> extends Model<T, TQueryHelpers, TMethods> {
76
+ paginate: {
77
+ <O extends PaginateOptions>(query?: FilterQuery<T>, options?: O, callback?: (err: any, result: PaginateResult<PaginateDocument<T, TMethods, TQueryHelpers, O>>) => void): Promise<PaginateResult<PaginateDocument<T, TMethods, TQueryHelpers, O>>>;
78
+ <UserType = T, O extends PaginateOptions = PaginateOptions>(query?: FilterQuery<T>, options?: O, callback?: (err: any, result: PaginateResult<PaginateDocument<UserType, TMethods, TQueryHelpers, O>>) => void): Promise<PaginateResult<PaginateDocument<UserType, TMethods, TQueryHelpers, O>>>;
79
+ <UserType = T>(query?: FilterQuery<T>, options?: PaginateOptions, callback?: (err: any, result: PaginateResult<PaginateDocument<UserType, TMethods, TQueryHelpers, PaginateOptions>>) => void): Promise<PaginateResult<PaginateDocument<UserType, TMethods, TQueryHelpers, PaginateOptions>>>;
80
+ };
74
81
  }
75
- interface Query<ResultType, DocType, THelpers = NonNullable<unknown>, RawDocType = DocType, QueryOp = 'find', TInstanceMethods = Record<string, never>> {
76
- paginate<O extends PaginateOptions>(options?: O): Promise<PaginateResult<PaginateDocument<RawDocType, TInstanceMethods, THelpers, O>>>;
77
- paginate<UserType = ResultType, O extends PaginateOptions = PaginateOptions>(options?: O): Promise<PaginateResult<PaginateDocument<UserType, TInstanceMethods, THelpers, O>>>;
78
- paginate<UserType = ResultType>(options?: PaginateOptions): Promise<PaginateResult<PaginateDocument<UserType, TInstanceMethods, THelpers, PaginateOptions>>>;
82
+ interface Query<ResultType, DocType, THelpers = NonNullable<unknown>, RawDocType = DocType, _QueryOp = 'find', TInstanceMethods = Record<string, never>> {
83
+ paginate: {
84
+ <O extends PaginateOptions>(options?: O): Promise<PaginateResult<PaginateDocument<RawDocType, TInstanceMethods, THelpers, O>>>;
85
+ <UserType = ResultType, O extends PaginateOptions = PaginateOptions>(options?: O): Promise<PaginateResult<PaginateDocument<UserType, TInstanceMethods, THelpers, O>>>;
86
+ <UserType = ResultType>(options?: PaginateOptions): Promise<PaginateResult<PaginateDocument<UserType, TInstanceMethods, THelpers, PaginateOptions>>>;
87
+ };
79
88
  }
80
89
  }
81
90
  declare function _(schema: Schema): void;
package/types/utils.d.ts CHANGED
@@ -1 +1 @@
1
- export type Readonlyable<T> = T | Readonly<T>;
1
+ export type Readonlyable<T> = Readonly<T> | T;
package/utils.cjs CHANGED
@@ -3,6 +3,7 @@
3
3
  const mongoose = require('mongoose');
4
4
  const mongooseAggregatePaginate = require('mongoose-aggregate-paginate-v2');
5
5
  const mongoosePaginate = require('mongoose-paginate-v2');
6
+ const node_process = require('node:process');
6
7
  const _internals = require('./_internals.cjs');
7
8
  const constants = require('./constants.cjs');
8
9
  const normalize = require('./plugins/normalize.cjs');
@@ -47,7 +48,7 @@ function buildMongooseModel(collection, name, schema, options) {
47
48
  schema.plugin(mongoosePaginate__default);
48
49
  schema.set('timestamps', options?.timestamps ?? true);
49
50
  _internals.customMongooseOptions.beforeModelBuild?.(schema);
50
- return (options?.connection || constants.mongooseConnections.default || (constants.mongooseConnections.default = mongoose__default.createConnection(process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017'))).model(name, schema, collection);
51
+ return (options?.connection || constants.mongooseConnections.default || (constants.mongooseConnections.default = mongoose__default.createConnection(node_process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017'))).model(name, schema, collection);
51
52
  }
52
53
  /**
53
54
  * Converts a document or ObjectId to a document using the specified model.
package/utils.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import mongoose from 'mongoose';
2
- import { setCustomMongooseOptions } from './options';
2
+ import type { setCustomMongooseOptions } from './options';
3
3
  import type { BuildMongooseModelOptions } from './types/options';
4
4
  export type DoNotRemoveOrUseThisType = typeof setCustomMongooseOptions;
5
5
  /**
@@ -29,7 +29,7 @@ export type DoNotRemoveOrUseThisType = typeof setCustomMongooseOptions;
29
29
  *
30
30
  * @returns The created Mongoose model.
31
31
  */
32
- export declare function buildMongooseModel<DocType, Model extends BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>, InstanceMethodsAndOverrides = {}, QueryHelpers = {}>(collection: string, name: string, schema: mongoose.Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>, options?: BuildMongooseModelOptions): Model;
32
+ export declare function buildMongooseModel<DocType, Model extends BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>, InstanceMethodsAndOverrides = object, QueryHelpers = object>(collection: string, name: string, schema: mongoose.Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>, options?: BuildMongooseModelOptions): Model;
33
33
  /**
34
34
  * Converts a document or ObjectId to a document using the specified model.
35
35
  *
package/utils.mjs CHANGED
@@ -1,6 +1,7 @@
1
1
  import mongoose, { Types } from 'mongoose';
2
2
  import mongooseAggregatePaginate from 'mongoose-aggregate-paginate-v2';
3
3
  import mongoosePaginate from 'mongoose-paginate-v2';
4
+ import { env } from 'node:process';
4
5
  import { customMongooseOptions } from './_internals.mjs';
5
6
  import { mongooseConnections } from './constants.mjs';
6
7
  import { mongooseNormalizePlugin } from './plugins/normalize.mjs';
@@ -39,7 +40,7 @@ function buildMongooseModel(collection, name, schema, options) {
39
40
  schema.plugin(mongoosePaginate);
40
41
  schema.set('timestamps', options?.timestamps ?? true);
41
42
  customMongooseOptions.beforeModelBuild?.(schema);
42
- return (options?.connection || mongooseConnections.default || (mongooseConnections.default = mongoose.createConnection(process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017'))).model(name, schema, collection);
43
+ return (options?.connection || mongooseConnections.default || (mongooseConnections.default = mongoose.createConnection(env.MONGODB_URI || 'mongodb://127.0.0.1:27017'))).model(name, schema, collection);
43
44
  }
44
45
  /**
45
46
  * Converts a document or ObjectId to a document using the specified model.