@kikiutils/mongoose 1.7.1 → 2.0.0-rc.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/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
@@ -1,5 +1,6 @@
1
1
  'use strict';
2
2
 
3
+ const customMongooseOptions = {};
3
4
  /**
4
5
  * Mongoose connections.
5
6
  *
@@ -9,4 +10,5 @@
9
10
  */
10
11
  const mongooseConnections = {};
11
12
 
13
+ exports.customMongooseOptions = customMongooseOptions;
12
14
  exports.mongooseConnections = mongooseConnections;
@@ -1,4 +1,6 @@
1
1
  import type { MongooseConnections } from './types';
2
+ import type { CustomMongooseOptions } from './types/options';
3
+ export declare const customMongooseOptions: CustomMongooseOptions;
2
4
  /**
3
5
  * Mongoose connections.
4
6
  *
@@ -1,3 +1,4 @@
1
+ const customMongooseOptions = {};
1
2
  /**
2
3
  * Mongoose connections.
3
4
  *
@@ -7,4 +8,4 @@
7
8
  */
8
9
  const mongooseConnections = {};
9
10
 
10
- export { mongooseConnections };
11
+ export { customMongooseOptions, mongooseConnections };
package/constants.cjs CHANGED
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  const utils = require('./utils.cjs');
4
- const _connections = require('./_connections.cjs');
4
+ const _internals = require('./_internals.cjs');
5
5
 
6
6
  /**
7
7
  * Common Mongoose schemas.
@@ -14,5 +14,5 @@ const _connections = require('./_connections.cjs');
14
14
  */
15
15
  const commonMongooseSchemas = utils.createCommonMongooseSchemas();
16
16
 
17
- exports.mongooseConnections = _connections.mongooseConnections;
17
+ exports.mongooseConnections = _internals.mongooseConnections;
18
18
  exports.commonMongooseSchemas = commonMongooseSchemas;
package/constants.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- export { mongooseConnections } from './_connections';
2
1
  import type { schemaBuilders } from './schema-builders';
2
+ export { mongooseConnections } from './_internals';
3
3
  export type DoNotUseOrRemoveThisType = typeof schemaBuilders;
4
4
  /**
5
5
  * Common Mongoose schemas.
package/constants.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { createCommonMongooseSchemas } from './utils.mjs';
2
- export { mongooseConnections } from './_connections.mjs';
2
+ export { mongooseConnections } from './_internals.mjs';
3
3
 
4
4
  /**
5
5
  * Common Mongoose schemas.
package/options.cjs CHANGED
@@ -1,5 +1,19 @@
1
1
  'use strict';
2
2
 
3
+ const _internals = require('./_internals.cjs');
4
+
5
+ /**
6
+ * @deprecated - Will be removed in the next major version. Use {@link setCustomMongooseOptions} instead.
7
+ */
3
8
  const customMongooseOptions = {};
9
+ /**
10
+ * Sets custom options.
11
+ *
12
+ * For example, you can set {@link CustomMongooseOptions.beforeModelBuild} to define a function that will be called
13
+ * every time {@link buildMongooseModel} is used. Inside this function, you can modify
14
+ * the schema, allowing you to apply common settings to all global schemas.
15
+ */
16
+ const setCustomMongooseOptions = (key, value) => (_internals.customMongooseOptions[key] = value);
4
17
 
5
18
  exports.customMongooseOptions = customMongooseOptions;
19
+ exports.setCustomMongooseOptions = setCustomMongooseOptions;
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
@@ -1,3 +1,16 @@
1
+ import { customMongooseOptions as customMongooseOptions$1 } from './_internals.mjs';
2
+
3
+ /**
4
+ * @deprecated - Will be removed in the next major version. Use {@link setCustomMongooseOptions} instead.
5
+ */
1
6
  const customMongooseOptions = {};
