@kikiutils/mongoose 1.3.1 → 1.4.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.cjs ADDED
@@ -0,0 +1,5 @@
1
+ 'use strict';
2
+
3
+ const customMongooseOptions = {};
4
+
5
+ exports.customMongooseOptions = customMongooseOptions;
package/options.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import type { CustomMongooseOptions } from './types/options';
2
+ export declare const customMongooseOptions: CustomMongooseOptions;
package/options.mjs ADDED
@@ -0,0 +1,3 @@
1
+ const customMongooseOptions = {};
2
+
3
+ export { customMongooseOptions };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kikiutils/mongoose",
3
- "version": "1.3.1",
3
+ "version": "1.4.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",
@@ -1,4 +1,7 @@
1
- import type { Connection, SchemaTimestampsConfig } from 'mongoose';
1
+ import type { Connection, Schema, SchemaTimestampsConfig } from 'mongoose';
2
+ export interface CustomMongooseOptions {
3
+ beforeModelBuild?: <DocType, Model extends BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>, InstanceMethodsAndOverrides = {}, QueryHelpers = {}>(schema: Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>) => void;
4
+ }
2
5
  /**
3
6
  * Interface for options used when building a Mongoose model.
4
7
  *
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 normalize = require('./plugins/normalize.cjs');
11
12
 
12
13
  const _interopDefaultCompat = e => e && typeof e === 'object' && 'default' in e ? e.default : e;
@@ -43,13 +44,14 @@ const { merge } = lodash__default;
43
44
  *
44
45
  * @returns The created Mongoose model.
45
46
  */
46
- function buildMongooseModel(collection, name, schema, options) {
47
- if (options?.enableNormalizePlugin !== false)
47
+ function buildMongooseModel(collection, name, schema, options$1) {
48
+ if (options$1?.enableNormalizePlugin !== false)
48
49
  schema.plugin(normalize.mongooseNormalizePlugin);
49
50
  schema.plugin(mongooseAggregatePaginate__default);
50
51
  schema.plugin(mongoosePaginate__default);
51
- schema.set('timestamps', options?.timestamps === undefined ? true : options.timestamps);
52
- return (options?.connection || _connections.mongooseConnections.default || (_connections.mongooseConnections.default = mongoose__default.createConnection(process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017'))).model(name, schema, collection);
52
+ 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);
53
55
  }
54
56
  /**
55
57
  * Creates common Mongoose schema definitions with optional custom schemas and settings.
package/utils.mjs CHANGED
@@ -5,6 +5,7 @@ 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 './options.mjs';
8
9
  import { mongooseNormalizePlugin } from './plugins/normalize.mjs';
9
10
 
10
11
  const { merge } = lodash;
@@ -38,6 +39,7 @@ function buildMongooseModel(collection, name, schema, options) {
38
39
  schema.plugin(mongooseAggregatePaginate);
39
40
  schema.plugin(mongoosePaginate);
40
41
  schema.set('timestamps', options?.timestamps === undefined ? true : options.timestamps);
42
+ customMongooseOptions.beforeModelBuild?.(schema);
41
43
  return (options?.connection || mongooseConnections.default || (mongooseConnections.default = mongoose.createConnection(process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017'))).model(name, schema, collection);
42
44
  }
43
45
  /**