@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 +5 -0
- package/options.d.ts +2 -0
- package/options.mjs +3 -0
- package/package.json +1 -1
- package/types/options.d.ts +4 -1
- package/utils.cjs +6 -4
- package/utils.mjs +2 -0
package/options.cjs
ADDED
package/options.d.ts
ADDED
package/options.mjs
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kikiutils/mongoose",
|
|
3
|
-
"version": "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",
|
package/types/options.d.ts
CHANGED
|
@@ -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
|
-
|
|
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
|
/**
|