@kikiutils/mongoose 2.0.0-rc.1 → 2.0.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/utils.mjs CHANGED
@@ -1,14 +1,10 @@
1
- import Decimal from 'decimal.js';
2
- import lodash from 'lodash';
3
1
  import mongoose, { Types } from 'mongoose';
4
2
  import mongooseAggregatePaginate from 'mongoose-aggregate-paginate-v2';
5
3
  import mongoosePaginate from 'mongoose-paginate-v2';
6
- import net from 'node:net';
7
- import { customMongooseOptions, mongooseConnections } from './_internals.mjs';
8
- import { customMongooseOptions as customMongooseOptions$1 } from './options.mjs';
4
+ import { customMongooseOptions } from './_internals.mjs';
5
+ import { mongooseConnections } from './constants.mjs';
9
6
  import { mongooseNormalizePlugin } from './plugins/normalize.mjs';
10
7
 
11
- const { merge } = lodash;
12
8
  /**
13
9
  * Builds and returns a Mongoose model with specified schema and options.
14
10
  *
@@ -41,281 +37,10 @@ function buildMongooseModel(collection, name, schema, options) {
41
37
  schema.plugin(mongooseNormalizePlugin);
42
38
  schema.plugin(mongooseAggregatePaginate);
43
39
  schema.plugin(mongoosePaginate);
44
- schema.set('timestamps', options?.timestamps === undefined ? true : options.timestamps);
45
- (customMongooseOptions.beforeModelBuild || customMongooseOptions$1.beforeModelBuild)?.(schema);
40
+ schema.set('timestamps', options?.timestamps ?? true);
41
+ customMongooseOptions.beforeModelBuild?.(schema);
46
42
  return (options?.connection || mongooseConnections.default || (mongooseConnections.default = mongoose.createConnection(process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017'))).model(name, schema, collection);
47
43
  }
48
- /**
49
- * Creates common Mongoose schema definitions with optional custom schemas and settings.
50
- *
51
- * This function generates a set of commonly used schema definitions, such as boolean,
52
- * number, objectId, and string schemas, with optional custom configurations and rounding
53
- * options for Decimal128 fields.
54
- *
55
- * @deprecated - Will be removed in the next major version. Use {@link schemaBuilders} instead.
56
- *
57
- * @template T - The type for custom schemas.
58
- *
59
- * @param customSchemas - Optional custom schemas to merge with the common schemas.
60
- * @param options - Optional settings for creating the common schemas.
61
- *
62
- * @returns The merged common and custom schemas.
63
- */
64
- function createCommonMongooseSchemas(customSchemas, options) {
65
- const autoRoundAndToFixedDecimal128Places = options?.autoRoundAndToFixedDecimal128?.places ?? 2;
66
- const autoRoundAndToFixedDecimal128Rounding = options?.autoRoundAndToFixedDecimal128?.rounding ?? Decimal.ROUND_DOWN;
67
- const baseAutoRoundAndToFixedDecimal128 = {
68
- set: (value) => new Decimal(value.toString()).toFixed(autoRoundAndToFixedDecimal128Places, autoRoundAndToFixedDecimal128Rounding),
69
- type: mongoose.Schema.Types.Decimal128
70
- };
71
- const baseIPv4 = {
72
- trim: true,
73
- type: String,
74
- validate: {
75
- message: '`{VALUE}` is not a valid IPv4 address for path `{PATH}`.',
76
- validator: (value) => net.isIPv4(value)
77
- }
78
- };
79
- const baseIPv6 = {
80
- ...baseIPv4,
81
- validate: {
82
- message: '`{VALUE}` is not a valid IPv6 address for path `{PATH}`.',
83
- validator: (value) => net.isIPv6(value)
84
- }
85
- };
86
- const baseRequiredBoolean = { required: true, type: Boolean };
87
- const baseRequiredNumber = { required: true, type: Number };
88
- const baseRequiredObjectId = { required: true, type: mongoose.Schema.Types.ObjectId };
89
- return merge({
90
- /**
91
- * @deprecated - Will be removed in the next major version. Use {@link schemaBuilders.boolean} instead.
92
- */
93
- boolean: {
94
- defaultFalse: {
95
- nonRequired: { default: false, type: Boolean },
96
- required: { ...baseRequiredBoolean, default: false }
97
- },
98
- defaultTrue: {
99
- nonRequired: { default: true, type: Boolean },
100
- required: { ...baseRequiredBoolean, default: true }
101
- },
102
- nonRequired: { type: Boolean },
103
- required: baseRequiredBoolean
104
- },
105
- /**
106
- * @deprecated - Will be removed in the next major version. Use {@link schemaBuilders.decimal128} instead.
107
- */
108
- decimal128: {
109
- autoRoundAndToFixed: {
110
- nonRequired: baseAutoRoundAndToFixedDecimal128,
111
- required: { ...baseAutoRoundAndToFixedDecimal128, required: true }
112
- },
113
- nonRequired: { type: mongoose.Schema.Types.Decimal128 },
114
- required: { required: true, type: mongoose.Schema.Types.Decimal128 }
115
- },
116
- /**
117
- * @deprecated - Will be removed in the next major version. Use {@link schemaBuilders.number} instead.
118
- */
119
- number: {
120
- nonRequired: { type: Number },
121
- required: baseRequiredNumber,
122
- unique: {
123
- nonRequired: { type: Number, unique: true },
124
- required: { ...baseRequiredNumber, unique: true }
125
- }
126
- },
127
- /**
128
- * @deprecated - Will be removed in the next major version. Use {@link schemaBuilders.objectId} instead.
129
- */
130
- objectId: {
131
- nonRequired: { type: mongoose.Schema.Types.ObjectId },
132
- required: baseRequiredObjectId,
133
- unique: {
134
- nonRequired: { type: mongoose.Schema.Types.ObjectId, unique: true },
135
- required: { ...baseRequiredObjectId, unique: true }
136
- }
137
- },
138
- /**
139
- * @deprecated - Will be removed in the next major version. Use {@link schemaBuilders.string} instead.
140
- */
141
- string: {
142
- ipv4: {
143
- nonRequired: baseIPv4,
144
- private: {
145
- nonRequired: { ...baseIPv4, private: true },
146
- required: {
147
- ...baseIPv4,
148
- private: true,
149
- required: true
150
- },
151
- unique: {
152
- nonRequired: {
153
- ...baseIPv4,
154
- private: true,
155
- unique: true
156
- },
157
- required: {
158
- ...baseIPv4,
159
- private: true,
160
- required: true,
161
- unique: true
162
- }
163
- }
164
- },
165
- required: { ...baseIPv4, required: true },
166
- unique: {
167
- nonRequired: { ...baseIPv4, unique: true },
168
- required: {
169
- ...baseIPv4,
170
- required: true,
171
- unique: true
172
- }
173
- }
174
- },
175
- ipv6: {
176
- nonRequired: baseIPv6,
177
- private: {
178
- nonRequired: { ...baseIPv6, private: true },
179
- required: {
180
- ...baseIPv6,
181
- private: true,
182
- required: true
183
- },
184
- unique: {
185
- nonRequired: {
186
- ...baseIPv6,
187
- private: true,
188
- unique: true
189
- },
190
- required: {
191
- ...baseIPv6,
192
- private: true,
193
- required: true,
194
- unique: true
195
- }
196
- }
197
- },
198
- required: { ...baseIPv6, required: true },
199
- unique: {
200
- nonRequired: { ...baseIPv6, unique: true },
201
- required: {
202
- ...baseIPv6,
203
- required: true,
204
- unique: true
205
- }
206
- }
207
- },
208
- nonRequired: createMongooseStringSchema(),
209
- private: {
210
- nonRequired: createMongooseStringSchema('private'),
211
- required: createMongooseStringSchema('private', 'required'),
212
- short: {
213
- nonRequired: createMongooseStringSchema('private', 'short'),
214
- required: createMongooseStringSchema('private', 'required', 'short'),
215
- trimmed: {
216
- nonRequired: createMongooseStringSchema('private', 'short', 'trim'),
217
- required: createMongooseStringSchema('private', 'required', 'short', 'trim'),
218
- unique: {
219
- nonRequired: createMongooseStringSchema('private', 'short', 'trim', 'unique'),
220
- required: createMongooseStringSchema('private', 'required', 'short', 'trim', 'unique')
221
- }
222
- },
223
- unique: {
224
- nonRequired: createMongooseStringSchema('private', 'short', 'unique'),
225
- required: createMongooseStringSchema('private', 'required', 'short', 'unique')
226
- }
227
- },
228
- trimmed: {
229
- nonRequired: createMongooseStringSchema('private', 'trim'),
230
- required: createMongooseStringSchema('private', 'required', 'trim'),
231
- unique: {
232
- nonRequired: createMongooseStringSchema('private', 'trim', 'unique'),
233
- required: createMongooseStringSchema('private', 'required', 'trim', 'unique')
234
- }
235
- },
236
- unique: {
237
- nonRequired: createMongooseStringSchema('private', 'unique'),
238
- required: createMongooseStringSchema('private', 'required', 'unique')
239
- }
240
- },
241
- required: createMongooseStringSchema('required'),
242
- short: {
243
- nonRequired: createMongooseStringSchema('short'),
244
- required: createMongooseStringSchema('required', 'short'),
245
- trimmed: {
246
- nonRequired: createMongooseStringSchema('short', 'trim'),
247
- required: createMongooseStringSchema('required', 'short', 'trim'),
248
- unique: {
249
- nonRequired: createMongooseStringSchema('short', 'trim', 'unique'),
250
- required: createMongooseStringSchema('required', 'short', 'trim', 'unique')
251
- }
252
- },
253
- unique: {
254
- nonRequired: createMongooseStringSchema('short', 'unique'),
255
- required: createMongooseStringSchema('required', 'short', 'unique')
256
- }
257
- },
258
- trimmed: {
259
- nonRequired: createMongooseStringSchema('trim'),
260
- required: createMongooseStringSchema('required', 'trim'),
261
- unique: {
262
- nonRequired: createMongooseStringSchema('trim', 'unique'),
263
- required: createMongooseStringSchema('required', 'trim', 'unique')
264
- }
265
- },
266
- unique: {
267
- nonRequired: createMongooseStringSchema('unique'),
268
- required: createMongooseStringSchema('required', 'unique')
269
- }
270
- }
271
- }, customSchemas);
272
- }
273
- /**
274
- * Creates a Mongoose schema definition for an ObjectId reference with specified attributes.
275
- *
276
- * This function generates a schema definition for an ObjectId reference field, including
277
- * attributes such as 'private', 'required', and 'unique'.
278
- *
279
- * @deprecated - Will be removed in the next major version. Use {@link schemaBuilders.ref} instead.
280
- *
281
- * @template R - The type of the reference model name.
282
- * @template T - An array of attributes to apply to the schema field.
283
- *
284
- * @param refModelName - The name of the reference model.
285
- * @param attributes - The attributes to apply to the schema field.
286
- *
287
- * @returns The generated ObjectId reference schema definition.
288
- */
289
- function createMongooseObjectIdRefSchema(refModelName, ...attributes) {
290
- const schema = { ref: refModelName, type: mongoose.Schema.Types.ObjectId };
291
- new Set(attributes).forEach((attribute) => (schema[attribute] = true));
292
- return schema;
293
- }
294
- /**
295
- * Creates a Mongoose schema definition for a string field with specified attributes.
296
- *
297
- * This function generates a schema definition for a string field, including attributes
298
- * such as 'private', 'required', 'short', 'trim', and 'unique'. If the 'short' attribute
299
- * is included, the `maxlength` is set to 16.
300
- *
301
- * @deprecated - Will be removed in the next major version. Use {@link schemaBuilders.string} instead.
302
- *
303
- * @template T - An array of attributes to apply to the schema field.
304
- *
305
- * @param attributes - The attributes to apply to the schema field.
306
- *
307
- * @returns The generated string schema definition.
308
- */
309
- function createMongooseStringSchema(...attributes) {
310
- const schema = { type: String };
311
- new Set(attributes).forEach((attribute) => {
312
- if (attribute === 'short')
313
- schema.maxlength = 16;
314
- else
315
- schema[attribute] = true;
316
- });
317
- return schema;
318
- }
319
44
  /**
320
45
  * Converts a document or ObjectId to a document using the specified model.
321
46
  *
@@ -338,24 +63,5 @@ async function mongooseDocumentOrObjectIdToDocument(documentOrObjectId, model, s
338
63
  return (await model.findById(documentOrObjectId).select(selectFields || []));
339
64
  return documentOrObjectId;
340
65
  }
341
- /**
342
- * Sets up a getter to convert Decimal128 fields to strings in a Mongoose schema.
343
- *
344
- * This function adds a getter to the specified Decimal128 fields in the schema,
345
- * converting the values to strings for easier use.
346
- *
347
- * @deprecated - Will be removed in the next major version. Use {@link schemaBuilders.decimal128} instead.
348
- *
349
- * @template DocType - The type of the document.
350
- * @template Model - The type of the model, which extends `BaseMongoosePaginateModel`.
351
- * @template InstanceMethodsAndOverrides - The type for instance methods and overrides.
352
- * @template QueryHelpers - The type for additional query helpers.
353
- *
354
- * @param schema - The Mongoose schema to modify.
355
- * @param fields - The Decimal128 fields to add the getter to.
356
- */
357
- function setupDecimal128FieldsToStringGetter(schema, ...fields) {
358
- fields.forEach((field) => schema.path(field).get((value) => value?.toString()));
359
- }
360
66
 
