@kikiutils/mongoose 2.0.1 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/options.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { CustomMongooseOptions } from './types/options';
2
2
  import type { buildMongooseModel } from './utils';
3
- export type DoNotUseOrRemoveThisType = typeof buildMongooseModel;
3
+ export type DoNotRemoveOrUseThisType = typeof buildMongooseModel;
4
4
  /**
5
5
  * Sets custom options.
6
6
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kikiutils/mongoose",
3
- "version": "2.0.1",
3
+ "version": "2.1.0",
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
  "license": "MIT",
6
6
  "author": "kiki-kanri",
@@ -19,7 +19,8 @@
19
19
  "lodash": "^4.17.21",
20
20
  "mongoose": "^8.8.0",
21
21
  "mongoose-aggregate-paginate-v2": "^1.1.2",
22
- "mongoose-paginate-v2": "^1.8.5"
22
+ "mongoose-paginate-v2": "^1.8.5",
23
+ "type-fest": "^4.26.1"
23
24
  },
24
25
  "devDependencies": {
25
26
  "@kikiutils/changelogen": "^0.7.0",
@@ -30,7 +31,7 @@
30
31
  "@types/node": "^22.8.6",
31
32
  "jest": "^29.7.0",
32
33
  "ts-jest": "^29.2.5",
33
- "ts-project-builder": "^3.3.0",
34
+ "ts-project-builder": "^3.3.1",
34
35
  "tslib": "^2.8.1"
35
36
  },
36
37
  "keywords": [
@@ -24,6 +24,7 @@ const createBaseSchemaBuilderFactory = (type) => {
24
24
  const isFunctionKeys = new Set([
25
25
  'default',
26
26
  'enum',
27
+ 'index',
27
28
  'max',
28
29
  'maxlength',
29
30
  'min',
@@ -22,6 +22,7 @@ const createBaseSchemaBuilderFactory = (type) => {
22
22
  const isFunctionKeys = new Set([
23
23
  'default',
24
24
  'enum',
25
+ 'index',
25
26
  'max',
26
27
  'maxlength',
27
28
  'min',
@@ -1,51 +1,29 @@
1
- import type { BooleanSchemaDefinition, DefaultType } from 'mongoose';
2
- export type ExtendBooleanSchemaBuilder<Props extends {
1
+ import type { BooleanSchemaDefinition, DefaultType, IndexDirection, IndexOptions } from 'mongoose';
2
+ import type { Merge } from 'type-fest';
3
+ type BaseProps = {
3
4
  type: BooleanSchemaDefinition;
4
- }, ExtraOmitFields extends string> = Omit<BooleanSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
5
- export interface BooleanSchemaBuilder<Props extends {
6
- type: BooleanSchemaDefinition;
7
- } = {
8
- type: BooleanSchemaDefinition;
9
- }, ExtraOmitFields extends string = never> {
10
- default: <T extends DefaultType<D> | ((this: any, doc: any) => DefaultType<D>) | null, D extends boolean>(value: T) => ExtendBooleanSchemaBuilder<{
11
- [key in keyof (Props & {
12
- default: T;
13
- })]: (Props & {
14
- default: T;
15
- })[key];
16
- }, ExtraOmitFields>;
17
- nonRequired: {
18
- [key in keyof Props]: Props[key];
19
- };
20
- private: ExtendBooleanSchemaBuilder<{
21
- [key in keyof (Props & {
22
- private: true;
23
- })]: (Props & {
24
- private: true;
25
- })[key];
26
- }, ExtraOmitFields>;
27
- required: {
28
- [key in keyof (Props & {
29
- required: true;
30
- })]: (Props & {
31
- required: true;
32
- })[key];
33
- };
34
- sparse: ExtendBooleanSchemaBuilder<{
35
- [key in keyof (Props & {
36
- sparse: true;
37
- })]: (Props & {
38
- sparse: true;
39
- })[key];
40
- }, ExtraOmitFields>;
41
- unique: ExtendBooleanSchemaBuilder<{
42
- [key in keyof (Props & {
43
- unique: true;
44
- })]: (Props & {
45
- unique: true;
46
- })[key];
47
- }, ExtraOmitFields>;
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, {
9
+ default: T;
10
+ }>, ExtraOmitFields>;
11
+ index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendBooleanSchemaBuilder<Merge<Props, {
12
+ index: T;
13
+ }>, ExtraOmitFields>;
14
+ nonRequired: Props;
15
+ private: ExtendBooleanSchemaBuilder<Merge<Props, {
16
+ private: true;
17
+ }>, ExtraOmitFields>;
18
+ required: Merge<Props, {
19
+ required: true;
20
+ }>;
21
+ sparse: ExtendBooleanSchemaBuilder<Merge<Props, {
22
+ sparse: true;
23
+ }>, ExtraOmitFields>;
24
+ unique: ExtendBooleanSchemaBuilder<Merge<Props, {
25
+ unique: true;
26
+ }>, ExtraOmitFields>;
48
27
  }
49
- export declare const booleanSchemaBuilder: (schema?: Record<string, any>) => BooleanSchemaBuilder<{
50
- type: BooleanSchemaDefinition;
51
- }, never>;
28
+ export declare const booleanSchemaBuilder: (schema?: Record<string, any>) => BooleanSchemaBuilder<BaseProps, never>;
29
+ export {};
@@ -1,78 +1,50 @@
1
- import type { DefaultType, DateSchemaDefinition } from 'mongoose';
2
- import type { MaybeReadonly } from '../types/utils';
3
- export type ExtendDateSchemaBuilder<Props extends {
1
+ import type { DefaultType, DateSchemaDefinition, IndexDirection, IndexOptions } from 'mongoose';
2
+ import type { Merge } from 'type-fest';
3
+ import type { Readonlyable } from '../types/utils';
4
+ type BaseProps = {
4
5
  type: DateSchemaDefinition;
5
- }, ExtraOmitFields extends string> = Omit<DateSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
6
+ };
7
+ export type ExtendDateSchemaBuilder<Props extends BaseProps, ExtraOmitFields extends string> = Omit<DateSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
6
8
  export interface DateSchemaBuilder<Props extends {
7
9
  type: DateSchemaDefinition;
8
10
  } = {
9
11
  type: DateSchemaDefinition;
10
12
  }, ExtraOmitFields extends string = never> {
11
- default: <T extends DefaultType<D> | ((this: any, doc: any) => DefaultType<D>) | null, D extends NativeDate>(value: T) => ExtendDateSchemaBuilder<{
12
- [key in keyof (Props & {
13
- default: T;
14
- })]: (Props & {
15
- default: T;
16
- })[key];
17
- }, ExtraOmitFields>;
18
- enum: <T extends MaybeReadonly<Array<D | null>> | {
13
+ default: <T extends DefaultType<D> | ((this: any, doc: any) => DefaultType<D>) | null, D extends NativeDate>(value: T) => ExtendDateSchemaBuilder<Merge<Props, {
14
+ default: T;
15
+ }>, ExtraOmitFields>;
16
+ enum: <T extends Readonlyable<Array<D | null>> | {
19
17
  message?: M;
20
- values: MaybeReadonly<Array<D | null>>;
18
+ values: Readonlyable<Array<D | null>>;
21
19
  } | {
22
20
  [path: string]: D | null;
23
- }, D extends NativeDate, M extends string>(value: T) => ExtendDateSchemaBuilder<{
24
- [key in keyof (Props & {
25
- enum: T;
26
- })]: (Props & {
27
- enum: T;
28
- })[key];
29
- }, ExtraOmitFields>;
30
- max: <T extends D | MaybeReadonly<[D, S]>, D extends NativeDate, S extends string>(value: T) => ExtendDateSchemaBuilder<{
31
- [key in keyof (Props & {
32
- max: T;
33
- })]: (Props & {
34
- max: T;
35
- })[key];
36
- }, ExtraOmitFields>;
37
- min: <T extends D | MaybeReadonly<[D, S]>, D extends NativeDate, S extends string>(value: T) => ExtendDateSchemaBuilder<{
38
- [key in keyof (Props & {
39
- min: T;
40
- })]: (Props & {
41
- min: T;
42
- })[key];
43
- }, ExtraOmitFields>;
44
- nonRequired: {
45
- [key in keyof Props]: Props[key];
46
- };
47
- private: ExtendDateSchemaBuilder<{
48
- [key in keyof (Props & {
49
- private: true;
50
- })]: (Props & {
51
- private: true;
52
- })[key];
53
- }, ExtraOmitFields>;
54
- required: {
55
- [key in keyof (Props & {
56
- required: true;
57
- })]: (Props & {
58
- required: true;
59
- })[key];
60
- };
61
- sparse: ExtendDateSchemaBuilder<{
62
- [key in keyof (Props & {
63
- sparse: true;
64
- })]: (Props & {
65
- sparse: true;
66
- })[key];
67
- }, ExtraOmitFields>;
68
- unique: ExtendDateSchemaBuilder<{
69
- [key in keyof (Props & {
70
- unique: true;
71
- })]: (Props & {
72
- unique: true;
73
- })[key];
74
- }, ExtraOmitFields>;
21
+ }, D extends NativeDate, M extends string>(value: T) => ExtendDateSchemaBuilder<Merge<Props, {
22
+ enum: T;
23
+ }>, ExtraOmitFields>;
24
+ index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendDateSchemaBuilder<Merge<Props, {
25
+ index: T;
26
+ }>, ExtraOmitFields>;
27
+ max: <T extends D | Readonlyable<[D, S]>, D extends NativeDate, S extends string>(value: T) => ExtendDateSchemaBuilder<Merge<Props, {
28
+ max: T;
29
+ }>, ExtraOmitFields>;
30
+ min: <T extends D | Readonlyable<[D, S]>, D extends NativeDate, S extends string>(value: T) => ExtendDateSchemaBuilder<Merge<Props, {
31
+ min: T;
32
+ }>, ExtraOmitFields>;
33
+ nonRequired: Props;
34
+ private: ExtendDateSchemaBuilder<Merge<Props, {
35
+ private: true;
36
+ }>, ExtraOmitFields>;
37
+ required: Merge<Props, {
38
+ required: true;
39
+ }>;
40
+ sparse: ExtendDateSchemaBuilder<Merge<Props, {
41
+ sparse: true;
42
+ }>, ExtraOmitFields>;
43
+ unique: ExtendDateSchemaBuilder<Merge<Props, {
44
+ unique: true;
45
+ }>, ExtraOmitFields>;
75
46
  }
76
47
  export declare const dateSchemaBuilder: (schema?: Record<string, any>) => DateSchemaBuilder<{
77
48
  type: DateSchemaDefinition;
78
49
  }, never>;
50
+ export {};
@@ -1,10 +1,12 @@
1
1
  import Decimal from 'decimal.js';
2
2
  import { Schema } from 'mongoose';
3
- import type { DefaultType, Types } from 'mongoose';
4
- import type { MaybeReadonly } from '../types/utils';
5
- export type ExtendDecimal128SchemaBuilder<Props extends {
3
+ import type { DefaultType, IndexDirection, IndexOptions, Types } from 'mongoose';
4
+ import type { Merge } from 'type-fest';
5
+ import type { Readonlyable } from '../types/utils';
6
+ type BaseProps = {
6
7
  type: Schema.Types.Decimal128;
7
- }, ExtraOmitFields extends string> = Omit<Decimal128SchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
8
+ };
9
+ export type ExtendDecimal128SchemaBuilder<Props extends BaseProps, ExtraOmitFields extends string> = Omit<Decimal128SchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
8
10
  type ToStringGetterSchema = {
9
11
  get: (value: Types.Decimal128) => string;
10
12
  };
@@ -18,42 +20,27 @@ export interface Decimal128SchemaBuilder<Props extends {
18
20
  } = {
19
21
  type: Schema.Types.Decimal128;
20
22
  }, ExtraOmitFields extends string = never> {
21
- default: <T extends DefaultType<D> | ((this: any, doc: any) => DefaultType<D>) | null, D extends Types.Decimal128>(value: T) => ExtendDecimal128SchemaBuilder<{
22
- [key in keyof (Props & {
23
- default: T;
24
- })]: (Props & {
25
- default: T;
26
- })[key];
27
- }, ExtraOmitFields>;
28
- enum: <T extends MaybeReadonly<Array<D | null>> | {
23
+ default: <T extends DefaultType<D> | ((this: any, doc: any) => DefaultType<D>) | null, D extends Types.Decimal128>(value: T) => ExtendDecimal128SchemaBuilder<Merge<Props, {
24
+ default: T;
25
+ }>, ExtraOmitFields>;
26
+ enum: <T extends Readonlyable<Array<D | null>> | {
29
27
  message?: M;
30
- values: MaybeReadonly<Array<D | null>>;
28
+ values: Readonlyable<Array<D | null>>;
31
29
  } | {
32
30
  [path: string]: D | null;
33
- }, D extends Types.Decimal128, M extends string>(value: T) => ExtendDecimal128SchemaBuilder<{
34
- [key in keyof (Props & {
35
- enum: T;
36
- })]: (Props & {
37
- enum: T;
38
- })[key];
39
- }, ExtraOmitFields>;
40
- nonRequired: {
41
- [key in keyof Props]: Props[key];
42
- };
43
- private: ExtendDecimal128SchemaBuilder<{
44
- [key in keyof (Props & {
45
- private: true;
46
- })]: (Props & {
47
- private: true;
48
- })[key];
49
- }, ExtraOmitFields>;
50
- required: {
51
- [key in keyof (Props & {
52
- required: true;
53
- })]: (Props & {
54
- required: true;
55
- })[key];
56
- };
31
+ }, D extends Types.Decimal128, M extends string>(value: T) => ExtendDecimal128SchemaBuilder<Merge<Props, {
32
+ enum: T;
33
+ }>, ExtraOmitFields>;
34
+ index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendDecimal128SchemaBuilder<Merge<Props, {
35
+ index: T;
36
+ }>, ExtraOmitFields>;
37
+ nonRequired: Props;
38
+ private: ExtendDecimal128SchemaBuilder<Merge<Props, {
39
+ private: true;
40
+ }>, ExtraOmitFields>;
41
+ required: Merge<Props, {
42
+ required: true;
43
+ }>;
57
44
  /**
58
45
  * Sets the rounding and toFixed behavior for Decimal128 fields.
59
46
  * This method allows specifying the number of decimal places and the rounding strategy to be used when
@@ -64,9 +51,7 @@ export interface Decimal128SchemaBuilder<Props extends {
64
51
  *
65
52
  * @returns A schema builder with the rounding and toFixed behavior applied to the `set` option in Mongoose.
66
53
  */
