@kikiutils/mongoose 0.1.0 → 0.2.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/constants.d.ts CHANGED
@@ -59,14 +59,14 @@ export declare const commonMongooseSchemas: {
59
59
  readonly decimal128: {
60
60
  readonly autoRoundAndToFixed: {
61
61
  readonly nonRequired: {
62
- readonly set: (value: import("decimal.js").default.Value | {
62
+ readonly set: (value: string | number | {
63
63
  toString(): string;
64
64
  }) => string;
65
65
  readonly type: typeof import("mongoose").Schema.Types.Decimal128;
66
66
  };
67
67
  readonly required: {
68
68
  readonly required: true;
69
- readonly set: (value: import("decimal.js").default.Value | {
69
+ readonly set: (value: string | number | {
70
70
  toString(): string;
71
71
  }) => string;
72
72
  readonly type: typeof import("mongoose").Schema.Types.Decimal128;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kikiutils/mongoose",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "license": "MIT",
5
5
  "author": "kiki-kanri",
6
6
  "repository": {
package/types/schema.d.ts CHANGED
@@ -24,8 +24,9 @@
24
24
  /// <reference types="mongoose/types/inferschematype" />
25
25
  /// <reference types="mongoose-paginate-v2" />
26
26
  import type Decimal from 'decimal.js';
27
- import type { SchemaTypeOptions } from 'mongoose';
28
- export type MongooseStringSchemaAttribute = 'lowercase' | 'private' | 'required' | 'short' | 'trim' | 'unique' | 'uppercase';
27
+ import type { SchemaTypeOptions, Types } from 'mongoose';
28
+ export type BaseSchemaAttribute = 'private' | 'required' | 'unique';
29
+ export type MongooseStringSchemaAttribute = BaseSchemaAttribute | 'lowercase' | 'short' | 'trim' | 'uppercase';
29
30
  export interface CreateCommonMongooseSchemasOptions {
30
31
  autoRoundAndToFixedDecimal128?: {
31
32
  /**
@@ -38,6 +39,12 @@ export interface CreateCommonMongooseSchemasOptions {
38
39
  rounding?: Decimal.Rounding;
39
40
  };
40
41
  }
42
+ export interface MongooseObjectIdRefSchema<T extends BaseSchemaAttribute[], R extends string> extends SchemaTypeOptions<Types.ObjectId> {
43
+ private: 'private' extends T[number] ? true : boolean | undefined;
44
+ ref: R;
45
+ required: 'required' extends T[number] ? true : SchemaTypeOptions<Types.ObjectId>['required'];
46
+ unique: 'unique' extends T[number] ? true : SchemaTypeOptions<Types.ObjectId>['unique'];
47
+ }
41
48
  export interface MongooseStringSchema<T extends MongooseStringSchemaAttribute[]> extends SchemaTypeOptions<string> {
42
49
  lowercase: 'lowercase' extends T[number] ? boolean : SchemaTypeOptions<string>['lowercase'];
43
50
  maxlength: 'short' extends T[number] ? number : SchemaTypeOptions<string>['maxlength'];
package/utils.cjs CHANGED
@@ -126,6 +126,11 @@ export const createCommonMongooseSchemas = (customSchemas, options) => {
126
126
  }
127
127
  }, customSchemas);
128
128
  };
129
+ export const createMongooseObjectIdRefSchema = (refModelName, ...attributes) => {
130
+ const schema = { ref: refModelName, type: Schema.Types.ObjectId };
131
+ new Set(attributes).forEach((attribute) => (schema[attribute] = true));
132
+ return schema;
133
+ };
129
134
  export const createMongooseStringSchema = (...attributes) => {
130
135
  const schema = { type: String };
131
136
  new Set(attributes).forEach((attribute) => {
package/utils.d.ts CHANGED
@@ -23,11 +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 Decimal from 'decimal.js';
27
26
  import { Schema } from 'mongoose';
28
27
  import type { Model as MongooseModel, PaginateModel } from 'mongoose';
29
28
  import type { BuildMongooseModelOptions } from './types/options';
30
- import type { CreateCommonMongooseSchemasOptions, MongooseStringSchemaAttribute, MongooseStringSchema } from './types/schema';
29
+ import type { BaseSchemaAttribute, CreateCommonMongooseSchemasOptions, MongooseObjectIdRefSchema, MongooseStringSchema, MongooseStringSchemaAttribute } from './types/schema';
31
30
  export declare function buildMongooseModel<DocType, Model extends MongooseModel<DocType, QueryHelpers, InstanceMethods>, InstanceMethods = {}, QueryHelpers = {}>(collectionName: string, name: string, schema: Schema<DocType, Model, InstanceMethods, QueryHelpers>, options: BuildMongooseModelOptions<DocType, Model, InstanceMethods, QueryHelpers> & {
32
31
  enablePaginatePlugin: false;
33
32
  }): Model;
@@ -67,14 +66,14 @@ export declare const createCommonMongooseSchemas: <T extends {}>(customSchemas?:
67
66
  readonly decimal128: {
68
67
  readonly autoRoundAndToFixed: {
69
68
  readonly nonRequired: {
70
- readonly set: (value: Decimal.Value | {
69
+ readonly set: (value: number | string | {
71
70
  toString(): string;
72
71
  }) => string;
73
72
  readonly type: typeof Schema.Types.Decimal128;
74
73
  };
75
74
  readonly required: {
76
75
  readonly required: true;
77
- readonly set: (value: Decimal.Value | {
76
+ readonly set: (value: number | string | {
78
77
  toString(): string;
79
78
  }) => string;
80
79
  readonly type: typeof Schema.Types.Decimal128;
@@ -193,5 +192,6 @@ export declare const createCommonMongooseSchemas: <T extends {}>(customSchemas?:
193
192
  };
194
193
  };
195
194
  } & T;
195
+ export declare const createMongooseObjectIdRefSchema: <R extends string, T extends BaseSchemaAttribute[]>(refModelName: R, ...attributes: T) => MongooseObjectIdRefSchema<T, R>;
196
196
  export declare const createMongooseStringSchema: <T extends MongooseStringSchemaAttribute[]>(...attributes: T) => MongooseStringSchema<T>;
197
197
  export declare const setupDecimal128FieldsToStringGetter: <DocType, Model extends MongooseModel<DocType, QueryHelpers, InstanceMethodsAndOverrides, {}, import("mongoose").IfAny<DocType, any, {} & InstanceMethodsAndOverrides extends infer T ? T extends {} & InstanceMethodsAndOverrides ? T extends Record<string, never> ? import("mongoose").Document<unknown, QueryHelpers, DocType> & import("mongoose").Require_id<DocType> : import("mongoose").IfAny<T, import("mongoose").Document<unknown, QueryHelpers, DocType> & import("mongoose").Require_id<DocType>, import("mongoose").Document<unknown, QueryHelpers, DocType> & Omit<import("mongoose").Require_id<DocType>, keyof T> & T> : never : never>, any> | PaginateModel<DocType, QueryHelpers, InstanceMethodsAndOverrides>, InstanceMethodsAndOverrides = {}, QueryHelpers = {}>(fields: string[], schema: Schema<DocType, Model, InstanceMethodsAndOverrides, QueryHelpers>) => void;
package/utils.mjs CHANGED
@@ -126,6 +126,11 @@ export const createCommonMongooseSchemas = (customSchemas, options) => {
126
126
  }
127
127
  }, customSchemas);
128
128
  };
129
+ export const createMongooseObjectIdRefSchema = (refModelName, ...attributes) => {
130
+ const schema = { ref: refModelName, type: Schema.Types.ObjectId };
131
+ new Set(attributes).forEach((attribute) => (schema[attribute] = true));
132
+ return schema;
133
+ };
129
134
  export const createMongooseStringSchema = (...attributes) => {
130
135
  const schema = { type: String };
131
136
  new Set(attributes).forEach((attribute) => {