@kikiutils/mongoose 0.2.0 → 0.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/package.json +4 -7
- package/plugins/normalize.mjs +1 -1
- package/utils.mjs +1 -1
- package/constants.cjs +0 -4
- package/plugins/normalize.cjs +0 -27
- package/types/index.cjs +0 -1
- package/types/options.cjs +0 -1
- package/types/schema.cjs +0 -1
- package/utils.cjs +0 -143
package/package.json
CHANGED
|
@@ -1,26 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kikiutils/mongoose",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "kiki-kanri",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
8
|
"url": "git+https://github.com/kiki-kanri/kikiutils-node-mongoose.git"
|
|
9
9
|
},
|
|
10
|
+
"type": "module",
|
|
10
11
|
"scripts": {
|
|
11
12
|
"build": "rm -rf ./dist && tsc-multi",
|
|
12
13
|
"bumplog": "changelogen --bump"
|
|
13
14
|
},
|
|
14
15
|
"dependencies": {
|
|
15
16
|
"decimal.js": "^10.4.3",
|
|
16
|
-
"lodash": "^4.17.21",
|
|
17
|
+
"lodash-es": "^4.17.21",
|
|
17
18
|
"mongoose": "^8.3.2",
|
|
18
19
|
"mongoose-paginate-v2": "^1.8.0"
|
|
19
20
|
},
|
|
20
21
|
"devDependencies": {
|
|
21
22
|
"@kikiutils/prettierrc": "^1.3.1",
|
|
22
23
|
"@kikiutils/tsconfigs": "^2.2.0",
|
|
23
|
-
"@types/lodash": "^4.17.
|
|
24
|
+
"@types/lodash-es": "^4.17.12",
|
|
24
25
|
"@types/node": "^20.12.7",
|
|
25
26
|
"changelogen": "^0.5.5",
|
|
26
27
|
"tsc-multi": "^1.1.0",
|
|
@@ -37,14 +38,10 @@
|
|
|
37
38
|
"exports": {
|
|
38
39
|
"./*": {
|
|
39
40
|
"default": "./*.mjs",
|
|
40
|
-
"import": "./*.mjs",
|
|
41
|
-
"require": "./*.cjs",
|
|
42
41
|
"types": "./*.d.ts"
|
|
43
42
|
},
|
|
44
43
|
"./plugins/*": {
|
|
45
44
|
"default": "./plugins/*.mjs",
|
|
46
|
-
"import": "./plugins/*.mjs",
|
|
47
|
-
"require": "./plugins/*.cjs",
|
|
48
45
|
"types": "./plugins/*.d.ts"
|
|
49
46
|
},
|
|
50
47
|
"./types": {
|
package/plugins/normalize.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { get as lodashGet, set as lodashSet, unset as lodashUnset } from 'lodash';
|
|
1
|
+
import { get as lodashGet, set as lodashSet, unset as lodashUnset } from 'lodash-es';
|
|
2
2
|
export const mongooseNormalizePlugin = (schema) => {
|
|
3
3
|
const toJSON = schema.get('toJSON');
|
|
4
4
|
const toJSONTransform = toJSON?.transform;
|
package/utils.mjs
CHANGED
package/constants.cjs
DELETED
package/plugins/normalize.cjs
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import { get as lodashGet, set as lodashSet, unset as lodashUnset } from 'lodash';
|
|
2
|
-
export const mongooseNormalizePlugin = (schema) => {
|
|
3
|
-
const toJSON = schema.get('toJSON');
|
|
4
|
-
const toJSONTransform = toJSON?.transform;
|
|
5
|
-
schema.set('toJSON', {
|
|
6
|
-
...toJSON,
|
|
7
|
-
transform(doc, ret, options) {
|
|
8
|
-
const copiedRet = { ...ret };
|
|
9
|
-
copiedRet.id = copiedRet._id?.toHexString();
|
|
10
|
-
delete copiedRet._id;
|
|
11
|
-
for (const path in schema.paths) {
|
|
12
|
-
if (schema.paths[path]?.options?.private)
|
|
13
|
-
lodashUnset(copiedRet, path);
|
|
14
|
-
if (schema.paths[path]?.instance === 'Decimal128') {
|
|
15
|
-
const value = lodashGet(copiedRet, path);
|
|
16
|
-
if (value)
|
|
17
|
-
lodashSet(copiedRet, path, value.toString());
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
delete copiedRet.__v;
|
|
21
|
-
if (toJSONTransform && typeof toJSONTransform !== 'boolean')
|
|
22
|
-
return toJSONTransform(doc, copiedRet, options);
|
|
23
|
-
return copiedRet;
|
|
24
|
-
}
|
|
25
|
-
});
|
|
26
|
-
};
|
|
27
|
-
export default mongooseNormalizePlugin;
|
package/types/index.cjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {} from 'mongoose-paginate-v2';
|
package/types/options.cjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/types/schema.cjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/utils.cjs
DELETED
|
@@ -1,143 +0,0 @@
|
|
|
1
|
-
import Decimal from 'decimal.js';
|
|
2
|
-
import { merge } from 'lodash';
|
|
3
|
-
import { Schema } from 'mongoose';
|
|
4
|
-
import mongoosePaginate from 'mongoose-paginate-v2';
|
|
5
|
-
import { defaultMongooseConnection } from "./constants.cjs";
|
|
6
|
-
import mongooseNormalizePlugin from "./plugins/normalize.cjs";
|
|
7
|
-
export function buildMongooseModel(collection, name, schema, options) {
|
|
8
|
-
if (options?.enableNormalizePlugin !== false)
|
|
9
|
-
schema.plugin(mongooseNormalizePlugin);
|
|
10
|
-
if (options?.enablePaginatePlugin !== false)
|
|
11
|
-
schema.plugin(mongoosePaginate);
|
|
12
|
-
schema.set('timestamps', options?.timestamps === undefined ? true : options.timestamps);
|
|
13
|
-
options?.beforeBuild?.(schema);
|
|
14
|
-
return (options?.connection || defaultMongooseConnection).model(name, schema, collection);
|
|
15
|
-
}
|
|
16
|
-
export const createCommonMongooseSchemas = (customSchemas, options) => {
|
|
17
|
-
const autoRoundAndToFixedDecimal128Places = options?.autoRoundAndToFixedDecimal128?.places || 2;
|
|
18
|
-
const autoRoundAndToFixedDecimal128Rounding = options?.autoRoundAndToFixedDecimal128?.rounding || Decimal.ROUND_DOWN;
|
|
19
|
-
const baseAutoRoundAndToFixedDecimal128 = {
|
|
20
|
-
set: (value) => new Decimal(value.toString()).toFixed(autoRoundAndToFixedDecimal128Places, autoRoundAndToFixedDecimal128Rounding),
|
|
21
|
-
type: Schema.Types.Decimal128
|
|
22
|
-
};
|
|
23
|
-
const baseRequiredBoolean = { required: true, type: Boolean };
|
|
24
|
-
const baseRequiredNumber = { required: true, type: Number };
|
|
25
|
-
const baseRequiredObjectId = { required: true, type: Schema.Types.ObjectId };
|
|
26
|
-
return merge({
|
|
27
|
-
boolean: {
|
|
28
|
-
defaultFalse: {
|
|
29
|
-
nonRequired: { default: false, type: Boolean },
|
|
30
|
-
required: { ...baseRequiredBoolean, default: false }
|
|
31
|
-
},
|
|
32
|
-
defaultTrue: {
|
|
33
|
-
nonRequired: { default: true, type: Boolean },
|
|
34
|
-
required: { ...baseRequiredBoolean, default: true }
|
|
35
|
-
},
|
|
36
|
-
nonRequired: { type: Boolean },
|
|
37
|
-
required: baseRequiredBoolean
|
|
38
|
-
},
|
|
39
|
-
decimal128: {
|
|
40
|
-
autoRoundAndToFixed: {
|
|
41
|
-
nonRequired: baseAutoRoundAndToFixedDecimal128,
|
|
42
|
-
required: { ...baseAutoRoundAndToFixedDecimal128, required: true }
|
|
43
|
-
},
|
|
44
|
-
nonRequired: { type: Schema.Types.Decimal128 },
|
|
45
|
-
required: { required: true, type: Schema.Types.Decimal128 }
|
|
46
|
-
},
|
|
47
|
-
number: {
|
|
48
|
-
nonRequired: { type: Number },
|
|
49
|
-
required: baseRequiredNumber,
|
|
50
|
-
unique: {
|
|
51
|
-
nonRequired: { type: Number, unique: true },
|
|
52
|
-
required: { ...baseRequiredNumber, unique: true }
|
|
53
|
-
}
|
|
54
|
-
},
|
|
55
|
-
objectId: {
|
|
56
|
-
nonRequired: { type: Schema.Types.ObjectId },
|
|
57
|
-
required: baseRequiredObjectId,
|
|
58
|
-
unique: {
|
|
59
|
-
nonRequired: { type: Schema.Types.ObjectId, unique: true },
|
|
60
|
-
required: { ...baseRequiredObjectId, unique: true }
|
|
61
|
-
}
|
|
62
|
-
},
|
|
63
|
-
string: {
|
|
64
|
-
nonRequired: createMongooseStringSchema(),
|
|
65
|
-
private: {
|
|
66
|
-
nonRequired: createMongooseStringSchema('private'),
|
|
67
|
-
required: createMongooseStringSchema('private', 'required'),
|
|
68
|
-
short: {
|
|
69
|
-
nonRequired: createMongooseStringSchema('private', 'short'),
|
|
70
|
-
required: createMongooseStringSchema('private', 'required', 'short'),
|
|
71
|
-
trimmed: {
|
|
72
|
-
nonRequired: createMongooseStringSchema('private', 'short', 'trim'),
|
|
73
|
-
required: createMongooseStringSchema('private', 'required', 'short', 'trim'),
|
|
74
|
-
unique: {
|
|
75
|
-
nonRequired: createMongooseStringSchema('private', 'short', 'trim', 'unique'),
|
|
76
|
-
required: createMongooseStringSchema('private', 'required', 'short', 'trim', 'unique')
|
|
77
|
-
}
|
|
78
|
-
},
|
|
79
|
-
unique: {
|
|
80
|
-
nonRequired: createMongooseStringSchema('private', 'short', 'unique'),
|
|
81
|
-
required: createMongooseStringSchema('private', 'required', 'short', 'unique')
|
|
82
|
-
}
|
|
83
|
-
},
|
|
84
|
-
trimmed: {
|
|
85
|
-
nonRequired: createMongooseStringSchema('private', 'trim'),
|
|
86
|
-
required: createMongooseStringSchema('private', 'required', 'trim'),
|
|
87
|
-
unique: {
|
|
88
|
-
nonRequired: createMongooseStringSchema('private', 'trim', 'unique'),
|
|
89
|
-
required: createMongooseStringSchema('private', 'required', 'trim', 'unique')
|
|
90
|
-
}
|
|
91
|
-
},
|
|
92
|
-
unique: {
|
|
93
|
-
nonRequired: createMongooseStringSchema('private', 'unique'),
|
|
94
|
-
required: createMongooseStringSchema('private', 'required', 'unique')
|
|
95
|
-
}
|
|
96
|
-
},
|
|
97
|
-
required: createMongooseStringSchema('required'),
|
|
98
|
-
short: {
|
|
99
|
-
nonRequired: createMongooseStringSchema('short'),
|
|
100
|
-
required: createMongooseStringSchema('required', 'short'),
|
|
101
|
-
trimmed: {
|
|
102
|
-
nonRequired: createMongooseStringSchema('short', 'trim'),
|
|
103
|
-
required: createMongooseStringSchema('required', 'short', 'trim'),
|
|
104
|
-
unique: {
|
|
105
|
-
nonRequired: createMongooseStringSchema('short', 'trim', 'unique'),
|
|
106
|
-
required: createMongooseStringSchema('required', 'short', 'trim', 'unique')
|
|
107
|
-
}
|
|
108
|
-
},
|
|
109
|
-
unique: {
|
|
110
|
-
nonRequired: createMongooseStringSchema('short', 'unique'),
|
|
111
|
-
required: createMongooseStringSchema('required', 'short', 'unique')
|
|
112
|
-
}
|
|
113
|
-
},
|
|
114
|
-
trimmed: {
|
|
115
|
-
nonRequired: createMongooseStringSchema('trim'),
|
|
116
|
-
required: createMongooseStringSchema('required', 'trim'),
|
|
117
|
-
unique: {
|
|
118
|
-
nonRequired: createMongooseStringSchema('trim', 'unique'),
|
|
119
|
-
required: createMongooseStringSchema('required', 'trim', 'unique')
|
|
120
|
-
}
|
|
121
|
-
},
|
|
122
|
-
unique: {
|
|
123
|
-
nonRequired: createMongooseStringSchema('unique'),
|
|
124
|
-
required: createMongooseStringSchema('required', 'unique')
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
}, customSchemas);
|
|
128
|
-
};
|
|
129
|
-
export const createMongooseObjectIdRefSchema = (refModelName, ...attributes) => {
|
|
130
|
-
const schema = { ref: refModelName, type: Schema.Types.ObjectId };
|
|
131
|
-
new Set(attributes).forEach((attribute) => (schema[attribute] = true));
|
|
132
|
-
return schema;
|
|
133
|
-
};
|
|
134
|
-
export const createMongooseStringSchema = (...attributes) => {
|
|
135
|
-
const schema = { type: String };
|
|
136
|
-
new Set(attributes).forEach((attribute) => {
|
|
137
|
-
if (attribute === 'short')
|
|
138
|
-
return (schema.maxlength = 16);
|
|
139
|
-
schema[attribute] = true;
|
|
140
|
-
});
|
|
141
|
-
return schema;
|
|
142
|
-
};
|
|
143
|
-
export const setupDecimal128FieldsToStringGetter = (fields, schema) => fields.forEach((field) => schema.path(field).get((value) => value?.toString()));
|