@kikiutils/mongoose 0.4.0 → 0.5.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/package.json +1 -1
- package/types/index.d.ts +1 -2
- package/types/options.d.ts +2 -7
- package/utils.d.ts +11 -15
- package/utils.mjs +12 -14
package/package.json
CHANGED
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
|
@@ -23,14 +23,10 @@
|
|
|
23
23
|
/// <reference types="mongoose/types/virtuals" />
|
|
24
24
|
/// <reference types="mongoose/types/inferschematype" />
|
|
25
25
|
/// <reference types="mongoose-paginate-v2" />
|
|
26
|
-
import
|
|
27
|
-
import type { Model as MongooseModel, PaginateModel } from 'mongoose';
|
|
26
|
+
import mongoose 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: 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: {
|
|
@@ -69,22 +65,22 @@ export declare const createCommonMongooseSchemas: <T extends {}>(customSchemas?:
|
|
|
69
65
|
readonly set: (value: number | string | {
|
|
70
66
|
toString(): string;
|
|
71
67
|
}) => string;
|
|
72
|
-
readonly type: typeof Schema.Types.Decimal128;
|
|
68
|
+
readonly type: typeof mongoose.Schema.Types.Decimal128;
|
|
73
69
|
};
|
|
74
70
|
readonly required: {
|
|
75
71
|
readonly required: true;
|
|
76
72
|
readonly set: (value: number | string | {
|
|
77
73
|
toString(): string;
|
|
78
74
|
}) => string;
|
|
79
|
-
readonly type: typeof Schema.Types.Decimal128;
|
|
75
|
+
readonly type: typeof mongoose.Schema.Types.Decimal128;
|
|
80
76
|
};
|
|
81
77
|
};
|
|
82
78
|
readonly nonRequired: {
|
|
83
|
-
readonly type: typeof Schema.Types.Decimal128;
|
|
79
|
+
readonly type: typeof mongoose.Schema.Types.Decimal128;
|
|
84
80
|
};
|
|
85
81
|
readonly required: {
|
|
86
82
|
readonly required: true;
|
|
87
|
-
readonly type: typeof Schema.Types.Decimal128;
|
|
83
|
+
readonly type: typeof mongoose.Schema.Types.Decimal128;
|
|
88
84
|
};
|
|
89
85
|
};
|
|
90
86
|
readonly number: {
|
|
@@ -109,21 +105,21 @@ export declare const createCommonMongooseSchemas: <T extends {}>(customSchemas?:
|
|
|
109
105
|
};
|
|
110
106
|
readonly objectId: {
|
|
111
107
|
readonly nonRequired: {
|
|
112
|
-
readonly type: typeof Schema.Types.ObjectId;
|
|
108
|
+
readonly type: typeof mongoose.Schema.Types.ObjectId;
|
|
113
109
|
};
|
|
114
110
|
readonly required: {
|
|
115
111
|
readonly required: true;
|
|
116
|
-
readonly type: typeof Schema.Types.ObjectId;
|
|
112
|
+
readonly type: typeof mongoose.Schema.Types.ObjectId;
|
|
117
113
|
};
|
|
118
114
|
readonly unique: {
|
|
119
115
|
readonly nonRequired: {
|
|
120
|
-
readonly type: typeof Schema.Types.ObjectId;
|
|
116
|
+
readonly type: typeof mongoose.Schema.Types.ObjectId;
|
|
121
117
|
readonly unique: true;
|
|
122
118
|
};
|
|
123
119
|
readonly required: {
|
|
124
120
|
readonly unique: true;
|
|
125
121
|
readonly required: true;
|
|
126
|
-
readonly type: typeof Schema.Types.ObjectId;
|
|
122
|
+
readonly type: typeof mongoose.Schema.Types.ObjectId;
|
|
127
123
|
};
|
|
128
124
|
};
|
|
129
125
|
};
|
|
@@ -194,4 +190,4 @@ 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 setupDecimal128FieldsToStringGetter: <DocType, Model extends
|
|
193
|
+
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,28 +1,26 @@
|
|
|
1
1
|
import Decimal from 'decimal.js';
|
|
2
2
|
import { merge } from 'lodash-es';
|
|
3
|
-
import
|
|
3
|
+
import mongoose from 'mongoose';
|
|
4
4
|
import mongoosePaginate from 'mongoose-paginate-v2';
|
|
5
5
|
import { mongooseConnections } from "./constants.mjs";
|
|
6
6
|
import mongooseNormalizePlugin from "./plugins/normalize.mjs";
|
|
7
|
-
export
|
|
7
|
+
export const buildMongooseModel = (collection, name, schema, options) => {
|
|
8
8
|
if (options?.enableNormalizePlugin !== false)
|
|
9
9
|
schema.plugin(mongooseNormalizePlugin);
|
|
10
|
-
|
|
11
|
-
schema.plugin(mongoosePaginate);
|
|
10
|
+
schema.plugin(mongoosePaginate);
|
|
12
11
|
schema.set('timestamps', options?.timestamps === undefined ? true : options.timestamps);
|
|
13
|
-
options?.
|
|
14
|
-
|
|
15
|
-
}
|
|
12
|
+
return (options?.connection || mongooseConnections.default || (mongooseConnections.default = mongoose.createConnection(process.env.MONGODB_URI || 'mongodb://localhost:27017'))).model(name, schema, collection);
|
|
13
|
+
};
|
|
16
14
|
export const createCommonMongooseSchemas = (customSchemas, options) => {
|
|
17
15
|
const autoRoundAndToFixedDecimal128Places = options?.autoRoundAndToFixedDecimal128?.places || 2;
|
|
18
16
|
const autoRoundAndToFixedDecimal128Rounding = options?.autoRoundAndToFixedDecimal128?.rounding || Decimal.ROUND_DOWN;
|
|
19
17
|
const baseAutoRoundAndToFixedDecimal128 = {
|
|
20
18
|
set: (value) => new Decimal(value.toString()).toFixed(autoRoundAndToFixedDecimal128Places, autoRoundAndToFixedDecimal128Rounding),
|
|
21
|
-
type: Schema.Types.Decimal128
|
|
19
|
+
type: mongoose.Schema.Types.Decimal128
|
|
22
20
|
};
|
|
23
21
|
const baseRequiredBoolean = { required: true, type: Boolean };
|
|
24
22
|
const baseRequiredNumber = { required: true, type: Number };
|
|
25
|
-
const baseRequiredObjectId = { required: true, type: Schema.Types.ObjectId };
|
|
23
|
+
const baseRequiredObjectId = { required: true, type: mongoose.Schema.Types.ObjectId };
|
|
26
24
|
return merge({
|
|
27
25
|
boolean: {
|
|
28
26
|
defaultFalse: {
|
|
@@ -41,8 +39,8 @@ export const createCommonMongooseSchemas = (customSchemas, options) => {
|
|
|
41
39
|
nonRequired: baseAutoRoundAndToFixedDecimal128,
|
|
42
40
|
required: { ...baseAutoRoundAndToFixedDecimal128, required: true }
|
|
43
41
|
},
|
|
44
|
-
nonRequired: { type: Schema.Types.Decimal128 },
|
|
45
|
-
required: { required: true, type: Schema.Types.Decimal128 }
|
|
42
|
+
nonRequired: { type: mongoose.Schema.Types.Decimal128 },
|
|
43
|
+
required: { required: true, type: mongoose.Schema.Types.Decimal128 }
|
|
46
44
|
},
|
|
47
45
|
number: {
|
|
48
46
|
nonRequired: { type: Number },
|
|
@@ -53,10 +51,10 @@ export const createCommonMongooseSchemas = (customSchemas, options) => {
|
|
|
53
51
|
}
|
|
54
52
|
},
|
|
55
53
|
objectId: {
|
|
56
|
-
nonRequired: { type: Schema.Types.ObjectId },
|
|
54
|
+
nonRequired: { type: mongoose.Schema.Types.ObjectId },
|
|
57
55
|
required: baseRequiredObjectId,
|
|
58
56
|
unique: {
|
|
59
|
-
nonRequired: { type: Schema.Types.ObjectId, unique: true },
|
|
57
|
+
nonRequired: { type: mongoose.Schema.Types.ObjectId, unique: true },
|
|
60
58
|
required: { ...baseRequiredObjectId, unique: true }
|
|
61
59
|
}
|
|
62
60
|
},
|
|
@@ -127,7 +125,7 @@ export const createCommonMongooseSchemas = (customSchemas, options) => {
|
|
|
127
125
|
}, customSchemas);
|
|
128
126
|
};
|
|
129
127
|
export const createMongooseObjectIdRefSchema = (refModelName, ...attributes) => {
|
|
130
|
-
const schema = { ref: refModelName, type: Schema.Types.ObjectId };
|
|
128
|
+
const schema = { ref: refModelName, type: mongoose.Schema.Types.ObjectId };
|
|
131
129
|
new Set(attributes).forEach((attribute) => (schema[attribute] = true));
|
|
132
130
|
return schema;
|
|
133
131
|
};
|