67
- setRoundAndToFixedSetter: (places?: number, rounding?: Decimal.Rounding) => ExtendDecimal128SchemaBuilder<{
68
- [key in keyof (Props & ToStringSetterSchema)]: (Props & ToStringSetterSchema)[key];
69
- }, ExtraOmitFields | 'setRoundAndToFixedSetter'>;
54
+ setRoundAndToFixedSetter: (places?: number, rounding?: Decimal.Rounding) => ExtendDecimal128SchemaBuilder<Merge<Props, ToStringSetterSchema>, ExtraOmitFields | 'setRoundAndToFixedSetter'>;
70
55
  /**
71
56
  * Sets the toString behavior for Decimal128 fields.
72
57
  * This method defines how the Decimal128 field should be converted to a string when processing or
@@ -76,23 +61,13 @@ export interface Decimal128SchemaBuilder<Props extends {
76
61
  * @returns A schema builder with the custom `get` behavior applied to convert the Decimal128 field to a string
77
62
  * when accessed in the application.
78
63
  */
79
- setToStringGetter: ExtendDecimal128SchemaBuilder<{
80
- [key in keyof (Props & ToStringGetterSchema)]: (Props & ToStringGetterSchema)[key];
81
- }, ExtraOmitFields | 'setToStringGetter'>;
82
- sparse: ExtendDecimal128SchemaBuilder<{
83
- [key in keyof (Props & {
84
- sparse: true;
85
- })]: (Props & {
86
- sparse: true;
87
- })[key];
88
- }, ExtraOmitFields>;
89
- unique: ExtendDecimal128SchemaBuilder<{
90
- [key in keyof (Props & {
91
- unique: true;
92
- })]: (Props & {
93
- unique: true;
94
- })[key];
95
- }, ExtraOmitFields>;
64
+ setToStringGetter: ExtendDecimal128SchemaBuilder<Merge<Props, ToStringGetterSchema>, ExtraOmitFields | 'setToStringGetter'>;
65
+ sparse: ExtendDecimal128SchemaBuilder<Merge<Props, {
66
+ sparse: true;
67
+ }>, ExtraOmitFields>;
68
+ unique: ExtendDecimal128SchemaBuilder<Merge<Props, {
69
+ unique: true;
70
+ }>, ExtraOmitFields>;
96
71
  }
