@kikiutils/mongoose 1.7.1 → 2.0.0-rc.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/README.md +7 -3
- package/_options.cjs +5 -0
- package/_options.d.ts +2 -0
- package/_options.mjs +3 -0
- package/options.cjs +3 -0
- package/options.d.ts +13 -0
- package/options.mjs +3 -0
- package/package.json +6 -1
- package/types/options.d.ts +5 -0
- package/types/schema.d.ts +8 -0
- package/utils.cjs +5 -1
- package/utils.d.ts +5 -1
- package/utils.mjs +6 -2
package/README.md
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
[![npm version][npm-version-src]][npm-version-href]
|
|
4
4
|
[![npm downloads][npm-downloads-src]][npm-downloads-href]
|
|
5
|
+
[![codecov][codecov-src]][codecov-href]
|
|
5
6
|
[![License][license-src]][license-href]
|
|
6
7
|
|
|
7
8
|
A Mongoose plugin for enhanced JSON normalization and common schema creation, with built-in support for pagination and automatic Decimal128 conversion.
|
|
@@ -46,11 +47,14 @@ Please refer to the [examples](./examples/README.md) folder for usage instructio
|
|
|
46
47
|
[MIT License](./LICENSE)
|
|
47
48
|
|
|
48
49
|
<!-- Badges -->
|
|
49
|
-
[npm-version-src]: https://img.shields.io/npm/v/@kikiutils/mongoose/latest.svg?style=flat&colorA=18181B&colorB=28CF8D
|
|
50
50
|
[npm-version-href]: https://npmjs.com/package/@kikiutils/mongoose
|
|
51
|
+
[npm-version-src]: https://img.shields.io/npm/v/@kikiutils/mongoose/latest.svg?style=flat&colorA=18181B&colorB=28CF8D
|
|
51
52
|
|
|
52
|
-
[npm-downloads-src]: https://img.shields.io/npm/dm/@kikiutils/mongoose.svg?style=flat&colorA=18181B&colorB=28CF8D
|
|
53
53
|
[npm-downloads-href]: https://npmjs.com/package/@kikiutils/mongoose
|
|
54
|
+
[npm-downloads-src]: https://img.shields.io/npm/dm/@kikiutils/mongoose.svg?style=flat&colorA=18181B&colorB=28CF8D
|
|
55
|
+
|
|
56
|
+
[codecov-href]: https://codecov.io/github/kiki-kanri/kikiutils-node-mongoose
|
|
57
|
+
[codecov-src]: https://codecov.io/github/kiki-kanri/kikiutils-node-mongoose/graph/badge.svg?token=GRSQ7JO39E
|
|
54
58
|
|
|
55
|
-
[license-src]: https://img.shields.io/npm/l/@kikiutils/mongoose.svg?style=flat&colorA=18181B&colorB=28CF8D
|
|
56
59
|
[license-href]: https://github.com/kiki-kanri/kikiutils-node-mongoose/blob/main/LICENSE
|
|
60
|
+
[license-src]: https://img.shields.io/npm/l/@kikiutils/mongoose.svg?style=flat&colorA=18181B&colorB=28CF8D
|
package/_options.cjs
ADDED
package/_options.d.ts
ADDED
package/_options.mjs
ADDED
package/options.cjs
CHANGED
package/options.d.ts
CHANGED
|
@@ -1,2 +1,15 @@
|
|
|
1
1
|
import type { CustomMongooseOptions } from './types/options';
|
|
2
|
+
import type { buildMongooseModel } from './utils';
|
|
3
|
+
export type DoNotUseOrRemoveThisType = typeof buildMongooseModel;
|
|
4
|
+
/**
|
|
5
|
+
* @deprecated - Will be removed in the next major version. Use {@link setCustomMongooseOptions} instead.
|
|
6
|
+
*/
|
|
2
7
|
export declare const customMongooseOptions: CustomMongooseOptions;
|
|
8
|
+
/**
|
|
9
|
+
* Sets custom options.
|
|
10
|
+
*
|
|
11
|
+
* For example, you can set {@link CustomMongooseOptions.beforeModelBuild} to define a function that will be called
|
|
12
|
+
* every time {@link buildMongooseModel} is used. Inside this function, you can modify
|
|
13
|
+
* the schema, allowing you to apply common settings to all global schemas.
|
|
14
|
+
*/
|
|
15
|
+
export declare const setCustomMongooseOptions: <K extends keyof CustomMongooseOptions>(key: K, value: CustomMongooseOptions[K]) => CustomMongooseOptions[K];
|
package/options.mjs
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kikiutils/mongoose",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0-rc.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",
|
|
@@ -57,6 +57,11 @@
|
|
|
57
57
|
"require": null,
|
|
58
58
|
"types": null
|
|
59
59
|
},
|
|
60
|
+
"./_options": {
|
|
61
|
+
"import": null,
|
|
62
|
+
"require": null,
|
|
63
|
+
"types": null
|
|
64
|
+
},
|
|
60
65
|
"./schema-builders": {
|
|
61
66
|
"import": "./schema-builders/index.mjs",
|
|
62
67
|
"require": "./schema-builders/index.cjs",
|
package/types/options.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import type { Connection, Schema, SchemaTimestampsConfig } from 'mongoose';
|
|
2
|
+
import type { buildMongooseModel } from '../utils';
|
|
3
|
+
export type DoNotUseOrRemoveThisType = typeof buildMongooseModel;
|
|
2
4
|
/**
|
|
3
5
|
* Interface for options used when building a Mongoose model.
|
|
4
6
|
*
|
|
@@ -35,5 +37,8 @@ export interface BuildMongooseModelOptions {
|
|
|
35
37
|
timestamps?: boolean | SchemaTimestampsConfig;
|
|
36
38
|
}
|
|
37
39
|
export interface CustomMongooseOptions {
|
|
40
|
+
/**
|
|
41
|
+
* A function that will be executed before the final build when using {@link buildMongooseModel}.
|
|
42
|
+
*/
|
|
38
43
|
beforeModelBuild?: <DocType, Model extends BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>, InstanceMethodsAndOverrides = {}, QueryHelpers = {}>(schema: Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>) => void;
|
|
39
44
|
}
|
package/types/schema.d.ts
CHANGED
|
@@ -6,6 +6,8 @@ export type DoNotUseOrRemoveThisType = typeof schemaBuilders;
|
|
|
6
6
|
* Base attributes for Mongoose schemas.
|
|
7
7
|
*
|
|
8
8
|
* This type defines common attributes that can be applied to any schema field.
|
|
9
|
+
*
|
|
10
|
+
* @deprecated - Will be removed in the next major version.
|
|
9
11
|
*/
|
|
10
12
|
export type BaseSchemaAttribute = 'private' | 'required' | 'unique';
|
|
11
13
|
/**
|
|
@@ -13,6 +15,8 @@ export type BaseSchemaAttribute = 'private' | 'required' | 'unique';
|
|
|
13
15
|
*
|
|
14
16
|
* This type extends the base schema attributes with string-specific attributes
|
|
15
17
|
* like `lowercase`, `short`, `trim`, and `uppercase`.
|
|
18
|
+
*
|
|
19
|
+
* @deprecated - Will be removed in the next major version.
|
|
16
20
|
*/
|
|
17
21
|
export type MongooseStringSchemaAttribute = BaseSchemaAttribute | 'lowercase' | 'short' | 'trim' | 'uppercase';
|
|
18
22
|
/**
|
|
@@ -49,6 +53,8 @@ export interface CreateCommonMongooseSchemasOptions {
|
|
|
49
53
|
* including whether the field is private, the reference model name, and standard
|
|
50
54
|
* schema options like `required` and `unique`.
|
|
51
55
|
*
|
|
56
|
+
* @deprecated - Will be removed in the next major version.
|
|
57
|
+
*
|
|
52
58
|
* @template R - The type of the reference model name.
|
|
53
59
|
* @template T - An array of attributes to apply to the schema field.
|
|
54
60
|
*/
|
|
@@ -89,6 +95,8 @@ export interface MongooseObjectIdRefSchema<R extends string, T extends BaseSchem
|
|
|
89
95
|
* whether the field should be lowercased, trimmed, or uppercased, as well as standard
|
|
90
96
|
* schema options like `required` and `unique`.
|
|
91
97
|
*
|
|
98
|
+
* @deprecated - Will be removed in the next major version.
|
|
99
|
+
*
|
|
92
100
|
* @template T - An array of attributes to apply to the schema field.
|
|
93
101
|
*/
|
|
94
102
|
export interface MongooseStringSchema<T extends MongooseStringSchemaAttribute[]> {
|
package/utils.cjs
CHANGED
|
@@ -7,6 +7,7 @@ const mongooseAggregatePaginate = require('mongoose-aggregate-paginate-v2');
|
|
|
7
7
|
const mongoosePaginate = require('mongoose-paginate-v2');
|
|
8
8
|
const net = require('node:net');
|
|
9
9
|
const _connections = require('./_connections.cjs');
|
|
10
|
+
const _options = require('./_options.cjs');
|
|
10
11
|
const options = require('./options.cjs');
|
|
11
12
|
const normalize = require('./plugins/normalize.cjs');
|
|
12
13
|
|
|
@@ -32,6 +33,9 @@ const { merge } = lodash__default;
|
|
|
32
33
|
* 3. If there's no default connection, create one using the MongoDB URI from the environment variables,
|
|
33
34
|
* or fallback to a local MongoDB instance.
|
|
34
35
|
*
|
|
36
|
+
* You can set a function to be executed before the final build by using {@link setCustomMongooseOptions}.
|
|
37
|
+
* However, ensure that this option is configured before calling {@link buildMongooseModel}.
|
|
38
|
+
*
|
|
35
39
|
* @template DocType - The type of the document.
|
|
36
40
|
* @template Model - The type of the model, which extends `BaseMongoosePaginateModel`.
|
|
37
41
|
* @template InstanceMethodsAndOverrides - The type for instance methods and overrides.
|
|
@@ -50,7 +54,7 @@ function buildMongooseModel(collection, name, schema, options$1) {
|
|
|
50
54
|
schema.plugin(mongooseAggregatePaginate__default);
|
|
51
55
|
schema.plugin(mongoosePaginate__default);
|
|
52
56
|
schema.set('timestamps', options$1?.timestamps === undefined ? true : options$1.timestamps);
|
|
53
|
-
options.customMongooseOptions.beforeModelBuild?.(schema);
|
|
57
|
+
(_options.customMongooseOptions.beforeModelBuild || options.customMongooseOptions.beforeModelBuild)?.(schema);
|
|
54
58
|
return (options$1?.connection || _connections.mongooseConnections.default || (_connections.mongooseConnections.default = mongoose__default.createConnection(process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017'))).model(name, schema, collection);
|
|
55
59
|
}
|
|
56
60
|
/**
|
package/utils.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import mongoose from 'mongoose';
|
|
2
|
+
import { setCustomMongooseOptions } from './options';
|
|
2
3
|
import type { schemaBuilders } from './schema-builders';
|
|
3
4
|
import type { BuildMongooseModelOptions } from './types/options';
|
|
4
5
|
import type { BaseSchemaAttribute, CreateCommonMongooseSchemasOptions, MongooseObjectIdRefSchema, MongooseStringSchema, MongooseStringSchemaAttribute } from './types/schema';
|
|
5
|
-
export type DoNotUseOrRemoveThisType = typeof schemaBuilders;
|
|
6
|
+
export type DoNotUseOrRemoveThisType = typeof schemaBuilders | typeof setCustomMongooseOptions;
|
|
6
7
|
/**
|
|
7
8
|
* Builds and returns a Mongoose model with specified schema and options.
|
|
8
9
|
*
|
|
@@ -15,6 +16,9 @@ export type DoNotUseOrRemoveThisType = typeof schemaBuilders;
|
|
|
15
16
|
* 3. If there's no default connection, create one using the MongoDB URI from the environment variables,
|
|
16
17
|
* or fallback to a local MongoDB instance.
|
|
17
18
|
*
|
|
19
|
+
* You can set a function to be executed before the final build by using {@link setCustomMongooseOptions}.
|
|
20
|
+
* However, ensure that this option is configured before calling {@link buildMongooseModel}.
|
|
21
|
+
*
|
|
18
22
|
* @template DocType - The type of the document.
|
|
19
23
|
* @template Model - The type of the model, which extends `BaseMongoosePaginateModel`.
|
|
20
24
|
* @template InstanceMethodsAndOverrides - The type for instance methods and overrides.
|
package/utils.mjs
CHANGED
|
@@ -5,7 +5,8 @@ import mongooseAggregatePaginate from 'mongoose-aggregate-paginate-v2';
|
|
|
5
5
|
import mongoosePaginate from 'mongoose-paginate-v2';
|
|
6
6
|
import net from 'node:net';
|
|
7
7
|
import { mongooseConnections } from './_connections.mjs';
|
|
8
|
-
import { customMongooseOptions } from './
|
|
8
|
+
import { customMongooseOptions } from './_options.mjs';
|
|
9
|
+
import { customMongooseOptions as customMongooseOptions$1 } from './options.mjs';
|
|
9
10
|
import { mongooseNormalizePlugin } from './plugins/normalize.mjs';
|
|
10
11
|
|
|
11
12
|
const { merge } = lodash;
|
|
@@ -21,6 +22,9 @@ const { merge } = lodash;
|
|
|
21
22
|
* 3. If there's no default connection, create one using the MongoDB URI from the environment variables,
|
|
22
23
|
* or fallback to a local MongoDB instance.
|
|
23
24
|
*
|
|
25
|
+
* You can set a function to be executed before the final build by using {@link setCustomMongooseOptions}.
|
|
26
|
+
* However, ensure that this option is configured before calling {@link buildMongooseModel}.
|
|
27
|
+
*
|
|
24
28
|
* @template DocType - The type of the document.
|
|
25
29
|
* @template Model - The type of the model, which extends `BaseMongoosePaginateModel`.
|
|
26
30
|
* @template InstanceMethodsAndOverrides - The type for instance methods and overrides.
|
|
@@ -39,7 +43,7 @@ function buildMongooseModel(collection, name, schema, options) {
|
|
|
39
43
|
schema.plugin(mongooseAggregatePaginate);
|
|
40
44
|
schema.plugin(mongoosePaginate);
|
|
41
45
|
schema.set('timestamps', options?.timestamps === undefined ? true : options.timestamps);
|
|
42
|
-
customMongooseOptions.beforeModelBuild?.(schema);
|
|
46
|
+
(customMongooseOptions.beforeModelBuild || customMongooseOptions$1.beforeModelBuild)?.(schema);
|
|
43
47
|
return (options?.connection || mongooseConnections.default || (mongooseConnections.default = mongoose.createConnection(process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017'))).model(name, schema, collection);
|
|
44
48
|
}
|
|
45
49
|
/**
|