@kikiutils/mongoose 2.0.2 → 2.1.1
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/package.json +39 -38
- package/plugins/normalize.cjs +3 -3
- package/plugins/normalize.d.ts +1 -1
- package/plugins/normalize.mjs +3 -3
- package/schema-builders/base.cjs +18 -13
- package/schema-builders/base.d.ts +1 -1
- package/schema-builders/base.mjs +18 -13
- package/schema-builders/boolean.d.ts +9 -7
- package/schema-builders/date.d.ts +7 -4
- package/schema-builders/decimal128.cjs +8 -6
- package/schema-builders/decimal128.d.ts +13 -10
- package/schema-builders/decimal128.mjs +8 -6
- package/schema-builders/index.cjs +4 -4
- package/schema-builders/index.d.ts +6 -6
- package/schema-builders/index.mjs +3 -3
- package/schema-builders/number.d.ts +7 -4
- package/schema-builders/object-id.d.ts +7 -4
- package/schema-builders/ref.d.ts +7 -4
- package/schema-builders/string.cjs +9 -5
- package/schema-builders/string.d.ts +10 -7
- package/schema-builders/string.mjs +9 -5
- package/types/aggregate-paginate.d.ts +2 -2
- package/types/data.d.ts +11 -5
- package/types/index.d.ts +6 -6
- package/types/options.d.ts +1 -1
- package/types/paginate.d.ts +13 -9
- package/utils.cjs +2 -1
- package/utils.d.ts +2 -2
- package/utils.mjs +2 -1
package/package.json
CHANGED
|
@@ -1,39 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kikiutils/mongoose",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.1",
|
|
4
4
|
"description": "A Mongoose plugin for enhanced JSON normalization and common schema creation, with built-in support for pagination and automatic Decimal128 conversion.",
|
|
5
|
-
"license": "MIT",
|
|
6
5
|
"author": "kiki-kanri",
|
|
6
|
+
"license": "MIT",
|
|
7
7
|
"repository": {
|
|
8
8
|
"type": "git",
|
|
9
9
|
"url": "git+https://github.com/kiki-kanri/kikiutils-node-mongoose.git"
|
|
10
10
|
},
|
|
11
|
-
"scripts": {
|
|
12
|
-
"build": "ts-project-builder ./src/constants.ts ./src/options.ts ./src/schema-builders/index.ts ./src/utils.ts --clean --preserve-modules",
|
|
13
|
-
"bumplog": "changelogen --bump",
|
|
14
|
-
"release": "pnpm run build && pnpm run test && changelogen --push --release && sh ./build-and-publish.sh",
|
|
15
|
-
"test": "jest --coverage"
|
|
16
|
-
},
|
|
17
|
-
"dependencies": {
|
|
18
|
-
"decimal.js": "^10.4.3",
|
|
19
|
-
"lodash": "^4.17.21",
|
|
20
|
-
"mongoose": "^8.8.0",
|
|
21
|
-
"mongoose-aggregate-paginate-v2": "^1.1.2",
|
|
22
|
-
"mongoose-paginate-v2": "^1.8.5",
|
|
23
|
-
"type-fest": "^4.26.1"
|
|
24
|
-
},
|
|
25
|
-
"devDependencies": {
|
|
26
|
-
"@kikiutils/changelogen": "^0.7.0",
|
|
27
|
-
"@kikiutils/prettierrc": "^1.3.2",
|
|
28
|
-
"@kikiutils/tsconfigs": "^3.0.2",
|
|
29
|
-
"@types/jest": "^29.5.14",
|
|
30
|
-
"@types/lodash": "^4.17.13",
|
|
31
|
-
"@types/node": "^22.8.6",
|
|
32
|
-
"jest": "^29.7.0",
|
|
33
|
-
"ts-jest": "^29.2.5",
|
|
34
|
-
"ts-project-builder": "^3.3.1",
|
|
35
|
-
"tslib": "^2.8.1"
|
|
36
|
-
},
|
|
37
11
|
"keywords": [
|
|
38
12
|
"Decimal128",
|
|
39
13
|
"json",
|
|
@@ -44,24 +18,21 @@
|
|
|
44
18
|
"plugin",
|
|
45
19
|
"schema"
|
|
46
20
|
],
|
|
47
|
-
"engines": {
|
|
48
|
-
"node": ">=18.12.1"
|
|
49
|
-
},
|
|
50
21
|
"exports": {
|
|
51
22
|
"./*": {
|
|
23
|
+
"types": "./*.d.ts",
|
|
52
24
|
"import": "./*.mjs",
|
|
53
|
-
"require": "./*.cjs"
|
|
54
|
-
"types": "./*.d.ts"
|
|
25
|
+
"require": "./*.cjs"
|
|
55
26
|
},
|
|
56
27
|
"./_internals": {
|
|
28
|
+
"types": null,
|
|
57
29
|
"import": null,
|
|
58
|
-
"require": null
|
|
59
|
-
"types": null
|
|
30
|
+
"require": null
|
|
60
31
|
},
|
|
61
32
|
"./schema-builders": {
|
|
33
|
+
"types": "./schema-builders/index.d.ts",
|
|
62
34
|
"import": "./schema-builders/index.mjs",
|
|
63
|
-
"require": "./schema-builders/index.cjs"
|
|
64
|
-
"types": "./schema-builders/index.d.ts"
|
|
35
|
+
"require": "./schema-builders/index.cjs"
|
|
65
36
|
},
|
|
66
37
|
"./types": {
|
|
67
38
|
"types": "./types/index.d.ts"
|
|
@@ -70,5 +41,35 @@
|
|
|
70
41
|
"types": "./types/*.d.ts"
|
|
71
42
|
}
|
|
72
43
|
},
|
|
73
|
-
"
|
|
44
|
+
"engines": {
|
|
45
|
+
"node": ">=18.12.1"
|
|
46
|
+
},
|
|
47
|
+
"scripts": {
|
|
48
|
+
"build": "ts-project-builder ./src/constants.ts ./src/options.ts ./src/schema-builders/index.ts ./src/utils.ts --clean --preserve-modules",
|
|
49
|
+
"bumplog": "changelogen --bump",
|
|
50
|
+
"lint": "eslint",
|
|
51
|
+
"lint:fix": "eslint --fix",
|
|
52
|
+
"release": "pnpm run lint && pnpm run build && pnpm run test && changelogen --push --release && sh ./build-and-publish.sh",
|
|
53
|
+
"test": "jest --coverage"
|
|
54
|
+
},
|
|
55
|
+
"dependencies": {
|
|
56
|
+
"decimal.js": "^10.4.3",
|
|
57
|
+
"lodash": "^4.17.21",
|
|
58
|
+
"mongoose": "^8.8.1",
|
|
59
|
+
"mongoose-aggregate-paginate-v2": "^1.1.2",
|
|
60
|
+
"mongoose-paginate-v2": "^1.8.5",
|
|
61
|
+
"type-fest": "^4.27.0"
|
|
62
|
+
},
|
|
63
|
+
"devDependencies": {
|
|
64
|
+
"@kikiutils/changelogen": "^0.7.0",
|
|
65
|
+
"@kikiutils/eslint-config": "^0.3.1",
|
|
66
|
+
"@kikiutils/tsconfigs": "^3.0.2",
|
|
67
|
+
"@types/jest": "^29.5.14",
|
|
68
|
+
"@types/lodash": "^4.17.13",
|
|
69
|
+
"@types/node": "^22.9.0",
|
|
70
|
+
"jest": "^29.7.0",
|
|
71
|
+
"ts-jest": "^29.2.5",
|
|
72
|
+
"ts-project-builder": "^3.3.2",
|
|
73
|
+
"tslib": "^2.8.1"
|
|
74
|
+
}
|
|
74
75
|
}
|
package/plugins/normalize.cjs
CHANGED
|
@@ -23,7 +23,7 @@ const { get: lodashGet, set: lodashSet, unset: lodashUnset } = lodash__default;
|
|
|
23
23
|
*
|
|
24
24
|
* @param schema - The Mongoose schema to apply the plugin to.
|
|
25
25
|
*/
|
|
26
|
-
|
|
26
|
+
function mongooseNormalizePlugin(schema) {
|
|
27
27
|
const toJSON = schema.get('toJSON');
|
|
28
28
|
const toJSONTransform = toJSON?.transform;
|
|
29
29
|
schema.set('toJSON', {
|
|
@@ -45,9 +45,9 @@ const mongooseNormalizePlugin = (schema) => {
|
|
|
45
45
|
if (toJSONTransform && typeof toJSONTransform !== 'boolean')
|
|
46
46
|
return toJSONTransform(doc, copiedRet, options);
|
|
47
47
|
return copiedRet;
|
|
48
|
-
}
|
|
48
|
+
},
|
|
49
49
|
});
|
|
50
|
-
}
|
|
50
|
+
}
|
|
51
51
|
|
|
52
52
|
exports.default = mongooseNormalizePlugin;
|
|
53
53
|
exports.mongooseNormalizePlugin = mongooseNormalizePlugin;
|
package/plugins/normalize.d.ts
CHANGED
|
@@ -13,5 +13,5 @@ import type { Schema } from 'mongoose';
|
|
|
13
13
|
*
|
|
14
14
|
* @param schema - The Mongoose schema to apply the plugin to.
|
|
15
15
|
*/
|
|
16
|
-
export declare
|
|
16
|
+
export declare function mongooseNormalizePlugin<S extends Schema>(schema: S): void;
|
|
17
17
|
export default mongooseNormalizePlugin;
|
package/plugins/normalize.mjs
CHANGED
|
@@ -15,7 +15,7 @@ const { get: lodashGet, set: lodashSet, unset: lodashUnset } = lodash;
|
|
|
15
15
|
*
|
|
16
16
|
* @param schema - The Mongoose schema to apply the plugin to.
|
|
17
17
|
*/
|
|
18
|
-
|
|
18
|
+
function mongooseNormalizePlugin(schema) {
|
|
19
19
|
const toJSON = schema.get('toJSON');
|
|
20
20
|
const toJSONTransform = toJSON?.transform;
|
|
21
21
|
schema.set('toJSON', {
|
|
@@ -37,8 +37,8 @@ const mongooseNormalizePlugin = (schema) => {
|
|
|
37
37
|
if (toJSONTransform && typeof toJSONTransform !== 'boolean')
|
|
38
38
|
return toJSONTransform(doc, copiedRet, options);
|
|
39
39
|
return copiedRet;
|
|
40
|
-
}
|
|
40
|
+
},
|
|
41
41
|
});
|
|
42
|
-
}
|
|
42
|
+
}
|
|
43
43
|
|
|
44
44
|
export { mongooseNormalizePlugin as default, mongooseNormalizePlugin };
|
package/schema-builders/base.cjs
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
3
|
+
const isFunctionKeys = new Set([
|
|
4
|
+
'default',
|
|
5
|
+
'enum',
|
|
6
|
+
'index',
|
|
7
|
+
'max',
|
|
8
|
+
'maxlength',
|
|
9
|
+
'min',
|
|
10
|
+
'minlength',
|
|
11
|
+
]);
|
|
12
|
+
function createBaseSchemaBuilderFactory(type) {
|
|
4
13
|
return (schema = {}) => {
|
|
5
14
|
schema.type = type;
|
|
6
15
|
return new Proxy(Object.freeze({}), {
|
|
@@ -9,25 +18,21 @@ const createBaseSchemaBuilderFactory = (type) => {
|
|
|
9
18
|
throw new Error('Cannot use symbol as a schema attribute');
|
|
10
19
|
if (key in schema)
|
|
11
20
|
throw new Error(`Duplicate schema attribute: ${key}`);
|
|
12
|
-
if (isFunctionKeys.has(key))
|
|
13
|
-
return (value) =>
|
|
21
|
+
if (isFunctionKeys.has(key)) {
|
|
22
|
+
return (value) => {
|
|
23
|
+
schema[key] = value;
|
|
24
|
+
return receiver;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
14
27
|
if (key === 'nonRequired')
|
|
15
28
|
return { ...schema };
|
|
16
29
|
if (key === 'required')
|
|
17
30
|
return { ...schema, required: true };
|
|
18
31
|
schema[key] = true;
|
|
19
32
|
return receiver;
|
|
20
|
-
}
|
|
33
|
+
},
|
|
21
34
|
});
|
|
22
35
|
};
|
|
23
|
-
}
|
|
24
|
-
const isFunctionKeys = new Set([
|
|
25
|
-
'default',
|
|
26
|
-
'enum',
|
|
27
|
-
'max',
|
|
28
|
-
'maxlength',
|
|
29
|
-
'min',
|
|
30
|
-
'minlength'
|
|
31
|
-
]);
|
|
36
|
+
}
|
|
32
37
|
|
|
33
38
|
exports.createBaseSchemaBuilderFactory = createBaseSchemaBuilderFactory;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { Schema } from 'mongoose';
|
|
2
|
-
export declare
|
|
2
|
+
export declare function createBaseSchemaBuilderFactory<Builder = Readonly<Record<string, any>>>(type: BooleanConstructor | DateConstructor | NumberConstructor | Schema.Types.ObjectId['constructor'] | StringConstructor): (schema?: Record<string, any>) => Builder;
|
package/schema-builders/base.mjs
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
|
-
const
|
|
1
|
+
const isFunctionKeys = new Set([
|
|
2
|
+
'default',
|
|
3
|
+
'enum',
|
|
4
|
+
'index',
|
|
5
|
+
'max',
|
|
6
|
+
'maxlength',
|
|
7
|
+
'min',
|
|
8
|
+
'minlength',
|
|
9
|
+
]);
|
|
10
|
+
function createBaseSchemaBuilderFactory(type) {
|
|
2
11
|
return (schema = {}) => {
|
|
3
12
|
schema.type = type;
|
|
4
13
|
return new Proxy(Object.freeze({}), {
|
|
@@ -7,25 +16,21 @@ const createBaseSchemaBuilderFactory = (type) => {
|
|
|
7
16
|
throw new Error('Cannot use symbol as a schema attribute');
|
|
8
17
|
if (key in schema)
|
|
9
18
|
throw new Error(`Duplicate schema attribute: ${key}`);
|
|
10
|
-
if (isFunctionKeys.has(key))
|
|
11
|
-
return (value) =>
|
|
19
|
+
if (isFunctionKeys.has(key)) {
|
|
20
|
+
return (value) => {
|
|
21
|
+
schema[key] = value;
|
|
22
|
+
return receiver;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
12
25
|
if (key === 'nonRequired')
|
|
13
26
|
return { ...schema };
|
|
14
27
|
if (key === 'required')
|
|
15
28
|
return { ...schema, required: true };
|
|
16
29
|
schema[key] = true;
|
|
17
30
|
return receiver;
|
|
18
|
-
}
|
|
31
|
+
},
|
|
19
32
|
});
|
|
20
33
|
};
|
|
21
|
-
}
|
|
22
|
-
const isFunctionKeys = new Set([
|
|
23
|
-
'default',
|
|
24
|
-
'enum',
|
|
25
|
-
'max',
|
|
26
|
-
'maxlength',
|
|
27
|
-
'min',
|
|
28
|
-
'minlength'
|
|
29
|
-
]);
|
|
34
|
+
}
|
|
30
35
|
|
|
31
36
|
export { createBaseSchemaBuilderFactory };
|
|
@@ -1,13 +1,16 @@
|
|
|
1
|
-
import type { BooleanSchemaDefinition, DefaultType } from 'mongoose';
|
|
1
|
+
import type { BooleanSchemaDefinition, DefaultType, IndexDirection, IndexOptions } from 'mongoose';
|
|
2
2
|
import type { Merge } from 'type-fest';
|
|
3
|
-
type
|
|
3
|
+
export type ExtendBooleanSchemaBuilder<Props extends BaseBooleanSchemaProps, ExtraOmitFields extends string> = Omit<BooleanSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
|
|
4
|
+
export interface BaseBooleanSchemaProps {
|
|
4
5
|
type: BooleanSchemaDefinition;
|
|
5
|
-
}
|
|
6
|
-
export
|
|
7
|
-
export interface BooleanSchemaBuilder<Props extends BaseProps = BaseProps, ExtraOmitFields extends string = never> {
|
|
6
|
+
}
|
|
7
|
+
export interface BooleanSchemaBuilder<Props extends BaseBooleanSchemaProps = BaseBooleanSchemaProps, ExtraOmitFields extends string = never> {
|
|
8
8
|
default: <T extends DefaultType<D> | ((this: any, doc: any) => DefaultType<D>) | null, D extends boolean>(value: T) => ExtendBooleanSchemaBuilder<Merge<Props, {
|
|
9
9
|
default: T;
|
|
10
10
|
}>, ExtraOmitFields>;
|
|
11
|
+
index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendBooleanSchemaBuilder<Merge<Props, {
|
|
12
|
+
index: T;
|
|
13
|
+
}>, ExtraOmitFields>;
|
|
11
14
|
nonRequired: Props;
|
|
12
15
|
private: ExtendBooleanSchemaBuilder<Merge<Props, {
|
|
13
16
|
private: true;
|
|
@@ -22,5 +25,4 @@ export interface BooleanSchemaBuilder<Props extends BaseProps = BaseProps, Extra
|
|
|
22
25
|
unique: true;
|
|
23
26
|
}>, ExtraOmitFields>;
|
|
24
27
|
}
|
|
25
|
-
export declare const booleanSchemaBuilder: (schema?: Record<string, any>) => BooleanSchemaBuilder<
|
|
26
|
-
export {};
|
|
28
|
+
export declare const booleanSchemaBuilder: (schema?: Record<string, any>) => BooleanSchemaBuilder<BaseBooleanSchemaProps, never>;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import type { DefaultType,
|
|
1
|
+
import type { DateSchemaDefinition, DefaultType, IndexDirection, IndexOptions } from 'mongoose';
|
|
2
2
|
import type { Merge } from 'type-fest';
|
|
3
3
|
import type { Readonlyable } from '../types/utils';
|
|
4
|
-
type BaseProps = {
|
|
5
|
-
type: DateSchemaDefinition;
|
|
6
|
-
};
|
|
7
4
|
export type ExtendDateSchemaBuilder<Props extends BaseProps, ExtraOmitFields extends string> = Omit<DateSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
|
|
5
|
+
interface BaseProps {
|
|
6
|
+
type: DateSchemaDefinition;
|
|
7
|
+
}
|
|
8
8
|
export interface DateSchemaBuilder<Props extends {
|
|
9
9
|
type: DateSchemaDefinition;
|
|
10
10
|
} = {
|
|
@@ -21,6 +21,9 @@ export interface DateSchemaBuilder<Props extends {
|
|
|
21
21
|
}, D extends NativeDate, M extends string>(value: T) => ExtendDateSchemaBuilder<Merge<Props, {
|
|
22
22
|
enum: T;
|
|
23
23
|
}>, ExtraOmitFields>;
|
|
24
|
+
index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendDateSchemaBuilder<Merge<Props, {
|
|
25
|
+
index: T;
|
|
26
|
+
}>, ExtraOmitFields>;
|
|
24
27
|
max: <T extends D | Readonlyable<[D, S]>, D extends NativeDate, S extends string>(value: T) => ExtendDateSchemaBuilder<Merge<Props, {
|
|
25
28
|
max: T;
|
|
26
29
|
}>, ExtraOmitFields>;
|
|
@@ -9,22 +9,24 @@ const _interopDefaultCompat = e => e && typeof e === 'object' && 'default' in e
|
|
|
9
9
|
const Decimal__default = /*#__PURE__*/_interopDefaultCompat(Decimal);
|
|
10
10
|
|
|
11
11
|
const baseBuilderFactory = base.createBaseSchemaBuilderFactory(mongoose.Schema.Types.Decimal128);
|
|
12
|
-
|
|
12
|
+
function decimal128SchemaBuilder() {
|
|
13
13
|
const schema = {};
|
|
14
14
|
const baseBuilder = baseBuilderFactory(schema);
|
|
15
15
|
return new Proxy(baseBuilder, {
|
|
16
16
|
get(target, key, receiver) {
|
|
17
17
|
if (key === 'setRoundAndToFixedSetter') {
|
|
18
18
|
return (places = 2, rounding = Decimal__default.ROUND_DOWN) => {
|
|
19
|
-
schema
|
|
19
|
+
schema.set = (value) => new Decimal__default(value.toString()).toFixed(places, rounding);
|
|
20
20
|
return receiver;
|
|
21
21
|
};
|
|
22
22
|
}
|
|
23
|
-
if (key === 'setToStringGetter')
|
|
24
|
-
|
|
23
|
+
if (key === 'setToStringGetter') {
|
|
24
|
+
(schema.get = (value) => value.toString());
|
|
25
|
+
return receiver;
|
|
26
|
+
}
|
|
25
27
|
return Reflect.get(target, key, receiver);
|
|
26
|
-
}
|
|
28
|
+
},
|
|
27
29
|
});
|
|
28
|
-
}
|
|
30
|
+
}
|
|
29
31
|
|
|
30
32
|
exports.decimal128SchemaBuilder = decimal128SchemaBuilder;
|
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
import Decimal from 'decimal.js';
|
|
2
2
|
import { Schema } from 'mongoose';
|
|
3
|
-
import type { DefaultType, Types } from 'mongoose';
|
|
3
|
+
import type { DefaultType, IndexDirection, IndexOptions, Types } from 'mongoose';
|
|
4
4
|
import type { Merge } from 'type-fest';
|
|
5
5
|
import type { Readonlyable } from '../types/utils';
|
|
6
|
-
type BaseProps = {
|
|
7
|
-
type: Schema.Types.Decimal128;
|
|
8
|
-
};
|
|
9
6
|
export type ExtendDecimal128SchemaBuilder<Props extends BaseProps, ExtraOmitFields extends string> = Omit<Decimal128SchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
|
|
10
|
-
|
|
7
|
+
interface BaseProps {
|
|
8
|
+
type: Schema.Types.Decimal128;
|
|
9
|
+
}
|
|
10
|
+
interface ToStringGetterSchema {
|
|
11
11
|
get: (value: Types.Decimal128) => string;
|
|
12
|
-
}
|
|
13
|
-
|
|
12
|
+
}
|
|
13
|
+
interface ToStringSetterSchema {
|
|
14
14
|
set: (value: {
|
|
15
|
-
toString()
|
|
15
|
+
toString: () => string;
|
|
16
16
|
}) => string;
|
|
17
|
-
}
|
|
17
|
+
}
|
|
18
18
|
export interface Decimal128SchemaBuilder<Props extends {
|
|
19
19
|
type: Schema.Types.Decimal128;
|
|
20
20
|
} = {
|
|
@@ -31,6 +31,9 @@ export interface Decimal128SchemaBuilder<Props extends {
|
|
|
31
31
|
}, D extends Types.Decimal128, M extends string>(value: T) => ExtendDecimal128SchemaBuilder<Merge<Props, {
|
|
32
32
|
enum: T;
|
|
33
33
|
}>, ExtraOmitFields>;
|
|
34
|
+
index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendDecimal128SchemaBuilder<Merge<Props, {
|
|
35
|
+
index: T;
|
|
36
|
+
}>, ExtraOmitFields>;
|
|
34
37
|
nonRequired: Props;
|
|
35
38
|
private: ExtendDecimal128SchemaBuilder<Merge<Props, {
|
|
36
39
|
private: true;
|
|
@@ -66,5 +69,5 @@ export interface Decimal128SchemaBuilder<Props extends {
|
|
|
66
69
|
unique: true;
|
|
67
70
|
}>, ExtraOmitFields>;
|
|
68
71
|
}
|
|
69
|
-
export declare
|
|
72
|
+
export declare function decimal128SchemaBuilder(): Decimal128SchemaBuilder;
|
|
70
73
|
export {};
|
|
@@ -3,22 +3,24 @@ import { Schema } from 'mongoose';
|
|
|
3
3
|
import { createBaseSchemaBuilderFactory } from './base.mjs';
|
|
4
4
|
|
|
5
5
|
const baseBuilderFactory = createBaseSchemaBuilderFactory(Schema.Types.Decimal128);
|
|
6
|
-
|
|
6
|
+
function decimal128SchemaBuilder() {
|
|
7
7
|
const schema = {};
|
|
8
8
|
const baseBuilder = baseBuilderFactory(schema);
|
|
9
9
|
return new Proxy(baseBuilder, {
|
|
10
10
|
get(target, key, receiver) {
|
|
11
11
|
if (key === 'setRoundAndToFixedSetter') {
|
|
12
12
|
return (places = 2, rounding = Decimal.ROUND_DOWN) => {
|
|
13
|
-
schema
|
|
13
|
+
schema.set = (value) => new Decimal(value.toString()).toFixed(places, rounding);
|
|
14
14
|
return receiver;
|
|
15
15
|
};
|
|
16
16
|
}
|
|
17
|
-
if (key === 'setToStringGetter')
|
|
18
|
-
|
|
17
|
+
if (key === 'setToStringGetter') {
|
|
18
|
+
(schema.get = (value) => value.toString());
|
|
19
|
+
return receiver;
|
|
20
|
+
}
|
|
19
21
|
return Reflect.get(target, key, receiver);
|
|
20
|
-
}
|
|
22
|
+
},
|
|
21
23
|
});
|
|
22
|
-
}
|
|
24
|
+
}
|
|
23
25
|
|
|
24
26
|
export { decimal128SchemaBuilder };
|
|
@@ -2,27 +2,27 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
+
const boolean = require('./boolean.cjs');
|
|
5
6
|
const date = require('./date.cjs');
|
|
6
7
|
const decimal128 = require('./decimal128.cjs');
|
|
7
|
-
const boolean = require('./boolean.cjs');
|
|
8
8
|
const number = require('./number.cjs');
|
|
9
9
|
const objectId = require('./object-id.cjs');
|
|
10
10
|
const ref = require('./ref.cjs');
|
|
11
11
|
const string = require('./string.cjs');
|
|
12
12
|
|
|
13
13
|
const schemaBuilders = {
|
|
14
|
+
boolean: boolean.booleanSchemaBuilder,
|
|
14
15
|
date: date.dateSchemaBuilder,
|
|
15
16
|
decimal128: decimal128.decimal128SchemaBuilder,
|
|
16
|
-
boolean: boolean.booleanSchemaBuilder,
|
|
17
17
|
number: number.numberSchemaBuilder,
|
|
18
18
|
objectId: objectId.objectIdSchemaBuilder,
|
|
19
19
|
ref: ref.refSchemaBuilder,
|
|
20
|
-
string: string.stringSchemaBuilder
|
|
20
|
+
string: string.stringSchemaBuilder,
|
|
21
21
|
};
|
|
22
22
|
|
|
23
|
+
exports.booleanSchemaBuilder = boolean.booleanSchemaBuilder;
|
|
23
24
|
exports.dateSchemaBuilder = date.dateSchemaBuilder;
|
|
24
25
|
exports.decimal128SchemaBuilder = decimal128.decimal128SchemaBuilder;
|
|
25
|
-
exports.booleanSchemaBuilder = boolean.booleanSchemaBuilder;
|
|
26
26
|
exports.numberSchemaBuilder = number.numberSchemaBuilder;
|
|
27
27
|
exports.refSchemaBuilder = ref.refSchemaBuilder;
|
|
28
28
|
exports.stringSchemaBuilder = string.stringSchemaBuilder;
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
+
import { decimal128SchemaBuilder } from './decimal128';
|
|
2
|
+
import { stringSchemaBuilder } from './string';
|
|
3
|
+
export * from './boolean';
|
|
1
4
|
export * from './date';
|
|
2
5
|
export * from './decimal128';
|
|
3
|
-
export * from './boolean';
|
|
4
6
|
export * from './number';
|
|
5
7
|
export * from './ref';
|
|
6
8
|
export * from './string';
|
|
7
9
|
export declare const schemaBuilders: {
|
|
10
|
+
boolean: (schema?: Record<string, any>) => import("./boolean").BooleanSchemaBuilder<import("./boolean").BaseBooleanSchemaProps, never>;
|
|
8
11
|
date: (schema?: Record<string, any>) => import("./date").DateSchemaBuilder<{
|
|
9
12
|
type: import("mongoose").DateSchemaDefinition;
|
|
10
13
|
}, never>;
|
|
11
|
-
decimal128:
|
|
12
|
-
boolean: (schema?: Record<string, any>) => import("./boolean").BooleanSchemaBuilder<{
|
|
13
|
-
type: import("mongoose").BooleanSchemaDefinition;
|
|
14
|
-
}, never>;
|
|
14
|
+
decimal128: typeof decimal128SchemaBuilder;
|
|
15
15
|
number: (schema?: Record<string, any>) => import("./number").NumberSchemaBuilder<{
|
|
16
16
|
type: import("mongoose").NumberSchemaDefinition;
|
|
17
17
|
}, never>;
|
|
@@ -22,6 +22,6 @@ export declare const schemaBuilders: {
|
|
|
22
22
|
ref: T;
|
|
23
23
|
type: import("mongoose").ObjectIdSchemaDefinition;
|
|
24
24
|
}>;
|
|
25
|
-
string:
|
|
25
|
+
string: typeof stringSchemaBuilder;
|
|
26
26
|
};
|
|
27
27
|
export default schemaBuilders;
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
+
import { booleanSchemaBuilder } from './boolean.mjs';
|
|
1
2
|
import { dateSchemaBuilder } from './date.mjs';
|
|
2
3
|
import { decimal128SchemaBuilder } from './decimal128.mjs';
|
|
3
|
-
import { booleanSchemaBuilder } from './boolean.mjs';
|
|
4
4
|
import { numberSchemaBuilder } from './number.mjs';
|
|
5
5
|
import { objectIdSchemaBuilder } from './object-id.mjs';
|
|
6
6
|
import { refSchemaBuilder } from './ref.mjs';
|
|
7
7
|
import { stringSchemaBuilder } from './string.mjs';
|
|
8
8
|
|
|
9
9
|
const schemaBuilders = {
|
|
10
|
+
boolean: booleanSchemaBuilder,
|
|
10
11
|
date: dateSchemaBuilder,
|
|
11
12
|
decimal128: decimal128SchemaBuilder,
|
|
12
|
-
boolean: booleanSchemaBuilder,
|
|
13
13
|
number: numberSchemaBuilder,
|
|
14
14
|
objectId: objectIdSchemaBuilder,
|
|
15
15
|
ref: refSchemaBuilder,
|
|
16
|
-
string: stringSchemaBuilder
|
|
16
|
+
string: stringSchemaBuilder,
|
|
17
17
|
};
|
|
18
18
|
|
|
19
19
|
export { booleanSchemaBuilder, dateSchemaBuilder, decimal128SchemaBuilder, schemaBuilders as default, numberSchemaBuilder, refSchemaBuilder, schemaBuilders, stringSchemaBuilder };
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import type { DefaultType, NumberSchemaDefinition } from 'mongoose';
|
|
1
|
+
import type { DefaultType, IndexDirection, IndexOptions, NumberSchemaDefinition } from 'mongoose';
|
|
2
2
|
import type { Merge } from 'type-fest';
|
|
3
3
|
import type { Readonlyable } from '../types/utils';
|
|
4
|
-
type BaseProps = {
|
|
5
|
-
type: NumberSchemaDefinition;
|
|
6
|
-
};
|
|
7
4
|
export type ExtendNumberSchemaBuilder<Props extends BaseProps, ExtraOmitFields extends string> = Omit<NumberSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
|
|
5
|
+
interface BaseProps {
|
|
6
|
+
type: NumberSchemaDefinition;
|
|
7
|
+
}
|
|
8
8
|
export interface NumberSchemaBuilder<Props extends {
|
|
9
9
|
type: NumberSchemaDefinition;
|
|
10
10
|
} = {
|
|
@@ -21,6 +21,9 @@ export interface NumberSchemaBuilder<Props extends {
|
|
|
21
21
|
}, M extends string, N extends number>(value: T) => ExtendNumberSchemaBuilder<Merge<Props, {
|
|
22
22
|
enum: T;
|
|
23
23
|
}>, ExtraOmitFields>;
|
|
24
|
+
index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendNumberSchemaBuilder<Merge<Props, {
|
|
25
|
+
index: T;
|
|
26
|
+
}>, ExtraOmitFields>;
|
|
24
27
|
max: <T extends N | Readonlyable<[N, S]>, N extends number, S extends string>(value: T) => ExtendNumberSchemaBuilder<Merge<Props, {
|
|
25
28
|
max: T;
|
|
26
29
|
}>, ExtraOmitFields>;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import type { DefaultType, ObjectIdSchemaDefinition, Types } from 'mongoose';
|
|
1
|
+
import type { DefaultType, IndexDirection, IndexOptions, ObjectIdSchemaDefinition, Types } from 'mongoose';
|
|
2
2
|
import type { Merge } from 'type-fest';
|
|
3
3
|
import type { Readonlyable } from '../types/utils';
|
|
4
|
-
type BaseProps = {
|
|
5
|
-
type: ObjectIdSchemaDefinition;
|
|
6
|
-
};
|
|
7
4
|
export type ExtendObjectIdSchemaBuilder<Props extends BaseProps, ExtraOmitFields extends string> = Omit<ObjectIdSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
|
|
5
|
+
interface BaseProps {
|
|
6
|
+
type: ObjectIdSchemaDefinition;
|
|
7
|
+
}
|
|
8
8
|
export interface ObjectIdSchemaBuilder<Props extends {
|
|
9
9
|
type: ObjectIdSchemaDefinition;
|
|
10
10
|
} = {
|
|
@@ -21,6 +21,9 @@ export interface ObjectIdSchemaBuilder<Props extends {
|
|
|
21
21
|
}, M extends string, O extends Types.ObjectId>(value: T) => ExtendObjectIdSchemaBuilder<Merge<Props, {
|
|
22
22
|
enum: T;
|
|
23
23
|
}>, ExtraOmitFields>;
|
|
24
|
+
index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendObjectIdSchemaBuilder<Merge<Props, {
|
|
25
|
+
index: T;
|
|
26
|
+
}>, ExtraOmitFields>;
|
|
24
27
|
nonRequired: Props;
|
|
25
28
|
private: ExtendObjectIdSchemaBuilder<Merge<Props, {
|
|
26
29
|
private: true;
|
package/schema-builders/ref.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import type { DefaultType, Model, ObjectIdSchemaDefinition, Types } from 'mongoose';
|
|
1
|
+
import type { DefaultType, IndexDirection, IndexOptions, Model, ObjectIdSchemaDefinition, Types } from 'mongoose';
|
|
2
2
|
import type { Merge } from 'type-fest';
|
|
3
3
|
import type { Readonlyable } from '../types/utils';
|
|
4
|
-
type BaseProps = {
|
|
5
|
-
type: ObjectIdSchemaDefinition;
|
|
6
|
-
};
|
|
7
4
|
export type ExtendRefSchemaBuilder<Props extends BaseProps, ExtraOmitFields extends string> = Omit<RefSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
|
|
5
|
+
interface BaseProps {
|
|
6
|
+
type: ObjectIdSchemaDefinition;
|
|
7
|
+
}
|
|
8
8
|
export interface RefSchemaBuilder<Props extends {
|
|
9
9
|
type: ObjectIdSchemaDefinition;
|
|
10
10
|
} = {
|
|
@@ -21,6 +21,9 @@ export interface RefSchemaBuilder<Props extends {
|
|
|
21
21
|
}, M extends string, O extends Types.ObjectId>(value: T) => ExtendRefSchemaBuilder<Merge<Props, {
|
|
22
22
|
enum: T;
|
|
23
23
|
}>, ExtraOmitFields>;
|
|
24
|
+
index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendRefSchemaBuilder<Merge<Props, {
|
|
25
|
+
index: T;
|
|
26
|
+
}>, ExtraOmitFields>;
|
|
24
27
|
nonRequired: Props;
|
|
25
28
|
private: ExtendRefSchemaBuilder<Merge<Props, {
|
|
26
29
|
private: true;
|
|
@@ -10,7 +10,7 @@ const net__default = /*#__PURE__*/_interopDefaultCompat(net);
|
|
|
10
10
|
const baseBuilderFactory = base.createBaseSchemaBuilderFactory(String);
|
|
11
11
|
const defaultIPv4ValidateMessage = '`{VALUE}` is not a valid IPv4 address for path `{PATH}`.';
|
|
12
12
|
const defaultIPv6ValidateMessage = '`{VALUE}` is not a valid IPv6 address for path `{PATH}`.';
|
|
13
|
-
|
|
13
|
+
function stringSchemaBuilder() {
|
|
14
14
|
const schema = {};
|
|
15
15
|
const baseBuilder = baseBuilderFactory(schema);
|
|
16
16
|
return new Proxy(baseBuilder, {
|
|
@@ -29,11 +29,15 @@ const stringSchemaBuilder = () => {
|
|
|
29
29
|
return receiver;
|
|
30
30
|
};
|
|
31
31
|
}
|
|
32
|
-
if (key === 'length')
|
|
33
|
-
return (value) =>
|
|
32
|
+
if (key === 'length') {
|
|
33
|
+
return (value) => {
|
|
34
|
+
(schema.maxlength = schema.minlength = value);
|
|
35
|
+
return receiver;
|
|
36
|
+
};
|
|
37
|
+
}
|
|
34
38
|
return Reflect.get(target, key, receiver);
|
|
35
|
-
}
|
|
39
|
+
},
|
|
36
40
|
});
|
|
37
|
-
}
|
|
41
|
+
}
|
|
38
42
|
|
|
39
43
|
exports.stringSchemaBuilder = stringSchemaBuilder;
|
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import type { DefaultType, StringSchemaDefinition } from 'mongoose';
|
|
1
|
+
import type { DefaultType, IndexDirection, IndexOptions, StringSchemaDefinition } from 'mongoose';
|
|
2
2
|
import type { Merge } from 'type-fest';
|
|
3
3
|
import type { Readonlyable } from '../types/utils';
|
|
4
|
-
type BaseProps = {
|
|
5
|
-
type: StringSchemaDefinition;
|
|
6
|
-
};
|
|
7
4
|
export type ExtendStringSchemaBuilder<Props extends BaseProps, ExtraOmitFields extends string> = Omit<StringSchemaBuilder<Props, ExtraOmitFields>, ExtraOmitFields | keyof Props>;
|
|
8
|
-
|
|
5
|
+
interface BaseProps {
|
|
6
|
+
type: StringSchemaDefinition;
|
|
7
|
+
}
|
|
8
|
+
interface IPSchema<T extends string> {
|
|
9
9
|
trim: true;
|
|
10
10
|
validate: {
|
|
11
11
|
message: T;
|
|
12
12
|
validator: (value: string) => boolean;
|
|
13
13
|
};
|
|
14
|
-
}
|
|
14
|
+
}
|
|
15
15
|
export interface StringSchemaBuilder<Props extends {
|
|
16
16
|
type: StringSchemaDefinition;
|
|
17
17
|
} = {
|
|
@@ -28,6 +28,9 @@ export interface StringSchemaBuilder<Props extends {
|
|
|
28
28
|
}, M extends string, S extends string>(value: T) => ExtendStringSchemaBuilder<Merge<Props, {
|
|
29
29
|
enum: T;
|
|
30
30
|
}>, ExtraOmitFields>;
|
|
31
|
+
index: <T extends boolean | IndexDirection | IndexOptions>(value: T) => ExtendStringSchemaBuilder<Merge<Props, {
|
|
32
|
+
index: T;
|
|
33
|
+
}>, ExtraOmitFields>;
|
|
31
34
|
/**
|
|
32
35
|
* Adds IPv4 validation to the string schema.
|
|
33
36
|
* Ensures the string is a valid IPv4 address and trims the input. The validation message can be customized.
|
|
@@ -92,5 +95,5 @@ export interface StringSchemaBuilder<Props extends {
|
|
|
92
95
|
}
|
|
93
96
|
declare const defaultIPv4ValidateMessage = "`{VALUE}` is not a valid IPv4 address for path `{PATH}`.";
|
|
94
97
|
declare const defaultIPv6ValidateMessage = "`{VALUE}` is not a valid IPv6 address for path `{PATH}`.";
|
|
95
|
-
export declare
|
|
98
|
+
export declare function stringSchemaBuilder(): StringSchemaBuilder;
|
|
96
99
|
export {};
|
|
@@ -4,7 +4,7 @@ import { createBaseSchemaBuilderFactory } from './base.mjs';
|
|
|
4
4
|
const baseBuilderFactory = createBaseSchemaBuilderFactory(String);
|
|
5
5
|
const defaultIPv4ValidateMessage = '`{VALUE}` is not a valid IPv4 address for path `{PATH}`.';
|
|
6
6
|
const defaultIPv6ValidateMessage = '`{VALUE}` is not a valid IPv6 address for path `{PATH}`.';
|
|
7
|
-
|
|
7
|
+
function stringSchemaBuilder() {
|
|
8
8
|
const schema = {};
|
|
9
9
|
const baseBuilder = baseBuilderFactory(schema);
|
|
10
10
|
return new Proxy(baseBuilder, {
|
|
@@ -23,11 +23,15 @@ const stringSchemaBuilder = () => {
|
|
|
23
23
|
return receiver;
|
|
24
24
|
};
|
|
25
25
|
}
|
|
26
|
-
if (key === 'length')
|
|
27
|
-
return (value) =>
|
|
26
|
+
if (key === 'length') {
|
|
27
|
+
return (value) => {
|
|
28
|
+
(schema.maxlength = schema.minlength = value);
|
|
29
|
+
return receiver;
|
|
30
|
+
};
|
|
31
|
+
}
|
|
28
32
|
return Reflect.get(target, key, receiver);
|
|
29
|
-
}
|
|
33
|
+
},
|
|
30
34
|
});
|
|
31
|
-
}
|
|
35
|
+
}
|
|
32
36
|
|
|
33
37
|
export { stringSchemaBuilder };
|
|
@@ -52,8 +52,8 @@ declare module 'mongoose' {
|
|
|
52
52
|
meta?: any;
|
|
53
53
|
[customLabel: string]: T[] | number | boolean | null | undefined;
|
|
54
54
|
}
|
|
55
|
-
interface AggregatePaginateModel<RawDocType, QueryHelpers =
|
|
56
|
-
aggregatePaginate<T>(query?: Aggregate<T[]>, options?: AggregatePaginateOptions, callback?: (err: any, result: AggregatePaginateResult<T>) => void)
|
|
55
|
+
interface AggregatePaginateModel<RawDocType, QueryHelpers = object, InstanceMethodsAndOverrides = object> extends Model<RawDocType, QueryHelpers, InstanceMethodsAndOverrides> {
|
|
56
|
+
aggregatePaginate: <T>(query?: Aggregate<T[]>, options?: AggregatePaginateOptions, callback?: (err: any, result: AggregatePaginateResult<T>) => void) => Promise<AggregatePaginateResult<T>>;
|
|
57
57
|
}
|
|
58
58
|
}
|
|
59
59
|
declare function mongooseAggregatePaginate(schema: Schema): void;
|
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
|
|
51
|
+
* Type definition to omit timestamp fields (`createdAt`, `updatedAt`)
|
|
52
|
+
* and other specified fields (`OT`) from a given type `T`.
|
|
52
53
|
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
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 =
|
|
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
|
*
|
package/types/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { AggregatePaginateModel, Connection, HydratedDocument, PaginateModel, QueryWithHelpers, Types } from 'mongoose';
|
|
2
2
|
export type {} from './aggregate-paginate';
|
|
3
3
|
export type {} from './paginate';
|
|
4
|
-
export interface BaseModelStatics<RawDocType, InstanceMethodsAndOverrides =
|
|
4
|
+
export interface BaseModelStatics<RawDocType, InstanceMethodsAndOverrides = object, QueryHelpers = object> {
|
|
5
5
|
}
|
|
6
6
|
export interface MongooseConnections {
|
|
7
7
|
default?: Connection;
|
|
@@ -20,9 +20,9 @@ declare global {
|
|
|
20
20
|
*/
|
|
21
21
|
type BaseMongooseDocType<T, CreatedAtField extends boolean = true, UpdatedAtField extends boolean = true> = Omit<T, 'createdAt' | 'id' | 'updatedAt'> & (CreatedAtField extends true ? {
|
|
22
22
|
createdAt: Date;
|
|
23
|
-
} :
|
|
23
|
+
} : object) & (UpdatedAtField extends true ? {
|
|
24
24
|
updatedAt: Date;
|
|
25
|
-
} :
|
|
25
|
+
} : object);
|
|
26
26
|
/**
|
|
27
27
|
* Type definition for a Mongoose model with pagination and aggregation capabilities.
|
|
28
28
|
*
|
|
@@ -33,7 +33,7 @@ declare global {
|
|
|
33
33
|
* @template InstanceMethodsAndOverrides - Optional type parameter for instance methods and overrides.
|
|
34
34
|
* @template QueryHelpers - Optional type parameter for additional query helper methods.
|
|
35
35
|
*/
|
|
36
|
-
type BaseMongoosePaginateModel<RawDocType, InstanceMethodsAndOverrides =
|
|
36
|
+
type BaseMongoosePaginateModel<RawDocType, InstanceMethodsAndOverrides = object, QueryHelpers = object> = AggregatePaginateModel<RawDocType, QueryHelpers, InstanceMethodsAndOverrides> & PaginateModel<RawDocType, QueryHelpers, InstanceMethodsAndOverrides> & BaseModelStatics<RawDocType, InstanceMethodsAndOverrides, QueryHelpers>;
|
|
37
37
|
/**
|
|
38
38
|
* Type definition for a Mongoose document or ObjectId.
|
|
39
39
|
*
|
|
@@ -58,7 +58,7 @@ declare global {
|
|
|
58
58
|
* @template QueryHelpers - Optional type parameter for additional query helper methods.
|
|
59
59
|
* @template InstanceMethodsAndOverrides - Optional type parameter for specifying custom instance methods and overrides on the document.
|
|
60
60
|
*/
|
|
61
|
-
type MongooseFindOneReturnType<RawDocType, DocType, QueryHelpers =
|
|
61
|
+
type MongooseFindOneReturnType<RawDocType, DocType, QueryHelpers = object, InstanceMethodsAndOverrides = object> = QueryWithHelpers<DocType | null, DocType, QueryHelpers, RawDocType, 'findOne', InstanceMethodsAndOverrides>;
|
|
62
62
|
/**
|
|
63
63
|
* Type definition for a hydrated Mongoose document.
|
|
64
64
|
*
|
|
@@ -71,5 +71,5 @@ declare global {
|
|
|
71
71
|
* @template InstanceMethodsAndOverrides - Optional type parameter for instance methods and overrides.
|
|
72
72
|
* @template QueryHelpers - Optional type parameter for additional query helper methods.
|
|
73
73
|
*/
|
|
74
|
-
type MongooseHydratedDocument<DocType, InstanceMethodsAndOverrides =
|
|
74
|
+
type MongooseHydratedDocument<DocType, InstanceMethodsAndOverrides = object, QueryHelpers = object> = HydratedDocument<DocType, InstanceMethodsAndOverrides, QueryHelpers>;
|
|
75
75
|
}
|
package/types/options.d.ts
CHANGED
|
@@ -40,5 +40,5 @@ export interface CustomMongooseOptions {
|
|
|
40
40
|
/**
|
|
41
41
|
* A function that will be executed before the final build when using {@link buildMongooseModel}.
|
|
42
42
|
*/
|
|
43
|
-
beforeModelBuild?: <DocType, Model extends BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>, InstanceMethodsAndOverrides =
|
|
43
|
+
beforeModelBuild?: <DocType, Model extends BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>, InstanceMethodsAndOverrides = object, QueryHelpers = object>(schema: Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>) => void;
|
|
44
44
|
}
|
package/types/paginate.d.ts
CHANGED
|
@@ -64,18 +64,22 @@ declare module 'mongoose' {
|
|
|
64
64
|
meta?: any;
|
|
65
65
|
[customLabel: string]: T[] | number | boolean | null | undefined;
|
|
66
66
|
}
|
|
67
|
-
type PaginateDocument<T, TMethods, TQueryHelpers, O extends PaginateOptions =
|
|
67
|
+
type PaginateDocument<T, TMethods, TQueryHelpers, O extends PaginateOptions = object> = O['lean'] extends true ? (O['leanWithId'] extends true ? T & {
|
|
68
68
|
id: string;
|
|
69
69
|
} : T) : HydratedDocument<T, TMethods, TQueryHelpers>;
|
|
70
|
-
interface PaginateModel<T, TQueryHelpers =
|
|
71
|
-
paginate
|
|
72
|
-
|
|
73
|
-
|
|
70
|
+
interface PaginateModel<T, TQueryHelpers = object, TMethods = object> extends Model<T, TQueryHelpers, TMethods> {
|
|
71
|
+
paginate: {
|
|
72
|
+
<O extends PaginateOptions>(query?: FilterQuery<T>, options?: O, callback?: (err: any, result: PaginateResult<PaginateDocument<T, TMethods, TQueryHelpers, O>>) => void): Promise<PaginateResult<PaginateDocument<T, TMethods, TQueryHelpers, O>>>;
|
|
73
|
+
<UserType = T, O extends PaginateOptions = PaginateOptions>(query?: FilterQuery<T>, options?: O, callback?: (err: any, result: PaginateResult<PaginateDocument<UserType, TMethods, TQueryHelpers, O>>) => void): Promise<PaginateResult<PaginateDocument<UserType, TMethods, TQueryHelpers, O>>>;
|
|
74
|
+
<UserType = T>(query?: FilterQuery<T>, options?: PaginateOptions, callback?: (err: any, result: PaginateResult<PaginateDocument<UserType, TMethods, TQueryHelpers, PaginateOptions>>) => void): Promise<PaginateResult<PaginateDocument<UserType, TMethods, TQueryHelpers, PaginateOptions>>>;
|
|
75
|
+
};
|
|
74
76
|
}
|
|
75
|
-
interface Query<ResultType, DocType, THelpers = NonNullable<unknown>, RawDocType = DocType,
|
|
76
|
-
paginate
|
|
77
|
-
|
|
78
|
-
|
|
77
|
+
interface Query<ResultType, DocType, THelpers = NonNullable<unknown>, RawDocType = DocType, _QueryOp = 'find', TInstanceMethods = Record<string, never>> {
|
|
78
|
+
paginate: {
|
|
79
|
+
<O extends PaginateOptions>(options?: O): Promise<PaginateResult<PaginateDocument<RawDocType, TInstanceMethods, THelpers, O>>>;
|
|
80
|
+
<UserType = ResultType, O extends PaginateOptions = PaginateOptions>(options?: O): Promise<PaginateResult<PaginateDocument<UserType, TInstanceMethods, THelpers, O>>>;
|
|
81
|
+
<UserType = ResultType>(options?: PaginateOptions): Promise<PaginateResult<PaginateDocument<UserType, TInstanceMethods, THelpers, PaginateOptions>>>;
|
|
82
|
+
};
|
|
79
83
|
}
|
|
80
84
|
}
|
|
81
85
|
declare function _(schema: Schema): void;
|
package/utils.cjs
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
const mongoose = require('mongoose');
|
|
4
4
|
const mongooseAggregatePaginate = require('mongoose-aggregate-paginate-v2');
|
|
5
5
|
const mongoosePaginate = require('mongoose-paginate-v2');
|
|
6
|
+
const node_process = require('node:process');
|
|
6
7
|
const _internals = require('./_internals.cjs');
|
|
7
8
|
const constants = require('./constants.cjs');
|
|
8
9
|
const normalize = require('./plugins/normalize.cjs');
|
|
@@ -47,7 +48,7 @@ function buildMongooseModel(collection, name, schema, options) {
|
|
|
47
48
|
schema.plugin(mongoosePaginate__default);
|
|
48
49
|
schema.set('timestamps', options?.timestamps ?? true);
|
|
49
50
|
_internals.customMongooseOptions.beforeModelBuild?.(schema);
|
|
50
|
-
return (options?.connection || constants.mongooseConnections.default || (constants.mongooseConnections.default = mongoose__default.createConnection(
|
|
51
|
+
return (options?.connection || constants.mongooseConnections.default || (constants.mongooseConnections.default = mongoose__default.createConnection(node_process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017'))).model(name, schema, collection);
|
|
51
52
|
}
|
|
52
53
|
/**
|
|
53
54
|
* Converts a document or ObjectId to a document using the specified model.
|
package/utils.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import mongoose from 'mongoose';
|
|
2
|
-
import { setCustomMongooseOptions } from './options';
|
|
2
|
+
import type { setCustomMongooseOptions } from './options';
|
|
3
3
|
import type { BuildMongooseModelOptions } from './types/options';
|
|
4
4
|
export type DoNotRemoveOrUseThisType = typeof setCustomMongooseOptions;
|
|
5
5
|
/**
|
|
@@ -29,7 +29,7 @@ export type DoNotRemoveOrUseThisType = typeof setCustomMongooseOptions;
|
|
|
29
29
|
*
|
|
30
30
|
* @returns The created Mongoose model.
|
|
31
31
|
*/
|
|
32
|
-
export declare function buildMongooseModel<DocType, Model extends BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>, InstanceMethodsAndOverrides =
|
|
32
|
+
export declare function buildMongooseModel<DocType, Model extends BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>, InstanceMethodsAndOverrides = object, QueryHelpers = object>(collection: string, name: string, schema: mongoose.Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>, options?: BuildMongooseModelOptions): Model;
|
|
33
33
|
/**
|
|
34
34
|
* Converts a document or ObjectId to a document using the specified model.
|
|
35
35
|
*
|
package/utils.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import mongoose, { Types } from 'mongoose';
|
|
2
2
|
import mongooseAggregatePaginate from 'mongoose-aggregate-paginate-v2';
|
|
3
3
|
import mongoosePaginate from 'mongoose-paginate-v2';
|
|
4
|
+
import { env } from 'node:process';
|
|
4
5
|
import { customMongooseOptions } from './_internals.mjs';
|
|
5
6
|
import { mongooseConnections } from './constants.mjs';
|
|
6
7
|
import { mongooseNormalizePlugin } from './plugins/normalize.mjs';
|
|
@@ -39,7 +40,7 @@ function buildMongooseModel(collection, name, schema, options) {
|
|
|
39
40
|
schema.plugin(mongoosePaginate);
|
|
40
41
|
schema.set('timestamps', options?.timestamps ?? true);
|
|
41
42
|
customMongooseOptions.beforeModelBuild?.(schema);
|
|
42
|
-
return (options?.connection || mongooseConnections.default || (mongooseConnections.default = mongoose.createConnection(
|
|
43
|
+
return (options?.connection || mongooseConnections.default || (mongooseConnections.default = mongoose.createConnection(env.MONGODB_URI || 'mongodb://127.0.0.1:27017'))).model(name, schema, collection);
|
|
43
44
|
}
|
|
44
45
|
/**
|
|
45
46
|
* Converts a document or ObjectId to a document using the specified model.
|