@kikiutils/mongoose 1.3.1 → 1.4.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/options.cjs ADDED
@@ -0,0 +1,5 @@
1
+ 'use strict';
2
+
3
+ const customMongooseOptions = {};
4
+
5
+ exports.customMongooseOptions = customMongooseOptions;
package/options.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import type { CustomMongooseOptions } from './types/options';
2
+ export declare const customMongooseOptions: CustomMongooseOptions;
package/options.mjs ADDED
@@ -0,0 +1,3 @@
1
+ const customMongooseOptions = {};
2
+
3
+ export { customMongooseOptions };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kikiutils/mongoose",
3
- "version": "1.3.1",
3
+ "version": "1.4.1",
4
4
  "description": "A Mongoose plugin for enhanced JSON normalization and common schema creation, with built-in support for pagination and automatic Decimal128 conversion.",
5
5
  "license": "MIT",
6
6
  "author": "kiki-kanri",
@@ -10,23 +10,23 @@
10
10
  },
11
11
  "scripts": {
12
12
  "build": "ts-project-builder ./src/constants.ts ./src/utils.ts --clean --preserve-modules",
13
- "bumplog": "changelogen --bump"
13
+ "bumplog": "changelogen --bump",
14
+ "release": "pnpm run build && changelogen --release && sh ./build-and-publish.sh"
14
15
  },
15
16
  "dependencies": {
16
17
  "decimal.js": "^10.4.3",
17
18
  "lodash": "^4.17.21",
18
- "mongoose": "^8.7.0",
19
+ "mongoose": "^8.7.2",
19
20
  "mongoose-aggregate-paginate-v2": "^1.1.2",
20
21
  "mongoose-paginate-v2": "^1.8.5"
21
22
  },
22
23
  "devDependencies": {
24
+ "@kikiutils/changelogen": "^0.6.0",
23
25
  "@kikiutils/prettierrc": "^1.3.2",
24
26
  "@kikiutils/tsconfigs": "^3.0.1",
25
- "@types/lodash": "^4.17.9",
26
- "@types/node": "^22.7.4",
27
- "changelogen": "^0.5.7",
28
- "ts-project-builder": "^3.0.1",
29
- "tslib": "^2.7.0"
27
+ "@types/lodash": "^4.17.10",
28
+ "@types/node": "^22.7.6",
29
+ "ts-project-builder": "^3.2.4"
30
30
  },