7
+ /**
8
+ * Sets custom options.
9
+ *
10
+ * For example, you can set {@link CustomMongooseOptions.beforeModelBuild} to define a function that will be called
11
+ * every time {@link buildMongooseModel} is used. Inside this function, you can modify
12
+ * the schema, allowing you to apply common settings to all global schemas.
13
+ */
14
+ const setCustomMongooseOptions = (key, value) => (customMongooseOptions$1[key] = value);
2
15
 
3
- export { customMongooseOptions };
16
+ export { customMongooseOptions, setCustomMongooseOptions };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kikiutils/mongoose",
3
- "version": "1.7.1",
3
+ "version": "2.0.0-rc.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
5
  "license": "MIT",
6
6
  "author": "kiki-kanri",
@@ -9,7 +9,7 @@
9
9
  "url": "git+https://github.com/kiki-kanri/kikiutils-node-mongoose.git"
10
10
  },
11
11
  "scripts": {
12
- "build": "ts-project-builder ./src/constants.ts ./src/schema-builders/index.ts ./src/utils.ts --clean --preserve-modules",
12
+ "build": "ts-project-builder ./src/constants.ts ./src/options.ts ./src/schema-builders/index.ts ./src/utils.ts --clean --preserve-modules",
13
13
  "bumplog": "changelogen --bump",
14
14
  "release": "pnpm run build && pnpm run test && changelogen --push --release && sh ./build-and-publish.sh",
15
15
  "test": "jest --coverage"
@@ -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",
@@ -61,6 +61,7 @@ export interface Decimal128SchemaBuilder<Props extends {
61
61
  *
62
62
  * @param places - The number of decimal places to round to (default is 2).
63
63
  * @param rounding - The rounding strategy from the Decimal.js library (default is Decimal.ROUND_DOWN).
64
+ *
64
65
  * @returns A schema builder with the rounding and toFixed behavior applied to the `set` option in Mongoose.
65
66
  */
66
67
  setRoundAndToFixedSetter: (places?: number, rounding?: Decimal.Rounding) => ExtendDecimal128SchemaBuilder<{
@@ -39,6 +39,7 @@ export interface StringSchemaBuilder<Props extends {
39
39
  * Ensures the string is a valid IPv4 address and trims the input. The validation message can be customized.
40
40
  *
41
41
  * @param message - The custom error message to return if validation fails. Defaults to a standard IPv4 validation message.
42
+ *
42
43
  * @returns A schema builder with IPv4 validation and the `trim` option enabled.
43
44
  */
44
45
  ipv4: <T extends string = typeof defaultIPv4ValidateMessage>(message?: T) => ExtendStringSchemaBuilder<{
@@ -49,6 +50,7 @@ export interface StringSchemaBuilder<Props extends {
49
50
  * Ensures the string is a valid IPv6 address and trims the input. The validation message can be customized.
50
51
  *
51
52
  * @param message - The custom error message to return if validation fails. Defaults to a standard IPv6 validation message.
53
+ *
52
54
  * @returns A schema builder with IPv6 validation and the `trim` option enabled.
53
55
  */
54
56
  ipv6: <T extends string = typeof defaultIPv6ValidateMessage>(message?: T) => ExtendStringSchemaBuilder<{
@@ -59,6 +61,7 @@ export interface StringSchemaBuilder<Props extends {
59
61
  * This method ensures the string length is exactly or within a specified range.
60
62
  *
61
63
  * @param value - A number representing the exact length or an array specifying [minimum, maximum] length.
64
+ *
62
65
  * @returns A schema builder with both `maxlength` and `minlength` options applied.
63
66
  */
64
67
  length: <T extends L | MaybeReadonly<[L, S]>, L extends number, S extends string>(value: T) => ExtendStringSchemaBuilder<{
@@ -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
@@ -6,7 +6,7 @@ const mongoose = require('mongoose');
6
6
  const mongooseAggregatePaginate = require('mongoose-aggregate-paginate-v2');
7
7
  const mongoosePaginate = require('mongoose-paginate-v2');
8
8
  const net = require('node:net');
9
- const _connections = require('./_connections.cjs');
9
+ const _internals = require('./_internals.cjs');
10
10
  const options = require('./options.cjs');
11
11
  const normalize = require('./plugins/normalize.cjs');
12
12
 
@@ -32,6 +32,9 @@ const { merge } = lodash__default;
32
32
  * 3. If there's no default connection, create one using the MongoDB URI from the environment variables,
33
33
  * or fallback to a local MongoDB instance.
34
34
  *
35
+ * You can set a function to be executed before the final build by using {@link setCustomMongooseOptions}.
36
+ * However, ensure that this option is configured before calling {@link buildMongooseModel}.
37
+ *
35
38
  * @template DocType - The type of the document.
36
39
  * @template Model - The type of the model, which extends `BaseMongoosePaginateModel`.
37
40
  * @template InstanceMethodsAndOverrides - The type for instance methods and overrides.
@@ -50,8 +53,8 @@ function buildMongooseModel(collection, name, schema, options$1) {
50
53
  schema.plugin(mongooseAggregatePaginate__default);
51
54
  schema.plugin(mongoosePaginate__default);
52
55
  schema.set('timestamps', options$1?.timestamps === undefined ? true : options$1.timestamps);
53
- options.customMongooseOptions.beforeModelBuild?.(schema);
54
- 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);
56
+ (_internals.customMongooseOptions.beforeModelBuild || options.customMongooseOptions.beforeModelBuild)?.(schema);
57
+ return (options$1?.connection || _internals.mongooseConnections.default || (_internals.mongooseConnections.default = mongoose__default.createConnection(process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017'))).model(name, schema, collection);
55
58
  }
56
59
  /**
57
60
  * Creates common Mongoose schema definitions with optional custom schemas and settings.
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
@@ -4,8 +4,8 @@ import mongoose, { Types } from 'mongoose';
4
4
  import mongooseAggregatePaginate from 'mongoose-aggregate-paginate-v2';
5
5
  import mongoosePaginate from 'mongoose-paginate-v2';
6
6
  import net from 'node:net';
7
- import { mongooseConnections } from './_connections.mjs';
8
- import { customMongooseOptions } from './options.mjs';
7
+ import { customMongooseOptions, mongooseConnections } from './_internals.mjs';
8
+ import { customMongooseOptions as customMongooseOptions$1 } from './options.mjs';
9
9
  import { mongooseNormalizePlugin } from './plugins/normalize.mjs';
10
10
 
11
11
  const { merge } = lodash;
@@ -21,6 +21,9 @@ const { merge } = lodash;
21
21
  * 3. If there's no default connection, create one using the MongoDB URI from the environment variables,
22
22
  * or fallback to a local MongoDB instance.
23
23
  *
24
+ * You can set a function to be executed before the final build by using {@link setCustomMongooseOptions}.
25
+ * However, ensure that this option is configured before calling {@link buildMongooseModel}.
26
+ *
24
27
  * @template DocType - The type of the document.
25
28
  * @template Model - The type of the model, which extends `BaseMongoosePaginateModel`.
26
29
  * @template InstanceMethodsAndOverrides - The type for instance methods and overrides.
@@ -39,7 +42,7 @@ function buildMongooseModel(collection, name, schema, options) {
39
42
  schema.plugin(mongooseAggregatePaginate);
40
43
  schema.plugin(mongoosePaginate);
41
44
  schema.set('timestamps', options?.timestamps === undefined ? true : options.timestamps);
42
- customMongooseOptions.beforeModelBuild?.(schema);
45
+ (customMongooseOptions.beforeModelBuild || customMongooseOptions$1.beforeModelBuild)?.(schema);
43
46
  return (options?.connection || mongooseConnections.default || (mongooseConnections.default = mongoose.createConnection(process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017'))).model(name, schema, collection);
44
47
  }
45
48
  /**