@kikiutils/mongoose 3.0.1 → 3.0.3

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.
@@ -1,13 +1,9 @@
1
1
  'use strict';
2
2
 
3
- const Decimal = require('decimal.js');
3
+ const decimal_js = require('decimal.js');
4
4
  const mongoose = require('mongoose');
5
5
  const base = require('./base.cjs');
6
6
 
7
- const _interopDefaultCompat = e => e && typeof e === 'object' && 'default' in e ? e.default : e;
8
-
9
- const Decimal__default = /*#__PURE__*/_interopDefaultCompat(Decimal);
10
-
11
7
  const baseBuilderFactory = base.createBaseSchemaBuilderFactory(mongoose.Schema.Types.Decimal128);
12
8
  function decimal128SchemaBuilder() {
13
9
  const schema = {};
@@ -15,10 +11,10 @@ function decimal128SchemaBuilder() {
15
11
  return new Proxy(baseBuilder, {
16
12
  get(target, key, receiver) {
17
13
  if (key === 'setRoundAndToFixedSetter') {
18
- return (places = 2, rounding = Decimal__default.ROUND_DOWN) => {
14
+ return (places = 2, rounding = decimal_js.Decimal.ROUND_DOWN) => {
19
15
  schema.set = (value) => {
20
16
  if (value)
21
- return new Decimal__default(value.toString()).toFixed(places, rounding);
17
+ return new decimal_js.Decimal(value.toString()).toFixed(places, rounding);
22
18
  };
23
19
  return receiver;
24
20
  };
@@ -1 +1 @@
1
- {"version":3,"file":"decimal128.cjs","sources":["../../src/schema-builders/decimal128.ts"],"sourcesContent":["import Decimal from 'decimal.js';\nimport { Schema } from 'mongoose';\nimport type {\n DefaultType,\n IndexDirection,\n IndexOptions,\n Types,\n} from 'mongoose';\nimport type { Merge } from 'type-fest';\n\nimport type { Readonlyable } from '../types/utils';\n\nimport { createBaseSchemaBuilderFactory } from './base';\n\nexport type ExtendDecimal128SchemaBuilder<\n Props extends BaseProps,\n ExtraOmitFields extends string,\n> = Omit<\n Decimal128SchemaBuilder<Props, ExtraOmitFields>,\n ExtraOmitFields | keyof Props\n>;\n\ninterface BaseProps {\n type: typeof Schema.Types.Decimal128;\n}\n\ninterface ToStringGetterSchema {\n get: (value?: Types.Decimal128) => string | undefined;\n}\n\ninterface ToStringSetterSchema {\n set: (value?: { toString: () => string }) => string;\n}\n\nexport interface Decimal128SchemaBuilder<\n Props extends { type: typeof Schema.Types.Decimal128 } = { type: typeof Schema.Types.Decimal128 },\n ExtraOmitFields extends string = never,\n> {\n default: <\n T extends ((this: any, doc: any) => DefaultType<D>) | DefaultType<D> | null,\n D extends Types.Decimal128,\n >(value: T) => ExtendDecimal128SchemaBuilder<\n Merge<Props, { default: T }>,\n ExtraOmitFields\n >;\n\n enum: <\n T extends\n | Readonlyable<Array<D | null>>\n | { [path: string]: D | null }\n | { message?: M; values: Readonlyable<Array<D | null>> },\n D extends Types.Decimal128,\n M extends string,\n >(value: T) => ExtendDecimal128SchemaBuilder<\n Merge<Props, { enum: T }>,\n ExtraOmitFields\n >;\n\n index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendDecimal128SchemaBuilder<\n Merge<Props, { index: T }>,\n ExtraOmitFields\n >;\n\n nonRequired: Props;\n private: ExtendDecimal128SchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;\n required: Merge<Props, { required: true }>;\n\n /**\n * Sets the rounding and toFixed behavior for Decimal128 fields.\n * This method allows specifying the number of decimal places and the rounding strategy to be used when\n * saving the field to the database.\n *\n * @param places - The number of decimal places to round to (default is 2).\n * @param rounding - The rounding strategy from the Decimal.js library (default is Decimal.ROUND_DOWN).\n *\n * @returns A schema builder with the rounding and toFixed behavior applied to the `set` option in Mongoose.\n */\n setRoundAndToFixedSetter: (places?: number, rounding?: Decimal.Rounding) => ExtendDecimal128SchemaBuilder<\n Merge<Props, ToStringSetterSchema>,\n 'setRoundAndToFixedSetter' | ExtraOmitFields\n >;\n\n /**\n * Sets the toString behavior for Decimal128 fields.\n * This method defines how the Decimal128 field should be converted to a string when processing or\n * retrieving the value in the application (e.g., via getters), while still storing the value as Decimal128\n * in the database.\n *\n * @returns A schema builder with the custom `get` behavior applied to convert the Decimal128 field to a string\n * when accessed in the application.\n */\n setToStringGetter: ExtendDecimal128SchemaBuilder<\n Merge<Props, ToStringGetterSchema>,\n 'setToStringGetter' | ExtraOmitFields\n >;\n\n sparse: ExtendDecimal128SchemaBuilder<Merge<Props, { sparse: true }>, ExtraOmitFields>;\n unique: ExtendDecimal128SchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;\n}\n\nconst baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.Decimal128);\n\nexport function decimal128SchemaBuilder() {\n const schema: Record<string, any> = {};\n const baseBuilder = baseBuilderFactory(schema);\n return new Proxy(\n baseBuilder,\n {\n get(target, key, receiver) {\n if (key === 'setRoundAndToFixedSetter') {\n return (places: number = 2, rounding: Decimal.Rounding = Decimal.ROUND_DOWN) => {\n schema.set = (value?: { toString: () => string }) => {\n if (value) return new Decimal(value.toString()).toFixed(places, rounding);\n };\n\n return receiver;\n };\n }\n\n if (key === 'setToStringGetter') {\n schema.get = (value?: Types.Decimal128) => value?.toString();\n return receiver;\n }\n\n return Reflect.get(target, key, receiver);\n },\n },\n ) as Decimal128SchemaBuilder;\n}\n"],"names":["createBaseSchemaBuilderFactory","Schema","Decimal"],"mappings":";;;;;;;;;;AAoGA,MAAM,kBAAkB,GAAGA,mCAA8B,CAACC,eAAM,CAAC,KAAK,CAAC,UAAU,CAAC;SAElE,uBAAuB,GAAA;IACnC,MAAM,MAAM,GAAwB,EAAE;AACtC,IAAA,MAAM,WAAW,GAAG,kBAAkB,CAAC,MAAM,CAAC;AAC9C,IAAA,OAAO,IAAI,KAAK,CACZ,WAAW,EACX;AACI,QAAA,GAAG,CAAC,MAAM,EAAE,GAAG,EAAE,QAAQ,EAAA;AACrB,YAAA,IAAI,GAAG,KAAK,0BAA0B,EAAE;gBACpC,OAAO,CAAC,SAAiB,CAAC,EAAE,WAA6BC,gBAAO,CAAC,UAAU,KAAI;AAC3E,oBAAA,MAAM,CAAC,GAAG,GAAG,CAAC,KAAkC,KAAI;AAChD,wBAAA,IAAI,KAAK;AAAE,4BAAA,OAAO,IAAIA,gBAAO,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,QAAQ,CAAC;AAC7E,qBAAC;AAED,oBAAA,OAAO,QAAQ;AACnB,iBAAC;;AAGL,YAAA,IAAI,GAAG,KAAK,mBAAmB,EAAE;AAC7B,gBAAA,MAAM,CAAC,GAAG,GAAG,CAAC,KAAwB,KAAK,KAAK,EAAE,QAAQ,EAAE;AAC5D,gBAAA,OAAO,QAAQ;;YAGnB,OAAO,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,GAAG,EAAE,QAAQ,CAAC;SAC5C;AACJ,KAAA,CACuB;AAChC;;;;"}
1
+ {"version":3,"file":"decimal128.cjs","sources":["../../src/schema-builders/decimal128.ts"],"sourcesContent":["import { Decimal } from 'decimal.js';\nimport { Schema } from 'mongoose';\nimport type {\n DefaultType,\n IndexDirection,\n IndexOptions,\n Types,\n} from 'mongoose';\nimport type { Merge } from 'type-fest';\n\nimport type { Readonlyable } from '../types/utils';\n\nimport { createBaseSchemaBuilderFactory } from './base';\n\nexport type ExtendDecimal128SchemaBuilder<\n Props extends BaseProps,\n ExtraOmitFields extends string,\n> = Omit<\n Decimal128SchemaBuilder<Props, ExtraOmitFields>,\n ExtraOmitFields | keyof Props\n>;\n\ninterface BaseProps {\n type: typeof Schema.Types.Decimal128;\n}\n\ninterface ToStringGetterSchema {\n get: (value?: Types.Decimal128) => string | undefined;\n}\n\ninterface ToStringSetterSchema {\n set: (value?: { toString: () => string }) => string;\n}\n\nexport interface Decimal128SchemaBuilder<\n Props extends { type: typeof Schema.Types.Decimal128 } = { type: typeof Schema.Types.Decimal128 },\n ExtraOmitFields extends string = never,\n> {\n default: <\n T extends ((this: any, doc: any) => DefaultType<D>) | DefaultType<D> | null,\n D extends Types.Decimal128,\n >(value: T) => ExtendDecimal128SchemaBuilder<\n Merge<Props, { default: T }>,\n ExtraOmitFields\n >;\n\n enum: <\n T extends\n | Readonlyable<Array<D | null>>\n | { [path: string]: D | null }\n | { message?: M; values: Readonlyable<Array<D | null>> },\n D extends Types.Decimal128,\n M extends string,\n >(value: T) => ExtendDecimal128SchemaBuilder<\n Merge<Props, { enum: T }>,\n ExtraOmitFields\n >;\n\n index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendDecimal128SchemaBuilder<\n Merge<Props, { index: T }>,\n ExtraOmitFields\n >;\n\n nonRequired: Props;\n private: ExtendDecimal128SchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;\n required: Merge<Props, { required: true }>;\n\n /**\n * Sets the rounding and toFixed behavior for Decimal128 fields.\n * This method allows specifying the number of decimal places and the rounding strategy to be used when\n * saving the field to the database.\n *\n * @param places - The number of decimal places to round to (default is 2).\n * @param rounding - The rounding strategy from the Decimal.js library (default is Decimal.ROUND_DOWN).\n *\n * @returns A schema builder with the rounding and toFixed behavior applied to the `set` option in Mongoose.\n */\n setRoundAndToFixedSetter: (places?: number, rounding?: Decimal.Rounding) => ExtendDecimal128SchemaBuilder<\n Merge<Props, ToStringSetterSchema>,\n 'setRoundAndToFixedSetter' | ExtraOmitFields\n >;\n\n /**\n * Sets the toString behavior for Decimal128 fields.\n * This method defines how the Decimal128 field should be converted to a string when processing or\n * retrieving the value in the application (e.g., via getters), while still storing the value as Decimal128\n * in the database.\n *\n * @returns A schema builder with the custom `get` behavior applied to convert the Decimal128 field to a string\n * when accessed in the application.\n */\n setToStringGetter: ExtendDecimal128SchemaBuilder<\n Merge<Props, ToStringGetterSchema>,\n 'setToStringGetter' | ExtraOmitFields\n >;\n\n sparse: ExtendDecimal128SchemaBuilder<Merge<Props, { sparse: true }>, ExtraOmitFields>;\n unique: ExtendDecimal128SchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;\n}\n\nconst baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.Decimal128);\n\nexport function decimal128SchemaBuilder() {\n const schema: Record<string, any> = {};\n const baseBuilder = baseBuilderFactory(schema);\n return new Proxy(\n baseBuilder,\n {\n get(target, key, receiver) {\n if (key === 'setRoundAndToFixedSetter') {\n return (places: number = 2, rounding: Decimal.Rounding = Decimal.ROUND_DOWN) => {\n schema.set = (value?: { toString: () => string }) => {\n if (value) return new Decimal(value.toString()).toFixed(places, rounding);\n };\n\n return receiver;\n };\n }\n\n if (key === 'setToStringGetter') {\n schema.get = (value?: Types.Decimal128) => value?.toString();\n return receiver;\n }\n\n return Reflect.get(target, key, receiver);\n },\n },\n ) as Decimal128SchemaBuilder;\n}\n"],"names":["createBaseSchemaBuilderFactory","Schema","Decimal"],"mappings":";;;;;;AAoGA,MAAM,kBAAkB,GAAGA,mCAA8B,CAACC,eAAM,CAAC,KAAK,CAAC,UAAU,CAAC;SAElE,uBAAuB,GAAA;IACnC,MAAM,MAAM,GAAwB,EAAE;AACtC,IAAA,MAAM,WAAW,GAAG,kBAAkB,CAAC,MAAM,CAAC;AAC9C,IAAA,OAAO,IAAI,KAAK,CACZ,WAAW,EACX;AACI,QAAA,GAAG,CAAC,MAAM,EAAE,GAAG,EAAE,QAAQ,EAAA;AACrB,YAAA,IAAI,GAAG,KAAK,0BAA0B,EAAE;gBACpC,OAAO,CAAC,SAAiB,CAAC,EAAE,WAA6BC,kBAAO,CAAC,UAAU,KAAI;AAC3E,oBAAA,MAAM,CAAC,GAAG,GAAG,CAAC,KAAkC,KAAI;AAChD,wBAAA,IAAI,KAAK;AAAE,4BAAA,OAAO,IAAIA,kBAAO,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,QAAQ,CAAC;AAC7E,qBAAC;AAED,oBAAA,OAAO,QAAQ;AACnB,iBAAC;;AAGL,YAAA,IAAI,GAAG,KAAK,mBAAmB,EAAE;AAC7B,gBAAA,MAAM,CAAC,GAAG,GAAG,CAAC,KAAwB,KAAK,KAAK,EAAE,QAAQ,EAAE;AAC5D,gBAAA,OAAO,QAAQ;;YAGnB,OAAO,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,GAAG,EAAE,QAAQ,CAAC;SAC5C;AACJ,KAAA,CACuB;AAChC;;;;"}
@@ -1,4 +1,4 @@
1
- import Decimal from 'decimal.js';
1
+ import { Decimal } from 'decimal.js';
2
2
  import { Schema } from 'mongoose';
3
3
  import type { DefaultType, IndexDirection, IndexOptions, Types } from 'mongoose';
4
4
  import type { Merge } from 'type-fest';
@@ -1 +1 @@
1
- {"version":3,"file":"decimal128.d.ts","sourceRoot":"","sources":["../../src/schema-builders/decimal128.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,YAAY,CAAC;AACjC,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAClC,OAAO,KAAK,EACR,WAAW,EACX,cAAc,EACd,YAAY,EACZ,KAAK,EACR,MAAM,UAAU,CAAC;AAClB,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,WAAW,CAAC;AAEvC,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAInD,MAAM,MAAM,6BAA6B,CACrC,KAAK,SAAS,SAAS,EACvB,eAAe,SAAS,MAAM,IAC9B,IAAI,CACJ,uBAAuB,CAAC,KAAK,EAAE,eAAe,CAAC,EAC/C,eAAe,GAAG,MAAM,KAAK,CAChC,CAAC;AAEF,UAAU,SAAS;IACf,IAAI,EAAE,OAAO,MAAM,CAAC,KAAK,CAAC,UAAU,CAAC;CACxC;AAED,UAAU,oBAAoB;IAC1B,GAAG,EAAE,CAAC,KAAK,CAAC,EAAE,KAAK,CAAC,UAAU,KAAK,MAAM,GAAG,SAAS,CAAC;CACzD;AAED,UAAU,oBAAoB;IAC1B,GAAG,EAAE,CAAC,KAAK,CAAC,EAAE;QAAE,QAAQ,EAAE,MAAM,MAAM,CAAA;KAAE,KAAK,MAAM,CAAC;CACvD;AAED,MAAM,WAAW,uBAAuB,CACpC,KAAK,SAAS;IAAE,IAAI,EAAE,OAAO,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AAE,6BAA6B,CAAC,KAAK,CAAC,KAAK,EAAE;QAAE,MAAM,EAAE,IAAI,CAAA;KAAE,CAAC,EAAE,eAAe,CAAC,CAAC;CAC1F;AAID,wBAAgB,uBAAuB,IAyB9B,uBAAuB,CAC/B"}
@@ -1,4 +1,4 @@
1
- import Decimal from 'decimal.js';
1
+ import { Decimal } from 'decimal.js';
2
2
  import { Schema } from 'mongoose';
3
3
  import { createBaseSchemaBuilderFactory } from './base.mjs';
4
4
 
@@ -1 +1 @@
1
- {"version":3,"file":"decimal128.mjs","sources":["../../src/schema-builders/decimal128.ts"],"sourcesContent":["import Decimal from 'decimal.js';\nimport { Schema } from 'mongoose';\nimport type {\n DefaultType,\n IndexDirection,\n IndexOptions,\n Types,\n} from 'mongoose';\nimport type { Merge } from 'type-fest';\n\nimport type { Readonlyable } from '../types/utils';\n\nimport { createBaseSchemaBuilderFactory } from './base';\n\nexport type ExtendDecimal128SchemaBuilder<\n Props extends BaseProps,\n ExtraOmitFields extends string,\n> = Omit<\n Decimal128SchemaBuilder<Props, ExtraOmitFields>,\n ExtraOmitFields | keyof Props\n>;\n\ninterface BaseProps {\n type: typeof Schema.Types.Decimal128;\n}\n\ninterface ToStringGetterSchema {\n get: (value?: Types.Decimal128) => string | undefined;\n}\n\ninterface ToStringSetterSchema {\n set: (value?: { toString: () => string }) => string;\n}\n\nexport interface Decimal128SchemaBuilder<\n Props extends { type: typeof Schema.Types.Decimal128 } = { type: typeof Schema.Types.Decimal128 },\n ExtraOmitFields extends string = never,\n> {\n default: <\n T extends ((this: any, doc: any) => DefaultType<D>) | DefaultType<D> | null,\n D extends Types.Decimal128,\n >(value: T) => ExtendDecimal128SchemaBuilder<\n Merge<Props, { default: T }>,\n ExtraOmitFields\n >;\n\n enum: <\n T extends\n | Readonlyable<Array<D | null>>\n | { [path: string]: D | null }\n | { message?: M; values: Readonlyable<Array<D | null>> },\n D extends Types.Decimal128,\n M extends string,\n >(value: T) => ExtendDecimal128SchemaBuilder<\n Merge<Props, { enum: T }>,\n ExtraOmitFields\n >;\n\n index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendDecimal128SchemaBuilder<\n Merge<Props, { index: T }>,\n ExtraOmitFields\n >;\n\n nonRequired: Props;\n private: ExtendDecimal128SchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;\n required: Merge<Props, { required: true }>;\n\n /**\n * Sets the rounding and toFixed behavior for Decimal128 fields.\n * This method allows specifying the number of decimal places and the rounding strategy to be used when\n * saving the field to the database.\n *\n * @param places - The number of decimal places to round to (default is 2).\n * @param rounding - The rounding strategy from the Decimal.js library (default is Decimal.ROUND_DOWN).\n *\n * @returns A schema builder with the rounding and toFixed behavior applied to the `set` option in Mongoose.\n */\n setRoundAndToFixedSetter: (places?: number, rounding?: Decimal.Rounding) => ExtendDecimal128SchemaBuilder<\n Merge<Props, ToStringSetterSchema>,\n 'setRoundAndToFixedSetter' | ExtraOmitFields\n >;\n\n /**\n * Sets the toString behavior for Decimal128 fields.\n * This method defines how the Decimal128 field should be converted to a string when processing or\n * retrieving the value in the application (e.g., via getters), while still storing the value as Decimal128\n * in the database.\n *\n * @returns A schema builder with the custom `get` behavior applied to convert the Decimal128 field to a string\n * when accessed in the application.\n */\n setToStringGetter: ExtendDecimal128SchemaBuilder<\n Merge<Props, ToStringGetterSchema>,\n 'setToStringGetter' | ExtraOmitFields\n >;\n\n sparse: ExtendDecimal128SchemaBuilder<Merge<Props, { sparse: true }>, ExtraOmitFields>;\n unique: ExtendDecimal128SchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;\n}\n\nconst baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.Decimal128);\n\nexport function decimal128SchemaBuilder() {\n const schema: Record<string, any> = {};\n const baseBuilder = baseBuilderFactory(schema);\n return new Proxy(\n baseBuilder,\n {\n get(target, key, receiver) {\n if (key === 'setRoundAndToFixedSetter') {\n return (places: number = 2, rounding: Decimal.Rounding = Decimal.ROUND_DOWN) => {\n schema.set = (value?: { toString: () => string }) => {\n if (value) return new Decimal(value.toString()).toFixed(places, rounding);\n };\n\n return receiver;\n };\n }\n\n if (key === 'setToStringGetter') {\n schema.get = (value?: Types.Decimal128) => value?.toString();\n return receiver;\n }\n\n return Reflect.get(target, key, receiver);\n },\n },\n ) as Decimal128SchemaBuilder;\n}\n"],"names":[],"mappings":";;;;AAoGA,MAAM,kBAAkB,GAAG,8BAA8B,CAAC,MAAM,CAAC,KAAK,CAAC,UAAU,CAAC;SAElE,uBAAuB,GAAA;IACnC,MAAM,MAAM,GAAwB,EAAE;AACtC,IAAA,MAAM,WAAW,GAAG,kBAAkB,CAAC,MAAM,CAAC;AAC9C,IAAA,OAAO,IAAI,KAAK,CACZ,WAAW,EACX;AACI,QAAA,GAAG,CAAC,MAAM,EAAE,GAAG,EAAE,QAAQ,EAAA;AACrB,YAAA,IAAI,GAAG,KAAK,0BAA0B,EAAE;gBACpC,OAAO,CAAC,SAAiB,CAAC,EAAE,WAA6B,OAAO,CAAC,UAAU,KAAI;AAC3E,oBAAA,MAAM,CAAC,GAAG,GAAG,CAAC,KAAkC,KAAI;AAChD,wBAAA,IAAI,KAAK;AAAE,4BAAA,OAAO,IAAI,OAAO,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,QAAQ,CAAC;AAC7E,qBAAC;AAED,oBAAA,OAAO,QAAQ;AACnB,iBAAC;;AAGL,YAAA,IAAI,GAAG,KAAK,mBAAmB,EAAE;AAC7B,gBAAA,MAAM,CAAC,GAAG,GAAG,CAAC,KAAwB,KAAK,KAAK,EAAE,QAAQ,EAAE;AAC5D,gBAAA,OAAO,QAAQ;;YAGnB,OAAO,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,GAAG,EAAE,QAAQ,CAAC;SAC5C;AACJ,KAAA,CACuB;AAChC;;;;"}
1
+ {"version":3,"file":"decimal128.mjs","sources":["../../src/schema-builders/decimal128.ts"],"sourcesContent":["import { Decimal } from 'decimal.js';\nimport { Schema } from 'mongoose';\nimport type {\n DefaultType,\n IndexDirection,\n IndexOptions,\n Types,\n} from 'mongoose';\nimport type { Merge } from 'type-fest';\n\nimport type { Readonlyable } from '../types/utils';\n\nimport { createBaseSchemaBuilderFactory } from './base';\n\nexport type ExtendDecimal128SchemaBuilder<\n Props extends BaseProps,\n ExtraOmitFields extends string,\n> = Omit<\n Decimal128SchemaBuilder<Props, ExtraOmitFields>,\n ExtraOmitFields | keyof Props\n>;\n\ninterface BaseProps {\n type: typeof Schema.Types.Decimal128;\n}\n\ninterface ToStringGetterSchema {\n get: (value?: Types.Decimal128) => string | undefined;\n}\n\ninterface ToStringSetterSchema {\n set: (value?: { toString: () => string }) => string;\n}\n\nexport interface Decimal128SchemaBuilder<\n Props extends { type: typeof Schema.Types.Decimal128 } = { type: typeof Schema.Types.Decimal128 },\n ExtraOmitFields extends string = never,\n> {\n default: <\n T extends ((this: any, doc: any) => DefaultType<D>) | DefaultType<D> | null,\n D extends Types.Decimal128,\n >(value: T) => ExtendDecimal128SchemaBuilder<\n Merge<Props, { default: T }>,\n ExtraOmitFields\n >;\n\n enum: <\n T extends\n | Readonlyable<Array<D | null>>\n | { [path: string]: D | null }\n | { message?: M; values: Readonlyable<Array<D | null>> },\n D extends Types.Decimal128,\n M extends string,\n >(value: T) => ExtendDecimal128SchemaBuilder<\n Merge<Props, { enum: T }>,\n ExtraOmitFields\n >;\n\n index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendDecimal128SchemaBuilder<\n Merge<Props, { index: T }>,\n ExtraOmitFields\n >;\n\n nonRequired: Props;\n private: ExtendDecimal128SchemaBuilder<Merge<Props, { private: true }>, ExtraOmitFields>;\n required: Merge<Props, { required: true }>;\n\n /**\n * Sets the rounding and toFixed behavior for Decimal128 fields.\n * This method allows specifying the number of decimal places and the rounding strategy to be used when\n * saving the field to the database.\n *\n * @param places - The number of decimal places to round to (default is 2).\n * @param rounding - The rounding strategy from the Decimal.js library (default is Decimal.ROUND_DOWN).\n *\n * @returns A schema builder with the rounding and toFixed behavior applied to the `set` option in Mongoose.\n */\n setRoundAndToFixedSetter: (places?: number, rounding?: Decimal.Rounding) => ExtendDecimal128SchemaBuilder<\n Merge<Props, ToStringSetterSchema>,\n 'setRoundAndToFixedSetter' | ExtraOmitFields\n >;\n\n /**\n * Sets the toString behavior for Decimal128 fields.\n * This method defines how the Decimal128 field should be converted to a string when processing or\n * retrieving the value in the application (e.g., via getters), while still storing the value as Decimal128\n * in the database.\n *\n * @returns A schema builder with the custom `get` behavior applied to convert the Decimal128 field to a string\n * when accessed in the application.\n */\n setToStringGetter: ExtendDecimal128SchemaBuilder<\n Merge<Props, ToStringGetterSchema>,\n 'setToStringGetter' | ExtraOmitFields\n >;\n\n sparse: ExtendDecimal128SchemaBuilder<Merge<Props, { sparse: true }>, ExtraOmitFields>;\n unique: ExtendDecimal128SchemaBuilder<Merge<Props, { unique: true }>, ExtraOmitFields>;\n}\n\nconst baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.Decimal128);\n\nexport function decimal128SchemaBuilder() {\n const schema: Record<string, any> = {};\n const baseBuilder = baseBuilderFactory(schema);\n return new Proxy(\n baseBuilder,\n {\n get(target, key, receiver) {\n if (key === 'setRoundAndToFixedSetter') {\n return (places: number = 2, rounding: Decimal.Rounding = Decimal.ROUND_DOWN) => {\n schema.set = (value?: { toString: () => string }) => {\n if (value) return new Decimal(value.toString()).toFixed(places, rounding);\n };\n\n return receiver;\n };\n }\n\n if (key === 'setToStringGetter') {\n schema.get = (value?: Types.Decimal128) => value?.toString();\n return receiver;\n }\n\n return Reflect.get(target, key, receiver);\n },\n },\n ) as Decimal128SchemaBuilder;\n}\n"],"names":[],"mappings":";;;;AAoGA,MAAM,kBAAkB,GAAG,8BAA8B,CAAC,MAAM,CAAC,KAAK,CAAC,UAAU,CAAC;SAElE,uBAAuB,GAAA;IACnC,MAAM,MAAM,GAAwB,EAAE;AACtC,IAAA,MAAM,WAAW,GAAG,kBAAkB,CAAC,MAAM,CAAC;AAC9C,IAAA,OAAO,IAAI,KAAK,CACZ,WAAW,EACX;AACI,QAAA,GAAG,CAAC,MAAM,EAAE,GAAG,EAAE,QAAQ,EAAA;AACrB,YAAA,IAAI,GAAG,KAAK,0BAA0B,EAAE;gBACpC,OAAO,CAAC,SAAiB,CAAC,EAAE,WAA6B,OAAO,CAAC,UAAU,KAAI;AAC3E,oBAAA,MAAM,CAAC,GAAG,GAAG,CAAC,KAAkC,KAAI;AAChD,wBAAA,IAAI,KAAK;AAAE,4BAAA,OAAO,IAAI,OAAO,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,QAAQ,CAAC;AAC7E,qBAAC;AAED,oBAAA,OAAO,QAAQ;AACnB,iBAAC;;AAGL,YAAA,IAAI,GAAG,KAAK,mBAAmB,EAAE;AAC7B,gBAAA,MAAM,CAAC,GAAG,GAAG,CAAC,KAAwB,KAAK,KAAK,EAAE,QAAQ,EAAE;AAC5D,gBAAA,OAAO,QAAQ;;YAGnB,OAAO,OAAO,CAAC,GAAG,CAAC,MAAM,EAAE,GAAG,EAAE,QAAQ,CAAC;SAC5C;AACJ,KAAA,CACuB;AAChC;;;;"}
@@ -1,53 +1,18 @@
1
1
  import type { IfElse } from './utils';
2
- /**
3
- * Interface for the base data of a Mongoose model, optionally including timestamp fields.
4
- *
5
- * This interface extends `LocalWithTimestampFieldsData` to optionally include `createdAt`
6
- * and `updatedAt` fields based on the `CreatedAtField` and `UpdatedAtField` flags.
7
- * It also includes a mandatory `id` field.
8
- *
9
- * @template CreatedAtField - A boolean flag indicating whether the `createdAt` field should be included.
10
- * @template UpdatedAtField - A boolean flag indicating whether the `updatedAt` field should be included.
11
- */
12
- interface LocalBaseMongooseModelData<CreatedAtField extends boolean = true, UpdatedAtField extends boolean = true> extends LocalWithTimestampFieldsData<CreatedAtField, UpdatedAtField> {
13
- id: string;
14
- }
15
- /**
16
- * Interface for including timestamp fields conditionally based on the provided flags.
17
- *
18
- * This interface conditionally includes `createdAt` and `updatedAt` fields based on
19
- * the `CreatedAt` and `UpdatedAt` boolean flags. If the flag is true, the respective
20
- * field is included, otherwise it is set to `never`.
21
- *
22
- * @template CreatedAt - A boolean flag indicating whether the `createdAt` field should be included.
23
- * @template UpdatedAt - A boolean flag indicating whether the `updatedAt` field should be included.
24
- */
25
- interface LocalWithTimestampFieldsData<CreatedAt extends boolean = true, UpdatedAt extends boolean = true> {
26
- createdAt: IfElse<CreatedAt, string, never>;
27
- updatedAt: IfElse<UpdatedAt, string, never>;
28
- }
29
- /**
30
- * Utility type to omit properties that are set to `never` in a given type.
31
- *
32
- * This type alias iterates over the properties of a given type `T` and removes any
33
- * properties that are set to `never`.
34
- *
35
- * @template T - The type from which to omit `never` properties.
36
- */
37
- type OmitNever<T> = {
38
- [P in keyof T as IfElse<T[P], never, P, never>]: T[P];
39
- };
40
2
  declare global {
41
3
  /**
42
4
  * Type definition for the base data of a Mongoose model, optionally including timestamp fields.
43
5
  *
44
- * This type alias uses `OmitNever` to remove any `never` properties from `LocalBaseMongooseModelData`
45
- * based on the presence of the `CreatedAtField` and `UpdatedAtField` flags.
46
- *
47
6
  * @template CreatedAtField - A boolean flag indicating whether the `createdAt` field should be included.
48
7
  * @template UpdatedAtField - A boolean flag indicating whether the `updatedAt` field should be included.
49
8
  */
50
- type BaseMongooseModelData<CreatedAtField extends boolean = true, UpdatedAtField extends boolean = true> = OmitNever<LocalBaseMongooseModelData<CreatedAtField, UpdatedAtField>>;
9
+ type BaseMongooseModelData<CreatedAtField extends boolean = true, UpdatedAtField extends boolean = true> = IfElse<CreatedAtField, {
10
+ createdAt: string;
11
+ }, unknown> & IfElse<UpdatedAtField, {
12
+ updatedAt: string;
13
+ }, unknown> & {
14
+ id: string;
15
+ };
51
16
  /**
52
17
  * Type definition to omit timestamp fields (`createdAt`, `updatedAt`)
53
18
  * and other specified fields (`OT`) from a given type `T`.
@@ -67,13 +32,13 @@ declare global {
67
32
  /**
68
33
  * Type definition for including timestamp fields conditionally based on the provided flags.
69
34
  *
70
- * This type alias uses `OmitNever` to remove any `never` properties from `LocalWithTimestampFieldsData`
71
- * based on the presence of the `CreatedAt` and `UpdatedAt` flags.
72
- *
73
35
  * @template CreatedAt - A boolean flag indicating whether the `createdAt` field should be included.
74
36
  * @template UpdatedAt - A boolean flag indicating whether the `updatedAt` field should be included.
75
37
  */
76
- type WithMongooseTimestampFieldsData<CreatedAt extends boolean = true, UpdatedAt extends boolean = true> = OmitNever<LocalWithTimestampFieldsData<CreatedAt, UpdatedAt>>;
38
+ type WithMongooseTimestampFieldsData<CreatedAt extends boolean = true, UpdatedAt extends boolean = true> = IfElse<CreatedAt, {
39
+ createdAt: string;
40
+ }, object> & IfElse<UpdatedAt, {
41
+ updatedAt: string;
42
+ }, object>;
77
43
  }
78
- export {};
79
44
  //# sourceMappingURL=data.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"data.d.ts","sourceRoot":"","sources":["../../src/types/data.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,SAAS,CAAC;AAEtC;;;;;;;;;GASG;AACH,UAAU,0BAA0B,CAChC,cAAc,SAAS,OAAO,GAAG,IAAI,EACrC,cAAc,SAAS,OAAO,GAAG,IAAI,CACvC,SAAQ,4BAA4B,CAAC,cAAc,EAAE,cAAc,CAAC;IAClE,EAAE,EAAE,MAAM,CAAC;CACd;AAED;;;;;;;;;GASG;AACH,UAAU,4BAA4B,CAAC,SAAS,SAAS,OAAO,GAAG,IAAI,EAAE,SAAS,SAAS,OAAO,GAAG,IAAI;IACrG,SAAS,EAAE,MAAM,CAAC,SAAS,EAAE,MAAM,EAAE,KAAK,CAAC,CAAC;IAC5C,SAAS,EAAE,MAAM,CAAC,SAAS,EAAE,MAAM,EAAE,KAAK,CAAC,CAAC;CAC/C;AAED;;;;;;;GAOG;AACH,KAAK,SAAS,CAAC,CAAC,IAAI;KAAG,CAAC,IAAI,MAAM,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C,GACtD,MAAM,CAAC,cAAc,EAAE;QAAE,SAAS,EAAE,MAAM,CAAA;KAAE,EAAE,OAAO,CAAC,GACtD;QAAE,EAAE,EAAE,MAAM,CAAA;KAAE,CAAC;IAEnB;;;;;;OAMG;IACH,KAAK,qCAAqC,CAAC,CAAC,EAAE,EAAE,SAAS,MAAM,CAAC,IAAI,IAAI,CAAC,CAAC,EAAE,WAAW,GAAG,WAAW,GAAG,EAAE,CAAC,CAAC;IAE5G;;;;;;OAMG;IACH,KAAK,mCAAmC,CACpC,CAAC,EACD,EAAE,SAAS,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,KAAK,IAC3C,IAAI,CACJ,CAAC,EACD,WAAW,GAAG,WAAW,GAAG,EAAE,CACjC,CAAC;IAEF;;;;;OAKG;IACH,KAAK,+BAA+B,CAChC,SAAS,SAAS,OAAO,GAAG,IAAI,EAChC,SAAS,SAAS,OAAO,GAAG,IAAI,IAEhC,MAAM,CAAC,SAAS,EAAE;QAAE,SAAS,EAAE,MAAM,CAAA;KAAE,EAAE,MAAM,CAAC,GAChD,MAAM,CAAC,SAAS,EAAE;QAAE,SAAS,EAAE,MAAM,CAAA;KAAE,EAAE,MAAM,CAAC,CAAC;CACxD"}
@@ -21,9 +21,9 @@ declare global {
21
21
  */
22
22
  type BaseMongooseDocType<T, CreatedAtField extends boolean = true, UpdatedAtField extends boolean = true> = IfElse<CreatedAtField, {
23
23
  createdAt: Date;
24
- }, object> & IfElse<UpdatedAtField, {
24
+ }, unknown> & IfElse<UpdatedAtField, {
25
25
  updatedAt: Date;
26
- }, object> & Omit<T, 'createdAt' | 'id' | 'updatedAt'>;
26
+ }, unknown> & Omit<T, 'createdAt' | 'id' | 'updatedAt'>;
27
27
  /**
28
28
  * Type definition for a Mongoose model with pagination and aggregation capabilities.
29
29
  *
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACR,sBA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;;;;;;;;OASG;IACH,KAAK,yBAAyB,CAC1B,UAAU,EACV,2BAA2B,GAAG,MAAM,EACpC,YAAY,GAAG,MAAM,IAErB,sBAAsB,CAAC,UAAU,EAAE,YAAY,EAAE,2BAA2B,CAAC,GAC7E,gBAAgB,CAAC,UAAU,EAAE,2BAA2B,EAAE,YAAY,CAAC,GACvE,aAAa,CAAC,UAAU,EAAE,YAAY,EAAE,2BAA2B,CAAC,CAAC;IAEzE;;;;;;;;OAQG;IACH,KAAK,0BAA0B,CAAC,CAAC,IAAI,CAAC,GAAG,MAAM,GAAG,KAAK,CAAC,QAAQ,CAAC;IAEjE;;;;;;;;;;;;;;OAcG;IACH,KAAK,yBAAyB,CAC1B,UAAU,EACV,OAAO,EACP,YAAY,GAAG,MAAM,EACrB,2BAA2B,GAAG,MAAM,IACpC,gBAAgB,CAChB,OAAO,GAAG,IAAI,EACd,OAAO,EACP,YAAY,EACZ,UAAU,EACV,SAAS,EACT,2BAA2B,CAC9B,CAAC;IAEF;;;;;;;;;;;OAWG;IACH,KAAK,wBAAwB,CACzB,OAAO,EACP,2BAA2B,GAAG,MAAM,EACpC,YAAY,GAAG,MAAM,IACrB,gBAAgB,CAChB,OAAO,EACP,2BAA2B,EAC3B,YAAY,CACf,CAAC;CACL"}
package/dist/utils.cjs CHANGED
@@ -9,6 +9,7 @@ const normalize = require('./plugins/normalize.cjs');
9
9
 
10
10
  const _interopDefaultCompat = e => e && typeof e === 'object' && 'default' in e ? e.default : e;
11
11
 
12
+ const mongoose__default = /*#__PURE__*/_interopDefaultCompat(mongoose);
12
13
  const mongooseAggregatePaginate__default = /*#__PURE__*/_interopDefaultCompat(mongooseAggregatePaginate);
13
14
  const mongoosePaginate__default = /*#__PURE__*/_interopDefaultCompat(mongoosePaginate);
14
15
 
@@ -70,7 +71,7 @@ function buildMongooseModel(collection, name, schema, options) {
70
71
  schema.set('timestamps', options?.timestamps ?? schema.get('timestamps') ?? true);
71
72
  _internals.customMongooseOptions.beforeModelBuild?.(schema);
72
73
  // eslint-disable-next-line style/max-len
73
- const connection = options?.connection || (constants.mongooseConnections.default ||= mongoose.createConnection(process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017'));
74
+ const connection = options?.connection || (constants.mongooseConnections.default ||= mongoose__default.createConnection(process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017'));
74
75
  return connection.model(name, schema, collection);
75
76
  }
76
77
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"utils.cjs","sources":["../src/utils.ts"],"sourcesContent":["import {\n createConnection,\n Types,\n} from 'mongoose';\nimport type {\n mongo,\n Schema,\n} from 'mongoose';\nimport mongooseAggregatePaginate from 'mongoose-aggregate-paginate-v2';\nimport mongoosePaginate from 'mongoose-paginate-v2';\n\nimport { customMongooseOptions } from './_internals';\nimport { mongooseConnections } from './constants';\nimport type { setCustomMongooseOptions } from './options';\nimport { mongooseNormalizePlugin } from './plugins/normalize';\nimport type { BuildMongooseModelOptions } from './types/options';\n\nexport type DoNotRemoveOrUseThisType = typeof setCustomMongooseOptions;\n\n/**\n * Asserts that a Mongoose update operation was successful by checking that it was acknowledged\n * and that at least the expected number of documents were modified.\n *\n * This function is intended for use with `updateOne`, `updateMany`, or any update operation\n * that returns a MongoDB `UpdateResult`.\n *\n * @template T - The shape of the document being updated.\n * @param updatePromise - A promise returned from a Mongoose update operation.\n * @param expectedModifiedCount - The minimum number of documents expected to be modified. Defaults to 1.\n * @throws Will throw an error if:\n * - The operation was not acknowledged by MongoDB,\n * - Fewer documents were modified than expected.\n */\nexport async function assertMongooseUpdateSuccess<T extends mongo.BSON.Document>(\n updatePromise: Promise<mongo.UpdateResult<T>>,\n expectedModifiedCount = 1,\n) {\n const updateResult = await updatePromise;\n if (!updateResult.acknowledged) throw new Error('Update was not acknowledged.');\n if (updateResult.modifiedCount < expectedModifiedCount) {\n // eslint-disable-next-line style/max-len\n throw new Error(`Expected to modify at least ${expectedModifiedCount} document(s), but modified ${updateResult.modifiedCount}.`);\n }\n}\n\n/**\n * Builds and returns a Mongoose model with specified schema and options.\n *\n * This function sets up a Mongoose model with optional plugins and timestamp settings.\n * It can optionally use a specified connection or the default connection.\n *\n * Determine the connection to use:\n * 1. Use the connection provided in the options, if available.\n * 2. Otherwise, use the default connection stored in `mongooseConnections`.\n * 3. If there's no default connection, create one using the MongoDB URI from the environment variables,\n * or fallback to a local MongoDB instance.\n *\n * You can set a function to be executed before the final build by using {@link setCustomMongooseOptions}.\n * However, ensure that this option is configured before calling {@link buildMongooseModel}.\n *\n * @template DocType - The type of the document.\n * @template Model - The type of the model, which extends `BaseMongoosePaginateModel`.\n * @template InstanceMethodsAndOverrides - The type for instance methods and overrides.\n * @template QueryHelpers - The type for additional query helpers.\n *\n * @param collection - The name of the collection.\n * @param name - The name of the model.\n * @param schema - The Mongoose schema to use for the model.\n * @param options - Optional settings for building the model.\n *\n * @returns The created Mongoose model.\n */\nexport function buildMongooseModel<\n DocType,\n Model extends BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>,\n InstanceMethodsAndOverrides = object,\n QueryHelpers = object,\n>(\n collection: string,\n name: string,\n schema: Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>,\n options?: BuildMongooseModelOptions,\n) {\n if (options?.enableNormalizePlugin !== false) schema.plugin(mongooseNormalizePlugin);\n schema.plugin(mongooseAggregatePaginate);\n schema.plugin(mongoosePaginate);\n schema.set('timestamps', options?.timestamps ?? schema.get('timestamps') ?? true);\n customMongooseOptions.beforeModelBuild?.(schema);\n // eslint-disable-next-line style/max-len\n const connection = options?.connection || (mongooseConnections.default ||= createConnection(process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017'));\n return connection.model<DocType, Model, QueryHelpers>(name, schema, collection);\n}\n\n/**\n * Converts a document or ObjectId to a document using the specified model.\n *\n * This function takes a document or ObjectId and returns the corresponding document\n * from the database using the specified model. Optionally, specific fields can be selected.\n *\n * @template D - The type of the hydrated document.\n * @template DocType - The type of the document.\n * @template InstanceMethodsAndOverrides - The type for instance methods and overrides.\n * @template QueryHelpers - The type for additional query helpers.\n *\n * @param documentOrObjectId - The document or ObjectId to convert.\n * @param model - The Mongoose model to use for the conversion.\n * @param selectFields - Optional fields to select from the document.\n *\n * @returns The corresponding document or null if not found.\n */\nexport async function mongooseDocumentOrObjectIdToDocument<\n D extends MongooseHydratedDocument<DocType, InstanceMethodsAndOverrides, QueryHelpers>,\n DocType,\n InstanceMethodsAndOverrides,\n QueryHelpers,\n>(\n documentOrObjectId: MongooseDocumentOrObjectId<D>,\n model: BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>,\n selectFields?: string[],\n): Promise<D | null> {\n if (typeof documentOrObjectId === 'string' || documentOrObjectId instanceof Types.ObjectId) {\n return (await model.findById(documentOrObjectId).select(selectFields || [])) as D | null;\n }\n\n return documentOrObjectId;\n}\n"],"names":["mongooseNormalizePlugin","mongooseAggregatePaginate","mongoosePaginate","customMongooseOptions","mongooseConnections","createConnection","Types"],"mappings":";;;;;;;;;;;;;;AAmBA;;;;;;;;;;;;;AAaG;AACI,eAAe,2BAA2B,CAC7C,aAA6C,EAC7C,qBAAqB,GAAG,CAAC,EAAA;AAEzB,IAAA,MAAM,YAAY,GAAG,MAAM,aAAa;IACxC,IAAI,CAAC,YAAY,CAAC,YAAY;AAAE,QAAA,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC;AAC/E,IAAA,IAAI,YAAY,CAAC,aAAa,GAAG,qBAAqB,EAAE;;QAEpD,MAAM,IAAI,KAAK,CAAC,CAA+B,4BAAA,EAAA,qBAAqB,CAA8B,2BAAA,EAAA,YAAY,CAAC,aAAa,CAAG,CAAA,CAAA,CAAC;;AAExI;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;AA0BG;AACG,SAAU,kBAAkB,CAM9B,UAAkB,EAClB,IAAY,EACZ,MAAyE,EACzE,OAAmC,EAAA;AAEnC,IAAA,IAAI,OAAO,EAAE,qBAAqB,KAAK,KAAK;AAAE,QAAA,MAAM,CAAC,MAAM,CAACA,iCAAuB,CAAC;AACpF,IAAA,MAAM,CAAC,MAAM,CAACC,kCAAyB,CAAC;AACxC,IAAA,MAAM,CAAC,MAAM,CAACC,yBAAgB,CAAC;AAC/B,IAAA,MAAM,CAAC,GAAG,CAAC,YAAY,EAAE,OAAO,EAAE,UAAU,IAAI,MAAM,CAAC,GAAG,CAAC,YAAY,CAAC,IAAI,IAAI,CAAC;AACjF,IAAAC,gCAAqB,CAAC,gBAAgB,GAAG,MAAM,CAAC;;IAEhD,MAAM,UAAU,GAAG,OAAO,EAAE,UAAU,KAAKC,6BAAmB,CAAC,OAAO,KAAKC,yBAAgB,CAAC,OAAO,CAAC,GAAG,CAAC,WAAW,IAAI,2BAA2B,CAAC,CAAC;IACpJ,OAAO,UAAU,CAAC,KAAK,CAA+B,IAAI,EAAE,MAAM,EAAE,UAAU,CAAC;AACnF;AAEA;;;;;;;;;;;;;;;;AAgBG;AACI,eAAe,oCAAoC,CAMtD,kBAAiD,EACjD,KAAoF,EACpF,YAAuB,EAAA;IAEvB,IAAI,OAAO,kBAAkB,KAAK,QAAQ,IAAI,kBAAkB,YAAYC,cAAK,CAAC,QAAQ,EAAE;AACxF,QAAA,QAAQ,MAAM,KAAK,CAAC,QAAQ,CAAC,kBAAkB,CAAC,CAAC,MAAM,CAAC,YAAY,IAAI,EAAE,CAAC;;AAG/E,IAAA,OAAO,kBAAkB;AAC7B;;;;;;"}
1
+ {"version":3,"file":"utils.cjs","sources":["../src/utils.ts"],"sourcesContent":["import mongoose, { Types } from 'mongoose';\nimport type {\n mongo,\n Schema,\n} from 'mongoose';\nimport mongooseAggregatePaginate from 'mongoose-aggregate-paginate-v2';\nimport mongoosePaginate from 'mongoose-paginate-v2';\n\nimport { customMongooseOptions } from './_internals';\nimport { mongooseConnections } from './constants';\nimport type { setCustomMongooseOptions } from './options';\nimport { mongooseNormalizePlugin } from './plugins/normalize';\nimport type { BuildMongooseModelOptions } from './types/options';\n\nexport type DoNotRemoveOrUseThisType = typeof setCustomMongooseOptions;\n\n/**\n * Asserts that a Mongoose update operation was successful by checking that it was acknowledged\n * and that at least the expected number of documents were modified.\n *\n * This function is intended for use with `updateOne`, `updateMany`, or any update operation\n * that returns a MongoDB `UpdateResult`.\n *\n * @template T - The shape of the document being updated.\n * @param updatePromise - A promise returned from a Mongoose update operation.\n * @param expectedModifiedCount - The minimum number of documents expected to be modified. Defaults to 1.\n * @throws Will throw an error if:\n * - The operation was not acknowledged by MongoDB,\n * - Fewer documents were modified than expected.\n */\nexport async function assertMongooseUpdateSuccess<T extends mongo.BSON.Document>(\n updatePromise: Promise<mongo.UpdateResult<T>>,\n expectedModifiedCount = 1,\n) {\n const updateResult = await updatePromise;\n if (!updateResult.acknowledged) throw new Error('Update was not acknowledged.');\n if (updateResult.modifiedCount < expectedModifiedCount) {\n // eslint-disable-next-line style/max-len\n throw new Error(`Expected to modify at least ${expectedModifiedCount} document(s), but modified ${updateResult.modifiedCount}.`);\n }\n}\n\n/**\n * Builds and returns a Mongoose model with specified schema and options.\n *\n * This function sets up a Mongoose model with optional plugins and timestamp settings.\n * It can optionally use a specified connection or the default connection.\n *\n * Determine the connection to use:\n * 1. Use the connection provided in the options, if available.\n * 2. Otherwise, use the default connection stored in `mongooseConnections`.\n * 3. If there's no default connection, create one using the MongoDB URI from the environment variables,\n * or fallback to a local MongoDB instance.\n *\n * You can set a function to be executed before the final build by using {@link setCustomMongooseOptions}.\n * However, ensure that this option is configured before calling {@link buildMongooseModel}.\n *\n * @template DocType - The type of the document.\n * @template Model - The type of the model, which extends `BaseMongoosePaginateModel`.\n * @template InstanceMethodsAndOverrides - The type for instance methods and overrides.\n * @template QueryHelpers - The type for additional query helpers.\n *\n * @param collection - The name of the collection.\n * @param name - The name of the model.\n * @param schema - The Mongoose schema to use for the model.\n * @param options - Optional settings for building the model.\n *\n * @returns The created Mongoose model.\n */\nexport function buildMongooseModel<\n DocType,\n Model extends BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>,\n InstanceMethodsAndOverrides = object,\n QueryHelpers = object,\n>(\n collection: string,\n name: string,\n schema: Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>,\n options?: BuildMongooseModelOptions,\n) {\n if (options?.enableNormalizePlugin !== false) schema.plugin(mongooseNormalizePlugin);\n schema.plugin(mongooseAggregatePaginate);\n schema.plugin(mongoosePaginate);\n schema.set('timestamps', options?.timestamps ?? schema.get('timestamps') ?? true);\n customMongooseOptions.beforeModelBuild?.(schema);\n // eslint-disable-next-line style/max-len\n const connection = options?.connection || (mongooseConnections.default ||= mongoose.createConnection(process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017'));\n return connection.model<DocType, Model, QueryHelpers>(name, schema, collection);\n}\n\n/**\n * Converts a document or ObjectId to a document using the specified model.\n *\n * This function takes a document or ObjectId and returns the corresponding document\n * from the database using the specified model. Optionally, specific fields can be selected.\n *\n * @template D - The type of the hydrated document.\n * @template DocType - The type of the document.\n * @template InstanceMethodsAndOverrides - The type for instance methods and overrides.\n * @template QueryHelpers - The type for additional query helpers.\n *\n * @param documentOrObjectId - The document or ObjectId to convert.\n * @param model - The Mongoose model to use for the conversion.\n * @param selectFields - Optional fields to select from the document.\n *\n * @returns The corresponding document or null if not found.\n */\nexport async function mongooseDocumentOrObjectIdToDocument<\n D extends MongooseHydratedDocument<DocType, InstanceMethodsAndOverrides, QueryHelpers>,\n DocType,\n InstanceMethodsAndOverrides,\n QueryHelpers,\n>(\n documentOrObjectId: MongooseDocumentOrObjectId<D>,\n model: BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>,\n selectFields?: string[],\n) {\n if (typeof documentOrObjectId === 'string' || documentOrObjectId instanceof Types.ObjectId) {\n return (await model.findById(documentOrObjectId).select(selectFields || [])) as D | null;\n }\n\n return documentOrObjectId;\n}\n"],"names":["mongooseNormalizePlugin","mongooseAggregatePaginate","mongoosePaginate","customMongooseOptions","mongooseConnections","mongoose","Types"],"mappings":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oC,CACtD,CAAC,SAAS,wBAAwB,CAAC,OAAO,EAAE,2BAA2B,EAAE,YAAY,CAAC,EACtF,OAAO,EACP,2BAA2B,EAC3B,YAAY,EAEZ,kBAAkB,EAAE,0BAA0B,CAAC,CAAC,CAAC,EACjD,KAAK,EAAE,yBAAyB,CAAC,OAAO,EAAE,2BAA2B,EAAE,YAAY,CAAC,EACpF,YAAY,CAAC,EAAE,MAAM,EAAE,qBAO1B"}
package/dist/utils.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { createConnection, Types } from 'mongoose';
1
+ import mongoose, { Types } from 'mongoose';
2
2
  import mongooseAggregatePaginate from 'mongoose-aggregate-paginate-v2';
3
3
  import mongoosePaginate from 'mongoose-paginate-v2';
4
4
  import { customMongooseOptions } from './_internals.mjs';
@@ -63,7 +63,7 @@ function buildMongooseModel(collection, name, schema, options) {
63
63
  schema.set('timestamps', options?.timestamps ?? schema.get('timestamps') ?? true);
64
64
  customMongooseOptions.beforeModelBuild?.(schema);
65
65
  // eslint-disable-next-line style/max-len
66
- const connection = options?.connection || (mongooseConnections.default ||= createConnection(process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017'));
66
+ const connection = options?.connection || (mongooseConnections.default ||= mongoose.createConnection(process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017'));
67
67
  return connection.model(name, schema, collection);
68
68
  }
69
69
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"utils.mjs","sources":["../src/utils.ts"],"sourcesContent":["import {\n createConnection,\n Types,\n} from 'mongoose';\nimport type {\n mongo,\n Schema,\n} from 'mongoose';\nimport mongooseAggregatePaginate from 'mongoose-aggregate-paginate-v2';\nimport mongoosePaginate from 'mongoose-paginate-v2';\n\nimport { customMongooseOptions } from './_internals';\nimport { mongooseConnections } from './constants';\nimport type { setCustomMongooseOptions } from './options';\nimport { mongooseNormalizePlugin } from './plugins/normalize';\nimport type { BuildMongooseModelOptions } from './types/options';\n\nexport type DoNotRemoveOrUseThisType = typeof setCustomMongooseOptions;\n\n/**\n * Asserts that a Mongoose update operation was successful by checking that it was acknowledged\n * and that at least the expected number of documents were modified.\n *\n * This function is intended for use with `updateOne`, `updateMany`, or any update operation\n * that returns a MongoDB `UpdateResult`.\n *\n * @template T - The shape of the document being updated.\n * @param updatePromise - A promise returned from a Mongoose update operation.\n * @param expectedModifiedCount - The minimum number of documents expected to be modified. Defaults to 1.\n * @throws Will throw an error if:\n * - The operation was not acknowledged by MongoDB,\n * - Fewer documents were modified than expected.\n */\nexport async function assertMongooseUpdateSuccess<T extends mongo.BSON.Document>(\n updatePromise: Promise<mongo.UpdateResult<T>>,\n expectedModifiedCount = 1,\n) {\n const updateResult = await updatePromise;\n if (!updateResult.acknowledged) throw new Error('Update was not acknowledged.');\n if (updateResult.modifiedCount < expectedModifiedCount) {\n // eslint-disable-next-line style/max-len\n throw new Error(`Expected to modify at least ${expectedModifiedCount} document(s), but modified ${updateResult.modifiedCount}.`);\n }\n}\n\n/**\n * Builds and returns a Mongoose model with specified schema and options.\n *\n * This function sets up a Mongoose model with optional plugins and timestamp settings.\n * It can optionally use a specified connection or the default connection.\n *\n * Determine the connection to use:\n * 1. Use the connection provided in the options, if available.\n * 2. Otherwise, use the default connection stored in `mongooseConnections`.\n * 3. If there's no default connection, create one using the MongoDB URI from the environment variables,\n * or fallback to a local MongoDB instance.\n *\n * You can set a function to be executed before the final build by using {@link setCustomMongooseOptions}.\n * However, ensure that this option is configured before calling {@link buildMongooseModel}.\n *\n * @template DocType - The type of the document.\n * @template Model - The type of the model, which extends `BaseMongoosePaginateModel`.\n * @template InstanceMethodsAndOverrides - The type for instance methods and overrides.\n * @template QueryHelpers - The type for additional query helpers.\n *\n * @param collection - The name of the collection.\n * @param name - The name of the model.\n * @param schema - The Mongoose schema to use for the model.\n * @param options - Optional settings for building the model.\n *\n * @returns The created Mongoose model.\n */\nexport function buildMongooseModel<\n DocType,\n Model extends BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>,\n InstanceMethodsAndOverrides = object,\n QueryHelpers = object,\n>(\n collection: string,\n name: string,\n schema: Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>,\n options?: BuildMongooseModelOptions,\n) {\n if (options?.enableNormalizePlugin !== false) schema.plugin(mongooseNormalizePlugin);\n schema.plugin(mongooseAggregatePaginate);\n schema.plugin(mongoosePaginate);\n schema.set('timestamps', options?.timestamps ?? schema.get('timestamps') ?? true);\n customMongooseOptions.beforeModelBuild?.(schema);\n // eslint-disable-next-line style/max-len\n const connection = options?.connection || (mongooseConnections.default ||= createConnection(process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017'));\n return connection.model<DocType, Model, QueryHelpers>(name, schema, collection);\n}\n\n/**\n * Converts a document or ObjectId to a document using the specified model.\n *\n * This function takes a document or ObjectId and returns the corresponding document\n * from the database using the specified model. Optionally, specific fields can be selected.\n *\n * @template D - The type of the hydrated document.\n * @template DocType - The type of the document.\n * @template InstanceMethodsAndOverrides - The type for instance methods and overrides.\n * @template QueryHelpers - The type for additional query helpers.\n *\n * @param documentOrObjectId - The document or ObjectId to convert.\n * @param model - The Mongoose model to use for the conversion.\n * @param selectFields - Optional fields to select from the document.\n *\n * @returns The corresponding document or null if not found.\n */\nexport async function mongooseDocumentOrObjectIdToDocument<\n D extends MongooseHydratedDocument<DocType, InstanceMethodsAndOverrides, QueryHelpers>,\n DocType,\n InstanceMethodsAndOverrides,\n QueryHelpers,\n>(\n documentOrObjectId: MongooseDocumentOrObjectId<D>,\n model: BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>,\n selectFields?: string[],\n): Promise<D | null> {\n if (typeof documentOrObjectId === 'string' || documentOrObjectId instanceof Types.ObjectId) {\n return (await model.findById(documentOrObjectId).select(selectFields || [])) as D | null;\n }\n\n return documentOrObjectId;\n}\n"],"names":[],"mappings":";;;;;;;AAmBA;;;;;;;;;;;;;AAaG;AACI,eAAe,2BAA2B,CAC7C,aAA6C,EAC7C,qBAAqB,GAAG,C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AC,QAAQ,EAAE;AACxF,QAAA,QAAQ,MAAM,KAAK,CAAC,QAAQ,CAAC,kBAAkB,CAAC,CAAC,MAAM,CAAC,YAAY,IAAI,EAAE,CAAC;;AAG/E,IAAA,OAAO,kBAAkB;AAC7B;;;;"}
1
+ {"version":3,"file":"utils.mjs","sources":["../src/utils.ts"],"sourcesContent":["import mongoose, { Types } from 'mongoose';\nimport type {\n mongo,\n Schema,\n} from 'mongoose';\nimport mongooseAggregatePaginate from 'mongoose-aggregate-paginate-v2';\nimport mongoosePaginate from 'mongoose-paginate-v2';\n\nimport { customMongooseOptions } from './_internals';\nimport { mongooseConnections } from './constants';\nimport type { setCustomMongooseOptions } from './options';\nimport { mongooseNormalizePlugin } from './plugins/normalize';\nimport type { BuildMongooseModelOptions } from './types/options';\n\nexport type DoNotRemoveOrUseThisType = typeof setCustomMongooseOptions;\n\n/**\n * Asserts that a Mongoose update operation was successful by checking that it was acknowledged\n * and that at least the expected number of documents were modified.\n *\n * This function is intended for use with `updateOne`, `updateMany`, or any update operation\n * that returns a MongoDB `UpdateResult`.\n *\n * @template T - The shape of the document being updated.\n * @param updatePromise - A promise returned from a Mongoose update operation.\n * @param expectedModifiedCount - The minimum number of documents expected to be modified. Defaults to 1.\n * @throws Will throw an error if:\n * - The operation was not acknowledged by MongoDB,\n * - Fewer documents were modified than expected.\n */\nexport async function assertMongooseUpdateSuccess<T extends mongo.BSON.Document>(\n updatePromise: Promise<mongo.UpdateResult<T>>,\n expectedModifiedCount = 1,\n) {\n const updateResult = await updatePromise;\n if (!updateResult.acknowledged) throw new Error('Update was not acknowledged.');\n if (updateResult.modifiedCount < expectedModifiedCount) {\n // eslint-disable-next-line style/max-len\n throw new Error(`Expected to modify at least ${expectedModifiedCount} document(s), but modified ${updateResult.modifiedCount}.`);\n }\n}\n\n/**\n * Builds and returns a Mongoose model with specified schema and options.\n *\n * This function sets up a Mongoose model with optional plugins and timestamp settings.\n * It can optionally use a specified connection or the default connection.\n *\n * Determine the connection to use:\n * 1. Use the connection provided in the options, if available.\n * 2. Otherwise, use the default connection stored in `mongooseConnections`.\n * 3. If there's no default connection, create one using the MongoDB URI from the environment variables,\n * or fallback to a local MongoDB instance.\n *\n * You can set a function to be executed before the final build by using {@link setCustomMongooseOptions}.\n * However, ensure that this option is configured before calling {@link buildMongooseModel}.\n *\n * @template DocType - The type of the document.\n * @template Model - The type of the model, which extends `BaseMongoosePaginateModel`.\n * @template InstanceMethodsAndOverrides - The type for instance methods and overrides.\n * @template QueryHelpers - The type for additional query helpers.\n *\n * @param collection - The name of the collection.\n * @param name - The name of the model.\n * @param schema - The Mongoose schema to use for the model.\n * @param options - Optional settings for building the model.\n *\n * @returns The created Mongoose model.\n */\nexport function buildMongooseModel<\n DocType,\n Model extends BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>,\n InstanceMethodsAndOverrides = object,\n QueryHelpers = object,\n>(\n collection: string,\n name: string,\n schema: Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>,\n options?: BuildMongooseModelOptions,\n) {\n if (options?.enableNormalizePlugin !== false) schema.plugin(mongooseNormalizePlugin);\n schema.plugin(mongooseAggregatePaginate);\n schema.plugin(mongoosePaginate);\n schema.set('timestamps', options?.timestamps ?? schema.get('timestamps') ?? true);\n customMongooseOptions.beforeModelBuild?.(schema);\n // eslint-disable-next-line style/max-len\n const connection = options?.connection || (mongooseConnections.default ||= mongoose.createConnection(process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017'));\n return connection.model<DocType, Model, QueryHelpers>(name, schema, collection);\n}\n\n/**\n * Converts a document or ObjectId to a document using the specified model.\n *\n * This function takes a document or ObjectId and returns the corresponding document\n * from the database using the specified model. Optionally, specific fields can be selected.\n *\n * @template D - The type of the hydrated document.\n * @template DocType - The type of the document.\n * @template InstanceMethodsAndOverrides - The type for instance methods and overrides.\n * @template QueryHelpers - The type for additional query helpers.\n *\n * @param documentOrObjectId - The document or ObjectId to convert.\n * @param model - The Mongoose model to use for the conversion.\n * @param selectFields - Optional fields to select from the document.\n *\n * @returns The corresponding document or null if not found.\n */\nexport async function mongooseDocumentOrObjectIdToDocument<\n D extends MongooseHydratedDocument<DocType, InstanceMethodsAndOverrides, QueryHelpers>,\n DocType,\n InstanceMethodsAndOverrides,\n QueryHelpers,\n>(\n documentOrObjectId: MongooseDocumentOrObjectId<D>,\n model: BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>,\n selectFields?: string[],\n) {\n if (typeof documentOrObjectId === 'string' || documentOrObjectId instanceof Types.ObjectId) {\n return (await model.findById(documentOrObjectId).select(selectFields || [])) as D | null;\n }\n\n return documentOrObjectId;\n}\n"],"names":[],"mappings":";;;;;;;AAgBA;;;;;;;;;;;;;AAaG;AACI,eAAe,2BAA2B,CAC7C,aAA6C,EAC7C,qBAAqB,GAAG,CAAC,EAAA;AAEzB,IAAA,MAAM,YAAY,GAAG,MAAM,aAAa;IACxC,IAAI,CAAC,YAAY,CAAC,YAAY;AAAE,QAAA,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC;AAC/E,IAAA,IAAI,YAAY,CAAC,aAAa,GAAG,qBAAqB,EAAE;;QAEpD,MAAM,IAAI,KAAK,CAAC,CAA+B,4BAAA,EAAA,qBAAqB,CAA8B,2BAAA,EAAA,YAAY,CAAC,aAAa,CAAG,CAAA,CAAA,CAAC;;AAExI;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;AA0BG;AACG,SAAU,kBAAkB,CAM9B,UAAkB,EAClB,IAAY,EACZ,MAAyE,EACzE,OAAmC,EAAA;AAEnC,IAAA,IAAI,OAAO,EAAE,qBAAqB,KAAK,KAAK;AAAE,QAAA,MAAM,CAAC,MAAM,CAAC,uBAAuB,CAAC;AACpF,IAAA,MAAM,CAAC,MAAM,CAAC,yBAAyB,CAAC;AACxC,IAAA,MAAM,CAAC,MAAM,CAAC,gBAAgB,CAAC;AAC/B,IAAA,MAAM,CAAC,GAAG,CAAC,YAAY,EAAE,OAAO,EAAE,UAAU,IAAI,MAAM,CAAC,GAAG,CAAC,YAAY,CAAC,IAAI,IAAI,CAAC;AACjF,IAAA,qBAAqB,CAAC,gBAAgB,GAAG,MAAM,CAAC;;IAEhD,MAAM,UAAU,GAAG,OAAO,EAAE,UAAU,KAAK,mBAAmB,CAAC,OAAO,KAAK,QAAQ,CAAC,gBAAgB,CAAC,OAAO,CAAC,GAAG,CAAC,WAAW,IAAI,2BAA2B,CAAC,CAAC;IAC7J,OAAO,UAAU,CAAC,KAAK,CAA+B,IAAI,EAAE,MAAM,EAAE,UAAU,CAAC;AACnF;AAEA;;;;;;;;;;;;;;;;AAgBG;AACI,eAAe,oCAAoC,CAMtD,kBAAiD,EACjD,KAAoF,EACpF,YAAuB,EAAA;IAEvB,IAAI,OAAO,kBAAkB,KAAK,QAAQ,IAAI,kBAAkB,YAAY,KAAK,CAAC,QAAQ,EAAE;AACxF,QAAA,QAAQ,MAAM,KAAK,CAAC,QAAQ,CAAC,kBAAkB,CAAC,CAAC,MAAM,CAAC,YAAY,IAAI,EAAE,CAAC;;AAG/E,IAAA,OAAO,kBAAkB;AAC7B;;;;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kikiutils/mongoose",
3
- "version": "3.0.1",
3
+ "version": "3.0.3",
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
5
  "author": "kiki-kanri",
6
6
  "license": "MIT",
@@ -18,6 +18,7 @@
18
18
  "plugin",
19
19
  "schema"
20
20
  ],
21
+ "sideEffects": false,
21
22
  "exports": {
22
23
  "./*": {
23
24
  "types": "./dist/*.d.ts",
@@ -73,8 +74,7 @@
73
74
  "@types/node": "^22.14.0",
74
75
  "jest": "^29.7.0",
75
76
  "ts-jest": "^29.3.1",
76
- "ts-project-builder": "^4.0.0",
77
- "tslib": "^2.8.1"
77
+ "ts-project-builder": "^4.0.0"
78
78
  },
79
79
  "pnpm": {
80
80
  "onlyBuiltDependencies": [
@@ -1,4 +1,4 @@
1
- import Decimal from 'decimal.js';
1
+ import { Decimal } from 'decimal.js';
2
2
  import { Schema } from 'mongoose';
3
3
  import type {
4
4
  DefaultType,
package/src/types/data.ts CHANGED
@@ -1,61 +1,19 @@
1
1
  import type { IfElse } from './utils';
2
2
 
3
- /**
4
- * Interface for the base data of a Mongoose model, optionally including timestamp fields.
5
- *
6
- * This interface extends `LocalWithTimestampFieldsData` to optionally include `createdAt`
7
- * and `updatedAt` fields based on the `CreatedAtField` and `UpdatedAtField` flags.
8
- * It also includes a mandatory `id` field.
9
- *
10
- * @template CreatedAtField - A boolean flag indicating whether the `createdAt` field should be included.
11
- * @template UpdatedAtField - A boolean flag indicating whether the `updatedAt` field should be included.
12
- */
13
- interface LocalBaseMongooseModelData<
14
- CreatedAtField extends boolean = true,
15
- UpdatedAtField extends boolean = true,
16
- > extends LocalWithTimestampFieldsData<CreatedAtField, UpdatedAtField> {
17
- id: string;
18
- }
19
-
20
- /**
21
- * Interface for including timestamp fields conditionally based on the provided flags.
22
- *
23
- * This interface conditionally includes `createdAt` and `updatedAt` fields based on
24
- * the `CreatedAt` and `UpdatedAt` boolean flags. If the flag is true, the respective
25
- * field is included, otherwise it is set to `never`.
26
- *
27
- * @template CreatedAt - A boolean flag indicating whether the `createdAt` field should be included.
28
- * @template UpdatedAt - A boolean flag indicating whether the `updatedAt` field should be included.
29
- */
30
- interface LocalWithTimestampFieldsData<CreatedAt extends boolean = true, UpdatedAt extends boolean = true> {
31
- createdAt: IfElse<CreatedAt, string, never>;
32
- updatedAt: IfElse<UpdatedAt, string, never>;
33
- }
34
-
35
- /**
36
- * Utility type to omit properties that are set to `never` in a given type.
37
- *
38
- * This type alias iterates over the properties of a given type `T` and removes any
39
- * properties that are set to `never`.
40
- *
41
- * @template T - The type from which to omit `never` properties.
42
- */
43
- type OmitNever<T> = { [P in keyof T as IfElse<T[P], never, P, never>]: T[P] };
44
-
45
3
  declare global {
46
4
  /**
47
5
  * Type definition for the base data of a Mongoose model, optionally including timestamp fields.
48
6
  *
49
- * This type alias uses `OmitNever` to remove any `never` properties from `LocalBaseMongooseModelData`
50
- * based on the presence of the `CreatedAtField` and `UpdatedAtField` flags.
51
- *
52
7
  * @template CreatedAtField - A boolean flag indicating whether the `createdAt` field should be included.
53
8
  * @template UpdatedAtField - A boolean flag indicating whether the `updatedAt` field should be included.
54
9
  */
55
10
  type BaseMongooseModelData<
56
11
  CreatedAtField extends boolean = true,
57
12
  UpdatedAtField extends boolean = true,
58
- > = OmitNever<LocalBaseMongooseModelData<CreatedAtField, UpdatedAtField>>;
13
+ > =
14
+ & IfElse<CreatedAtField, { createdAt: string }, unknown>
15
+ & IfElse<UpdatedAtField, { updatedAt: string }, unknown>
16
+ & { id: string };
59
17
 
60
18
  /**
61
19
  * Type definition to omit timestamp fields (`createdAt`, `updatedAt`)
@@ -84,16 +42,13 @@ declare global {
84
42
  /**
85
43
  * Type definition for including timestamp fields conditionally based on the provided flags.
86
44
  *
87
- * This type alias uses `OmitNever` to remove any `never` properties from `LocalWithTimestampFieldsData`
88
- * based on the presence of the `CreatedAt` and `UpdatedAt` flags.
89
- *
90
45
  * @template CreatedAt - A boolean flag indicating whether the `createdAt` field should be included.
91
46
  * @template UpdatedAt - A boolean flag indicating whether the `updatedAt` field should be included.
92
47
  */
93
48
  type WithMongooseTimestampFieldsData<
94
49
  CreatedAt extends boolean = true,
95
50
  UpdatedAt extends boolean = true,
96
- > = OmitNever<LocalWithTimestampFieldsData<CreatedAt, UpdatedAt>>;
51
+ > =
52
+ & IfElse<CreatedAt, { createdAt: string }, object>
53
+ & IfElse<UpdatedAt, { updatedAt: string }, object>;
97
54
  }
98
-
99
- export {};
@@ -37,8 +37,8 @@ declare global {
37
37
  CreatedAtField extends boolean = true,
38
38
  UpdatedAtField extends boolean = true,
39
39
  > =
40
- & IfElse<CreatedAtField, { createdAt: Date }, object>
41
- & IfElse<UpdatedAtField, { updatedAt: Date }, object>
40
+ & IfElse<CreatedAtField, { createdAt: Date }, unknown>
41
+ & IfElse<UpdatedAtField, { updatedAt: Date }, unknown>
42
42
  & Omit<T, 'createdAt' | 'id' | 'updatedAt'>;
43
43
 
44
44
  /**
package/src/utils.ts CHANGED
@@ -1,7 +1,4 @@
1
- import {
2
- createConnection,
3
- Types,
4
- } from 'mongoose';
1
+ import mongoose, { Types } from 'mongoose';
5
2
  import type {
6
3
  mongo,
7
4
  Schema,
@@ -87,7 +84,7 @@ export function buildMongooseModel<
87
84
  schema.set('timestamps', options?.timestamps ?? schema.get('timestamps') ?? true);
88
85
  customMongooseOptions.beforeModelBuild?.(schema);
89
86
  // eslint-disable-next-line style/max-len
90
- const connection = options?.connection || (mongooseConnections.default ||= createConnection(process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017'));
87
+ const connection = options?.connection || (mongooseConnections.default ||= mongoose.createConnection(process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017'));
91
88
  return connection.model<DocType, Model, QueryHelpers>(name, schema, collection);
92
89
  }
93
90
 
@@ -117,7 +114,7 @@ export async function mongooseDocumentOrObjectIdToDocument<
117
114
  documentOrObjectId: MongooseDocumentOrObjectId<D>,
118
115
  model: BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>,
119
116
  selectFields?: string[],
120
- ): Promise<D | null> {
117
+ ) {
121
118
  if (typeof documentOrObjectId === 'string' || documentOrObjectId instanceof Types.ObjectId) {
122
119
  return (await model.findById(documentOrObjectId).select(selectFields || [])) as D | null;
123
120
  }