97
72
  export declare const decimal128SchemaBuilder: () => Decimal128SchemaBuilder;
98
73
  export {};
@@ -1,78 +1,50 @@
1
- import type { DefaultType, NumberSchemaDefinition } from 'mongoose';
2
- import type { MaybeReadonly } from '../types/utils';
3
- export type ExtendNumberSchemaBuilder<Props extends {
1
+ import type { DefaultType, IndexDirection, IndexOptions, NumberSchemaDefinition } from 'mongoose';
2
+ import type { Merge } from 'type-fest';
3
+ import type { Readonlyable } from '../types/utils';
4
+ type BaseProps = {
4
5
  type: NumberSchemaDefinition;
5
- }, ExtraOmitFields extends string> = Omit<NumberSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
6
+ };
7
+ export type ExtendNumberSchemaBuilder<Props extends BaseProps, ExtraOmitFields extends string> = Omit<NumberSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
6
8
  export interface NumberSchemaBuilder<Props extends {
7
9
  type: NumberSchemaDefinition;
8
10
  } = {
9
11
  type: NumberSchemaDefinition;
10
12
  }, ExtraOmitFields extends string = never> {
11
- default: <T extends DefaultType<D> | ((this: any, doc: any) => DefaultType<D>) | null, D extends number>(value: T) => ExtendNumberSchemaBuilder<{
12
- [key in keyof (Props & {
13
- default: T;
14
- })]: (Props & {
15
- default: T;
16
- })[key];
17
- }, ExtraOmitFields>;
18
- enum: <T extends MaybeReadonly<Array<N | null>> | {
13
+ default: <T extends DefaultType<D> | ((this: any, doc: any) => DefaultType<D>) | null, D extends number>(value: T) => ExtendNumberSchemaBuilder<Merge<Props, {
14
+ default: T;
15
+ }>, ExtraOmitFields>;
16
+ enum: <T extends Readonlyable<Array<N | null>> | {
19
17
  message?: M;
20
- values: MaybeReadonly<Array<N | null>>;
18
+ values: Readonlyable<Array<N | null>>;
21
19
  } | {
22
20
  [path: string]: N | null;
23
- }, M extends string, N extends number>(value: T) => ExtendNumberSchemaBuilder<{
24
- [key in keyof (Props & {
25
- enum: T;
26
- })]: (Props & {
27
- enum: T;
28
- })[key];
29
- }, ExtraOmitFields>;
30
- max: <T extends N | MaybeReadonly<[N, S]>, N extends number, S extends string>(value: T) => ExtendNumberSchemaBuilder<{
31
- [key in keyof (Props & {
32
- max: T;
33
- })]: (Props & {
34
- max: T;
35
- })[key];
36
- }, ExtraOmitFields>;
37
- min: <T extends N | MaybeReadonly<[N, S]>, N extends number, S extends string>(value: T) => ExtendNumberSchemaBuilder<{
38
- [key in keyof (Props & {
39
- min: T;
40
- })]: (Props & {
41
- min: T;
42
- })[key];
43
- }, ExtraOmitFields>;
44
- nonRequired: {
45
- [key in keyof Props]: Props[key];
46
- };
47
- private: ExtendNumberSchemaBuilder<{
48
- [key in keyof (Props & {
49
- private: true;
50
- })]: (Props & {
51
- private: true;
52
- })[key];
53
- }, ExtraOmitFields>;
54
- required: {
55
- [key in keyof (Props & {
56
- required: true;
57
- })]: (Props & {
58
- required: true;
59
- })[key];
60
- };
61
- sparse: ExtendNumberSchemaBuilder<{
62
- [key in keyof (Props & {
63
- sparse: true;
64
- })]: (Props & {
65
- sparse: true;
66
- })[key];
67
- }, ExtraOmitFields>;
68
- unique: ExtendNumberSchemaBuilder<{
69
- [key in keyof (Props & {
70
- unique: true;
71
- })]: (Props & {
72
- unique: true;
73
- })[key];
74
- }, ExtraOmitFields>;
21
+ }, M extends string, N extends number>(value: T) => ExtendNumberSchemaBuilder<Merge<Props, {
22
+ enum: T;
23
+ }>, ExtraOmitFields>;
24
+ index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendNumberSchemaBuilder<Merge<Props, {
25
+ index: T;
26
+ }>, ExtraOmitFields>;
27
+ max: <T extends N | Readonlyable<[N, S]>, N extends number, S extends string>(value: T) => ExtendNumberSchemaBuilder<Merge<Props, {
28
+ max: T;
29
+ }>, ExtraOmitFields>;
30
+ min: <T extends N | Readonlyable<[N, S]>, N extends number, S extends string>(value: T) => ExtendNumberSchemaBuilder<Merge<Props, {
31
+ min: T;
32
+ }>, ExtraOmitFields>;
33
+ nonRequired: Props;
34
+ private: ExtendNumberSchemaBuilder<Merge<Props, {
35
+ private: true;
36
+ }>, ExtraOmitFields>;
37
+ required: Merge<Props, {
38
+ required: true;
39
+ }>;
40
+ sparse: ExtendNumberSchemaBuilder<Merge<Props, {
41
+ sparse: true;
42
+ }>, ExtraOmitFields>;
43
+ unique: ExtendNumberSchemaBuilder<Merge<Props, {
44
+ unique: true;
45
+ }>, ExtraOmitFields>;
75
46
  }