361
- export { buildMongooseModel, createCommonMongooseSchemas, createMongooseObjectIdRefSchema, createMongooseStringSchema, mongooseDocumentOrObjectIdToDocument, setupDecimal128FieldsToStringGetter };
67
+ export { buildMongooseModel, mongooseDocumentOrObjectIdToDocument };
package/types/schema.d.ts DELETED
@@ -1,142 +0,0 @@
1
- import type Decimal from 'decimal.js';
2
- import type { ObjectIdSchemaDefinition, SchemaTypeOptions, StringSchemaDefinition, Types } from 'mongoose';
3
- import type { schemaBuilders } from '../schema-builders';
4
- export type DoNotUseOrRemoveThisType = typeof schemaBuilders;
5
- /**
6
- * Base attributes for Mongoose schemas.
7
- *
8
- * This type defines common attributes that can be applied to any schema field.
9
- *
10
- * @deprecated - Will be removed in the next major version.
11
- */
12
- export type BaseSchemaAttribute = 'private' | 'required' | 'unique';
13
- /**
14
- * Additional attributes specific to string schema fields in Mongoose.
15
- *
16
- * This type extends the base schema attributes with string-specific attributes
17
- * like `lowercase`, `short`, `trim`, and `uppercase`.
18
- *
19
- * @deprecated - Will be removed in the next major version.
20
- */
21
- export type MongooseStringSchemaAttribute = BaseSchemaAttribute | 'lowercase' | 'short' | 'trim' | 'uppercase';
22
- /**
23
- * Interface for options to create common Mongoose schemas.
24
- *
25
- * This interface defines options for creating common Mongoose schemas, including
26
- * settings for automatic rounding and fixed decimal places for Decimal128 fields.
27
- *
28
- * @deprecated - Will be removed in the next major version. Use {@link schemaBuilders} instead.
29
- */
30
- export interface CreateCommonMongooseSchemasOptions {
31
- /**
32
- * Configuration for automatic rounding and fixed decimal places for Decimal128 fields.
33
- */
34
- autoRoundAndToFixedDecimal128?: {
35
- /**
36
- * Number of decimal places to round to.
37
- *
38
- * @default 2
39
- */
40
- places?: number;
41
- /**
42
- * Rounding mode to be used.
43
- *
44
- * @default Decimal.ROUND_DOWN
45
- */
46
- rounding?: Decimal.Rounding;
47
- };
48
- }
49
- /**
50
- * Interface for defining a Mongoose ObjectId reference schema field.
51
- *
52
- * This interface allows specifying attributes for an ObjectId reference field,
53
- * including whether the field is private, the reference model name, and standard
54
- * schema options like `required` and `unique`.
55
- *
56
- * @deprecated - Will be removed in the next major version.
57
- *
58
- * @template R - The type of the reference model name.
59
- * @template T - An array of attributes to apply to the schema field.
60
- */
61
- export interface MongooseObjectIdRefSchema<R extends string, T extends BaseSchemaAttribute[]> {
62
- /**
63
- * Indicates whether the field is private.
64
- *
65
- * If the 'private' attribute is included in `T`, this is `true`, otherwise it
66
- * can be `boolean` or `undefined`. If using the normalize plugin, fields marked
67
- * as private will be automatically excluded from the `toJSON` output.
68
- */
69
- private: 'private' extends T[number] ? true : boolean | undefined;
70
- /**
71
- * The model that `populate()` should use if populating this path.
72
- */
73
- ref: R;
74
- /**
75
- * If true, attach a required validator to this path, which ensures this path
76
- * path cannot be set to a nullish value. If a function, Mongoose calls the
77
- * function and only checks for nullish values if the function returns a truthy value.
78
- */
79
- required: 'required' extends T[number] ? true : SchemaTypeOptions<Types.ObjectId>['required'];
80
- /**
81
- * The schema type definition for ObjectId.
82
- */
83
- type: ObjectIdSchemaDefinition;
84
- /**
85
- * If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose
86
- * will build a unique index on this path when the
87
- * model is compiled. [The `unique` option is **not** a validator](/docs/validation.html#the-unique-option-is-not-a-validator).
88
- */
89
- unique: 'unique' extends T[number] ? true : SchemaTypeOptions<Types.ObjectId>['unique'];
90
- }
91
- /**
92
- * Interface for defining a Mongoose string schema field.
93
- *
94
- * This interface allows specifying attributes for a string schema field, including
95
- * whether the field should be lowercased, trimmed, or uppercased, as well as standard
96
- * schema options like `required` and `unique`.
97
- *
98
- * @deprecated - Will be removed in the next major version.
99
- *
100
- * @template T - An array of attributes to apply to the schema field.
101
- */
102
- export interface MongooseStringSchema<T extends MongooseStringSchemaAttribute[]> {
103
- /** If truthy, Mongoose will add a custom setter that lowercases this string using JavaScript's built-in `String#toLowerCase()`. */
104
- lowercase: 'lowercase' extends T[number] ? boolean : SchemaTypeOptions<string>['lowercase'];
105
- /**
106
- * The maximum length of the string.
107
- *
108
- * If the 'short' attribute is included in `T`, this is set to 16. Otherwise, it
109
- * takes the value from `SchemaTypeOptions<string>['maxlength']`.
110
- *
111
- * If set, Mongoose will add a custom validator that ensures the given string's `length` is at most the given number.
112
- */
113
- maxlength: 'short' extends T[number] ? 16 : SchemaTypeOptions<string>['maxlength'];
114
- /**
115
- * Indicates whether the field is private.
116
- *
117
- * If the 'private' attribute is included in `T`, this is `true`, otherwise it
118
- * can be `boolean` or `undefined`. If using the normalize plugin, fields marked
119
- * as private will be automatically excluded from the `toJSON` output.
120
- */
121
- private: 'private' extends T[number] ? true : boolean | undefined;
122
- /**
123
- * If true, attach a required validator to this path, which ensures this path
124
- * path cannot be set to a nullish value. If a function, Mongoose calls the
125
- * function and only checks for nullish values if the function returns a truthy value.
126
- */
127
- required: 'required' extends T[number] ? true : SchemaTypeOptions<string>['required'];
128
- /** If truthy, Mongoose will add a custom setter that removes leading and trailing whitespace using JavaScript's built-in `String#trim()`. */
129
- trim: 'trim' extends T[number] ? true : SchemaTypeOptions<string>['trim'];
130
- /**
131
- * The schema type definition for string.
132
- */
133
- type: StringSchemaDefinition;
134
- /**
135
- * If [truthy](https://masteringjs.io/tutorials/fundamentals/truthy), Mongoose
136
- * will build a unique index on this path when the
137
- * model is compiled. [The `unique` option is **not** a validator](/docs/validation.html#the-unique-option-is-not-a-validator).
138
- */
139
- unique: 'unique' extends T[number] ? true : SchemaTypeOptions<string>['unique'];
140
- /** If truthy, Mongoose will add a custom setter that uppercases this string using JavaScript's built-in `String#toUpperCase()`. */
141
- uppercase: 'uppercase' extends T[number] ? boolean : SchemaTypeOptions<string>['uppercase'];
142
- }