@lyxa.ai/core 1.3.160 → 1.3.162
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/dist/types/README.md +1 -1
- package/dist/types/package.json +1 -1
- package/dist/types/utilities/validation/common-validation.d.ts +12 -12
- package/dist/utilities/pagination.d.ts +18 -11
- package/dist/utilities/pagination.js +1 -1
- package/dist/utilities/pagination.js.map +1 -1
- package/dist/utilities/transformers.d.ts +1 -0
- package/dist/utilities/transformers.js.map +1 -1
- package/package.json +1 -1
package/dist/types/README.md
CHANGED
package/dist/types/package.json
CHANGED
|
@@ -26,8 +26,8 @@ export declare const FilterSchema: z.ZodOptional<z.ZodObject<{
|
|
|
26
26
|
searchFields?: string[] | undefined;
|
|
27
27
|
} | undefined;
|
|
28
28
|
sort?: Record<string, 1 | -1> | undefined;
|
|
29
|
-
select?: Record<string, 0 | 1> | undefined;
|
|
30
29
|
populate?: any;
|
|
30
|
+
select?: Record<string, 0 | 1> | undefined;
|
|
31
31
|
query?: Record<string, any> | undefined;
|
|
32
32
|
}, {
|
|
33
33
|
search?: {
|
|
@@ -36,8 +36,8 @@ export declare const FilterSchema: z.ZodOptional<z.ZodObject<{
|
|
|
36
36
|
} | undefined;
|
|
37
37
|
sort?: Record<string, 1 | -1> | undefined;
|
|
38
38
|
size?: number | undefined;
|
|
39
|
-
select?: Record<string, 0 | 1> | undefined;
|
|
40
39
|
populate?: any;
|
|
40
|
+
select?: Record<string, 0 | 1> | undefined;
|
|
41
41
|
query?: Record<string, any> | undefined;
|
|
42
42
|
page?: number | undefined;
|
|
43
43
|
}>>;
|
|
@@ -733,12 +733,12 @@ export declare const GetByIdInputSchema: z.ZodObject<{
|
|
|
733
733
|
populate: z.ZodOptional<z.ZodUnion<[z.ZodType<any, z.ZodTypeDef, any>, z.ZodArray<z.ZodType<any, z.ZodTypeDef, any>, "many">]>>;
|
|
734
734
|
}, "strip", z.ZodTypeAny, {
|
|
735
735
|
_id: import("mongoose").Types.ObjectId;
|
|
736
|
-
select?: Record<string, 0 | 1> | undefined;
|
|
737
736
|
populate?: any;
|
|
737
|
+
select?: Record<string, 0 | 1> | undefined;
|
|
738
738
|
}, {
|
|
739
739
|
_id: string | import("mongoose").Types.ObjectId;
|
|
740
|
-
select?: Record<string, 0 | 1> | undefined;
|
|
741
740
|
populate?: any;
|
|
741
|
+
select?: Record<string, 0 | 1> | undefined;
|
|
742
742
|
}>;
|
|
743
743
|
export type GetByIdInputDTO = DTO<typeof GetByIdInputSchema>;
|
|
744
744
|
export declare const GetProductByIdInputSchema: z.ZodObject<{
|
|
@@ -751,12 +751,12 @@ export declare const GetProductByIdInputSchema: z.ZodObject<{
|
|
|
751
751
|
_id: import("mongoose").Types.ObjectId;
|
|
752
752
|
withAttributeHiddenItems: boolean;
|
|
753
753
|
withAddonsHiddenItems: boolean;
|
|
754
|
-
select?: Record<string, 0 | 1> | undefined;
|
|
755
754
|
populate?: any;
|
|
755
|
+
select?: Record<string, 0 | 1> | undefined;
|
|
756
756
|
}, {
|
|
757
757
|
_id: string | import("mongoose").Types.ObjectId;
|
|
758
|
-
select?: Record<string, 0 | 1> | undefined;
|
|
759
758
|
populate?: any;
|
|
759
|
+
select?: Record<string, 0 | 1> | undefined;
|
|
760
760
|
withAttributeHiddenItems?: boolean | undefined;
|
|
761
761
|
withAddonsHiddenItems?: boolean | undefined;
|
|
762
762
|
}>;
|
|
@@ -768,13 +768,13 @@ export declare const GetOrderByIdInputSchema: z.ZodObject<{
|
|
|
768
768
|
userOrderCompletionScope: z.ZodOptional<z.ZodNativeEnum<typeof UserOrderCompletionScope>>;
|
|
769
769
|
}, "strip", z.ZodTypeAny, {
|
|
770
770
|
_id: import("mongoose").Types.ObjectId;
|
|
771
|
-
select?: Record<string, 0 | 1> | undefined;
|
|
772
771
|
populate?: any;
|
|
772
|
+
select?: Record<string, 0 | 1> | undefined;
|
|
773
773
|
userOrderCompletionScope?: UserOrderCompletionScope | undefined;
|
|
774
774
|
}, {
|
|
775
775
|
_id: string | import("mongoose").Types.ObjectId;
|
|
776
|
-
select?: Record<string, 0 | 1> | undefined;
|
|
777
776
|
populate?: any;
|
|
777
|
+
select?: Record<string, 0 | 1> | undefined;
|
|
778
778
|
userOrderCompletionScope?: UserOrderCompletionScope | undefined;
|
|
779
779
|
}>;
|
|
780
780
|
export type GetOrderByIdInputDTO = DTO<typeof GetOrderByIdInputSchema>;
|
|
@@ -782,11 +782,11 @@ export declare const GetByTokenInputSchema: z.ZodOptional<z.ZodObject<{
|
|
|
782
782
|
select: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodLiteral<0>, z.ZodLiteral<1>]>>>;
|
|
783
783
|
populate: z.ZodOptional<z.ZodUnion<[z.ZodType<any, z.ZodTypeDef, any>, z.ZodArray<z.ZodType<any, z.ZodTypeDef, any>, "many">]>>;
|
|
784
784
|
}, "strip", z.ZodTypeAny, {
|
|
785
|
-
select?: Record<string, 0 | 1> | undefined;
|
|
786
785
|
populate?: any;
|
|
787
|
-
}, {
|
|
788
786
|
select?: Record<string, 0 | 1> | undefined;
|
|
787
|
+
}, {
|
|
789
788
|
populate?: any;
|
|
789
|
+
select?: Record<string, 0 | 1> | undefined;
|
|
790
790
|
}>>;
|
|
791
791
|
export type GetByTokenInputDTO = DTO<typeof GetByTokenInputSchema>;
|
|
792
792
|
export declare const GetOneQuerySchema: z.ZodObject<{
|
|
@@ -794,12 +794,12 @@ export declare const GetOneQuerySchema: z.ZodObject<{
|
|
|
794
794
|
select: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnion<[z.ZodLiteral<0>, z.ZodLiteral<1>]>>>;
|
|
795
795
|
populate: z.ZodOptional<z.ZodUnion<[z.ZodType<any, z.ZodTypeDef, any>, z.ZodArray<z.ZodType<any, z.ZodTypeDef, any>, "many">]>>;
|
|
796
796
|
}, "strip", z.ZodTypeAny, {
|
|
797
|
-
select?: Record<string, 0 | 1> | undefined;
|
|
798
797
|
populate?: any;
|
|
798
|
+
select?: Record<string, 0 | 1> | undefined;
|
|
799
799
|
query?: Record<string, any> | undefined;
|
|
800
800
|
}, {
|
|
801
|
-
select?: Record<string, 0 | 1> | undefined;
|
|
802
801
|
populate?: any;
|
|
802
|
+
select?: Record<string, 0 | 1> | undefined;
|
|
803
803
|
query?: Record<string, any> | undefined;
|
|
804
804
|
}>;
|
|
805
805
|
export type GetOneQueryDTO = DTO<typeof GetOneQuerySchema>;
|
|
@@ -8,17 +8,17 @@ export declare function createPaginatedSchema<ItemType extends z.ZodTypeAny>(ite
|
|
|
8
8
|
size: z.ZodNumber;
|
|
9
9
|
totalElements: z.ZodNumber;
|
|
10
10
|
totalPages: z.ZodNumber;
|
|
11
|
-
}, "strip", z.
|
|
12
|
-
page:
|
|
13
|
-
size:
|
|
14
|
-
totalElements:
|
|
15
|
-
totalPages:
|
|
16
|
-
}, {
|
|
17
|
-
page:
|
|
18
|
-
size:
|
|
19
|
-
totalElements:
|
|
20
|
-
totalPages:
|
|
21
|
-
}
|
|
11
|
+
}, "strip", z.ZodUnion<[z.ZodUnion<[z.ZodUnion<[z.ZodUnion<[z.ZodString, z.ZodNumber]>, z.ZodBoolean]>, z.ZodNull]>, z.ZodUndefined]>, z.objectOutputType<{
|
|
12
|
+
page: z.ZodNumber;
|
|
13
|
+
size: z.ZodNumber;
|
|
14
|
+
totalElements: z.ZodNumber;
|
|
15
|
+
totalPages: z.ZodNumber;
|
|
16
|
+
}, z.ZodUnion<[z.ZodUnion<[z.ZodUnion<[z.ZodUnion<[z.ZodString, z.ZodNumber]>, z.ZodBoolean]>, z.ZodNull]>, z.ZodUndefined]>, "strip">, z.objectInputType<{
|
|
17
|
+
page: z.ZodNumber;
|
|
18
|
+
size: z.ZodNumber;
|
|
19
|
+
totalElements: z.ZodNumber;
|
|
20
|
+
totalPages: z.ZodNumber;
|
|
21
|
+
}, z.ZodUnion<[z.ZodUnion<[z.ZodUnion<[z.ZodUnion<[z.ZodString, z.ZodNumber]>, z.ZodBoolean]>, z.ZodNull]>, z.ZodUndefined]>, "strip">>;
|
|
22
22
|
items: z.ZodArray<ItemType, "many">;
|
|
23
23
|
}, "strip", z.ZodTypeAny, {
|
|
24
24
|
metadata: {
|
|
@@ -26,6 +26,8 @@ export declare function createPaginatedSchema<ItemType extends z.ZodTypeAny>(ite
|
|
|
26
26
|
size: number;
|
|
27
27
|
totalElements: number;
|
|
28
28
|
totalPages: number;
|
|
29
|
+
} & {
|
|
30
|
+
[k: string]: string | number | boolean | null | undefined;
|
|
29
31
|
};
|
|
30
32
|
items: ItemType["_output"][];
|
|
31
33
|
}, {
|
|
@@ -34,6 +36,8 @@ export declare function createPaginatedSchema<ItemType extends z.ZodTypeAny>(ite
|
|
|
34
36
|
size: number;
|
|
35
37
|
totalElements: number;
|
|
36
38
|
totalPages: number;
|
|
39
|
+
} & {
|
|
40
|
+
[k: string]: string | number | boolean | null | undefined;
|
|
37
41
|
};
|
|
38
42
|
items: ItemType["_input"][];
|
|
39
43
|
}>;
|
|
@@ -43,6 +47,7 @@ export interface RawPaginatedResult<T extends AnyParamConstructor<any>> {
|
|
|
43
47
|
size: number;
|
|
44
48
|
totalElements: number;
|
|
45
49
|
totalPages: number;
|
|
50
|
+
[key: string]: string | number | boolean | null | undefined;
|
|
46
51
|
};
|
|
47
52
|
documents: DocumentType<T>[];
|
|
48
53
|
}
|
|
@@ -61,6 +66,8 @@ export declare function createPaginatorObject<T extends AnyParamConstructor<any>
|
|
|
61
66
|
size: number;
|
|
62
67
|
totalElements: number;
|
|
63
68
|
totalPages: number;
|
|
69
|
+
} & {
|
|
70
|
+
[k: string]: string | number | boolean | null | undefined;
|
|
64
71
|
};
|
|
65
72
|
items: any[];
|
|
66
73
|
}>;
|
|
@@ -12,7 +12,7 @@ function createPaginatedSchema(itemSchema) {
|
|
|
12
12
|
size: zod_1.z.number(),
|
|
13
13
|
totalElements: zod_1.z.number(),
|
|
14
14
|
totalPages: zod_1.z.number(),
|
|
15
|
-
}),
|
|
15
|
+
}).catchall(zod_1.z.string().or(zod_1.z.number()).or(zod_1.z.boolean()).or(zod_1.z.null()).or(zod_1.z.undefined())),
|
|
16
16
|
items: zod_1.z.array(itemSchema),
|
|
17
17
|
});
|
|
18
18
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pagination.js","sourceRoot":"/","sources":["utilities/pagination.ts"],"names":[],"mappings":";;AASA,sDAUC;
|
|
1
|
+
{"version":3,"file":"pagination.js","sourceRoot":"/","sources":["utilities/pagination.ts"],"names":[],"mappings":";;AASA,sDAUC;AAuED,4EAWC;AASD,sDAmBC;AACD,kDA6CC;AA7KD,6BAAwB;AAOxB,SAAgB,qBAAqB,CAAgC,UAAoB;IACxF,OAAO,OAAC,CAAC,MAAM,CAAC;QACf,QAAQ,EAAE,OAAC,CAAC,MAAM,CAAC;YAClB,IAAI,EAAE,OAAC,CAAC,MAAM,EAAE;YAChB,IAAI,EAAE,OAAC,CAAC,MAAM,EAAE;YAChB,aAAa,EAAE,OAAC,CAAC,MAAM,EAAE;YACzB,UAAU,EAAE,OAAC,CAAC,MAAM,EAAE;SACtB,CAAC,CAAC,QAAQ,CAAC,OAAC,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC,OAAC,CAAC,MAAM,EAAE,CAAC,CAAC,EAAE,CAAC,OAAC,CAAC,OAAO,EAAE,CAAC,CAAC,EAAE,CAAC,OAAC,CAAC,IAAI,EAAE,CAAC,CAAC,EAAE,CAAC,OAAC,CAAC,SAAS,EAAE,CAAC,CAAC;QACrF,KAAK,EAAE,OAAC,CAAC,KAAK,CAAC,UAAU,CAAC;KAC1B,CAAC,CAAC;AACJ,CAAC;AAiCD,KAAK,UAAU,aAAa,CAC3B,KAAyB,EACzB,OAA4B;IAE5B,MAAM,EAAE,KAAK,GAAG,EAAE,EAAE,IAAI,GAAG,CAAC,EAAE,IAAI,GAAG,EAAE,EAAE,IAAI,GAAG,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,OAAO,CAA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AiB,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC;IAG1C,MAAM,IAAI,GAAG,MAAM,KAAK,CAAC,SAAS,CAAC,iBAAiB,CAAC,CAAC,IAAI,EAAE,CAAC;IAG7D,MAAM,aAAa,GAAG,eAAe,CAAC,CAAC,CAAC,MAAM,KAAK,CAAC,QAAQ,CAAC,IAAI,EAAE,eAAe,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;IAG3F,OAAO;QACN,QAAQ,EAAE;YACT,IAAI;YACJ,IAAI;YACJ,aAAa;YACb,UAAU;SACV;QACD,SAAS,EAAE,aAAa;KACxB,CAAC;AACH,CAAC","sourcesContent":["import { DocumentType, ReturnModelType } from '@typegoose/typegoose';\nimport { FilterQuery, PopulateOptions } from 'mongoose';\nimport { z } from 'zod';\nimport { AnyParamConstructor } from '@typegoose/typegoose/lib/types';\nimport { FilterDTO, PaginatedResponse } from './validation';\n\n/**\n * A helper to create a paginated Zod schema for an item type.\n */\nexport function createPaginatedSchema<ItemType extends z.ZodTypeAny>(itemSchema: ItemType) {\n\treturn z.object({\n\t\tmetadata: z.object({\n\t\t\tpage: z.number(),\n\t\t\tsize: z.number(),\n\t\t\ttotalElements: z.number(),\n\t\t\ttotalPages: z.number(),\n\t\t}).catchall(z.string().or(z.number()).or(z.boolean()).or(z.null()).or(z.undefined())),\n\t\titems: z.array(itemSchema),\n\t});\n}\n\nexport interface RawPaginatedResult<T extends AnyParamConstructor<any>> {\n\tmetadata: {\n\t\tpage: number;\n\t\tsize: number;\n\t\ttotalElements: number;\n\t\ttotalPages: number;\n\t\t// Use a more specific index signature\n \t[key: string]: string | number | boolean | null | undefined;\n\t};\n\tdocuments: DocumentType<T>[];\n}\n\n/**\n * The common pagination options.\n */\nexport interface PaginatorOptions<TClass extends AnyParamConstructor<any>> {\n\tquery?: FilterQuery<DocumentType<TClass>>;\n\tpage?: number;\n\tsize?: number;\n\tsort?: Record<string, 1 | -1>;\n\tselect?: Record<string, 0 | 1>;\n\tpopulate?: PopulateOptions | (PopulateOptions | string)[];\n}\n\n/**\n * This internal function wraps the common query logic:\n * - filter, page, size, sort, populate\n * - fetch documents, count documents\n * - compute totalPages\n * Returns a standard object with `metadata` & `documents`.\n */\nasync function _basePaginate<T extends AnyParamConstructor<any>>(\n\tmodel: ReturnModelType<T>,\n\toptions: PaginatorOptions<T>\n): Promise<RawPaginatedResult<T>> {\n\tconst { query = {}, page = 1, size = 10, sort = {}, select, populate } = options;\n\n\tconst skip = (page - 1) * size;\n\n\tlet result = model.find(query).sort(sort).skip(skip).limit(size);\n\n\tif (select) {\n\t\tresult = result.select(select) as any;\n\t}\n\tif (populate) {\n\t\tresult = result.populate(populate);\n\t}\n\n\tconst [documents, totalElements] = await Promise.all([result.exec(), model.countDocuments(query)]);\n\n\tconst totalPages = Math.ceil(totalElements / size);\n\n\treturn {\n\t\tmetadata: {\n\t\t\tpage,\n\t\t\tsize,\n\t\t\ttotalElements,\n\t\t\ttotalPages,\n\t\t},\n\t\tdocuments: documents as unknown as DocumentType<T>[],\n\t};\n}\n\n/**\n * Creates a paginator returning raw documents.\n *\n * @param model The Typegoose model\n * @returns A function that, when passed `PaginatorOptions`, returns raw docs + metadata.\n */\nexport function createRawDocumentPaginatorObject<T extends AnyParamConstructor<any>>(\n\tmodel: ReturnModelType<T>\n) {\n\treturn async function paginate(options: PaginatorOptions<T>): Promise<RawPaginatedResult<T>> {\n\t\tconst { metadata, documents } = await _basePaginate(model, options);\n\n\t\treturn {\n\t\t\tmetadata,\n\t\t\tdocuments,\n\t\t};\n\t};\n}\n\n/**\n * Creates a paginator that returns Zod-validated items.\n *\n * @param model The Typegoose model\n * @param itemSchema A Zod schema describing the shape of each item\n * @param transformFn Optional transform function to convert raw doc -> item\n */\nexport function createPaginatorObject<T extends AnyParamConstructor<any>>(\n\tmodel: ReturnModelType<T>,\n\titemSchema: z.ZodTypeAny,\n\ttransformFn?: (doc: DocumentType<T>) => z.infer<typeof itemSchema>\n) {\n\tconst PaginatedSchema = createPaginatedSchema(itemSchema);\n\n\treturn async function paginate(options: PaginatorOptions<T>) {\n\t\tconst { metadata, documents } = await _basePaginate(model, options);\n\n\t\tconst items = transformFn ? documents.map(transformFn) : documents;\n\n\t\tconst paginatedResult = {\n\t\t\tmetadata,\n\t\t\titems,\n\t\t};\n\n\t\treturn PaginatedSchema.parse(paginatedResult);\n\t};\n}\nexport async function paginateAggregation(\n\tmodel: any,\n\tpipeline: any[],\n\tpage: number = 1,\n\tsize: number = 10,\n\tsort: any = { createdAt: -1 },\n\tpopulateOptions?: any\n) {\n\t// Calculate skip and limit based on the page and size\n\tconst skip = (page - 1) * size;\n\tconst limit = size;\n\n\t// Copy the pipeline to avoid mutation\n\tconst pipelineWithCount = [...pipeline];\n\n\t// Step 1: Calculate total count\n\tpipelineWithCount.push({ $count: 'totalCount' });\n\n\t// Step 2: Execute to get total count\n\tconst totalCountResult = await model.aggregate(pipelineWithCount).exec();\n\tconst totalElements = totalCountResult?.[0]?.totalCount || 0;\n\tconst totalPages = Math.ceil(totalElements / size);\n\n\t// Step 3: Apply pagination to the original pipeline\n\tconst paginatedPipeline = [...pipeline];\n\tpaginatedPipeline.push({ $sort: sort });\n\tpaginatedPipeline.push({ $skip: skip });\n\tpaginatedPipeline.push({ $limit: limit });\n\n\t// Step 4: Execute the paginated pipeline\n\tconst data = await model.aggregate(paginatedPipeline).exec();\n\n\t// Step 5: Optional populate\n\tconst populatedData = populateOptions ? await model.populate(data, populateOptions) : data;\n\n\t// Return the paginated data with metadata\n\treturn {\n\t\tmetadata: {\n\t\t\tpage,\n\t\t\tsize,\n\t\t\ttotalElements,\n\t\t\ttotalPages,\n\t\t},\n\t\tdocuments: populatedData,\n\t};\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transformers.js","sourceRoot":"/","sources":["utilities/transformers.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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AAA;AA9MY,8CAAiB;4BAAjB,iBAAiB;IAD7B,IAAA,gBAAO,GAAE;qCAOmB,OAAC,CAAC,OAAO;GANzB,iBAAiB,CA8M7B;AAWD,SAAgB,iBAAiB,CAChC,MAAsB;IAEtB,IAAI,CAAC;QACJ,OAAO,gBAAS,CAAC,GAAG,CAAC,iBAAiB,CAA0C,CAAC;IAClF,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QAChB,OAAO,IAAI,iBAAiB,CAAqB,MAAM,CAAC,CAAC;IAC1D,CAAC;AACF,CAAC","sourcesContent":["import { DocumentType } from '@typegoose/typegoose';\nimport Container, { Service } from 'typedi';\nimport { z } from 'zod';\n\n/**\n * Type definition for pagination metadata\n */\ntype Metadata = {\n\tpage: number;\n\tsize: number;\n\ttotalElements: number;\n\ttotalPages: number;\n};\n\n/**\n * Type definition for paginated response\n */\ntype PaginatedResponse<T> = {\n\tmetadata?: Metadata;\n\tdocuments: T[];\n};\n\n/**\n * GlobalTransformer provides a reusable way to transform Mongoose documents\n * into DTOs using Zod schemas for validation and type safety.\n *\n * @template ModelType - The Mongoose model type\n * @template DTOType - The output DTO type\n */\n@Service()\nexport class GlobalTransformer<ModelType, DTOType> {\n\t/**\n\t * Creates a new GlobalTransformer\n\t *\n\t * @param {z.ZodType<any>} schema - Zod schema used for validation and transformation\n\t */\n\tconstructor(private schema: z.ZodType<any>) {}\n\n\t/**\n\t * Transforms a single document to a DTO\n\t *\n\t * @param {DocumentType<ModelType>} record - The document to transform\n\t * @returns {DTOType} The transformed DTO\n\t * @throws {Error} When record is not found\n\t */\n\tpublic transformToBasicView(record: DocumentType<ModelType> | DTOType): DTOType {\n\t\tif (!record) {\n\t\t\tthrow new Error('Record not found');\n\t\t}\n\n\t\treturn this.schema.parse(record) as DTOType;\n\t}\n\n\t/**\n\t * Transforms an array of documents or a paginated response to a paginated DTO response\n\t *\n\t * @param {DocumentType<ModelType>[] | DTOType[] | PaginatedResponse<DocumentType<ModelType>> | PaginatedResponse<DTOType>} records - Records to transform\n\t * @returns {PaginatedResponse<DTOType>} Paginated response with transformed DTOs\n\t * @throws {Error} When input type is invalid\n\t */\n\tpublic transformToBasicViewArray<T extends DocumentType<ModelType> | DTOType>(\n\t\trecords: T[] | PaginatedResponse<T>\n\t): PaginatedResponse<DTOType> {\n\t\t// Case 1: If records is already in the paginated format\n\t\tif (records && typeof records === 'object' && 'metadata' in records && 'documents' in records) {\n\t\t\tconst paginatedRecords = records as\n\t\t\t\t| PaginatedResponse<DocumentType<ModelType>>\n\t\t\t\t| PaginatedResponse<DTOType>;\n\n\t\t\t// Process the documents array\n\t\t\tconst processedDocuments = Array.isArray(paginatedRecords.documents)\n\t\t\t\t? paginatedRecords.documents.map(doc => this.transformToBasicView(doc))\n\t\t\t\t: [];\n\n\t\t\t// Return the entire paginated structure\n\t\t\treturn {\n\t\t\t\tmetadata: paginatedRecords.metadata,\n\t\t\t\tdocuments: processedDocuments,\n\t\t\t};\n\t\t}\n\n\t\t// Case 2: If records is an array\n\t\tif (Array.isArray(records)) {\n\t\t\tif (records.length === 0) {\n\t\t\t\treturn {\n\t\t\t\t\tdocuments: [],\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tconst transformedRecords = records.map(record => this.transformToBasicView(record));\n\n\t\t\t// Return in paginated format with default metadata\n\t\t\treturn {\n\t\t\t\tdocuments: transformedRecords,\n\t\t\t};\n\t\t}\n\n\t\t// Fallback for unexpected input\n\t\tthrow new Error('Invalid input type for transformToBasicViewArray');\n\t}\n\n\t/**r\n\t * Creates a partial view with only the specified fields\n\t *\n\t * @param {DocumentType<ModelType>} record - The document to transform\n\t * @param {(keyof DTOType)[]} fields - Fields to include in the output\n\t * @returns {Partial<DTOType>} Partial DTO with only the specified fields\n\t */\n\tpublic transformToPartialView(\n\t\trecord: DocumentType<ModelType>,\n\t\tfields: (keyof DTOType)[]\n\t): Partial<DTOType> {\n\t\tconst fullView = this.transformToBasicView(record);\n\t\tconst partialView: Partial<DTOType> = {};\n\n\t\tfields.forEach(field => {\n\t\t\tconst dto = fullView as Record<keyof DTOType, any>;\n\t\t\tpartialView[field] = dto[field];\n\t\t});\n\n\t\treturn partialView;\n\t}\n\n\t/**\n\t * Creates partial views for an array of documents\n\t *\n\t * @param {DocumentType<ModelType>[]} records - Array of documents to transform\n\t * @param {(keyof DTOType)[]} fields - Fields to include in the output\n\t * @returns {Partial<DTOType>[]} Array of partial DTOs\n\t */\n\tpublic transformToPartialViewArray(\n\t\trecords: DocumentType<ModelType>[] | PaginatedResponse<DocumentType<ModelType>>,\n\t\tfields: (keyof DTOType)[]\n\t): Partial<DTOType>[] | PaginatedResponse<Partial<DTOType>> {\n\t\t// Case 1: If records is already in the paginated format\n\t\tif (records && typeof records === 'object' && 'metadata' in records && 'documents' in records) {\n\t\t\tconst paginatedRecords = records as PaginatedResponse<DocumentType<ModelType>>;\n\n\t\t\t// Process the documents array\n\t\t\tconst processedDocuments = Array.isArray(paginatedRecords.documents)\n\t\t\t\t? paginatedRecords.documents.map(doc => this.transformToPartialView(doc, fields))\n\t\t\t\t: [];\n\n\t\t\t// Return the entire paginated structure\n\t\t\treturn {\n\t\t\t\tmetadata: paginatedRecords.metadata,\n\t\t\t\tdocuments: processedDocuments,\n\t\t\t};\n\t\t}\n\n\t\t// Case 2: If records is an array\n\t\tif (Array.isArray(records)) {\n\t\t\tif (records.length === 0) {\n\t\t\t\treturn [];\n\t\t\t}\n\t\t\treturn records.map(record => this.transformToPartialView(record, fields));\n\t\t}\n\n\t\t// Fallback for unexpected input\n\t\tthrow new Error('Invalid input type for transformToPartialViewArray');\n\t}\n\t/**\n\t * Transforms a document and adds additional computed fields\n\t *\n\t * @param {DocumentType<ModelType>} record - The document to transform\n\t * @param {Partial<Record<string, any>>} additionalData - Additional fields to add\n\t * @returns {DTOType & Record<string, any>} DTO with additional fields\n\t */\n\tpublic transformWithAdditionalData(\n\t\trecord: DocumentType<ModelType>,\n\t\tadditionalData: Partial<Record<string, any>>\n\t): DTOType & Record<string, any> {\n\t\tconst basicView = this.transformToBasicView(record);\n\t\treturn { ...basicView, ...additionalData };\n\t}\n\n\t/**\n\t * Transforms a document using a custom mapping function\n\t *\n\t * @template OutputType - The output type after mapping\n\t * @param {DocumentType<ModelType>} record - The document to transform\n\t * @param {(input: DTOType) => OutputType} mapFn - Mapping function for custom transformation\n\t * @returns {OutputType} Custom transformed output\n\t */\n\tpublic transformWithMapping<OutputType = DTOType>(\n\t\trecord: DocumentType<ModelType>,\n\t\tmapFn: (input: DTOType) => OutputType\n\t): OutputType {\n\t\tconst basicView = this.transformToBasicView(record);\n\t\treturn mapFn(basicView);\n\t}\n\n\t/**\n\t * Transforms a nested property of the document\n\t *\n\t * @template NestedType - The type of the nested property\n\t * @param {DocumentType<ModelType>} record - The document to transform\n\t * @param {string} path - Dot-notation path to the nested property\n\t * @param {z.ZodType<any>} nestedSchema - Zod schema for the nested property\n\t * @returns {NestedType} The transformed nested property\n\t * @throws {Error} When path is not found in record\n\t */\n\tpublic transformNestedProperty<NestedType>(\n\t\trecord: DocumentType<ModelType>,\n\t\tpath: string,\n\t\tnestedSchema: z.ZodType<any>\n\t): NestedType {\n\t\tif (!record) {\n\t\t\tthrow new Error('Record not found');\n\t\t}\n\n\t\tconst parts = path.split('.');\n\t\tlet current: any = record;\n\n\t\tfor (const part of parts) {\n\t\t\tif (current && part in current) {\n\t\t\t\tcurrent = current[part];\n\t\t\t} else {\n\t\t\t\tthrow new Error(`Path ${path} not found in record`);\n\t\t\t}\n\t\t}\n\n\t\treturn nestedSchema.parse(current) as NestedType;\n\t}\n\n\t/**\n\t * Transforms a document with default values for missing fields\n\t *\n\t * @param {DocumentType<ModelType>} record - The document to transform\n\t * @param {Partial<DTOType>} defaults - Default values for fields\n\t * @returns {DTOType} DTO with defaults applied\n\t */\n\tpublic transformWithDefaults(record: DocumentType<ModelType>, defaults: Partial<DTOType>): DTOType {\n\t\tconst transformed = this.transformToBasicView(record);\n\t\treturn { ...defaults, ...transformed };\n\t}\n}\n\n/**\n * Creates a transformer instance for the given schema.\n * Uses dependency injection if available, falls back to creating a new instance.\n *\n * @template ModelType - The Mongoose model type\n * @template DTOType - The output DTO type\n * @param {z.ZodType<any>} schema - Zod schema for validation and transformation\n * @returns {GlobalTransformer<ModelType, DTOType>} Instance of GlobalTransformer\n */\nexport function createTransformer<ModelType, DTOType>(\n\tschema: z.ZodType<any>\n): GlobalTransformer<ModelType, DTOType> {\n\ttry {\n\t\treturn Container.get(GlobalTransformer) as GlobalTransformer<ModelType, DTOType>;\n\t} catch (error) {\n\t\treturn new GlobalTransformer<ModelType, DTOType>(schema);\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"file":"transformers.js","sourceRoot":"/","sources":["utilities/transformers.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwPA,8CAQC;AA/PD,iDAA4C;AAC5C,6BAAwB;AA6BjB,IAAM,iBAAiB,GAAvB,MAAM,iBAAiB;IAMT;IAApB,YAAoB,MAAsB;QAAtB,WAAM,GAAN,MAAM,CAAgB;IAAG,CAAC;IASvC,oBAAoB,CAAC,MAAyC;QACpE,IAAI,CAAC,MAAM,EAAE,CAAC;YACb,MAAM,IAAI,KAAK,CAAC,kBAAkB,CAAC,CAAC;QACrC,CAAC;QAED,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,CAAY,CAAC;IAC7C,CAAC;IASM,yBAAyB,CAC/B,OAAmC;QAGnC,IAAI,OAAO,IAAI,OAAO,OAAO,KAAK,QAAQ,IAAI,UAAU,IAAI,OAAO,IAAI,WAAW,IAAI,OAAO,EAAE,CAAC;YAC/F,MAAM,gBAAgB,GAAG,OAEI,CAAC;YAG9B,MAAM,kBAAkB,GAAG,KAAK,CAAC,OAAO,CAAC,gBAAgB,CAAC,SAAS,CAAC;gBACnE,CAAC,CAAC,gBAAgB,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,oBAA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,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;YAC1B,IAAI,OAAO,IAAI,IAAI,IAAI,OAAO,EAAE,CAAC;gBAChC,OAAO,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;YACzB,CAAC;iBAAM,CAAC;gBACP,MAAM,IAAI,KAAK,CAAC,QAAQ,IAAI,sBAAsB,CAAC,CAAC;YACrD,CAAC;QACF,CAAC;QAED,OAAO,YAAY,CAAC,KAAK,CAAC,OAAO,CAAe,CAAC;IAClD,CAAC;IASM,qBAAqB,CAAC,MAA+B,EAAE,QAA0B;QACvF,MAAM,WAAW,GAAG,IAAI,CAAC,oBAAoB,CAAC,MAAM,CAAC,CAAC;QACtD,OAAO,EAAE,GAAG,QAAQ,EAAE,GAAG,WAAW,EAAE,CAAC;IACxC,CAAC;CACD,CAAA;AA9MY,8CAAiB;4BAAjB,iBAAiB;IAD7B,IAAA,gBAAO,GAAE;qCAOmB,OAAC,CAAC,OAAO;GANzB,iBAAiB,CA8M7B;AAWD,SAAgB,iBAAiB,CAChC,MAAsB;IAEtB,IAAI,CAAC;QACJ,OAAO,gBAAS,CAAC,GAAG,CAAC,iBAAiB,CAA0C,CAAC;IAClF,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QAChB,OAAO,IAAI,iBAAiB,CAAqB,MAAM,CAAC,CAAC;IAC1D,CAAC;AACF,CAAC","sourcesContent":["import { DocumentType } from '@typegoose/typegoose';\nimport Container, { Service } from 'typedi';\nimport { z } from 'zod';\n\n/**\n * Type definition for pagination metadata\n */\ntype Metadata = {\n\tpage: number;\n\tsize: number;\n\ttotalElements: number;\n\ttotalPages: number;\n\t[key: string]: string | number | boolean | null | undefined;\n};\n\n/**\n * Type definition for paginated response\n */\ntype PaginatedResponse<T> = {\n\tmetadata?: Metadata;\n\tdocuments: T[];\n};\n\n/**\n * GlobalTransformer provides a reusable way to transform Mongoose documents\n * into DTOs using Zod schemas for validation and type safety.\n *\n * @template ModelType - The Mongoose model type\n * @template DTOType - The output DTO type\n */\n@Service()\nexport class GlobalTransformer<ModelType, DTOType> {\n\t/**\n\t * Creates a new GlobalTransformer\n\t *\n\t * @param {z.ZodType<any>} schema - Zod schema used for validation and transformation\n\t */\n\tconstructor(private schema: z.ZodType<any>) {}\n\n\t/**\n\t * Transforms a single document to a DTO\n\t *\n\t * @param {DocumentType<ModelType>} record - The document to transform\n\t * @returns {DTOType} The transformed DTO\n\t * @throws {Error} When record is not found\n\t */\n\tpublic transformToBasicView(record: DocumentType<ModelType> | DTOType): DTOType {\n\t\tif (!record) {\n\t\t\tthrow new Error('Record not found');\n\t\t}\n\n\t\treturn this.schema.parse(record) as DTOType;\n\t}\n\n\t/**\n\t * Transforms an array of documents or a paginated response to a paginated DTO response\n\t *\n\t * @param {DocumentType<ModelType>[] | DTOType[] | PaginatedResponse<DocumentType<ModelType>> | PaginatedResponse<DTOType>} records - Records to transform\n\t * @returns {PaginatedResponse<DTOType>} Paginated response with transformed DTOs\n\t * @throws {Error} When input type is invalid\n\t */\n\tpublic transformToBasicViewArray<T extends DocumentType<ModelType> | DTOType>(\n\t\trecords: T[] | PaginatedResponse<T>\n\t): PaginatedResponse<DTOType> {\n\t\t// Case 1: If records is already in the paginated format\n\t\tif (records && typeof records === 'object' && 'metadata' in records && 'documents' in records) {\n\t\t\tconst paginatedRecords = records as\n\t\t\t\t| PaginatedResponse<DocumentType<ModelType>>\n\t\t\t\t| PaginatedResponse<DTOType>;\n\n\t\t\t// Process the documents array\n\t\t\tconst processedDocuments = Array.isArray(paginatedRecords.documents)\n\t\t\t\t? paginatedRecords.documents.map(doc => this.transformToBasicView(doc))\n\t\t\t\t: [];\n\n\t\t\t// Return the entire paginated structure\n\t\t\treturn {\n\t\t\t\tmetadata: paginatedRecords.metadata,\n\t\t\t\tdocuments: processedDocuments,\n\t\t\t};\n\t\t}\n\n\t\t// Case 2: If records is an array\n\t\tif (Array.isArray(records)) {\n\t\t\tif (records.length === 0) {\n\t\t\t\treturn {\n\t\t\t\t\tdocuments: [],\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tconst transformedRecords = records.map(record => this.transformToBasicView(record));\n\n\t\t\t// Return in paginated format with default metadata\n\t\t\treturn {\n\t\t\t\tdocuments: transformedRecords,\n\t\t\t};\n\t\t}\n\n\t\t// Fallback for unexpected input\n\t\tthrow new Error('Invalid input type for transformToBasicViewArray');\n\t}\n\n\t/**r\n\t * Creates a partial view with only the specified fields\n\t *\n\t * @param {DocumentType<ModelType>} record - The document to transform\n\t * @param {(keyof DTOType)[]} fields - Fields to include in the output\n\t * @returns {Partial<DTOType>} Partial DTO with only the specified fields\n\t */\n\tpublic transformToPartialView(\n\t\trecord: DocumentType<ModelType>,\n\t\tfields: (keyof DTOType)[]\n\t): Partial<DTOType> {\n\t\tconst fullView = this.transformToBasicView(record);\n\t\tconst partialView: Partial<DTOType> = {};\n\n\t\tfields.forEach(field => {\n\t\t\tconst dto = fullView as Record<keyof DTOType, any>;\n\t\t\tpartialView[field] = dto[field];\n\t\t});\n\n\t\treturn partialView;\n\t}\n\n\t/**\n\t * Creates partial views for an array of documents\n\t *\n\t * @param {DocumentType<ModelType>[]} records - Array of documents to transform\n\t * @param {(keyof DTOType)[]} fields - Fields to include in the output\n\t * @returns {Partial<DTOType>[]} Array of partial DTOs\n\t */\n\tpublic transformToPartialViewArray(\n\t\trecords: DocumentType<ModelType>[] | PaginatedResponse<DocumentType<ModelType>>,\n\t\tfields: (keyof DTOType)[]\n\t): Partial<DTOType>[] | PaginatedResponse<Partial<DTOType>> {\n\t\t// Case 1: If records is already in the paginated format\n\t\tif (records && typeof records === 'object' && 'metadata' in records && 'documents' in records) {\n\t\t\tconst paginatedRecords = records as PaginatedResponse<DocumentType<ModelType>>;\n\n\t\t\t// Process the documents array\n\t\t\tconst processedDocuments = Array.isArray(paginatedRecords.documents)\n\t\t\t\t? paginatedRecords.documents.map(doc => this.transformToPartialView(doc, fields))\n\t\t\t\t: [];\n\n\t\t\t// Return the entire paginated structure\n\t\t\treturn {\n\t\t\t\tmetadata: paginatedRecords.metadata,\n\t\t\t\tdocuments: processedDocuments,\n\t\t\t};\n\t\t}\n\n\t\t// Case 2: If records is an array\n\t\tif (Array.isArray(records)) {\n\t\t\tif (records.length === 0) {\n\t\t\t\treturn [];\n\t\t\t}\n\t\t\treturn records.map(record => this.transformToPartialView(record, fields));\n\t\t}\n\n\t\t// Fallback for unexpected input\n\t\tthrow new Error('Invalid input type for transformToPartialViewArray');\n\t}\n\t/**\n\t * Transforms a document and adds additional computed fields\n\t *\n\t * @param {DocumentType<ModelType>} record - The document to transform\n\t * @param {Partial<Record<string, any>>} additionalData - Additional fields to add\n\t * @returns {DTOType & Record<string, any>} DTO with additional fields\n\t */\n\tpublic transformWithAdditionalData(\n\t\trecord: DocumentType<ModelType>,\n\t\tadditionalData: Partial<Record<string, any>>\n\t): DTOType & Record<string, any> {\n\t\tconst basicView = this.transformToBasicView(record);\n\t\treturn { ...basicView, ...additionalData };\n\t}\n\n\t/**\n\t * Transforms a document using a custom mapping function\n\t *\n\t * @template OutputType - The output type after mapping\n\t * @param {DocumentType<ModelType>} record - The document to transform\n\t * @param {(input: DTOType) => OutputType} mapFn - Mapping function for custom transformation\n\t * @returns {OutputType} Custom transformed output\n\t */\n\tpublic transformWithMapping<OutputType = DTOType>(\n\t\trecord: DocumentType<ModelType>,\n\t\tmapFn: (input: DTOType) => OutputType\n\t): OutputType {\n\t\tconst basicView = this.transformToBasicView(record);\n\t\treturn mapFn(basicView);\n\t}\n\n\t/**\n\t * Transforms a nested property of the document\n\t *\n\t * @template NestedType - The type of the nested property\n\t * @param {DocumentType<ModelType>} record - The document to transform\n\t * @param {string} path - Dot-notation path to the nested property\n\t * @param {z.ZodType<any>} nestedSchema - Zod schema for the nested property\n\t * @returns {NestedType} The transformed nested property\n\t * @throws {Error} When path is not found in record\n\t */\n\tpublic transformNestedProperty<NestedType>(\n\t\trecord: DocumentType<ModelType>,\n\t\tpath: string,\n\t\tnestedSchema: z.ZodType<any>\n\t): NestedType {\n\t\tif (!record) {\n\t\t\tthrow new Error('Record not found');\n\t\t}\n\n\t\tconst parts = path.split('.');\n\t\tlet current: any = record;\n\n\t\tfor (const part of parts) {\n\t\t\tif (current && part in current) {\n\t\t\t\tcurrent = current[part];\n\t\t\t} else {\n\t\t\t\tthrow new Error(`Path ${path} not found in record`);\n\t\t\t}\n\t\t}\n\n\t\treturn nestedSchema.parse(current) as NestedType;\n\t}\n\n\t/**\n\t * Transforms a document with default values for missing fields\n\t *\n\t * @param {DocumentType<ModelType>} record - The document to transform\n\t * @param {Partial<DTOType>} defaults - Default values for fields\n\t * @returns {DTOType} DTO with defaults applied\n\t */\n\tpublic transformWithDefaults(record: DocumentType<ModelType>, defaults: Partial<DTOType>): DTOType {\n\t\tconst transformed = this.transformToBasicView(record);\n\t\treturn { ...defaults, ...transformed };\n\t}\n}\n\n/**\n * Creates a transformer instance for the given schema.\n * Uses dependency injection if available, falls back to creating a new instance.\n *\n * @template ModelType - The Mongoose model type\n * @template DTOType - The output DTO type\n * @param {z.ZodType<any>} schema - Zod schema for validation and transformation\n * @returns {GlobalTransformer<ModelType, DTOType>} Instance of GlobalTransformer\n */\nexport function createTransformer<ModelType, DTOType>(\n\tschema: z.ZodType<any>\n): GlobalTransformer<ModelType, DTOType> {\n\ttry {\n\t\treturn Container.get(GlobalTransformer) as GlobalTransformer<ModelType, DTOType>;\n\t} catch (error) {\n\t\treturn new GlobalTransformer<ModelType, DTOType>(schema);\n\t}\n}\n"]}
|