76
47
  export declare const numberSchemaBuilder: (schema?: Record<string, any>) => NumberSchemaBuilder<{
77
48
  type: NumberSchemaDefinition;
78
49
  }, never>;
50
+ export {};
@@ -1,64 +1,44 @@
1
- import type { DefaultType, ObjectIdSchemaDefinition, Types } from 'mongoose';
2
- import type { MaybeReadonly } from '../types/utils';
3
- export type ExtendObjectIdSchemaBuilder<Props extends {
1
+ import type { DefaultType, IndexDirection, IndexOptions, ObjectIdSchemaDefinition, Types } from 'mongoose';
2
+ import type { Merge } from 'type-fest';
3
+ import type { Readonlyable } from '../types/utils';
4
+ type BaseProps = {
4
5
  type: ObjectIdSchemaDefinition;
5
- }, ExtraOmitFields extends string> = Omit<ObjectIdSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
6
+ };
7
+ export type ExtendObjectIdSchemaBuilder<Props extends BaseProps, ExtraOmitFields extends string> = Omit<ObjectIdSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
6
8
  export interface ObjectIdSchemaBuilder<Props extends {
7
9
  type: ObjectIdSchemaDefinition;
8
10
  } = {
9
11
  type: ObjectIdSchemaDefinition;
10
12
  }, ExtraOmitFields extends string = never> {
11
- default: <T extends DefaultType<D> | ((this: any, doc: any) => DefaultType<D>) | null, D extends Types.ObjectId>(value: T) => ExtendObjectIdSchemaBuilder<{
12
- [key in keyof (Props & {
13
- default: T;
14
- })]: (Props & {
15
- default: T;
16
- })[key];
17
- }, ExtraOmitFields>;
18
- enum: <T extends MaybeReadonly<Array<O | null>> | {
13
+ default: <T extends DefaultType<D> | ((this: any, doc: any) => DefaultType<D>) | null, D extends Types.ObjectId>(value: T) => ExtendObjectIdSchemaBuilder<Merge<Props, {
14
+ default: T;
15
+ }>, ExtraOmitFields>;
16
+ enum: <T extends Readonlyable<Array<O | null>> | {
19
17
  message?: M;
20
- values: MaybeReadonly<Array<O | null>>;
18
+ values: Readonlyable<Array<O | null>>;
21
19
  } | {
22
20
  [path: string]: O | null;
23
- }, M extends string, O extends Types.ObjectId>(value: T) => ExtendObjectIdSchemaBuilder<{
24
- [key in keyof (Props & {
25
- enum: T;
26
- })]: (Props & {
27
- enum: T;
28
- })[key];
29
- }, ExtraOmitFields>;
30
- nonRequired: {
31
- [key in keyof Props]: Props[key];
32
- };
33
- private: ExtendObjectIdSchemaBuilder<{
34
- [key in keyof (Props & {
35
- private: true;
36
- })]: (Props & {
37
- private: true;
38
- })[key];
39
- }, ExtraOmitFields>;
40
- required: {
41
- [key in keyof (Props & {
42
- required: true;
43
- })]: (Props & {
44
- required: true;
45
- })[key];
46
- };
47
- sparse: ExtendObjectIdSchemaBuilder<{
48
- [key in keyof (Props & {
49
- sparse: true;
50
- })]: (Props & {
51
- sparse: true;
52
- })[key];
53
- }, ExtraOmitFields>;
54
- unique: ExtendObjectIdSchemaBuilder<{
55
- [key in keyof (Props & {
56
- unique: true;
57
- })]: (Props & {
58
- unique: true;
59
- })[key];
60
- }, ExtraOmitFields>;
21
+ }, M extends string, O extends Types.ObjectId>(value: T) => ExtendObjectIdSchemaBuilder<Merge<Props, {
22
+ enum: T;
23
+ }>, ExtraOmitFields>;
24
+ index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendObjectIdSchemaBuilder<Merge<Props, {
25
+ index: T;
26
+ }>, ExtraOmitFields>;
27
+ nonRequired: Props;
28
+ private: ExtendObjectIdSchemaBuilder<Merge<Props, {
29
+ private: true;
30
+ }>, ExtraOmitFields>;
31
+ required: Merge<Props, {
32
+ required: true;
33
+ }>;
34
+ sparse: ExtendObjectIdSchemaBuilder<Merge<Props, {
35
+ sparse: true;
36
+ }>, ExtraOmitFields>;
37
+ unique: ExtendObjectIdSchemaBuilder<Merge<Props, {
38
+ unique: true;
39
+ }>, ExtraOmitFields>;
61
40
  }
