@kikiutils/mongoose 0.4.1 → 0.6.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/LICENSE +21 -0
- package/package.json +4 -3
- package/types/index.d.ts +1 -2
- package/types/options.d.ts +2 -7
- package/utils.d.ts +3 -6
- package/utils.mjs +9 -5
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 kiki-kanri
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kikiutils/mongoose",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "kiki-kanri",
|
|
6
6
|
"repository": {
|
|
@@ -15,14 +15,14 @@
|
|
|
15
15
|
"dependencies": {
|
|
16
16
|
"decimal.js": "^10.4.3",
|
|
17
17
|
"lodash-es": "^4.17.21",
|
|
18
|
-
"mongoose": "^8.3.
|
|
18
|
+
"mongoose": "^8.3.5",
|
|
19
19
|
"mongoose-paginate-v2": "^1.8.0"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
22
|
"@kikiutils/prettierrc": "^1.3.1",
|
|
23
23
|
"@kikiutils/tsconfigs": "^2.2.0",
|
|
24
24
|
"@types/lodash-es": "^4.17.12",
|
|
25
|
-
"@types/node": "^20.12.
|
|
25
|
+
"@types/node": "^20.12.12",
|
|
26
26
|
"changelogen": "^0.5.5",
|
|
27
27
|
"tsc-multi": "^1.1.0",
|
|
28
28
|
"typescript": "^5.4.5"
|
|
@@ -30,6 +30,7 @@
|
|
|
30
30
|
"keywords": [
|
|
31
31
|
"mongooose",
|
|
32
32
|
"mongooose-plugins",
|
|
33
|
+
"mongooose-types",
|
|
33
34
|
"mongooose-utils"
|
|
34
35
|
],
|
|
35
36
|
"engines": {
|
package/types/index.d.ts
CHANGED
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
/// <reference types="mongoose/types/validation" />
|
|
23
23
|
/// <reference types="mongoose/types/virtuals" />
|
|
24
24
|
/// <reference types="mongoose/types/inferschematype" />
|
|
25
|
-
import type { HydratedDocument,
|
|
25
|
+
import type { HydratedDocument, PaginateModel, QueryWithHelpers, Types } from 'mongoose';
|
|
26
26
|
export {} from 'mongoose-paginate-v2';
|
|
27
27
|
export interface BaseModelStatics<RawDocType, InstanceMethodsAndOverrides = {}, QueryHelpers = {}> {
|
|
28
28
|
}
|
|
@@ -32,7 +32,6 @@ declare global {
|
|
|
32
32
|
} : {}) & (UpdatedAtField extends true ? {
|
|
33
33
|
updatedAt: Date;
|
|
34
34
|
} : {});
|
|
35
|
-
type BaseMongooseModel<RawDocType, InstanceMethodsAndOverrides = {}, QueryHelpers = {}> = MongooseModel<RawDocType, QueryHelpers, InstanceMethodsAndOverrides> & BaseModelStatics<RawDocType, InstanceMethodsAndOverrides, QueryHelpers>;
|
|
36
35
|
type BaseMongoosePaginateModel<RawDocType, InstanceMethodsAndOverrides = {}, QueryHelpers = {}> = PaginateModel<RawDocType, QueryHelpers, InstanceMethodsAndOverrides> & BaseModelStatics<RawDocType, InstanceMethodsAndOverrides, QueryHelpers>;
|
|
37
36
|
type MongooseDocumentOrObjectId<D> = D | string | Types.ObjectId;
|
|
38
37
|
type MongooseFindOneReturnType<RawDocType, DocType, QueryHelpers = {}> = QueryWithHelpers<DocType | null, DocType, QueryHelpers, RawDocType, 'findOne'>;
|
package/types/options.d.ts
CHANGED
|
@@ -23,18 +23,13 @@
|
|
|
23
23
|
/// <reference types="mongoose/types/virtuals" />
|
|
24
24
|
/// <reference types="mongoose/types/inferschematype" />
|
|
25
25
|
/// <reference types="mongoose-paginate-v2" />
|
|
26
|
-
import type { Connection,
|
|
27
|
-
export interface BuildMongooseModelOptions
|
|
28
|
-
beforeBuild?: (schema: Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>) => void;
|
|
26
|
+
import type { Connection, SchemaTimestampsConfig } from 'mongoose';
|
|
27
|
+
export interface BuildMongooseModelOptions {
|
|
29
28
|
connection?: Connection;
|
|
30
29
|
/**
|
|
31
30
|
* @default true
|
|
32
31
|
*/
|
|
33
32
|
enableNormalizePlugin?: boolean;
|
|
34
|
-
/**
|
|
35
|
-
* @default true
|
|
36
|
-
*/
|
|
37
|
-
enablePaginatePlugin?: boolean;
|
|
38
33
|
/**
|
|
39
34
|
* @default true
|
|
40
35
|
*/
|
package/utils.d.ts
CHANGED
|
@@ -24,13 +24,9 @@
|
|
|
24
24
|
/// <reference types="mongoose/types/inferschematype" />
|
|
25
25
|
/// <reference types="mongoose-paginate-v2" />
|
|
26
26
|
import mongoose from 'mongoose';
|
|
27
|
-
import type { Model as MongooseModel, PaginateModel } from 'mongoose';
|
|
28
27
|
import type { BuildMongooseModelOptions } from './types/options';
|
|
29
28
|
import type { BaseSchemaAttribute, CreateCommonMongooseSchemasOptions, MongooseObjectIdRefSchema, MongooseStringSchema, MongooseStringSchemaAttribute } from './types/schema';
|
|
30
|
-
export declare
|
|
31
|
-
enablePaginatePlugin: false;
|
|
32
|
-
}): Model;
|
|
33
|
-
export declare function buildMongooseModel<DocType, Model extends PaginateModel<DocType, QueryHelpers, InstanceMethodsAndOverrides>, InstanceMethodsAndOverrides = {}, QueryHelpers = {}>(collection: string, name: string, schema: mongoose.Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>, options?: BuildMongooseModelOptions<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>): Model;
|
|
29
|
+
export declare const buildMongooseModel: <DocType, Model extends mongoose.PaginateModel<DocType, QueryHelpers, InstanceMethodsAndOverrides>, InstanceMethodsAndOverrides = {}, QueryHelpers = {}>(collection: string, name: string, schema: mongoose.Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>, options?: BuildMongooseModelOptions) => Model;
|
|
34
30
|
export declare const createCommonMongooseSchemas: <T extends {}>(customSchemas?: T, options?: CreateCommonMongooseSchemasOptions) => {
|
|
35
31
|
readonly boolean: {
|
|
36
32
|
readonly defaultFalse: {
|
|
@@ -194,4 +190,5 @@ export declare const createCommonMongooseSchemas: <T extends {}>(customSchemas?:
|
|
|
194
190
|
} & T;
|
|
195
191
|
export declare const createMongooseObjectIdRefSchema: <R extends string, T extends BaseSchemaAttribute[]>(refModelName: R, ...attributes: T) => MongooseObjectIdRefSchema<R, T>;
|
|
196
192
|
export declare const createMongooseStringSchema: <T extends MongooseStringSchemaAttribute[]>(...attributes: T) => MongooseStringSchema<T>;
|
|
197
|
-
export declare const
|
|
193
|
+
export declare const mongooseDocumentOrObjectIdToDocument: <D extends mongoose.IfAny<DocType, any, InstanceMethodsAndOverrides extends Record<string, never> ? mongoose.Document<unknown, QueryHelpers, DocType> & mongoose.Require_id<DocType> : mongoose.IfAny<InstanceMethodsAndOverrides, mongoose.Document<unknown, QueryHelpers, DocType> & mongoose.Require_id<DocType>, mongoose.Document<unknown, QueryHelpers, DocType> & Omit<mongoose.Require_id<DocType>, keyof InstanceMethodsAndOverrides> & InstanceMethodsAndOverrides>>, DocType, InstanceMethodsAndOverrides, QueryHelpers>(documentOrObjectId: MongooseDocumentOrObjectId<D>, model: BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>, selectFields?: string[]) => Promise<D | null>;
|
|
194
|
+
export declare const setupDecimal128FieldsToStringGetter: <DocType, Model extends mongoose.PaginateModel<DocType, QueryHelpers, InstanceMethodsAndOverrides>, InstanceMethodsAndOverrides = {}, QueryHelpers = {}>(schema: mongoose.Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>, ...fields: string[]) => void;
|
package/utils.mjs
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
1
|
import Decimal from 'decimal.js';
|
|
2
2
|
import { merge } from 'lodash-es';
|
|
3
3
|
import mongoose from 'mongoose';
|
|
4
|
+
import { Types } from 'mongoose';
|
|
4
5
|
import mongoosePaginate from 'mongoose-paginate-v2';
|
|
5
6
|
import { mongooseConnections } from "./constants.mjs";
|
|
6
7
|
import mongooseNormalizePlugin from "./plugins/normalize.mjs";
|
|
7
|
-
export
|
|
8
|
+
export const buildMongooseModel = (collection, name, schema, options) => {
|
|
8
9
|
if (options?.enableNormalizePlugin !== false)
|
|
9
10
|
schema.plugin(mongooseNormalizePlugin);
|
|
10
|
-
|
|
11
|
-
schema.plugin(mongoosePaginate);
|
|
11
|
+
schema.plugin(mongoosePaginate);
|
|
12
12
|
schema.set('timestamps', options?.timestamps === undefined ? true : options.timestamps);
|
|
13
|
-
options?.beforeBuild?.(schema);
|
|
14
13
|
return (options?.connection || mongooseConnections.default || (mongooseConnections.default = mongoose.createConnection(process.env.MONGODB_URI || 'mongodb://localhost:27017'))).model(name, schema, collection);
|
|
15
|
-
}
|
|
14
|
+
};
|
|
16
15
|
export const createCommonMongooseSchemas = (customSchemas, options) => {
|
|
17
16
|
const autoRoundAndToFixedDecimal128Places = options?.autoRoundAndToFixedDecimal128?.places || 2;
|
|
18
17
|
const autoRoundAndToFixedDecimal128Rounding = options?.autoRoundAndToFixedDecimal128?.rounding || Decimal.ROUND_DOWN;
|
|
@@ -140,4 +139,9 @@ export const createMongooseStringSchema = (...attributes) => {
|
|
|
140
139
|
});
|
|
141
140
|
return schema;
|
|
142
141
|
};
|
|
142
|
+
export const mongooseDocumentOrObjectIdToDocument = async (documentOrObjectId, model, selectFields) => {
|
|
143
|
+
if (typeof documentOrObjectId === 'string' || documentOrObjectId instanceof Types.ObjectId)
|
|
144
|
+
return (await model.findById(documentOrObjectId).select(selectFields || []));
|
|
145
|
+
return documentOrObjectId;
|
|
146
|
+
};
|
|
143
147
|
export const setupDecimal128FieldsToStringGetter = (schema, ...fields) => fields.forEach((field) => schema.path(field).get((value) => value?.toString()));
|