@kikiutils/mongoose 1.1.0 → 1.2.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 +1 -1
- package/_connections.cjs +12 -0
- package/_connections.d.ts +9 -0
- package/_connections.mjs +10 -0
- package/constants.cjs +2 -9
- package/constants.d.ts +1 -11
- package/constants.mjs +2 -9
- package/package.json +12 -11
- package/plugins/normalize.cjs +8 -3
- package/plugins/normalize.mjs +5 -4
- package/types/index.d.ts +4 -1
- package/utils.cjs +7 -5
- package/utils.mjs +6 -5
package/README.md
CHANGED
|
@@ -35,7 +35,7 @@ pnpm add @kikiutils/mongoose
|
|
|
35
35
|
|
|
36
36
|
You can also use yarn, npm, or bun to add the dependency.
|
|
37
37
|
|
|
38
|
-
That's it! You're ready to use this package in your project. Check out the
|
|
38
|
+
That's it! You're ready to use this package in your project. Check out the [usage instructions](#usage) below ✨.
|
|
39
39
|
|
|
40
40
|
# Usage
|
|
41
41
|
|
package/_connections.cjs
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Mongoose connections.
|
|
5
|
+
*
|
|
6
|
+
* This constant is a record of named Mongoose connections, allowing multiple
|
|
7
|
+
* database connections to be managed within the application. An optional `default`
|
|
8
|
+
* connection can also be specified for use when a specific connection is not provided.
|
|
9
|
+
*/
|
|
10
|
+
const mongooseConnections = {};
|
|
11
|
+
|
|
12
|
+
exports.mongooseConnections = mongooseConnections;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { MongooseConnections } from './types';
|
|
2
|
+
/**
|
|
3
|
+
* Mongoose connections.
|
|
4
|
+
*
|
|
5
|
+
* This constant is a record of named Mongoose connections, allowing multiple
|
|
6
|
+
* database connections to be managed within the application. An optional `default`
|
|
7
|
+
* connection can also be specified for use when a specific connection is not provided.
|
|
8
|
+
*/
|
|
9
|
+
export declare const mongooseConnections: MongooseConnections;
|
package/_connections.mjs
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mongoose connections.
|
|
3
|
+
*
|
|
4
|
+
* This constant is a record of named Mongoose connections, allowing multiple
|
|
5
|
+
* database connections to be managed within the application. An optional `default`
|
|
6
|
+
* connection can also be specified for use when a specific connection is not provided.
|
|
7
|
+
*/
|
|
8
|
+
const mongooseConnections = {};
|
|
9
|
+
|
|
10
|
+
export { mongooseConnections };
|
package/constants.cjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const utils = require('./utils.cjs');
|
|
4
|
+
const _connections = require('./_connections.cjs');
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* Common Mongoose schemas.
|
|
@@ -10,14 +11,6 @@ const utils = require('./utils.cjs');
|
|
|
10
11
|
* field types and configurations used across multiple models.
|
|
11
12
|
*/
|
|
12
13
|
const commonMongooseSchemas = utils.createCommonMongooseSchemas();
|
|
13
|
-
/**
|
|
14
|
-
* Mongoose connections.
|
|
15
|
-
*
|
|
16
|
-
* This constant is a record of named Mongoose connections, allowing multiple
|
|
17
|
-
* database connections to be managed within the application. An optional `default`
|
|
18
|
-
* connection can also be specified for use when a specific connection is not provided.
|
|
19
|
-
*/
|
|
20
|
-
const mongooseConnections = {};
|
|
21
14
|
|
|
15
|
+
exports.mongooseConnections = _connections.mongooseConnections;
|
|
22
16
|
exports.commonMongooseSchemas = commonMongooseSchemas;
|
|
23
|
-
exports.mongooseConnections = mongooseConnections;
|
package/constants.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
export { mongooseConnections } from './_connections';
|
|
2
2
|
/**
|
|
3
3
|
* Common Mongoose schemas.
|
|
4
4
|
*
|
|
@@ -167,13 +167,3 @@ export declare const commonMongooseSchemas: {
|
|
|
167
167
|
};
|
|
168
168
|
};
|
|
169
169
|
};
|
|
170
|
-
/**
|
|
171
|
-
* Mongoose connections.
|
|
172
|
-
*
|
|
173
|
-
* This constant is a record of named Mongoose connections, allowing multiple
|
|
174
|
-
* database connections to be managed within the application. An optional `default`
|
|
175
|
-
* connection can also be specified for use when a specific connection is not provided.
|
|
176
|
-
*/
|
|
177
|
-
export declare const mongooseConnections: Record<string, Connection> & {
|
|
178
|
-
default?: Connection;
|
|
179
|
-
};
|
package/constants.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { createCommonMongooseSchemas } from './utils.mjs';
|
|
2
|
+
export { mongooseConnections } from './_connections.mjs';
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* Common Mongoose schemas.
|
|
@@ -8,13 +9,5 @@ import { createCommonMongooseSchemas } from './utils.mjs';
|
|
|
8
9
|
* field types and configurations used across multiple models.
|
|
9
10
|
*/
|
|
10
11
|
const commonMongooseSchemas = createCommonMongooseSchemas();
|
|
11
|
-
/**
|
|
12
|
-
* Mongoose connections.
|
|
13
|
-
*
|
|
14
|
-
* This constant is a record of named Mongoose connections, allowing multiple
|
|
15
|
-
* database connections to be managed within the application. An optional `default`
|
|
16
|
-
* connection can also be specified for use when a specific connection is not provided.
|
|
17
|
-
*/
|
|
18
|
-
const mongooseConnections = {};
|
|
19
12
|
|
|
20
|
-
export { commonMongooseSchemas
|
|
13
|
+
export { commonMongooseSchemas };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kikiutils/mongoose",
|
|
3
|
-
"version": "1.1
|
|
3
|
+
"version": "1.2.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,27 +9,23 @@
|
|
|
9
9
|
"url": "git+https://github.com/kiki-kanri/kikiutils-node-mongoose.git"
|
|
10
10
|
},
|
|
11
11
|
"scripts": {
|
|
12
|
-
"build": "
|
|
12
|
+
"build": "ts-project-builder ./src/constants.ts ./src/utils.ts --clean --preserve-modules",
|
|
13
13
|
"bumplog": "changelogen --bump"
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
16
|
"decimal.js": "^10.4.3",
|
|
17
17
|
"lodash": "^4.17.21",
|
|
18
|
-
"mongoose": "^8.5.
|
|
19
|
-
"mongoose-aggregate-paginate-v2": "^1.1.
|
|
20
|
-
"mongoose-paginate-v2": "^1.8.
|
|
18
|
+
"mongoose": "^8.5.2",
|
|
19
|
+
"mongoose-aggregate-paginate-v2": "^1.1.2",
|
|
20
|
+
"mongoose-paginate-v2": "^1.8.3"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
23
|
"@kikiutils/prettierrc": "^1.3.2",
|
|
24
24
|
"@kikiutils/tsconfigs": "^3.0.1",
|
|
25
|
-
"@rollup/plugin-node-resolve": "^15.2.3",
|
|
26
|
-
"@rollup/plugin-typescript": "^11.1.6",
|
|
27
25
|
"@types/lodash": "^4.17.7",
|
|
28
|
-
"@types/node": "^
|
|
26
|
+
"@types/node": "^22.2.0",
|
|
29
27
|
"changelogen": "^0.5.5",
|
|
30
|
-
"
|
|
31
|
-
"rollup-plugin-delete": "^2.0.0",
|
|
32
|
-
"rollup-plugin-node-externals": "^7.1.2"
|
|
28
|
+
"ts-project-builder": "^3.0.1"
|
|
33
29
|
},
|
|
34
30
|
"keywords": [
|
|
35
31
|
"Decimal128",
|
|
@@ -50,6 +46,11 @@
|
|
|
50
46
|
"require": "./*.cjs",
|
|
51
47
|
"types": "./*.d.ts"
|
|
52
48
|
},
|
|
49
|
+
"./_connections": {
|
|
50
|
+
"import": null,
|
|
51
|
+
"require": null,
|
|
52
|
+
"types": null
|
|
53
|
+
},
|
|
53
54
|
"./plugins/*": {
|
|
54
55
|
"import": "./plugins/*.mjs",
|
|
55
56
|
"require": "./plugins/*.cjs",
|
package/plugins/normalize.cjs
CHANGED
|
@@ -4,6 +4,11 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
4
4
|
|
|
5
5
|
const lodash = require('lodash');
|
|
6
6
|
|
|
7
|
+
const _interopDefaultCompat = e => e && typeof e === 'object' && 'default' in e ? e.default : e;
|
|
8
|
+
|
|
9
|
+
const lodash__default = /*#__PURE__*/_interopDefaultCompat(lodash);
|
|
10
|
+
|
|
11
|
+
const { get: lodashGet, set: lodashSet, unset: lodashUnset } = lodash__default;
|
|
7
12
|
/**
|
|
8
13
|
* Mongoose plugin to normalize the JSON output of documents.
|
|
9
14
|
*
|
|
@@ -29,11 +34,11 @@ const mongooseNormalizePlugin = (schema) => {
|
|
|
29
34
|
delete copiedRet._id;
|
|
30
35
|
for (const path in schema.paths) {
|
|
31
36
|
if (schema.paths[path]?.options?.private)
|
|
32
|
-
|
|
37
|
+
lodashUnset(copiedRet, path);
|
|
33
38
|
if (schema.paths[path]?.instance === 'Decimal128') {
|
|
34
|
-
const value =
|
|
39
|
+
const value = lodashGet(copiedRet, path);
|
|
35
40
|
if (value)
|
|
36
|
-
|
|
41
|
+
lodashSet(copiedRet, path, value.toString());
|
|
37
42
|
}
|
|
38
43
|
}
|
|
39
44
|
delete copiedRet.__v;
|
package/plugins/normalize.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import lodash from 'lodash';
|
|
2
2
|
|
|
3
|
+
const { get: lodashGet, set: lodashSet, unset: lodashUnset } = lodash;
|
|
3
4
|
/**
|
|
4
5
|
* Mongoose plugin to normalize the JSON output of documents.
|
|
5
6
|
*
|
|
@@ -25,11 +26,11 @@ const mongooseNormalizePlugin = (schema) => {
|
|
|
25
26
|
delete copiedRet._id;
|
|
26
27
|
for (const path in schema.paths) {
|
|
27
28
|
if (schema.paths[path]?.options?.private)
|
|
28
|
-
|
|
29
|
+
lodashUnset(copiedRet, path);
|
|
29
30
|
if (schema.paths[path]?.instance === 'Decimal128') {
|
|
30
|
-
const value =
|
|
31
|
+
const value = lodashGet(copiedRet, path);
|
|
31
32
|
if (value)
|
|
32
|
-
|
|
33
|
+
lodashSet(copiedRet, path, value.toString());
|
|
33
34
|
}
|
|
34
35
|
}
|
|
35
36
|
delete copiedRet.__v;
|
package/types/index.d.ts
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import type { AggregatePaginateModel, HydratedDocument, PaginateModel, QueryWithHelpers, Types } from 'mongoose';
|
|
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
4
|
export interface BaseModelStatics<RawDocType, InstanceMethodsAndOverrides = {}, QueryHelpers = {}> {
|
|
5
5
|
}
|
|
6
|
+
export interface MongooseConnections {
|
|
7
|
+
default?: Connection;
|
|
8
|
+
}
|
|
6
9
|
declare global {
|
|
7
10
|
/**
|
|
8
11
|
* Type definition for a base Mongoose document type.
|
package/utils.cjs
CHANGED
|
@@ -5,16 +5,18 @@ const lodash = require('lodash');
|
|
|
5
5
|
const mongoose = require('mongoose');
|
|
6
6
|
const mongooseAggregatePaginate = require('mongoose-aggregate-paginate-v2');
|
|
7
7
|
const mongoosePaginate = require('mongoose-paginate-v2');
|
|
8
|
-
const
|
|
8
|
+
const _connections = require('./_connections.cjs');
|
|
9
9
|
const normalize = require('./plugins/normalize.cjs');
|
|
10
10
|
|
|
11
11
|
const _interopDefaultCompat = e => e && typeof e === 'object' && 'default' in e ? e.default : e;
|
|
12
12
|
|
|
13
13
|
const Decimal__default = /*#__PURE__*/_interopDefaultCompat(Decimal);
|
|
14
|
+
const lodash__default = /*#__PURE__*/_interopDefaultCompat(lodash);
|
|
14
15
|
const mongoose__default = /*#__PURE__*/_interopDefaultCompat(mongoose);
|
|
15
16
|
const mongooseAggregatePaginate__default = /*#__PURE__*/_interopDefaultCompat(mongooseAggregatePaginate);
|
|
16
17
|
const mongoosePaginate__default = /*#__PURE__*/_interopDefaultCompat(mongoosePaginate);
|
|
17
18
|
|
|
19
|
+
const { merge } = lodash__default;
|
|
18
20
|
/**
|
|
19
21
|
* Builds and returns a Mongoose model with specified schema and options.
|
|
20
22
|
*
|
|
@@ -45,7 +47,7 @@ function buildMongooseModel(collection, name, schema, options) {
|
|
|
45
47
|
schema.plugin(mongooseAggregatePaginate__default);
|
|
46
48
|
schema.plugin(mongoosePaginate__default);
|
|
47
49
|
schema.set('timestamps', options?.timestamps === undefined ? true : options.timestamps);
|
|
48
|
-
return (options?.connection ||
|
|
50
|
+
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);
|
|
49
51
|
}
|
|
50
52
|
/**
|
|
51
53
|
* Creates common Mongoose schema definitions with optional custom schemas and settings.
|
|
@@ -62,8 +64,8 @@ function buildMongooseModel(collection, name, schema, options) {
|
|
|
62
64
|
* @returns The merged common and custom schemas.
|
|
63
65
|
*/
|
|
64
66
|
function createCommonMongooseSchemas(customSchemas, options) {
|
|
65
|
-
const autoRoundAndToFixedDecimal128Places = options?.autoRoundAndToFixedDecimal128?.places
|
|
66
|
-
const autoRoundAndToFixedDecimal128Rounding = options?.autoRoundAndToFixedDecimal128?.rounding
|
|
67
|
+
const autoRoundAndToFixedDecimal128Places = options?.autoRoundAndToFixedDecimal128?.places ?? 2;
|
|
68
|
+
const autoRoundAndToFixedDecimal128Rounding = options?.autoRoundAndToFixedDecimal128?.rounding ?? Decimal__default.ROUND_DOWN;
|
|
67
69
|
const baseAutoRoundAndToFixedDecimal128 = {
|
|
68
70
|
set: (value) => new Decimal__default(value.toString()).toFixed(autoRoundAndToFixedDecimal128Places, autoRoundAndToFixedDecimal128Rounding),
|
|
69
71
|
type: mongoose__default.Schema.Types.Decimal128
|
|
@@ -71,7 +73,7 @@ function createCommonMongooseSchemas(customSchemas, options) {
|
|
|
71
73
|
const baseRequiredBoolean = { required: true, type: Boolean };
|
|
72
74
|
const baseRequiredNumber = { required: true, type: Number };
|
|
73
75
|
const baseRequiredObjectId = { required: true, type: mongoose__default.Schema.Types.ObjectId };
|
|
74
|
-
return
|
|
76
|
+
return merge({
|
|
75
77
|
boolean: {
|
|
76
78
|
defaultFalse: {
|
|
77
79
|
nonRequired: { default: false, type: Boolean },
|
package/utils.mjs
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import Decimal from 'decimal.js';
|
|
2
|
-
import
|
|
2
|
+
import lodash from 'lodash';
|
|
3
3
|
import mongoose, { Types } from 'mongoose';
|
|
4
4
|
import mongooseAggregatePaginate from 'mongoose-aggregate-paginate-v2';
|
|
5
5
|
import mongoosePaginate from 'mongoose-paginate-v2';
|
|
6
|
-
import { mongooseConnections } from './
|
|
6
|
+
import { mongooseConnections } from './_connections.mjs';
|
|
7
7
|
import { mongooseNormalizePlugin } from './plugins/normalize.mjs';
|
|
8
8
|
|
|
9
|
+
const { merge } = lodash;
|
|
9
10
|
/**
|
|
10
11
|
* Builds and returns a Mongoose model with specified schema and options.
|
|
11
12
|
*
|
|
@@ -36,7 +37,7 @@ function buildMongooseModel(collection, name, schema, options) {
|
|
|
36
37
|
schema.plugin(mongooseAggregatePaginate);
|
|
37
38
|
schema.plugin(mongoosePaginate);
|
|
38
39
|
schema.set('timestamps', options?.timestamps === undefined ? true : options.timestamps);
|
|
39
|
-
return (options?.connection || mongooseConnections.default || (mongooseConnections.default = mongoose.createConnection(process.env.MONGODB_URI || 'mongodb://
|
|
40
|
+
return (options?.connection || mongooseConnections.default || (mongooseConnections.default = mongoose.createConnection(process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017'))).model(name, schema, collection);
|
|
40
41
|
}
|
|
41
42
|
/**
|
|
42
43
|
* Creates common Mongoose schema definitions with optional custom schemas and settings.
|
|
@@ -53,8 +54,8 @@ function buildMongooseModel(collection, name, schema, options) {
|
|
|
53
54
|
* @returns The merged common and custom schemas.
|
|
54
55
|
*/
|
|
55
56
|
function createCommonMongooseSchemas(customSchemas, options) {
|
|
56
|
-
const autoRoundAndToFixedDecimal128Places = options?.autoRoundAndToFixedDecimal128?.places
|
|
57
|
-
const autoRoundAndToFixedDecimal128Rounding = options?.autoRoundAndToFixedDecimal128?.rounding
|
|
57
|
+
const autoRoundAndToFixedDecimal128Places = options?.autoRoundAndToFixedDecimal128?.places ?? 2;
|
|
58
|
+
const autoRoundAndToFixedDecimal128Rounding = options?.autoRoundAndToFixedDecimal128?.rounding ?? Decimal.ROUND_DOWN;
|
|
58
59
|
const baseAutoRoundAndToFixedDecimal128 = {
|
|
59
60
|
set: (value) => new Decimal(value.toString()).toFixed(autoRoundAndToFixedDecimal128Places, autoRoundAndToFixedDecimal128Rounding),
|
|
60
61
|
type: mongoose.Schema.Types.Decimal128
|