62
41
  export declare const objectIdSchemaBuilder: (schema?: Record<string, any>) => ObjectIdSchemaBuilder<{
63
42
  type: ObjectIdSchemaDefinition;
64
43
  }, never>;
44
+ export {};
@@ -1,65 +1,45 @@
1
- import type { DefaultType, Model, ObjectIdSchemaDefinition, Types } from 'mongoose';
2
- import type { MaybeReadonly } from '../types/utils';
3
- export type ExtendRefSchemaBuilder<Props extends {
1
+ import type { DefaultType, IndexDirection, IndexOptions, Model, ObjectIdSchemaDefinition, Types } from 'mongoose';
2
+ import type { Merge } from 'type-fest';
3
+ import type { Readonlyable } from '../types/utils';
4
+ type BaseProps = {
4
5
  type: ObjectIdSchemaDefinition;
5
- }, ExtraOmitFields extends string> = Omit<RefSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
6
+ };
7
+ export type ExtendRefSchemaBuilder<Props extends BaseProps, ExtraOmitFields extends string> = Omit<RefSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
6
8
  export interface RefSchemaBuilder<Props extends {
7
9
  type: ObjectIdSchemaDefinition;
8
10
  } = {
9
11
  type: ObjectIdSchemaDefinition;
10
12
  }, ExtraOmitFields extends string = never> {
11
- default: <T extends DefaultType<D> | ((this: any, doc: any) => DefaultType<D>) | null, D extends Types.ObjectId>(value: T) => ExtendRefSchemaBuilder<{
12
- [key in keyof (Props & {
13
- default: T;
14
- })]: (Props & {
15
- default: T;
16
- })[key];
17
- }, ExtraOmitFields>;
18
- enum: <T extends MaybeReadonly<Array<O | null>> | {
13
+ default: <T extends DefaultType<D> | ((this: any, doc: any) => DefaultType<D>) | null, D extends Types.ObjectId>(value: T) => ExtendRefSchemaBuilder<Merge<Props, {
14
+ default: T;
15
+ }>, ExtraOmitFields>;
16
+ enum: <T extends Readonlyable<Array<O | null>> | {
19
17
  message?: M;
20
- values: MaybeReadonly<Array<O | null>>;
18
+ values: Readonlyable<Array<O | null>>;
21
19
  } | {
22
20
  [path: string]: O | null;
23
- }, M extends string, O extends Types.ObjectId>(value: T) => ExtendRefSchemaBuilder<{
24
- [key in keyof (Props & {
25
- enum: T;
26
- })]: (Props & {
27
- enum: T;
28
- })[key];
29
- }, ExtraOmitFields>;
30
- nonRequired: {
31
- [key in keyof Props]: Props[key];
32
- };
33
- private: ExtendRefSchemaBuilder<{
34
- [key in keyof (Props & {
35
- private: true;
36
- })]: (Props & {
37
- private: true;
38
- })[key];
39
- }, ExtraOmitFields>;
40
- required: {
41
- [key in keyof (Props & {
42
- required: true;
43
- })]: (Props & {
44
- required: true;
45
- })[key];
46
- };
47
- sparse: ExtendRefSchemaBuilder<{
48
- [key in keyof (Props & {
49
- sparse: true;
50
- })]: (Props & {
51
- sparse: true;
52
- })[key];
53
- }, ExtraOmitFields>;
54
- unique: ExtendRefSchemaBuilder<{
55
- [key in keyof (Props & {
56
- unique: true;
57
- })]: (Props & {
58
- unique: true;
59
- })[key];
60
- }, ExtraOmitFields>;
21
+ }, M extends string, O extends Types.ObjectId>(value: T) => ExtendRefSchemaBuilder<Merge<Props, {
22
+ enum: T;
23
+ }>, ExtraOmitFields>;
24
+ index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendRefSchemaBuilder<Merge<Props, {
25
+ index: T;
26
+ }>, ExtraOmitFields>;
27
+ nonRequired: Props;
28
+ private: ExtendRefSchemaBuilder<Merge<Props, {
29
+ private: true;
30
+ }>, ExtraOmitFields>;
31
+ required: Merge<Props, {
32
+ required: true;
33
+ }>;
34
+ sparse: ExtendRefSchemaBuilder<Merge<Props, {
35
+ sparse: true;
36
+ }>, ExtraOmitFields>;
37
+ unique: ExtendRefSchemaBuilder<Merge<Props, {
38
+ unique: true;
39
+ }>, ExtraOmitFields>;
61
40
  }