31
31
  "keywords": [
32
32
  "Decimal128",
@@ -32,7 +32,7 @@ declare module 'mongoose' {
32
32
  /** optional query conditions to match */
33
33
  match?: any;
34
34
  /** optional model to use for population */
35
- model?: string | Model<any> | undefined;
35
+ model?: string | Model<any>;
36
36
  /** optional query options like sort, limit, etc */
37
37
  options?: any;
38
38
  /** deep populate */
package/types/data.d.ts CHANGED
@@ -1,14 +1,14 @@
1
1
  /**
2
2
  * Interface for the base data of a Mongoose model, optionally including timestamp fields.
3
3
  *
4
- * This interface extends `_WithTimestampFieldsData` to optionally include `createdAt`
4
+ * This interface extends `LocalWithTimestampFieldsData` to optionally include `createdAt`
5
5
  * and `updatedAt` fields based on the `CreatedAtField` and `UpdatedAtField` flags.
6
6
  * It also includes a mandatory `id` field.
7
7
  *
8
8
  * @template CreatedAtField - A boolean flag indicating whether the `createdAt` field should be included.
9
9
  * @template UpdatedAtField - A boolean flag indicating whether the `updatedAt` field should be included.
10
10
  */
11
- interface _BaseMongooseModelData<CreatedAtField extends boolean = true, UpdatedAtField extends boolean = true> extends _WithTimestampFieldsData<CreatedAtField, UpdatedAtField> {
11
+ interface LocalBaseMongooseModelData<CreatedAtField extends boolean = true, UpdatedAtField extends boolean = true> extends LocalWithTimestampFieldsData<CreatedAtField, UpdatedAtField> {
12
12
  id: string;
13
13
  }
14
14
  /**
@@ -21,7 +21,7 @@ interface _BaseMongooseModelData<CreatedAtField extends boolean = true, UpdatedA
21
21
  * @template CreatedAt - A boolean flag indicating whether the `createdAt` field should be included.
22
22
  * @template UpdatedAt - A boolean flag indicating whether the `updatedAt` field should be included.
23
23
  */
24
- interface _WithTimestampFieldsData<CreatedAt extends boolean = true, UpdatedAt extends boolean = true> {
24
+ interface LocalWithTimestampFieldsData<CreatedAt extends boolean = true, UpdatedAt extends boolean = true> {
25
25
  createdAt: CreatedAt extends true ? string : never;
26
26
  updatedAt: UpdatedAt extends true ? string : never;
27
27
  }
@@ -40,13 +40,13 @@ declare global {
40
40
  /**
41
41
  * Type definition for the base data of a Mongoose model, optionally including timestamp fields.
42
42
  *
43
- * This type alias uses `OmitNever` to remove any `never` properties from `_BaseMongooseModelData`
43
+ * This type alias uses `OmitNever` to remove any `never` properties from `LocalBaseMongooseModelData`
44
44
  * based on the presence of the `CreatedAtField` and `UpdatedAtField` flags.
45
45
  *
46
46
  * @template CreatedAtField - A boolean flag indicating whether the `createdAt` field should be included.
47
47
  * @template UpdatedAtField - A boolean flag indicating whether the `updatedAt` field should be included.
48
48
  */
49
- type BaseMongooseModelData<CreatedAtField extends boolean = true, UpdatedAtField extends boolean = true> = OmitNever<_BaseMongooseModelData<CreatedAtField, UpdatedAtField>>;
49
+ type BaseMongooseModelData<CreatedAtField extends boolean = true, UpdatedAtField extends boolean = true> = OmitNever<LocalBaseMongooseModelData<CreatedAtField, UpdatedAtField>>;
50
50
  /**
51
51
  * Type definition to omit timestamp fields and other specified fields from a given type.
52
52
  *
@@ -60,12 +60,12 @@ declare global {
60
60
  /**
61
61
  * Type definition for including timestamp fields conditionally based on the provided flags.
62
62
  *
63
- * This type alias uses `OmitNever` to remove any `never` properties from `_WithTimestampFieldsData`
63
+ * This type alias uses `OmitNever` to remove any `never` properties from `LocalWithTimestampFieldsData`
64
64
  * based on the presence of the `CreatedAt` and `UpdatedAt` flags.
65
65
  *
66
66
  * @template CreatedAt - A boolean flag indicating whether the `createdAt` field should be included.
67
67
  * @template UpdatedAt - A boolean flag indicating whether the `updatedAt` field should be included.
68
68
  */
69
- type WithMongooseTimestampFieldsData<CreatedAt extends boolean = true, UpdatedAt extends boolean = true> = OmitNever<_WithTimestampFieldsData<CreatedAt, UpdatedAt>>;
69
+ type WithMongooseTimestampFieldsData<CreatedAt extends boolean = true, UpdatedAt extends boolean = true> = OmitNever<LocalWithTimestampFieldsData<CreatedAt, UpdatedAt>>;
70
70
  }
71
71
  export {};
@@ -1,4 +1,7 @@
1
- import type { Connection, SchemaTimestampsConfig } from 'mongoose';
1
+ import type { Connection, Schema, SchemaTimestampsConfig } from 'mongoose';
2
+ export interface CustomMongooseOptions {
3
+ beforeModelBuild?: <DocType, Model extends BaseMongoosePaginateModel<DocType, InstanceMethodsAndOverrides, QueryHelpers>, InstanceMethodsAndOverrides = {}, QueryHelpers = {}>(schema: Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>) => void;
4
+ }
2
5
  /**
3
6
  * Interface for options used when building a Mongoose model.
4
7
  *
@@ -39,13 +39,13 @@ declare module 'mongoose' {
39
39
  }
40
40
  interface SubPaginateOptions {
41
41
  select?: object | string;
42
- populate?: PopulateOptions[] | string[] | PopulateOptions | string | PopulateOptions;
42
+ populate?: PopulateOptions[] | string[] | PopulateOptions | string;
43
43
  pagination?: boolean;
44
44
  read?: PaginateReadOptions;
45
45
  pagingOptions: SubDocumentPagingOptions | undefined;
46
46
  }
47
47
  interface SubDocumentPagingOptions {
48
- populate?: PopulateOptions[] | string[] | PopulateOptions | string | PopulateOptions;
48
+ populate?: PopulateOptions[] | string[] | PopulateOptions | string;
49
49
  page?: number;
50
50
  limit?: number;
51
51
  }
package/utils.cjs CHANGED
@@ -7,6 +7,7 @@ const mongooseAggregatePaginate = require('mongoose-aggregate-paginate-v2');
7
7
  const mongoosePaginate = require('mongoose-paginate-v2');
8
8
  const net = require('node:net');
9
9
  const _connections = require('./_connections.cjs');
10
+ const options = require('./options.cjs');
10
11
  const normalize = require('./plugins/normalize.cjs');
11
12
 
12
13
  const _interopDefaultCompat = e => e && typeof e === 'object' && 'default' in e ? e.default : e;
@@ -43,13 +44,14 @@ const { merge } = lodash__default;
43
44
  *
44
45
  * @returns The created Mongoose model.
45
46
  */
46
- function buildMongooseModel(collection, name, schema, options) {
47
- if (options?.enableNormalizePlugin !== false)
47
+ function buildMongooseModel(collection, name, schema, options$1) {
48
+ if (options$1?.enableNormalizePlugin !== false)
48
49
  schema.plugin(normalize.mongooseNormalizePlugin);
49
50
  schema.plugin(mongooseAggregatePaginate__default);
50
51
  schema.plugin(mongoosePaginate__default);
51
- schema.set('timestamps', options?.timestamps === undefined ? true : options.timestamps);
52
- return (options?.connection || _connections.mongooseConnections.default || (_connections.mongooseConnections.default = mongoose__default.createConnection(process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017'))).model(name, schema, collection);
52
+ schema.set('timestamps', options$1?.timestamps === undefined ? true : options$1.timestamps);
53
+ options.customMongooseOptions.beforeModelBuild?.(schema);
54
+ return (options$1?.connection || _connections.mongooseConnections.default || (_connections.mongooseConnections.default = mongoose__default.createConnection(process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017'))).model(name, schema, collection);
53
55
  }
54
56
  /**
55
57
  * Creates common Mongoose schema definitions with optional custom schemas and settings.
@@ -295,8 +297,9 @@ function createMongooseStringSchema(...attributes) {
295
297
  const schema = { type: String };
296
298
  new Set(attributes).forEach((attribute) => {
297
299
  if (attribute === 'short')
298
- return (schema.maxlength = 16);
299
- schema[attribute] = true;
300
+ schema.maxlength = 16;
301
+ else
302
+ schema[attribute] = true;
300
303
  });
301
304
  return schema;
302
305
  }
package/utils.mjs CHANGED
@@ -5,6 +5,7 @@ import mongooseAggregatePaginate from 'mongoose-aggregate-paginate-v2';
5
5
  import mongoosePaginate from 'mongoose-paginate-v2';
6
6
  import net from 'node:net';
7
7
  import { mongooseConnections } from './_connections.mjs';
8
+ import { customMongooseOptions } from './options.mjs';
8
9
  import { mongooseNormalizePlugin } from './plugins/normalize.mjs';
9
10
 
10
11
  const { merge } = lodash;
@@ -38,6 +39,7 @@ function buildMongooseModel(collection, name, schema, options) {
38
39
  schema.plugin(mongooseAggregatePaginate);
39
40
  schema.plugin(mongoosePaginate);
40
41
  schema.set('timestamps', options?.timestamps === undefined ? true : options.timestamps);
42
+ customMongooseOptions.beforeModelBuild?.(schema);
41
43
  return (options?.connection || mongooseConnections.default || (mongooseConnections.default = mongoose.createConnection(process.env.MONGODB_URI || 'mongodb://127.0.0.1:27017'))).model(name, schema, collection);
42
44
  }
43
45
  /**
@@ -284,8 +286,9 @@ function createMongooseStringSchema(...attributes) {
284
286
  const schema = { type: String };
285
287
  new Set(attributes).forEach((attribute) => {
286
288
  if (attribute === 'short')
287
- return (schema.maxlength = 16);
288
- schema[attribute] = true;
289
+ schema.maxlength = 16;
290
+ else
291
+ schema[attribute] = true;
289
292
  });
290
293
  return schema;
291
294
  }