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