62
41
  export declare const refSchemaBuilder: <T extends string | Model<any> | ((this: any, doc: any) => string | Model<any>)>(ref: T) => RefSchemaBuilder<{
63
42
  ref: T;
64
43
  type: ObjectIdSchemaDefinition;
65
44
  }>;
45
+ export {};
@@ -1,8 +1,10 @@
1
- import type { DefaultType, StringSchemaDefinition } from 'mongoose';
2
- import type { MaybeReadonly } from '../types/utils';
3
- export type ExtendStringSchemaBuilder<Props extends {
1
+ import type { DefaultType, IndexDirection, IndexOptions, StringSchemaDefinition } from 'mongoose';
2
+ import type { Merge } from 'type-fest';
3
+ import type { Readonlyable } from '../types/utils';
4
+ type BaseProps = {
4
5
  type: StringSchemaDefinition;
5
- }, ExtraOmitFields extends string> = Omit<StringSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
6
+ };
7
+ export type ExtendStringSchemaBuilder<Props extends BaseProps, ExtraOmitFields extends string> = Omit<StringSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
6
8
  type IPSchema<T extends string> = {
7
9
  trim: true;
8
10
  validate: {
@@ -15,25 +17,20 @@ export interface StringSchemaBuilder<Props extends {
15
17
  } = {
16
18
  type: StringSchemaDefinition;
17
19
  }, ExtraOmitFields extends string = never> {
18
- default: <T extends DefaultType<D> | ((this: any, doc: any) => DefaultType<D>) | null, D extends string>(value: T) => ExtendStringSchemaBuilder<{
19
- [key in keyof (Props & {
20
- default: T;
21
- })]: (Props & {
22
- default: T;
23
- })[key];
24
- }, ExtraOmitFields>;
25
- enum: <T extends MaybeReadonly<Array<S | null>> | {
20
+ default: <T extends DefaultType<D> | ((this: any, doc: any) => DefaultType<D>) | null, D extends string>(value: T) => ExtendStringSchemaBuilder<Merge<Props, {
21
+ default: T;
22
+ }>, ExtraOmitFields>;
23
+ enum: <T extends Readonlyable<Array<S | null>> | {
26
24
  message?: M;
27
- values: MaybeReadonly<Array<S | null>>;
25
+ values: Readonlyable<Array<S | null>>;
28
26
  } | {
29
27
  [path: string]: S | null;
30
- }, M extends string, S extends string>(value: T) => ExtendStringSchemaBuilder<{
31
- [key in keyof (Props & {
32
- enum: T;
33
- })]: (Props & {
34
- enum: T;
35
- })[key];
36
- }, ExtraOmitFields>;
28
+ }, M extends string, S extends string>(value: T) => ExtendStringSchemaBuilder<Merge<Props, {
29
+ enum: T;
30
+ }>, ExtraOmitFields>;
31
+ index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendStringSchemaBuilder<Merge<Props, {
32
+ index: T;
33
+ }>, ExtraOmitFields>;
37
34
  /**
38
35
  * Adds IPv4 validation to the string schema.
39
36
  * Ensures the string is a valid IPv4 address and trims the input. The validation message can be customized.
@@ -42,9 +39,7 @@ export interface StringSchemaBuilder<Props extends {
42
39
  *
43
40
  * @returns A schema builder with IPv4 validation and the `trim` option enabled.
44
41
  */
45
- ipv4: <T extends string = typeof defaultIPv4ValidateMessage>(message?: T) => ExtendStringSchemaBuilder<{
46
- [key in keyof (Props & IPSchema<T>)]: (Props & IPSchema<T>)[key];
47
- }, ExtraOmitFields | 'ipv4' | 'ipv6'>;
42
+ ipv4: <T extends string = typeof defaultIPv4ValidateMessage>(message?: T) => ExtendStringSchemaBuilder<Merge<Props, IPSchema<T>>, ExtraOmitFields | 'ipv4' | 'ipv6'>;
48
43
  /**
49
44
  * Adds IPv6 validation to the string schema.
50
45
  * Ensures the string is a valid IPv6 address and trims the input. The validation message can be customized.
@@ -53,9 +48,7 @@ export interface StringSchemaBuilder<Props extends {
53
48
  *
54
49
  * @returns A schema builder with IPv6 validation and the `trim` option enabled.
55
50
  */
56
- ipv6: <T extends string = typeof defaultIPv6ValidateMessage>(message?: T) => ExtendStringSchemaBuilder<{
57
- [key in keyof (Props & IPSchema<T>)]: (Props & IPSchema<T>)[key];
58
- }, ExtraOmitFields | 'ipv4' | 'ipv6'>;
51
+ ipv6: <T extends string = typeof defaultIPv6ValidateMessage>(message?: T) => ExtendStringSchemaBuilder<Merge<Props, IPSchema<T>>, ExtraOmitFields | 'ipv4' | 'ipv6'>;
59
52
  /**
60
53
  * Sets both the maximum and minimum length of the string.
61
54
  * This method ensures the string length is exactly or within a specified range.
@@ -64,88 +57,41 @@ export interface StringSchemaBuilder<Props extends {
64
57
  *
65
58
  * @returns A schema builder with both `maxlength` and `minlength` options applied.
66
59
  */
67
- length: <T extends L | MaybeReadonly<[L, S]>, L extends number, S extends string>(value: T) => ExtendStringSchemaBuilder<{
68
- [key in keyof (Props & {
69
- maxlength: T;
70
- minlength: T;
71
- })]: (Props & {
72
- maxlength: T;
73
- minlength: T;
74
- })[key];
75
- }, ExtraOmitFields>;
76
- lowercase: ExtendStringSchemaBuilder<{
77
- [key in keyof (Props & {
78
- lowercase: true;
79
- })]: (Props & {
80
- lowercase: true;
81
- })[key];
82
- }, ExtraOmitFields>;
83
- maxlength: <T extends L | MaybeReadonly<[L, S]>, L extends number, S extends string>(value: T) => ExtendStringSchemaBuilder<{
84
- [key in keyof (Props & {
85
- maxlength: T;
86
- })]: (Props & {
87
- maxlength: T;
88
- })[key];
89
- }, ExtraOmitFields>;
90
- minlength: <T extends L | MaybeReadonly<[L, S]>, L extends number, S extends string>(value: T) => ExtendStringSchemaBuilder<{
91
- [key in keyof (Props & {
92
- minlength: T;
93
- })]: (Props & {
94
- minlength: T;
95
- })[key];
96
- }, ExtraOmitFields>;
97
- nonRequired: {
98
- [key in keyof Props]: Props[key];
99
- };
100
- private: ExtendStringSchemaBuilder<{
101
- [key in keyof (Props & {
102
- private: true;
103
- })]: (Props & {
104
- private: true;
105
- })[key];
106
- }, ExtraOmitFields>;
107
- required: {
108
- [key in keyof (Props & {
109
- required: true;
110
- })]: (Props & {
111
- required: true;
112
- })[key];
113
- };
114
- sparse: ExtendStringSchemaBuilder<{
115
- [key in keyof (Props & {
116
- sparse: true;
117
- })]: (Props & {
118
- sparse: true;
119
- })[key];
120
- }, ExtraOmitFields>;
121
- text: ExtendStringSchemaBuilder<{
122
- [key in keyof (Props & {
123
- text: true;
124
- })]: (Props & {
125
- text: true;
126
- })[key];
127
- }, ExtraOmitFields>;
128
- trim: ExtendStringSchemaBuilder<{
129
- [key in keyof (Props & {
130
- trim: true;
131
- })]: (Props & {
132
- trim: true;
133
- })[key];
134
- }, ExtraOmitFields>;
135
- unique: ExtendStringSchemaBuilder<{
136
- [key in keyof (Props & {
137
- unique: true;
138
- })]: (Props & {
139
- unique: true;
140
- })[key];
141
- }, ExtraOmitFields>;
142
- uppercase: ExtendStringSchemaBuilder<{
143
- [key in keyof (Props & {
144
- uppercase: true;
145
- })]: (Props & {
146
- uppercase: true;
147
- })[key];
148
- }, ExtraOmitFields>;
60
+ length: <T extends L | Readonlyable<[L, S]>, L extends number, S extends string>(value: T) => ExtendStringSchemaBuilder<Merge<Props, {
61
+ maxlength: T;
62
+ minlength: T;
63
+ }>, ExtraOmitFields>;
64
+ lowercase: ExtendStringSchemaBuilder<Merge<Props, {
65
+ lowercase: true;
66
+ }>, ExtraOmitFields>;
67
+ maxlength: <T extends L | Readonlyable<[L, S]>, L extends number, S extends string>(value: T) => ExtendStringSchemaBuilder<Merge<Props, {
68
+ maxlength: T;
69
+ }>, ExtraOmitFields>;
70
+ minlength: <T extends L | Readonlyable<[L, S]>, L extends number, S extends string>(value: T) => ExtendStringSchemaBuilder<Merge<Props, {
71
+ minlength: T;
72
+ }>, ExtraOmitFields>;
73
+ nonRequired: Props;
74
+ private: ExtendStringSchemaBuilder<Merge<Props, {
75
+ private: true;
76
+ }>, ExtraOmitFields>;
77
+ required: Merge<Props, {
78
+ required: true;
79
+ }>;
80
+ sparse: ExtendStringSchemaBuilder<Merge<Props, {
81
+ sparse: true;
82
+ }>, ExtraOmitFields>;
83
+ text: ExtendStringSchemaBuilder<Merge<Props, {
84
+ text: true;
85
+ }>, ExtraOmitFields>;
86
+ trim: ExtendStringSchemaBuilder<Merge<Props, {
87
+ trim: true;
88
+ }>, ExtraOmitFields>;
89
+ unique: ExtendStringSchemaBuilder<Merge<Props, {
90
+ unique: true;
91
+ }>, ExtraOmitFields>;
92
+ uppercase: ExtendStringSchemaBuilder<Merge<Props, {
93
+ uppercase: true;
94
+ }>, ExtraOmitFields>;
149
95
  }
150
96
  declare const defaultIPv4ValidateMessage = "`{VALUE}` is not a valid IPv4 address for path `{PATH}`.";
151
97
  declare const defaultIPv6ValidateMessage = "`{VALUE}` is not a valid IPv6 address for path `{PATH}`.";
package/types/data.d.ts CHANGED
@@ -48,15 +48,21 @@ declare global {
48
48
  */
49
49
  type BaseMongooseModelData<CreatedAtField extends boolean = true, UpdatedAtField extends boolean = true> = OmitNever<LocalBaseMongooseModelData<CreatedAtField, UpdatedAtField>>;
50
50
  /**
51
- * Type definition to omit timestamp fields and other specified fields from a given type.
51
+ * Type definition to omit timestamp fields (`createdAt`, `updatedAt`)
52
+ * and other specified fields (`OT`) from a given type `T`.
52
53
  *
53
- * This type alias uses `Omit` to remove `createdAt`, `updatedAt`, and any other specified fields (`OT`)
54
- * from the type `T`.
54
+ * @template T - The type from which to omit fields.
55
+ * @template OT - Additional fields to omit, specified as a union of keys in `T`.
56
+ */
57
+ type ExceptMongooseTimestampAndOtherFields<T, OT extends keyof T> = Omit<T, 'createdAt' | 'updatedAt' | OT>;
58
+ /**
59
+ * Type definition to omit timestamp fields (`createdAt`, `updatedAt`)
60
+ * and other specified fields (`OT`) from a given type `T`.
55
61
  *
56
62
  * @template T - The type from which to omit fields.
57
- * @template OT - Additional fields to omit, specified as a union of string, number, or symbol.
63
+ * @template OT - Additional fields to omit, specified as a union of string, number, or symbol keys.
58
64
  */
59
- type OmitMongooseTimestampAndOtherFields<T, OT extends number | symbol | string = ''> = Omit<T, 'createdAt' | 'updatedAt' | OT>;
65
+ type OmitMongooseTimestampAndOtherFields<T, OT extends number | symbol | string = never> = Omit<T, 'createdAt' | 'updatedAt' | OT>;
60
66
  /**
61
67
  * Type definition for including timestamp fields conditionally based on the provided flags.
62
68
  *
@@ -1,6 +1,6 @@
1
1
  import type { Connection, Schema, SchemaTimestampsConfig } from 'mongoose';
2
2
  import type { buildMongooseModel } from '../utils';
3
- export type DoNotUseOrRemoveThisType = typeof buildMongooseModel;
3
+ export type DoNotRemoveOrUseThisType = typeof buildMongooseModel;
4
4
  /**
5
5
  * Interface for options used when building a Mongoose model.
6
6
  *
package/types/utils.d.ts CHANGED
@@ -1 +1 @@
1
- export type MaybeReadonly<T> = T | Readonly<T>;
1
+ export type Readonlyable<T> = T | Readonly<T>;
package/utils.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import mongoose from 'mongoose';
2
2
  import { setCustomMongooseOptions } from './options';
3
3
  import type { BuildMongooseModelOptions } from './types/options';
4
- export type DoNotUseOrRemoveThisType = typeof setCustomMongooseOptions;
4
+ export type DoNotRemoveOrUseThisType = typeof setCustomMongooseOptions;
5
5
  /**
6
6
  * Builds and returns a Mongoose model with specified schema and options.
7
7
  *