@buka/nestjs-kit 4.7.0 → 4.7.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/CHANGELOG.md +7 -0
- package/dist/index.js +791 -769
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +791 -769
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/skills/nestjs-kit-urn/SKILL.md +34 -2
package/dist/index.mjs
CHANGED
|
@@ -2,14 +2,14 @@ import { ApiExtension, ApiExtraModels, ApiHideProperty, ApiOkResponse, ApiOperat
|
|
|
2
2
|
import { BadRequestException, Catch, ConfigurableModuleBuilder, Controller, Get, Global, HttpException as HttpException$1, HttpStatus, Inject, Injectable, InternalServerErrorException, Logger as Logger$1, Module, Query, VERSION_NEUTRAL, ValidationPipe, ValidationPipeOptions, Version, applyDecorators, createParamDecorator } from "@nestjs/common";
|
|
3
3
|
import { APP_FILTER, APP_PIPE } from "@nestjs/core";
|
|
4
4
|
import { BigIntType, Config, Cursor, EntityManager, EntityMetadata, EntitySchema, FlushMode, HiddenProps, MetadataStorage, MikroORM, OptionalProps, PrimaryKeyProp, Reference, Utils, helper } from "@mikro-orm/core";
|
|
5
|
-
import {
|
|
5
|
+
import { inheritTransformationMetadata, inheritValidationMetadata } from "@nestjs/mapped-types";
|
|
6
6
|
import { ArrayMinSize, IS_OPTIONAL, IsArray, IsBoolean, IsCurrency, IsDefined, IsEnum, IsISO8601, IsIn, IsInt, IsNotEmpty, IsNumber, IsNumberString, IsObject, IsOptional, IsString, IsUUID, IsUrl, Matches, MaxLength, Min, MinLength, ValidateNested, getMetadataStorage, isEnum, registerDecorator, validate, validateSync } from "class-validator";
|
|
7
7
|
import { validate as validate$1 } from "jsonschema";
|
|
8
8
|
import { Transform, Type as Type$1, plainToInstance } from "class-transformer";
|
|
9
9
|
import * as R from "ramda";
|
|
10
10
|
import * as classTransformer from "class-transformer/cjs/storage.js";
|
|
11
11
|
import { v7, validate as validate$2, version } from "uuid";
|
|
12
|
-
import {
|
|
12
|
+
import { Embeddable, Embedded, Enum as Enum$1, ManyToMany, ManyToOne, OneToMany, OneToOne, PrimaryKey, Property as Property$1 } from "@mikro-orm/decorators/legacy";
|
|
13
13
|
import { ToBoolean, ToNumber } from "@buka/class-transformer-extra";
|
|
14
14
|
import * as util from "util";
|
|
15
15
|
import { Migrator } from "@mikro-orm/migrations";
|
|
@@ -2066,271 +2066,571 @@ function FilterQuery(classRef) {
|
|
|
2066
2066
|
};
|
|
2067
2067
|
}
|
|
2068
2068
|
//#endregion
|
|
2069
|
-
//#region src/modules/mikro-orm/
|
|
2069
|
+
//#region src/modules/mikro-orm/utils/mikro-orm-utils.ts
|
|
2070
|
+
function getMetadata(classRef) {
|
|
2071
|
+
const metadatas = [];
|
|
2072
|
+
let parent = classRef;
|
|
2073
|
+
do {
|
|
2074
|
+
const meta = MetadataStorage.getMetadata(parent.name, parent[MetadataStorage.PATH_SYMBOL]);
|
|
2075
|
+
if (meta instanceof EntityMetadata) metadatas.push(meta);
|
|
2076
|
+
parent = Object.getPrototypeOf(parent);
|
|
2077
|
+
} while (parent && parent !== Object.prototype);
|
|
2078
|
+
return R.unnest(metadatas.map((meta) => R.values(meta.properties)));
|
|
2079
|
+
}
|
|
2080
|
+
//#endregion
|
|
2081
|
+
//#region src/modules/mikro-orm/converters/entity-primary-key-type/entity-primary-key-type.ts
|
|
2082
|
+
const PrimaryKeyTypeClassMetadataPropertyKey = Symbol("PrimaryKeyTypeClassMetadataPropertyKey");
|
|
2083
|
+
const storage = /* @__PURE__ */ new WeakMap();
|
|
2084
|
+
function PrimaryKeyType(classRef) {
|
|
2085
|
+
if (storage.has(classRef)) return storage.get(classRef);
|
|
2086
|
+
const primaryProperties = getMetadata(classRef).filter((prop) => prop.primary).map((prop) => prop.name);
|
|
2087
|
+
if (!primaryProperties.length) throw new Error(`Cannot create PrimaryKeyType for ${classRef.name} because it has no primary properties.`);
|
|
2088
|
+
class PrimaryKeyTypeClass {}
|
|
2089
|
+
Object.defineProperty(PrimaryKeyTypeClass, "name", { value: `${classRef.name}PrimaryKeyType` });
|
|
2090
|
+
PrimaryKeyTypeClass[PrimaryKeyTypeClassMetadataPropertyKey] = classRef;
|
|
2091
|
+
cloneMetadata(PrimaryKeyTypeClass, classRef, primaryProperties);
|
|
2092
|
+
inheritValidationMetadata(classRef, PrimaryKeyTypeClass, (key) => primaryProperties.includes(key));
|
|
2093
|
+
inheritTransformationMetadata(classRef, PrimaryKeyTypeClass, (key) => primaryProperties.includes(key));
|
|
2094
|
+
for (const propertyName of primaryProperties) ModelRegister.copyProperty(classRef, PrimaryKeyTypeClass, propertyName);
|
|
2095
|
+
storage.set(classRef, PrimaryKeyTypeClass);
|
|
2096
|
+
return PrimaryKeyTypeClass;
|
|
2097
|
+
}
|
|
2098
|
+
//#endregion
|
|
2099
|
+
//#region src/modules/core/pipes/validation.pipe.ts
|
|
2100
|
+
var _ref$14, _ref2$4, _ref3$1, _BukaValidationPipe;
|
|
2101
|
+
function deepMap(obj, fn) {
|
|
2102
|
+
if (Array.isArray(obj)) return obj.map((item) => deepMap(item, fn));
|
|
2103
|
+
else return fn(obj);
|
|
2104
|
+
}
|
|
2070
2105
|
/**
|
|
2071
|
-
*
|
|
2072
|
-
*
|
|
2073
|
-
* 自动应用 MikroORM `@Property({ type: 'varchar' })`、`@IsString()` 验证及 Swagger schema。
|
|
2106
|
+
* 判断 ctor 是否为 `PrimaryKeyType(Entity)` 生成的引用类
|
|
2074
2107
|
*
|
|
2075
|
-
*
|
|
2108
|
+
* 这类类上挂着原始实体类,转换时不能当普通 DTO 继续递归,而要换成 `em.getReference`。
|
|
2109
|
+
*/
|
|
2110
|
+
function isPrimaryKeyTypeClass(ctor) {
|
|
2111
|
+
return typeof ctor === "function" && PrimaryKeyTypeClassMetadataPropertyKey in ctor;
|
|
2112
|
+
}
|
|
2113
|
+
function transformReference(em, value, metatype) {
|
|
2114
|
+
if (typeof value !== "object" || value === null) return value;
|
|
2115
|
+
const primaryProperties = getMetadata(metatype).filter((p) => p.primary);
|
|
2116
|
+
if (primaryProperties.length === 1) {
|
|
2117
|
+
const propertyKey = primaryProperties[0].name;
|
|
2118
|
+
return em.getReference(metatype, value[propertyKey], { wrapped: true });
|
|
2119
|
+
} else if (primaryProperties.length > 1) {
|
|
2120
|
+
const propertyKeys = primaryProperties.map((p) => p.name);
|
|
2121
|
+
return em.getReference(metatype, propertyKeys.map((key) => value[key]), { wrapped: true });
|
|
2122
|
+
}
|
|
2123
|
+
return value;
|
|
2124
|
+
}
|
|
2125
|
+
/**
|
|
2126
|
+
* 按属性声明的类型转换单个值
|
|
2076
2127
|
*
|
|
2077
|
-
*
|
|
2078
|
-
*
|
|
2079
|
-
* @Entity()
|
|
2080
|
-
* class User {
|
|
2081
|
-
* @Varchar({ length: 255, comment: '用户名' })
|
|
2082
|
-
* name: string
|
|
2083
|
-
* }
|
|
2084
|
-
* ```
|
|
2128
|
+
* `PrimaryKeyType` 换成托管引用;其它 DTO 继续递归。单值、列表元素与字典值共用这一套判断,
|
|
2129
|
+
* 否则列表 / 字典里的 `PrimaryKeyType` 会被当成普通对象,最终被 ORM 当作新实体插入。
|
|
2085
2130
|
*/
|
|
2086
|
-
function
|
|
2087
|
-
|
|
2088
|
-
|
|
2089
|
-
|
|
2090
|
-
|
|
2091
|
-
|
|
2092
|
-
|
|
2093
|
-
|
|
2094
|
-
|
|
2095
|
-
|
|
2096
|
-
|
|
2097
|
-
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2131
|
+
function transformNested(em, value, ctor) {
|
|
2132
|
+
if (isPrimaryKeyTypeClass(ctor)) {
|
|
2133
|
+
const entity = ctor[PrimaryKeyTypeClassMetadataPropertyKey];
|
|
2134
|
+
if (entity) return transformReference(em, value, entity);
|
|
2135
|
+
}
|
|
2136
|
+
return deepTransform(em, value, ctor);
|
|
2137
|
+
}
|
|
2138
|
+
function deepTransform(em, value, metatype) {
|
|
2139
|
+
if (typeof value !== "object" || value === null) return value;
|
|
2140
|
+
const propertyKeys = ModelRegister.getModelPropertyKeys(metatype);
|
|
2141
|
+
const result = { ...value };
|
|
2142
|
+
for (const propertyKey of propertyKeys) {
|
|
2143
|
+
const propertyMetadata = ModelRegister.getProperty(metatype, propertyKey);
|
|
2144
|
+
if (!propertyMetadata) continue;
|
|
2145
|
+
if (!(propertyKey in value)) continue;
|
|
2146
|
+
const propertyValue = value[propertyKey];
|
|
2147
|
+
if (propertyMetadata.kind === "composite") result[propertyKey] = transformNested(em, propertyValue, propertyMetadata.type());
|
|
2148
|
+
else if (propertyMetadata.kind === "list") {
|
|
2149
|
+
if (propertyMetadata.type && !isScalarClass(propertyMetadata.type)) {
|
|
2150
|
+
const itemCtor = propertyMetadata.type();
|
|
2151
|
+
result[propertyKey] = deepMap(propertyValue, (item) => transformNested(em, item, itemCtor));
|
|
2102
2152
|
}
|
|
2103
|
-
}
|
|
2104
|
-
|
|
2153
|
+
} else if (propertyMetadata.kind === "dictionary") {
|
|
2154
|
+
if (propertyMetadata.type && !isScalarClass(propertyMetadata.type)) {
|
|
2155
|
+
const valueCtor = propertyMetadata.type();
|
|
2156
|
+
if (propertyValue instanceof Map) result[propertyKey] = new Map(Array.from(propertyValue.entries()).map(([k, v]) => [k, transformNested(em, v, valueCtor)]));
|
|
2157
|
+
else result[propertyKey] = Object.fromEntries(Object.entries(propertyValue).map(([k, v]) => [k, transformNested(em, v, valueCtor)]));
|
|
2158
|
+
}
|
|
2159
|
+
}
|
|
2160
|
+
}
|
|
2161
|
+
return result;
|
|
2105
2162
|
}
|
|
2163
|
+
let BukaValidationPipe = _BukaValidationPipe = class BukaValidationPipe extends ValidationPipe {
|
|
2164
|
+
logger = new Logger$1(_BukaValidationPipe.name);
|
|
2165
|
+
validationOptions;
|
|
2166
|
+
constructor(orm, em, options) {
|
|
2167
|
+
super(options);
|
|
2168
|
+
this.orm = orm;
|
|
2169
|
+
this.em = em;
|
|
2170
|
+
this.validationOptions = options;
|
|
2171
|
+
}
|
|
2172
|
+
async transform(value, metadata) {
|
|
2173
|
+
value = await super.transform(value, metadata);
|
|
2174
|
+
if (metadata.type === "custom") return value;
|
|
2175
|
+
if (this.validationOptions && !this.validationOptions.transform) return value;
|
|
2176
|
+
if (!metadata.metatype || typeof metadata.metatype !== "function") return value;
|
|
2177
|
+
const metatype = metadata.metatype;
|
|
2178
|
+
return await deepTransform(this.em, value, metatype);
|
|
2179
|
+
}
|
|
2180
|
+
};
|
|
2181
|
+
BukaValidationPipe = _BukaValidationPipe = __decorate([Injectable(), __decorateMetadata("design:paramtypes", [
|
|
2182
|
+
typeof (_ref$14 = typeof MikroORM !== "undefined" && MikroORM) === "function" ? _ref$14 : Object,
|
|
2183
|
+
typeof (_ref2$4 = typeof EntityManager !== "undefined" && EntityManager) === "function" ? _ref2$4 : Object,
|
|
2184
|
+
typeof (_ref3$1 = typeof ValidationPipeOptions !== "undefined" && ValidationPipeOptions) === "function" ? _ref3$1 : Object
|
|
2185
|
+
])], BukaValidationPipe);
|
|
2106
2186
|
//#endregion
|
|
2107
|
-
//#region src/modules/
|
|
2187
|
+
//#region src/modules/core/buka.module-definition.ts
|
|
2188
|
+
const { ConfigurableModuleClass: BukaConfigurableModuleClass, MODULE_OPTIONS_TOKEN: BUKA_MODULE_OPTIONS_TOKEN } = new ConfigurableModuleBuilder().build();
|
|
2189
|
+
//#endregion
|
|
2190
|
+
//#region src/modules/core/buka.module.ts
|
|
2191
|
+
let BukaModule = class BukaModule extends BukaConfigurableModuleClass {};
|
|
2192
|
+
BukaModule = __decorate([Global(), Module({ providers: [{
|
|
2193
|
+
provide: APP_PIPE,
|
|
2194
|
+
inject: [
|
|
2195
|
+
BUKA_MODULE_OPTIONS_TOKEN,
|
|
2196
|
+
MikroORM,
|
|
2197
|
+
EntityManager
|
|
2198
|
+
],
|
|
2199
|
+
useFactory: (options, orm, em) => {
|
|
2200
|
+
return new BukaValidationPipe(orm, em, options?.validation);
|
|
2201
|
+
}
|
|
2202
|
+
}] })], BukaModule);
|
|
2203
|
+
//#endregion
|
|
2204
|
+
//#region src/modules/core/models/offset-pagination.ts
|
|
2205
|
+
let OffsetPagination = class OffsetPagination {
|
|
2206
|
+
total;
|
|
2207
|
+
limit;
|
|
2208
|
+
offset;
|
|
2209
|
+
constructor(total, parameters) {
|
|
2210
|
+
this.total = total;
|
|
2211
|
+
this.limit = parameters.limit;
|
|
2212
|
+
this.offset = parameters.offset;
|
|
2213
|
+
}
|
|
2214
|
+
};
|
|
2215
|
+
__decorate([Property({ schema: {
|
|
2216
|
+
type: "number",
|
|
2217
|
+
description: "总数"
|
|
2218
|
+
} }), __decorateMetadata("design:type", Number)], OffsetPagination.prototype, "total", void 0);
|
|
2219
|
+
__decorate([Property({ schema: {
|
|
2220
|
+
type: "number",
|
|
2221
|
+
description: "每页数量"
|
|
2222
|
+
} }), __decorateMetadata("design:type", Number)], OffsetPagination.prototype, "limit", void 0);
|
|
2223
|
+
__decorate([Property({ schema: {
|
|
2224
|
+
type: "number",
|
|
2225
|
+
description: "偏移量"
|
|
2226
|
+
} }), __decorateMetadata("design:type", Number)], OffsetPagination.prototype, "offset", void 0);
|
|
2227
|
+
OffsetPagination = __decorate([Model({ schema: {
|
|
2228
|
+
description: "基于偏移量的分页响应,通过 offset + limit 定位数据位置,适用于传统翻页场景",
|
|
2229
|
+
additionalProperties: false
|
|
2230
|
+
} }), __decorateMetadata("design:paramtypes", [Number, Object])], OffsetPagination);
|
|
2231
|
+
//#endregion
|
|
2232
|
+
//#region src/modules/core/models/cursor-pagination.ts
|
|
2233
|
+
var _ref$13;
|
|
2234
|
+
let CursorPagination = class CursorPagination {
|
|
2235
|
+
total;
|
|
2236
|
+
limit;
|
|
2237
|
+
startCursor;
|
|
2238
|
+
endCursor;
|
|
2239
|
+
hasNextPage;
|
|
2240
|
+
hasPrevPage;
|
|
2241
|
+
constructor(cursor) {
|
|
2242
|
+
this.total = cursor.totalCount;
|
|
2243
|
+
this.limit = cursor.length;
|
|
2244
|
+
this.startCursor = cursor.startCursor;
|
|
2245
|
+
this.endCursor = cursor.endCursor;
|
|
2246
|
+
this.hasNextPage = cursor.hasNextPage;
|
|
2247
|
+
this.hasPrevPage = cursor.hasPrevPage;
|
|
2248
|
+
}
|
|
2249
|
+
};
|
|
2250
|
+
__decorate([Property({ schema: {
|
|
2251
|
+
type: "number",
|
|
2252
|
+
description: "总数"
|
|
2253
|
+
} }), __decorateMetadata("design:type", Number)], CursorPagination.prototype, "total", void 0);
|
|
2254
|
+
__decorate([Property({ schema: {
|
|
2255
|
+
type: "number",
|
|
2256
|
+
description: "每页数量"
|
|
2257
|
+
} }), __decorateMetadata("design:type", Number)], CursorPagination.prototype, "limit", void 0);
|
|
2258
|
+
__decorate([Property({ schema: {
|
|
2259
|
+
type: "string",
|
|
2260
|
+
description: "开始游标",
|
|
2261
|
+
nullable: true
|
|
2262
|
+
} }), __decorateMetadata("design:type", Object)], CursorPagination.prototype, "startCursor", void 0);
|
|
2263
|
+
__decorate([Property({ schema: {
|
|
2264
|
+
type: "string",
|
|
2265
|
+
description: "结束游标",
|
|
2266
|
+
nullable: true
|
|
2267
|
+
} }), __decorateMetadata("design:type", Object)], CursorPagination.prototype, "endCursor", void 0);
|
|
2268
|
+
__decorate([Property({ schema: {
|
|
2269
|
+
type: "boolean",
|
|
2270
|
+
description: "是否有下一页"
|
|
2271
|
+
} }), __decorateMetadata("design:type", Boolean)], CursorPagination.prototype, "hasNextPage", void 0);
|
|
2272
|
+
__decorate([Property({ schema: {
|
|
2273
|
+
type: "boolean",
|
|
2274
|
+
description: "是否有上一页"
|
|
2275
|
+
} }), __decorateMetadata("design:type", Boolean)], CursorPagination.prototype, "hasPrevPage", void 0);
|
|
2276
|
+
CursorPagination = __decorate([Model({ schema: {
|
|
2277
|
+
description: "基于游标的分页响应,通过 startCursor/endCursor 定位数据位置,适用于无限滚动等场景",
|
|
2278
|
+
additionalProperties: false
|
|
2279
|
+
} }), __decorateMetadata("design:paramtypes", [typeof (_ref$13 = typeof Cursor !== "undefined" && Cursor) === "function" ? _ref$13 : Object])], CursorPagination);
|
|
2280
|
+
//#endregion
|
|
2281
|
+
//#region src/modules/core/models/slice.ts
|
|
2282
|
+
var Slice = class Slice {
|
|
2283
|
+
data;
|
|
2284
|
+
pagination;
|
|
2285
|
+
constructor(data, pagination) {
|
|
2286
|
+
this.data = data;
|
|
2287
|
+
this.pagination = pagination;
|
|
2288
|
+
}
|
|
2289
|
+
*[Symbol.iterator]() {
|
|
2290
|
+
for (const item of this.data) yield item;
|
|
2291
|
+
}
|
|
2292
|
+
static fromOffset(data, total, parameters) {
|
|
2293
|
+
return new Slice(data, new OffsetPagination(total, parameters));
|
|
2294
|
+
}
|
|
2295
|
+
static fromCursor(cursor) {
|
|
2296
|
+
const pagination = new CursorPagination(cursor);
|
|
2297
|
+
return new Slice(cursor.items, pagination);
|
|
2298
|
+
}
|
|
2299
|
+
map(fn) {
|
|
2300
|
+
return new Slice(this.data.map(fn), this.pagination);
|
|
2301
|
+
}
|
|
2302
|
+
};
|
|
2303
|
+
//#endregion
|
|
2304
|
+
//#region src/modules/core/converters/pick-type/pick-type.ts
|
|
2108
2305
|
/**
|
|
2109
|
-
*
|
|
2306
|
+
* 从模型中选取指定属性,生成新的子集类型。
|
|
2110
2307
|
*
|
|
2111
|
-
*
|
|
2112
|
-
* 当指定 `length` 时,同时校验最小和最大长度。
|
|
2308
|
+
* 类似 TypeScript 的 `Pick<T, K>`,同时保留 class-validator、class-transformer 和 Swagger 的元数据。
|
|
2113
2309
|
*
|
|
2114
|
-
* @param
|
|
2310
|
+
* @param classRef - 使用 `@Model()` 标注的类引用
|
|
2311
|
+
* @param keys - 需要选取的属性名数组
|
|
2312
|
+
* @returns 仅包含指定属性的新类
|
|
2115
2313
|
*
|
|
2116
2314
|
* @example
|
|
2117
2315
|
* ```typescript
|
|
2118
|
-
*
|
|
2119
|
-
* class Country {
|
|
2120
|
-
* @Char({ length: 2, comment: '国家代码' })
|
|
2121
|
-
* code: string
|
|
2122
|
-
* }
|
|
2316
|
+
* const CreateUserDTO = PickType(UserDTO, ['name', 'email'])
|
|
2123
2317
|
* ```
|
|
2124
2318
|
*/
|
|
2125
|
-
function
|
|
2126
|
-
|
|
2127
|
-
|
|
2128
|
-
|
|
2129
|
-
|
|
2130
|
-
|
|
2131
|
-
|
|
2132
|
-
|
|
2133
|
-
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
schema: {
|
|
2139
|
-
type: "string",
|
|
2140
|
-
maxLength: options?.length,
|
|
2141
|
-
minLength: options?.length,
|
|
2142
|
-
description: options?.comment,
|
|
2143
|
-
example: options?.example,
|
|
2144
|
-
examples: options?.examples,
|
|
2145
|
-
required: !options?.nullable
|
|
2146
|
-
}
|
|
2147
|
-
}))(target, propertyKey);
|
|
2148
|
-
};
|
|
2319
|
+
function PickType(classRef, keys) {
|
|
2320
|
+
if (!ModelRegister.isModel(classRef)) throw new TypeError("PickType only accepts a class annotated with @Model().");
|
|
2321
|
+
const PickTypeClass = PickType$1(classRef, keys);
|
|
2322
|
+
for (const key of keys) {
|
|
2323
|
+
ModelRegister.copyProperty(classRef, PickTypeClass, key);
|
|
2324
|
+
const metadataKeys = Reflect.getMetadataKeys(classRef.prototype, key);
|
|
2325
|
+
for (const metadataKey of metadataKeys) {
|
|
2326
|
+
const metadata = Reflect.getMetadata(metadataKey, classRef.prototype, key);
|
|
2327
|
+
if (Reflect.hasMetadata(metadataKey, PickTypeClass.prototype, key)) continue;
|
|
2328
|
+
Reflect.defineMetadata(metadataKey, metadata, PickTypeClass.prototype, key);
|
|
2329
|
+
}
|
|
2330
|
+
}
|
|
2331
|
+
return PickTypeClass;
|
|
2149
2332
|
}
|
|
2150
2333
|
//#endregion
|
|
2151
|
-
//#region src/modules/
|
|
2334
|
+
//#region src/modules/core/converters/omit-type/omit-type.ts
|
|
2152
2335
|
/**
|
|
2153
|
-
*
|
|
2336
|
+
* 从模型中排除指定属性,生成新的类型。
|
|
2154
2337
|
*
|
|
2155
|
-
*
|
|
2338
|
+
* 类似 TypeScript 的 `Omit<T, K>`,同时保留 class-validator、class-transformer 和 Swagger 的元数据。
|
|
2156
2339
|
*
|
|
2157
|
-
* @param
|
|
2340
|
+
* @param classRef - 使用 `@Model()` 标注的类引用
|
|
2341
|
+
* @param keys - 需要排除的属性名数组
|
|
2342
|
+
* @returns 排除了指定属性的新类
|
|
2158
2343
|
*
|
|
2159
2344
|
* @example
|
|
2160
2345
|
* ```typescript
|
|
2161
|
-
*
|
|
2162
|
-
* class Article {
|
|
2163
|
-
* @Text({ comment: '文章内容' })
|
|
2164
|
-
* content: string
|
|
2165
|
-
* }
|
|
2346
|
+
* const UpdateUserDTO = OmitType(UserDTO, ['id', 'createdAt'])
|
|
2166
2347
|
* ```
|
|
2167
2348
|
*/
|
|
2168
|
-
function
|
|
2169
|
-
|
|
2170
|
-
|
|
2171
|
-
|
|
2172
|
-
|
|
2173
|
-
|
|
2174
|
-
|
|
2175
|
-
|
|
2176
|
-
|
|
2177
|
-
|
|
2178
|
-
|
|
2179
|
-
|
|
2180
|
-
|
|
2181
|
-
|
|
2182
|
-
required: !options?.nullable
|
|
2183
|
-
}
|
|
2184
|
-
}))(target, propertyKey);
|
|
2185
|
-
};
|
|
2349
|
+
function OmitType(classRef, keys) {
|
|
2350
|
+
if (!ModelRegister.isModel(classRef)) throw new TypeError("OmitType only accepts a class annotated with @Model().");
|
|
2351
|
+
const OmitTypeClass = OmitType$1(classRef, keys);
|
|
2352
|
+
for (const propertyKey of ModelRegister.getModelPropertyKeys(classRef)) {
|
|
2353
|
+
if (keys.includes(propertyKey)) continue;
|
|
2354
|
+
ModelRegister.copyProperty(classRef, OmitTypeClass, propertyKey);
|
|
2355
|
+
const metadataKeys = Reflect.getMetadataKeys(classRef.prototype, propertyKey);
|
|
2356
|
+
for (const metadataKey of metadataKeys) {
|
|
2357
|
+
const metadata = Reflect.getMetadata(metadataKey, classRef.prototype, propertyKey);
|
|
2358
|
+
if (Reflect.hasMetadata(metadataKey, OmitTypeClass.prototype, propertyKey)) continue;
|
|
2359
|
+
Reflect.defineMetadata(metadataKey, metadata, OmitTypeClass.prototype, propertyKey);
|
|
2360
|
+
}
|
|
2361
|
+
}
|
|
2362
|
+
return OmitTypeClass;
|
|
2186
2363
|
}
|
|
2187
2364
|
//#endregion
|
|
2188
|
-
//#region src/modules/
|
|
2365
|
+
//#region src/modules/core/converters/partial-type/partial-type.ts
|
|
2189
2366
|
/**
|
|
2190
|
-
*
|
|
2367
|
+
* 将模型的所有属性变为可选,生成新的 Partial 类型。
|
|
2191
2368
|
*
|
|
2192
|
-
*
|
|
2369
|
+
* 类似 TypeScript 的 `Partial<T>`,同时保留 class-validator、class-transformer 和 Swagger 的元数据。
|
|
2193
2370
|
*
|
|
2194
|
-
* @param
|
|
2371
|
+
* @param classRef - 使用 `@Model()` 标注的类引用
|
|
2372
|
+
* @returns 所有属性均为可选的新类
|
|
2195
2373
|
*
|
|
2196
2374
|
* @example
|
|
2197
2375
|
* ```typescript
|
|
2198
|
-
*
|
|
2199
|
-
* class Order {
|
|
2200
|
-
* @Money({ comment: '订单金额' })
|
|
2201
|
-
* totalAmount: string
|
|
2202
|
-
* }
|
|
2376
|
+
* const PatchUserDTO = PartialType(UserDTO)
|
|
2203
2377
|
* ```
|
|
2204
2378
|
*/
|
|
2205
|
-
function
|
|
2206
|
-
|
|
2207
|
-
|
|
2208
|
-
|
|
2209
|
-
|
|
2210
|
-
|
|
2211
|
-
|
|
2212
|
-
optional:
|
|
2213
|
-
|
|
2214
|
-
|
|
2215
|
-
|
|
2216
|
-
|
|
2217
|
-
|
|
2218
|
-
|
|
2219
|
-
|
|
2220
|
-
|
|
2221
|
-
|
|
2222
|
-
}))(target, propertyKey);
|
|
2223
|
-
};
|
|
2379
|
+
function PartialType(classRef) {
|
|
2380
|
+
const PartialTypeClass = PartialType$1(classRef);
|
|
2381
|
+
for (const propertyKey of ModelRegister.getModelPropertyKeys(classRef)) {
|
|
2382
|
+
ModelRegister.copyProperty(classRef, PartialTypeClass, propertyKey);
|
|
2383
|
+
const propertyMetadata = ModelRegister.getProperty(PartialTypeClass, propertyKey);
|
|
2384
|
+
if (propertyMetadata) ModelRegister.setProperty(PartialTypeClass, propertyKey, {
|
|
2385
|
+
...propertyMetadata,
|
|
2386
|
+
optional: true
|
|
2387
|
+
});
|
|
2388
|
+
const metadataKeys = Reflect.getMetadataKeys(classRef.prototype, propertyKey);
|
|
2389
|
+
for (const metadataKey of metadataKeys) {
|
|
2390
|
+
const metadata = Reflect.getMetadata(metadataKey, classRef.prototype, propertyKey);
|
|
2391
|
+
if (Reflect.hasMetadata(metadataKey, PartialTypeClass.prototype, propertyKey)) continue;
|
|
2392
|
+
Reflect.defineMetadata(metadataKey, metadata, PartialTypeClass.prototype, propertyKey);
|
|
2393
|
+
}
|
|
2394
|
+
}
|
|
2395
|
+
return PartialTypeClass;
|
|
2224
2396
|
}
|
|
2225
2397
|
//#endregion
|
|
2226
|
-
//#region src/modules/
|
|
2398
|
+
//#region src/modules/core/converters/intersection-type/intersection-type.ts
|
|
2227
2399
|
/**
|
|
2228
|
-
*
|
|
2400
|
+
* 将多个模型类合并为一个交叉类型。
|
|
2229
2401
|
*
|
|
2230
|
-
*
|
|
2402
|
+
* 类似 TypeScript 的交叉类型 `A & B`,同时合并 class-validator、class-transformer 和 Swagger 的元数据。
|
|
2231
2403
|
*
|
|
2232
|
-
* @param
|
|
2404
|
+
* @param classRefs - 需要合并的模型类引用列表
|
|
2405
|
+
* @returns 包含所有类属性的新类
|
|
2233
2406
|
*
|
|
2234
2407
|
* @example
|
|
2235
2408
|
* ```typescript
|
|
2236
|
-
*
|
|
2237
|
-
* class Product {
|
|
2238
|
-
* @Int({ unsigned: true, comment: '库存数量' })
|
|
2239
|
-
* stock: number
|
|
2240
|
-
* }
|
|
2409
|
+
* const CreateUserDTO = IntersectionType(UserBaseDTO, UserProfileDTO)
|
|
2241
2410
|
* ```
|
|
2242
2411
|
*/
|
|
2243
|
-
function
|
|
2244
|
-
|
|
2245
|
-
|
|
2246
|
-
|
|
2247
|
-
|
|
2248
|
-
|
|
2249
|
-
|
|
2250
|
-
|
|
2251
|
-
|
|
2252
|
-
|
|
2253
|
-
|
|
2254
|
-
|
|
2255
|
-
|
|
2256
|
-
|
|
2257
|
-
|
|
2258
|
-
|
|
2259
|
-
|
|
2260
|
-
|
|
2261
|
-
|
|
2412
|
+
function IntersectionType(...classRefs) {
|
|
2413
|
+
const IntersectionTypeClass = IntersectionType$1(...classRefs);
|
|
2414
|
+
for (const classRef of classRefs) for (const propertyKey of ModelRegister.getModelPropertyKeys(classRef)) {
|
|
2415
|
+
ModelRegister.copyProperty(classRef, IntersectionTypeClass, propertyKey);
|
|
2416
|
+
const metadataKeys = Reflect.getMetadataKeys(classRef.prototype, propertyKey);
|
|
2417
|
+
for (const metadataKey of metadataKeys) {
|
|
2418
|
+
const metadata = Reflect.getMetadata(metadataKey, classRef.prototype, propertyKey);
|
|
2419
|
+
if (Reflect.hasMetadata(metadataKey, IntersectionTypeClass.prototype, propertyKey)) continue;
|
|
2420
|
+
Reflect.defineMetadata(metadataKey, metadata, IntersectionTypeClass.prototype, propertyKey);
|
|
2421
|
+
}
|
|
2422
|
+
}
|
|
2423
|
+
return IntersectionTypeClass;
|
|
2424
|
+
}
|
|
2425
|
+
//#endregion
|
|
2426
|
+
//#region src/modules/core/converters/order-query-type/get-order-query-schema.ts
|
|
2427
|
+
function getOrderQueryEnum() {
|
|
2428
|
+
return {
|
|
2429
|
+
type: "string",
|
|
2430
|
+
enum: [
|
|
2431
|
+
"asc",
|
|
2432
|
+
"asc nulls last",
|
|
2433
|
+
"asc nulls first",
|
|
2434
|
+
"desc",
|
|
2435
|
+
"desc nulls last",
|
|
2436
|
+
"desc nulls first"
|
|
2437
|
+
]
|
|
2438
|
+
};
|
|
2439
|
+
}
|
|
2440
|
+
function getOrderQueryMapSchema(classRef) {
|
|
2441
|
+
const properties = {};
|
|
2442
|
+
const schema = {
|
|
2443
|
+
type: "object",
|
|
2444
|
+
properties,
|
|
2445
|
+
required: []
|
|
2446
|
+
};
|
|
2447
|
+
for (const propertyKey of ModelRegister.getModelPropertyKeys(classRef)) {
|
|
2448
|
+
const propertyMetadata = ModelRegister.getProperty(classRef, propertyKey);
|
|
2449
|
+
if (!!propertyMetadata?.association) properties[propertyKey] = getOrderQueryMapSchema(propertyMetadata.association.type());
|
|
2450
|
+
else properties[propertyKey] = getOrderQueryEnum();
|
|
2451
|
+
}
|
|
2452
|
+
return schema;
|
|
2453
|
+
}
|
|
2454
|
+
function getOrderQuerySchema(classRef) {
|
|
2455
|
+
const orderQueryMapSchema = getOrderQueryMapSchema(classRef);
|
|
2456
|
+
return {
|
|
2457
|
+
description: `Order query for ${classRef.name}`,
|
|
2458
|
+
oneOf: [{
|
|
2459
|
+
type: "array",
|
|
2460
|
+
items: orderQueryMapSchema
|
|
2461
|
+
}, orderQueryMapSchema],
|
|
2462
|
+
required: false
|
|
2262
2463
|
};
|
|
2263
2464
|
}
|
|
2264
2465
|
//#endregion
|
|
2265
|
-
//#region src/modules/
|
|
2466
|
+
//#region src/modules/core/converters/order-query-type/order-query-type.ts
|
|
2266
2467
|
/**
|
|
2267
|
-
*
|
|
2468
|
+
* 根据模型类自动生成排序查询 DTO 类型。
|
|
2268
2469
|
*
|
|
2269
|
-
*
|
|
2470
|
+
* 生成的类型包含 `orderBy` 属性,其值通过 JSON Schema 校验,确保只能按模型中的属性排序。
|
|
2270
2471
|
*
|
|
2271
|
-
* @param
|
|
2472
|
+
* @param classRef - 使用 `@Model()` 标注的类引用
|
|
2473
|
+
* @returns 包含 `orderBy` 属性的查询 DTO 类
|
|
2272
2474
|
*
|
|
2273
2475
|
* @example
|
|
2274
2476
|
* ```typescript
|
|
2275
|
-
* @
|
|
2276
|
-
* class
|
|
2277
|
-
* @
|
|
2278
|
-
*
|
|
2477
|
+
* @Model()
|
|
2478
|
+
* class User {
|
|
2479
|
+
* @Property()
|
|
2480
|
+
* name: string
|
|
2481
|
+
*
|
|
2482
|
+
* @Property()
|
|
2483
|
+
* createdAt: Date
|
|
2279
2484
|
* }
|
|
2485
|
+
*
|
|
2486
|
+
* const UserOrderQuery = OrderQueryType(User)
|
|
2487
|
+
* // 允许按 name、createdAt 排序
|
|
2280
2488
|
* ```
|
|
2281
2489
|
*/
|
|
2282
|
-
function
|
|
2283
|
-
|
|
2284
|
-
|
|
2285
|
-
|
|
2286
|
-
|
|
2287
|
-
|
|
2288
|
-
|
|
2289
|
-
|
|
2290
|
-
|
|
2291
|
-
|
|
2292
|
-
|
|
2293
|
-
minimum: options?.unsigned ? 0 : -32768,
|
|
2294
|
-
maximum: options?.unsigned ? 65535 : 32767,
|
|
2295
|
-
description: options?.comment,
|
|
2296
|
-
example: options?.example,
|
|
2297
|
-
examples: options?.examples,
|
|
2298
|
-
required: !options?.nullable
|
|
2299
|
-
}
|
|
2300
|
-
}))(target, propertyKey);
|
|
2301
|
-
};
|
|
2490
|
+
function OrderQueryType(classRef) {
|
|
2491
|
+
const schema = getOrderQuerySchema(classRef);
|
|
2492
|
+
class OrderQueryClass {
|
|
2493
|
+
orderBy;
|
|
2494
|
+
}
|
|
2495
|
+
Property({
|
|
2496
|
+
optional: true,
|
|
2497
|
+
schema
|
|
2498
|
+
})(OrderQueryClass.prototype, "orderBy");
|
|
2499
|
+
MatchJsonSchema(schema, { message: "Invalid order query" })(OrderQueryClass.prototype, "orderBy");
|
|
2500
|
+
return OrderQueryClass;
|
|
2302
2501
|
}
|
|
2303
2502
|
//#endregion
|
|
2304
|
-
//#region src/modules/
|
|
2503
|
+
//#region src/modules/core/converters/list-response-body-type/list-response-body-type.ts
|
|
2305
2504
|
/**
|
|
2306
|
-
*
|
|
2505
|
+
* 生成列表响应体类型,将数据包裹在 `{ data: T[], meta: { pagination } }` 结构中。
|
|
2307
2506
|
*
|
|
2308
|
-
*
|
|
2507
|
+
* 支持 offset 和 cursor 两种分页模式,通过 `fromSlice()` 静态方法从分页切片创建响应。
|
|
2309
2508
|
*
|
|
2310
|
-
* @param
|
|
2509
|
+
* @param classRef - 列表项的模型类引用
|
|
2510
|
+
* @param mode - 分页模式:`'offset'` 或 `'cursor'`,不传则同时支持两种
|
|
2511
|
+
* @returns 包含 `data` 和 `meta.pagination` 的列表响应体类
|
|
2512
|
+
*
|
|
2513
|
+
* @example
|
|
2514
|
+
* ```typescript
|
|
2515
|
+
* const UserListResponse = ListResponseBodyType(UserDTO, 'offset')
|
|
2516
|
+
*
|
|
2517
|
+
* @Controller('users')
|
|
2518
|
+
* class UserController {
|
|
2519
|
+
* @Get()
|
|
2520
|
+
* async findAll(@PageQuery('offset') page): Promise<UserListResponse> {
|
|
2521
|
+
* const slice = await this.userService.findAll(page)
|
|
2522
|
+
* return UserListResponse.fromSlice(slice)
|
|
2523
|
+
* }
|
|
2524
|
+
* }
|
|
2525
|
+
* ```
|
|
2526
|
+
*/
|
|
2527
|
+
function ListResponseBodyType(classRef, mode) {
|
|
2528
|
+
class ResponseBodyMeta {
|
|
2529
|
+
pagination;
|
|
2530
|
+
}
|
|
2531
|
+
class ListResponseBody {
|
|
2532
|
+
data;
|
|
2533
|
+
meta;
|
|
2534
|
+
constructor(data, meta) {
|
|
2535
|
+
this.data = data;
|
|
2536
|
+
this.meta = meta;
|
|
2537
|
+
}
|
|
2538
|
+
static fromSlice(slice) {
|
|
2539
|
+
const data = slice.data;
|
|
2540
|
+
return new ListResponseBody(data, { pagination: slice.pagination });
|
|
2541
|
+
}
|
|
2542
|
+
toJSON() {
|
|
2543
|
+
return {
|
|
2544
|
+
data: this.data.map((item) => serializeModel(item, classRef)),
|
|
2545
|
+
meta: serializeModel(this.meta, ResponseBodyMeta)
|
|
2546
|
+
};
|
|
2547
|
+
}
|
|
2548
|
+
}
|
|
2549
|
+
__decorate([List({ type: () => classRef }), __decorateMetadata("design:type", Array)], ListResponseBody.prototype, "data", void 0);
|
|
2550
|
+
ApiExtraModels(CursorPagination, OffsetPagination)(ListResponseBody);
|
|
2551
|
+
Composite({
|
|
2552
|
+
type: () => Object,
|
|
2553
|
+
schema: {
|
|
2554
|
+
type: "object",
|
|
2555
|
+
properties: { pagination: mode === "cursor" ? { $ref: getSchemaPath(CursorPagination) } : mode === "offset" ? { $ref: getSchemaPath(OffsetPagination) } : { oneOf: [{ $ref: getSchemaPath(OffsetPagination) }, { $ref: getSchemaPath(CursorPagination) }] } },
|
|
2556
|
+
additionalProperties: true,
|
|
2557
|
+
required: ["pagination"]
|
|
2558
|
+
}
|
|
2559
|
+
})(ListResponseBody.prototype, "meta");
|
|
2560
|
+
return ListResponseBody;
|
|
2561
|
+
}
|
|
2562
|
+
//#endregion
|
|
2563
|
+
//#region src/modules/core/converters/response-body-type/response-body-type.ts
|
|
2564
|
+
/**
|
|
2565
|
+
* 生成标准响应体类型,将数据包裹在 `{ data: T }` 结构中。
|
|
2566
|
+
*
|
|
2567
|
+
* 返回的类包含 `from()` 静态方法和 `toJSON()` 序列化支持。
|
|
2568
|
+
*
|
|
2569
|
+
* @param classRef - 响应数据的模型类引用
|
|
2570
|
+
* @returns 包含 `data` 属性的响应体类
|
|
2571
|
+
*
|
|
2572
|
+
* @example
|
|
2573
|
+
* ```typescript
|
|
2574
|
+
* const UserResponse = ResponseBodyType(UserDTO)
|
|
2575
|
+
*
|
|
2576
|
+
* @Controller('users')
|
|
2577
|
+
* class UserController {
|
|
2578
|
+
* @Get(':id')
|
|
2579
|
+
* async findOne(@Param('id') id: string): Promise<UserResponse> {
|
|
2580
|
+
* const user = await this.userService.findOne(id)
|
|
2581
|
+
* return UserResponse.from(user)
|
|
2582
|
+
* }
|
|
2583
|
+
* }
|
|
2584
|
+
* ```
|
|
2585
|
+
*/
|
|
2586
|
+
function ResponseBodyType(classRef) {
|
|
2587
|
+
class ResponseBodyClass {
|
|
2588
|
+
constructor(data) {
|
|
2589
|
+
this.data = data;
|
|
2590
|
+
}
|
|
2591
|
+
static from(data, meta) {
|
|
2592
|
+
return new ResponseBodyClass(data);
|
|
2593
|
+
}
|
|
2594
|
+
toJSON() {
|
|
2595
|
+
return { data: serializeModel(this.data, classRef) };
|
|
2596
|
+
}
|
|
2597
|
+
}
|
|
2598
|
+
Composite({
|
|
2599
|
+
type: () => classRef,
|
|
2600
|
+
schema: { type: () => classRef }
|
|
2601
|
+
})(ResponseBodyClass.prototype, "data");
|
|
2602
|
+
return ResponseBodyClass;
|
|
2603
|
+
}
|
|
2604
|
+
//#endregion
|
|
2605
|
+
//#region src/modules/mikro-orm/decorators/columns/varchar.column.ts
|
|
2606
|
+
/**
|
|
2607
|
+
* VARCHAR 列装饰器,声明一个可变长度字符串列。
|
|
2608
|
+
*
|
|
2609
|
+
* 自动应用 MikroORM `@Property({ type: 'varchar' })`、`@IsString()` 验证及 Swagger schema。
|
|
2610
|
+
*
|
|
2611
|
+
* @param options - 列配置,支持 `length` 限制最大长度
|
|
2311
2612
|
*
|
|
2312
2613
|
* @example
|
|
2313
2614
|
* ```typescript
|
|
2314
2615
|
* @Entity()
|
|
2315
|
-
* class
|
|
2316
|
-
* @
|
|
2317
|
-
*
|
|
2616
|
+
* class User {
|
|
2617
|
+
* @Varchar({ length: 255, comment: '用户名' })
|
|
2618
|
+
* name: string
|
|
2318
2619
|
* }
|
|
2319
2620
|
* ```
|
|
2320
2621
|
*/
|
|
2321
|
-
function
|
|
2622
|
+
function Varchar(options) {
|
|
2322
2623
|
return (target, propertyKey) => {
|
|
2323
2624
|
Property$1({
|
|
2324
2625
|
...options,
|
|
2325
|
-
type: "
|
|
2626
|
+
type: "varchar"
|
|
2326
2627
|
})(target, propertyKey);
|
|
2327
|
-
applyDecorators(
|
|
2628
|
+
applyDecorators(...options?.length ? [IsString(), MaxLength(options.length)] : [IsString()], Property({
|
|
2328
2629
|
optional: options?.nullable,
|
|
2329
2630
|
lazy: options?.lazy,
|
|
2330
2631
|
schema: {
|
|
2331
|
-
type: "
|
|
2332
|
-
|
|
2333
|
-
maximum: options?.unsigned ? 255 : 127,
|
|
2632
|
+
type: "string",
|
|
2633
|
+
maxLength: options?.length,
|
|
2334
2634
|
description: options?.comment,
|
|
2335
2635
|
example: options?.example,
|
|
2336
2636
|
examples: options?.examples,
|
|
@@ -2340,47 +2640,41 @@ function Tinyint(options) {
|
|
|
2340
2640
|
};
|
|
2341
2641
|
}
|
|
2342
2642
|
//#endregion
|
|
2343
|
-
//#region src/modules/mikro-orm/decorators/columns/
|
|
2643
|
+
//#region src/modules/mikro-orm/decorators/columns/char.column.ts
|
|
2344
2644
|
/**
|
|
2345
|
-
*
|
|
2645
|
+
* CHAR 列装饰器,声明一个固定长度字符串列。
|
|
2346
2646
|
*
|
|
2347
|
-
*
|
|
2348
|
-
*
|
|
2647
|
+
* 自动应用 MikroORM `@Property({ type: 'char' })`、`@IsString()` 验证及 Swagger schema。
|
|
2648
|
+
* 当指定 `length` 时,同时校验最小和最大长度。
|
|
2349
2649
|
*
|
|
2350
|
-
* @param options - 列配置,支持 `
|
|
2650
|
+
* @param options - 列配置,支持 `length` 指定固定长度
|
|
2351
2651
|
*
|
|
2352
2652
|
* @example
|
|
2353
2653
|
* ```typescript
|
|
2354
2654
|
* @Entity()
|
|
2355
|
-
* class
|
|
2356
|
-
* @
|
|
2357
|
-
*
|
|
2358
|
-
*
|
|
2359
|
-
* @Bigint({ mode: 'number' })
|
|
2360
|
-
* amount: number
|
|
2655
|
+
* class Country {
|
|
2656
|
+
* @Char({ length: 2, comment: '国家代码' })
|
|
2657
|
+
* code: string
|
|
2361
2658
|
* }
|
|
2362
2659
|
* ```
|
|
2363
2660
|
*/
|
|
2364
|
-
function
|
|
2661
|
+
function Char(options) {
|
|
2365
2662
|
return (target, propertyKey) => {
|
|
2366
|
-
const mode = options?.mode ?? "string";
|
|
2367
2663
|
Property$1({
|
|
2368
2664
|
...options,
|
|
2369
|
-
type:
|
|
2665
|
+
type: "char"
|
|
2370
2666
|
})(target, propertyKey);
|
|
2371
|
-
|
|
2372
|
-
|
|
2667
|
+
applyDecorators(...options?.length ? [
|
|
2668
|
+
IsString(),
|
|
2669
|
+
MaxLength(options.length),
|
|
2670
|
+
MinLength(options.length)
|
|
2671
|
+
] : [IsString()], Property({
|
|
2373
2672
|
optional: options?.nullable,
|
|
2374
2673
|
lazy: options?.lazy,
|
|
2375
|
-
schema:
|
|
2674
|
+
schema: {
|
|
2376
2675
|
type: "string",
|
|
2377
|
-
|
|
2378
|
-
|
|
2379
|
-
examples: options?.examples,
|
|
2380
|
-
required: !options?.nullable
|
|
2381
|
-
} : {
|
|
2382
|
-
type: "integer",
|
|
2383
|
-
format: "int64",
|
|
2676
|
+
maxLength: options?.length,
|
|
2677
|
+
minLength: options?.length,
|
|
2384
2678
|
description: options?.comment,
|
|
2385
2679
|
example: options?.example,
|
|
2386
2680
|
examples: options?.examples,
|
|
@@ -2390,36 +2684,34 @@ function Bigint(options) {
|
|
|
2390
2684
|
};
|
|
2391
2685
|
}
|
|
2392
2686
|
//#endregion
|
|
2393
|
-
//#region src/modules/mikro-orm/decorators/columns/
|
|
2687
|
+
//#region src/modules/mikro-orm/decorators/columns/text.column.ts
|
|
2394
2688
|
/**
|
|
2395
|
-
*
|
|
2689
|
+
* TEXT 列装饰器,声明一个无长度限制的文本列。
|
|
2396
2690
|
*
|
|
2397
|
-
* 自动应用 MikroORM `@Property({ type: '
|
|
2691
|
+
* 自动应用 MikroORM `@Property({ type: 'text' })`、`@IsString()` 验证及 Swagger schema。
|
|
2398
2692
|
*
|
|
2399
2693
|
* @param options - 列配置
|
|
2400
2694
|
*
|
|
2401
2695
|
* @example
|
|
2402
2696
|
* ```typescript
|
|
2403
2697
|
* @Entity()
|
|
2404
|
-
* class
|
|
2405
|
-
* @
|
|
2406
|
-
*
|
|
2698
|
+
* class Article {
|
|
2699
|
+
* @Text({ comment: '文章内容' })
|
|
2700
|
+
* content: string
|
|
2407
2701
|
* }
|
|
2408
2702
|
* ```
|
|
2409
2703
|
*/
|
|
2410
|
-
function
|
|
2704
|
+
function Text(options) {
|
|
2411
2705
|
return (target, propertyKey) => {
|
|
2412
2706
|
Property$1({
|
|
2413
2707
|
...options,
|
|
2414
|
-
type: "
|
|
2708
|
+
type: "text"
|
|
2415
2709
|
})(target, propertyKey);
|
|
2416
|
-
applyDecorators(
|
|
2710
|
+
applyDecorators(IsString(), Property({
|
|
2417
2711
|
optional: options?.nullable,
|
|
2418
2712
|
lazy: options?.lazy,
|
|
2419
2713
|
schema: {
|
|
2420
|
-
type: "
|
|
2421
|
-
format: "double",
|
|
2422
|
-
minimum: options?.unsigned ? 0 : void 0,
|
|
2714
|
+
type: "string",
|
|
2423
2715
|
description: options?.comment,
|
|
2424
2716
|
example: options?.example,
|
|
2425
2717
|
examples: options?.examples,
|
|
@@ -2429,37 +2721,35 @@ function Double(options) {
|
|
|
2429
2721
|
};
|
|
2430
2722
|
}
|
|
2431
2723
|
//#endregion
|
|
2432
|
-
//#region src/modules/mikro-orm/decorators/columns/
|
|
2724
|
+
//#region src/modules/mikro-orm/decorators/columns/money.column.ts
|
|
2433
2725
|
/**
|
|
2434
|
-
*
|
|
2726
|
+
* MONEY 列装饰器,声明一个货币类型列。
|
|
2435
2727
|
*
|
|
2436
|
-
* 自动应用 MikroORM `@Property({ type: '
|
|
2437
|
-
* 可通过 `precision` 和 `scale` 控制精度。
|
|
2728
|
+
* 自动应用 MikroORM `@Property({ type: 'money' })`、`@IsCurrency()` 验证及 Swagger schema(format: money)。
|
|
2438
2729
|
*
|
|
2439
2730
|
* @param options - 列配置
|
|
2440
2731
|
*
|
|
2441
2732
|
* @example
|
|
2442
2733
|
* ```typescript
|
|
2443
2734
|
* @Entity()
|
|
2444
|
-
* class
|
|
2445
|
-
* @
|
|
2446
|
-
*
|
|
2735
|
+
* class Order {
|
|
2736
|
+
* @Money({ comment: '订单金额' })
|
|
2737
|
+
* totalAmount: string
|
|
2447
2738
|
* }
|
|
2448
2739
|
* ```
|
|
2449
2740
|
*/
|
|
2450
|
-
function
|
|
2741
|
+
function Money(options) {
|
|
2451
2742
|
return (target, propertyKey) => {
|
|
2452
2743
|
Property$1({
|
|
2453
2744
|
...options,
|
|
2454
|
-
type: "
|
|
2745
|
+
type: "money"
|
|
2455
2746
|
})(target, propertyKey);
|
|
2456
|
-
applyDecorators(
|
|
2747
|
+
applyDecorators(IsCurrency({ symbol: "" }), Property({
|
|
2457
2748
|
optional: options?.nullable,
|
|
2458
2749
|
lazy: options?.lazy,
|
|
2459
2750
|
schema: {
|
|
2460
|
-
type: "
|
|
2461
|
-
format: "
|
|
2462
|
-
minimum: options?.unsigned ? 0 : void 0,
|
|
2751
|
+
type: "string",
|
|
2752
|
+
format: "money",
|
|
2463
2753
|
description: options?.comment,
|
|
2464
2754
|
example: options?.example,
|
|
2465
2755
|
examples: options?.examples,
|
|
@@ -2469,35 +2759,281 @@ function Numeric(options) {
|
|
|
2469
2759
|
};
|
|
2470
2760
|
}
|
|
2471
2761
|
//#endregion
|
|
2472
|
-
//#region src/modules/mikro-orm/decorators/columns/
|
|
2762
|
+
//#region src/modules/mikro-orm/decorators/columns/int.column.ts
|
|
2473
2763
|
/**
|
|
2474
|
-
*
|
|
2764
|
+
* INT 列装饰器,声明一个 32 位整数列。
|
|
2475
2765
|
*
|
|
2476
|
-
* 自动应用 MikroORM `@Property({ type: '
|
|
2766
|
+
* 自动应用 MikroORM `@Property({ type: 'int' })`、`@IsInt()` 验证及 Swagger schema。
|
|
2477
2767
|
*
|
|
2478
|
-
* @param options -
|
|
2768
|
+
* @param options - 列配置,支持 `unsigned` 设置无符号
|
|
2479
2769
|
*
|
|
2480
2770
|
* @example
|
|
2481
2771
|
* ```typescript
|
|
2482
2772
|
* @Entity()
|
|
2483
|
-
* class
|
|
2484
|
-
* @
|
|
2485
|
-
*
|
|
2773
|
+
* class Product {
|
|
2774
|
+
* @Int({ unsigned: true, comment: '库存数量' })
|
|
2775
|
+
* stock: number
|
|
2486
2776
|
* }
|
|
2487
2777
|
* ```
|
|
2488
2778
|
*/
|
|
2489
|
-
function
|
|
2779
|
+
function Int(options) {
|
|
2490
2780
|
return (target, propertyKey) => {
|
|
2491
2781
|
Property$1({
|
|
2492
2782
|
...options,
|
|
2493
|
-
type: "
|
|
2783
|
+
type: "int"
|
|
2494
2784
|
})(target, propertyKey);
|
|
2495
|
-
applyDecorators(
|
|
2785
|
+
applyDecorators(IsInt(), Property({
|
|
2496
2786
|
optional: options?.nullable,
|
|
2497
2787
|
lazy: options?.lazy,
|
|
2498
2788
|
schema: {
|
|
2499
|
-
type: "
|
|
2500
|
-
|
|
2789
|
+
type: "integer",
|
|
2790
|
+
minimum: options?.unsigned ? 0 : -2147483647,
|
|
2791
|
+
maximum: options?.unsigned ? 4294967295 : 2147483647,
|
|
2792
|
+
description: options?.comment,
|
|
2793
|
+
example: options?.example,
|
|
2794
|
+
examples: options?.examples,
|
|
2795
|
+
required: !options?.nullable
|
|
2796
|
+
}
|
|
2797
|
+
}))(target, propertyKey);
|
|
2798
|
+
};
|
|
2799
|
+
}
|
|
2800
|
+
//#endregion
|
|
2801
|
+
//#region src/modules/mikro-orm/decorators/columns/smallint.column.ts
|
|
2802
|
+
/**
|
|
2803
|
+
* SMALLINT 列装饰器,声明一个 16 位整数列。
|
|
2804
|
+
*
|
|
2805
|
+
* 自动应用 MikroORM `@Property({ type: 'smallint' })`、`@IsInt()` 验证及 Swagger schema。
|
|
2806
|
+
*
|
|
2807
|
+
* @param options - 列配置,支持 `unsigned` 设置无符号
|
|
2808
|
+
*
|
|
2809
|
+
* @example
|
|
2810
|
+
* ```typescript
|
|
2811
|
+
* @Entity()
|
|
2812
|
+
* class Config {
|
|
2813
|
+
* @Smallint({ comment: '排序权重' })
|
|
2814
|
+
* weight: number
|
|
2815
|
+
* }
|
|
2816
|
+
* ```
|
|
2817
|
+
*/
|
|
2818
|
+
function Smallint(options) {
|
|
2819
|
+
return (target, propertyKey) => {
|
|
2820
|
+
Property$1({
|
|
2821
|
+
...options,
|
|
2822
|
+
type: "smallint"
|
|
2823
|
+
})(target, propertyKey);
|
|
2824
|
+
applyDecorators(IsInt(), Property({
|
|
2825
|
+
optional: options?.nullable,
|
|
2826
|
+
lazy: options?.lazy,
|
|
2827
|
+
schema: {
|
|
2828
|
+
type: "integer",
|
|
2829
|
+
minimum: options?.unsigned ? 0 : -32768,
|
|
2830
|
+
maximum: options?.unsigned ? 65535 : 32767,
|
|
2831
|
+
description: options?.comment,
|
|
2832
|
+
example: options?.example,
|
|
2833
|
+
examples: options?.examples,
|
|
2834
|
+
required: !options?.nullable
|
|
2835
|
+
}
|
|
2836
|
+
}))(target, propertyKey);
|
|
2837
|
+
};
|
|
2838
|
+
}
|
|
2839
|
+
//#endregion
|
|
2840
|
+
//#region src/modules/mikro-orm/decorators/columns/tinyint.column.ts
|
|
2841
|
+
/**
|
|
2842
|
+
* TINYINT 列装饰器,声明一个 8 位整数列。
|
|
2843
|
+
*
|
|
2844
|
+
* 自动应用 MikroORM `@Property({ type: 'tinyint' })`、`@IsInt()` 验证及 Swagger schema。
|
|
2845
|
+
*
|
|
2846
|
+
* @param options - 列配置,支持 `unsigned` 设置无符号
|
|
2847
|
+
*
|
|
2848
|
+
* @example
|
|
2849
|
+
* ```typescript
|
|
2850
|
+
* @Entity()
|
|
2851
|
+
* class Config {
|
|
2852
|
+
* @Tinyint({ unsigned: true, comment: '状态码' })
|
|
2853
|
+
* status: number
|
|
2854
|
+
* }
|
|
2855
|
+
* ```
|
|
2856
|
+
*/
|
|
2857
|
+
function Tinyint(options) {
|
|
2858
|
+
return (target, propertyKey) => {
|
|
2859
|
+
Property$1({
|
|
2860
|
+
...options,
|
|
2861
|
+
type: "tinyint"
|
|
2862
|
+
})(target, propertyKey);
|
|
2863
|
+
applyDecorators(IsInt(), Property({
|
|
2864
|
+
optional: options?.nullable,
|
|
2865
|
+
lazy: options?.lazy,
|
|
2866
|
+
schema: {
|
|
2867
|
+
type: "integer",
|
|
2868
|
+
minimum: options?.unsigned ? 0 : -128,
|
|
2869
|
+
maximum: options?.unsigned ? 255 : 127,
|
|
2870
|
+
description: options?.comment,
|
|
2871
|
+
example: options?.example,
|
|
2872
|
+
examples: options?.examples,
|
|
2873
|
+
required: !options?.nullable
|
|
2874
|
+
}
|
|
2875
|
+
}))(target, propertyKey);
|
|
2876
|
+
};
|
|
2877
|
+
}
|
|
2878
|
+
//#endregion
|
|
2879
|
+
//#region src/modules/mikro-orm/decorators/columns/bigint.column.ts
|
|
2880
|
+
/**
|
|
2881
|
+
* BIGINT 列装饰器,声明一个 64 位整数列。
|
|
2882
|
+
*
|
|
2883
|
+
* 默认以字符串模式(`mode: 'string'`)映射以避免 JavaScript 精度丢失,
|
|
2884
|
+
* 可设置 `mode: 'number'` 以数值模式映射。
|
|
2885
|
+
*
|
|
2886
|
+
* @param options - 列配置,支持 `mode` 切换映射模式
|
|
2887
|
+
*
|
|
2888
|
+
* @example
|
|
2889
|
+
* ```typescript
|
|
2890
|
+
* @Entity()
|
|
2891
|
+
* class Order {
|
|
2892
|
+
* @Bigint({ comment: '订单号' })
|
|
2893
|
+
* orderNo: string
|
|
2894
|
+
*
|
|
2895
|
+
* @Bigint({ mode: 'number' })
|
|
2896
|
+
* amount: number
|
|
2897
|
+
* }
|
|
2898
|
+
* ```
|
|
2899
|
+
*/
|
|
2900
|
+
function Bigint(options) {
|
|
2901
|
+
return (target, propertyKey) => {
|
|
2902
|
+
const mode = options?.mode ?? "string";
|
|
2903
|
+
Property$1({
|
|
2904
|
+
...options,
|
|
2905
|
+
type: mode === "string" ? new BigIntType("string") : new BigIntType("number")
|
|
2906
|
+
})(target, propertyKey);
|
|
2907
|
+
const isStringMode = mode === "string";
|
|
2908
|
+
applyDecorators(...isStringMode ? [IsString()] : [IsInt()], Property({
|
|
2909
|
+
optional: options?.nullable,
|
|
2910
|
+
lazy: options?.lazy,
|
|
2911
|
+
schema: isStringMode ? {
|
|
2912
|
+
type: "string",
|
|
2913
|
+
description: options?.comment,
|
|
2914
|
+
example: options?.example,
|
|
2915
|
+
examples: options?.examples,
|
|
2916
|
+
required: !options?.nullable
|
|
2917
|
+
} : {
|
|
2918
|
+
type: "integer",
|
|
2919
|
+
format: "int64",
|
|
2920
|
+
description: options?.comment,
|
|
2921
|
+
example: options?.example,
|
|
2922
|
+
examples: options?.examples,
|
|
2923
|
+
required: !options?.nullable
|
|
2924
|
+
}
|
|
2925
|
+
}))(target, propertyKey);
|
|
2926
|
+
};
|
|
2927
|
+
}
|
|
2928
|
+
//#endregion
|
|
2929
|
+
//#region src/modules/mikro-orm/decorators/columns/double.column.ts
|
|
2930
|
+
/**
|
|
2931
|
+
* DOUBLE 列装饰器,声明一个双精度浮点数列。
|
|
2932
|
+
*
|
|
2933
|
+
* 自动应用 MikroORM `@Property({ type: 'double' })`、`@IsNumber()` 验证及 Swagger schema。
|
|
2934
|
+
*
|
|
2935
|
+
* @param options - 列配置
|
|
2936
|
+
*
|
|
2937
|
+
* @example
|
|
2938
|
+
* ```typescript
|
|
2939
|
+
* @Entity()
|
|
2940
|
+
* class Location {
|
|
2941
|
+
* @Double({ comment: '经度' })
|
|
2942
|
+
* longitude: number
|
|
2943
|
+
* }
|
|
2944
|
+
* ```
|
|
2945
|
+
*/
|
|
2946
|
+
function Double(options) {
|
|
2947
|
+
return (target, propertyKey) => {
|
|
2948
|
+
Property$1({
|
|
2949
|
+
...options,
|
|
2950
|
+
type: "double"
|
|
2951
|
+
})(target, propertyKey);
|
|
2952
|
+
applyDecorators(IsNumber(), Property({
|
|
2953
|
+
optional: options?.nullable,
|
|
2954
|
+
lazy: options?.lazy,
|
|
2955
|
+
schema: {
|
|
2956
|
+
type: "number",
|
|
2957
|
+
format: "double",
|
|
2958
|
+
minimum: options?.unsigned ? 0 : void 0,
|
|
2959
|
+
description: options?.comment,
|
|
2960
|
+
example: options?.example,
|
|
2961
|
+
examples: options?.examples,
|
|
2962
|
+
required: !options?.nullable
|
|
2963
|
+
}
|
|
2964
|
+
}))(target, propertyKey);
|
|
2965
|
+
};
|
|
2966
|
+
}
|
|
2967
|
+
//#endregion
|
|
2968
|
+
//#region src/modules/mikro-orm/decorators/columns/numeric.column.ts
|
|
2969
|
+
/**
|
|
2970
|
+
* NUMERIC 列装饰器,声明一个精确数值列,适用于需要精确小数的场景(如金额计算)。
|
|
2971
|
+
*
|
|
2972
|
+
* 自动应用 MikroORM `@Property({ type: 'numeric' })`、`@IsNumber()` 验证及 Swagger schema。
|
|
2973
|
+
* 可通过 `precision` 和 `scale` 控制精度。
|
|
2974
|
+
*
|
|
2975
|
+
* @param options - 列配置
|
|
2976
|
+
*
|
|
2977
|
+
* @example
|
|
2978
|
+
* ```typescript
|
|
2979
|
+
* @Entity()
|
|
2980
|
+
* class Product {
|
|
2981
|
+
* @Numeric({ precision: 10, scale: 2, comment: '价格' })
|
|
2982
|
+
* price: number
|
|
2983
|
+
* }
|
|
2984
|
+
* ```
|
|
2985
|
+
*/
|
|
2986
|
+
function Numeric(options) {
|
|
2987
|
+
return (target, propertyKey) => {
|
|
2988
|
+
Property$1({
|
|
2989
|
+
...options,
|
|
2990
|
+
type: "numeric"
|
|
2991
|
+
})(target, propertyKey);
|
|
2992
|
+
applyDecorators(options?.scale ? IsNumber({ maxDecimalPlaces: options.scale }) : IsNumber(), Property({
|
|
2993
|
+
optional: options?.nullable,
|
|
2994
|
+
lazy: options?.lazy,
|
|
2995
|
+
schema: {
|
|
2996
|
+
type: "number",
|
|
2997
|
+
format: "double",
|
|
2998
|
+
minimum: options?.unsigned ? 0 : void 0,
|
|
2999
|
+
description: options?.comment,
|
|
3000
|
+
example: options?.example,
|
|
3001
|
+
examples: options?.examples,
|
|
3002
|
+
required: !options?.nullable
|
|
3003
|
+
}
|
|
3004
|
+
}))(target, propertyKey);
|
|
3005
|
+
};
|
|
3006
|
+
}
|
|
3007
|
+
//#endregion
|
|
3008
|
+
//#region src/modules/mikro-orm/decorators/columns/boolean.column.ts
|
|
3009
|
+
/**
|
|
3010
|
+
* BOOLEAN 列装饰器,声明一个布尔类型列。
|
|
3011
|
+
*
|
|
3012
|
+
* 自动应用 MikroORM `@Property({ type: 'boolean' })`、`@IsBoolean()` 验证及 Swagger schema。
|
|
3013
|
+
*
|
|
3014
|
+
* @param options - 列配置
|
|
3015
|
+
*
|
|
3016
|
+
* @example
|
|
3017
|
+
* ```typescript
|
|
3018
|
+
* @Entity()
|
|
3019
|
+
* class User {
|
|
3020
|
+
* @Boolean({ comment: '是否激活' })
|
|
3021
|
+
* isActive: boolean
|
|
3022
|
+
* }
|
|
3023
|
+
* ```
|
|
3024
|
+
*/
|
|
3025
|
+
function Boolean$1(options) {
|
|
3026
|
+
return (target, propertyKey) => {
|
|
3027
|
+
Property$1({
|
|
3028
|
+
...options,
|
|
3029
|
+
type: "boolean"
|
|
3030
|
+
})(target, propertyKey);
|
|
3031
|
+
applyDecorators(IsBoolean(), Property({
|
|
3032
|
+
optional: options?.nullable,
|
|
3033
|
+
lazy: options?.lazy,
|
|
3034
|
+
schema: {
|
|
3035
|
+
type: "boolean",
|
|
3036
|
+
description: options?.comment,
|
|
2501
3037
|
example: options?.example,
|
|
2502
3038
|
examples: options?.examples,
|
|
2503
3039
|
required: !options?.nullable
|
|
@@ -2842,7 +3378,7 @@ const Column = {
|
|
|
2842
3378
|
};
|
|
2843
3379
|
//#endregion
|
|
2844
3380
|
//#region src/modules/mikro-orm/models/timestamped-entity.ts
|
|
2845
|
-
var _ref$
|
|
3381
|
+
var _ref$12, _ref2$3;
|
|
2846
3382
|
var TimestampedEntity = class {
|
|
2847
3383
|
[OptionalProps];
|
|
2848
3384
|
createdAt = /* @__PURE__ */ new Date();
|
|
@@ -2852,12 +3388,12 @@ __decorate([Column.Timestamptz({
|
|
|
2852
3388
|
onCreate: () => /* @__PURE__ */ new Date(),
|
|
2853
3389
|
defaultRaw: "CURRENT_TIMESTAMP",
|
|
2854
3390
|
comment: "创建时间"
|
|
2855
|
-
}), __decorateMetadata("design:type", typeof (_ref$
|
|
3391
|
+
}), __decorateMetadata("design:type", typeof (_ref$12 = typeof Date !== "undefined" && Date) === "function" ? _ref$12 : Object)], TimestampedEntity.prototype, "createdAt", void 0);
|
|
2856
3392
|
__decorate([Column.Timestamptz({
|
|
2857
3393
|
onUpdate: () => /* @__PURE__ */ new Date(),
|
|
2858
3394
|
defaultRaw: "CURRENT_TIMESTAMP",
|
|
2859
3395
|
comment: "更新时间"
|
|
2860
|
-
}), __decorateMetadata("design:type", typeof (_ref2$
|
|
3396
|
+
}), __decorateMetadata("design:type", typeof (_ref2$3 = typeof Date !== "undefined" && Date) === "function" ? _ref2$3 : Object)], TimestampedEntity.prototype, "updatedAt", void 0);
|
|
2861
3397
|
//#endregion
|
|
2862
3398
|
//#region src/modules/mikro-orm/models/linear-entity.ts
|
|
2863
3399
|
var LinearEntity = class extends TimestampedEntity {
|
|
@@ -2968,36 +3504,6 @@ function EntityDto(classRef) {
|
|
|
2968
3504
|
return EntityDtoClass;
|
|
2969
3505
|
}
|
|
2970
3506
|
//#endregion
|
|
2971
|
-
//#region src/modules/mikro-orm/utils/mikro-orm-utils.ts
|
|
2972
|
-
function getMetadata(classRef) {
|
|
2973
|
-
const metadatas = [];
|
|
2974
|
-
let parent = classRef;
|
|
2975
|
-
do {
|
|
2976
|
-
const meta = MetadataStorage.getMetadata(parent.name, parent[MetadataStorage.PATH_SYMBOL]);
|
|
2977
|
-
if (meta instanceof EntityMetadata) metadatas.push(meta);
|
|
2978
|
-
parent = Object.getPrototypeOf(parent);
|
|
2979
|
-
} while (parent && parent !== Object.prototype);
|
|
2980
|
-
return R.unnest(metadatas.map((meta) => R.values(meta.properties)));
|
|
2981
|
-
}
|
|
2982
|
-
//#endregion
|
|
2983
|
-
//#region src/modules/mikro-orm/converters/entity-primary-key-type/entity-primary-key-type.ts
|
|
2984
|
-
const PrimaryKeyTypeClassMetadataPropertyKey = Symbol("PrimaryKeyTypeClassMetadataPropertyKey");
|
|
2985
|
-
const storage = /* @__PURE__ */ new WeakMap();
|
|
2986
|
-
function PrimaryKeyType(classRef) {
|
|
2987
|
-
if (storage.has(classRef)) return storage.get(classRef);
|
|
2988
|
-
const primaryProperties = getMetadata(classRef).filter((prop) => prop.primary).map((prop) => prop.name);
|
|
2989
|
-
if (!primaryProperties.length) throw new Error(`Cannot create PrimaryKeyType for ${classRef.name} because it has no primary properties.`);
|
|
2990
|
-
class PrimaryKeyTypeClass {}
|
|
2991
|
-
Object.defineProperty(PrimaryKeyTypeClass, "name", { value: `${classRef.name}PrimaryKeyType` });
|
|
2992
|
-
PrimaryKeyTypeClass[PrimaryKeyTypeClassMetadataPropertyKey] = classRef;
|
|
2993
|
-
cloneMetadata(PrimaryKeyTypeClass, classRef, primaryProperties);
|
|
2994
|
-
inheritValidationMetadata(classRef, PrimaryKeyTypeClass, (key) => primaryProperties.includes(key));
|
|
2995
|
-
inheritTransformationMetadata(classRef, PrimaryKeyTypeClass, (key) => primaryProperties.includes(key));
|
|
2996
|
-
for (const propertyName of primaryProperties) ModelRegister.copyProperty(classRef, PrimaryKeyTypeClass, propertyName);
|
|
2997
|
-
storage.set(classRef, PrimaryKeyTypeClass);
|
|
2998
|
-
return PrimaryKeyTypeClass;
|
|
2999
|
-
}
|
|
3000
|
-
//#endregion
|
|
3001
3507
|
//#region src/modules/mikro-orm/decorators/cardinality/_resolve-entity-class.ts
|
|
3002
3508
|
/**
|
|
3003
3509
|
* 将 `EntityName<T>` 解析为类引用(`Type`)。
|
|
@@ -3317,490 +3823,6 @@ __decorate([
|
|
|
3317
3823
|
__decorateMetadata("design:type", Number)
|
|
3318
3824
|
], DatabaseConfig.prototype, "poolIdleTimeoutMillis", void 0);
|
|
3319
3825
|
//#endregion
|
|
3320
|
-
//#region src/modules/core/pipes/validation.pipe.ts
|
|
3321
|
-
var _ref$13, _ref2$3, _ref3$1, _BukaValidationPipe;
|
|
3322
|
-
function deepMap(obj, fn) {
|
|
3323
|
-
if (Array.isArray(obj)) return obj.map((item) => deepMap(item, fn));
|
|
3324
|
-
else return fn(obj);
|
|
3325
|
-
}
|
|
3326
|
-
function transformReference(em, value, metatype) {
|
|
3327
|
-
if (typeof value !== "object" || value === null) return value;
|
|
3328
|
-
const primaryProperties = getMetadata(metatype).filter((p) => p.primary);
|
|
3329
|
-
if (primaryProperties.length === 1) {
|
|
3330
|
-
const propertyKey = primaryProperties[0].name;
|
|
3331
|
-
return em.getReference(metatype, value[propertyKey], { wrapped: true });
|
|
3332
|
-
} else if (primaryProperties.length > 1) {
|
|
3333
|
-
const propertyKeys = primaryProperties.map((p) => p.name);
|
|
3334
|
-
return em.getReference(metatype, propertyKeys.map((key) => value[key]), { wrapped: true });
|
|
3335
|
-
}
|
|
3336
|
-
return value;
|
|
3337
|
-
}
|
|
3338
|
-
function deepTransform(em, value, metatype) {
|
|
3339
|
-
if (typeof value !== "object" || value === null) return value;
|
|
3340
|
-
const propertyKeys = ModelRegister.getModelPropertyKeys(metatype);
|
|
3341
|
-
const result = { ...value };
|
|
3342
|
-
for (const propertyKey of propertyKeys) {
|
|
3343
|
-
const propertyMetadata = ModelRegister.getProperty(metatype, propertyKey);
|
|
3344
|
-
if (!propertyMetadata) continue;
|
|
3345
|
-
if (!(propertyKey in value)) continue;
|
|
3346
|
-
const propertyValue = value[propertyKey];
|
|
3347
|
-
if (propertyMetadata.kind === "composite") {
|
|
3348
|
-
const ctor = propertyMetadata.type();
|
|
3349
|
-
if (PrimaryKeyTypeClassMetadataPropertyKey in ctor) {
|
|
3350
|
-
const originalEntity = ctor[PrimaryKeyTypeClassMetadataPropertyKey];
|
|
3351
|
-
result[propertyKey] = transformReference(em, propertyValue, originalEntity);
|
|
3352
|
-
} else result[propertyKey] = deepTransform(em, propertyValue, ctor);
|
|
3353
|
-
} else if (propertyMetadata.kind === "list") {
|
|
3354
|
-
if (propertyMetadata.type && !isScalarClass(propertyMetadata.type)) result[propertyKey] = deepMap(propertyValue, (item) => deepTransform(em, item, propertyMetadata.type()));
|
|
3355
|
-
} else if (propertyMetadata.kind === "dictionary") {
|
|
3356
|
-
if (propertyMetadata.type && !isScalarClass(propertyMetadata.type)) if (propertyValue instanceof Map) result[propertyKey] = new Map(Array.from(propertyValue.entries()).map(([k, v]) => [k, deepTransform(em, v, propertyMetadata.type())]));
|
|
3357
|
-
else result[propertyKey] = Object.fromEntries(Object.entries(propertyValue).map(([k, v]) => [k, deepTransform(em, v, propertyMetadata.type())]));
|
|
3358
|
-
}
|
|
3359
|
-
}
|
|
3360
|
-
return result;
|
|
3361
|
-
}
|
|
3362
|
-
let BukaValidationPipe = _BukaValidationPipe = class BukaValidationPipe extends ValidationPipe {
|
|
3363
|
-
logger = new Logger$1(_BukaValidationPipe.name);
|
|
3364
|
-
validationOptions;
|
|
3365
|
-
constructor(orm, em, options) {
|
|
3366
|
-
super(options);
|
|
3367
|
-
this.orm = orm;
|
|
3368
|
-
this.em = em;
|
|
3369
|
-
this.validationOptions = options;
|
|
3370
|
-
}
|
|
3371
|
-
async transform(value, metadata) {
|
|
3372
|
-
value = await super.transform(value, metadata);
|
|
3373
|
-
if (metadata.type === "custom") return value;
|
|
3374
|
-
if (this.validationOptions && !this.validationOptions.transform) return value;
|
|
3375
|
-
if (!metadata.metatype || typeof metadata.metatype !== "function") return value;
|
|
3376
|
-
const metatype = metadata.metatype;
|
|
3377
|
-
return await deepTransform(this.em, value, metatype);
|
|
3378
|
-
}
|
|
3379
|
-
};
|
|
3380
|
-
BukaValidationPipe = _BukaValidationPipe = __decorate([Injectable(), __decorateMetadata("design:paramtypes", [
|
|
3381
|
-
typeof (_ref$13 = typeof MikroORM !== "undefined" && MikroORM) === "function" ? _ref$13 : Object,
|
|
3382
|
-
typeof (_ref2$3 = typeof EntityManager !== "undefined" && EntityManager) === "function" ? _ref2$3 : Object,
|
|
3383
|
-
typeof (_ref3$1 = typeof ValidationPipeOptions !== "undefined" && ValidationPipeOptions) === "function" ? _ref3$1 : Object
|
|
3384
|
-
])], BukaValidationPipe);
|
|
3385
|
-
//#endregion
|
|
3386
|
-
//#region src/modules/core/buka.module-definition.ts
|
|
3387
|
-
const { ConfigurableModuleClass: BukaConfigurableModuleClass, MODULE_OPTIONS_TOKEN: BUKA_MODULE_OPTIONS_TOKEN } = new ConfigurableModuleBuilder().build();
|
|
3388
|
-
//#endregion
|
|
3389
|
-
//#region src/modules/core/buka.module.ts
|
|
3390
|
-
let BukaModule = class BukaModule extends BukaConfigurableModuleClass {};
|
|
3391
|
-
BukaModule = __decorate([Global(), Module({ providers: [{
|
|
3392
|
-
provide: APP_PIPE,
|
|
3393
|
-
inject: [
|
|
3394
|
-
BUKA_MODULE_OPTIONS_TOKEN,
|
|
3395
|
-
MikroORM,
|
|
3396
|
-
EntityManager
|
|
3397
|
-
],
|
|
3398
|
-
useFactory: (options, orm, em) => {
|
|
3399
|
-
return new BukaValidationPipe(orm, em, options?.validation);
|
|
3400
|
-
}
|
|
3401
|
-
}] })], BukaModule);
|
|
3402
|
-
//#endregion
|
|
3403
|
-
//#region src/modules/core/models/offset-pagination.ts
|
|
3404
|
-
let OffsetPagination = class OffsetPagination {
|
|
3405
|
-
total;
|
|
3406
|
-
limit;
|
|
3407
|
-
offset;
|
|
3408
|
-
constructor(total, parameters) {
|
|
3409
|
-
this.total = total;
|
|
3410
|
-
this.limit = parameters.limit;
|
|
3411
|
-
this.offset = parameters.offset;
|
|
3412
|
-
}
|
|
3413
|
-
};
|
|
3414
|
-
__decorate([Property({ schema: {
|
|
3415
|
-
type: "number",
|
|
3416
|
-
description: "总数"
|
|
3417
|
-
} }), __decorateMetadata("design:type", Number)], OffsetPagination.prototype, "total", void 0);
|
|
3418
|
-
__decorate([Property({ schema: {
|
|
3419
|
-
type: "number",
|
|
3420
|
-
description: "每页数量"
|
|
3421
|
-
} }), __decorateMetadata("design:type", Number)], OffsetPagination.prototype, "limit", void 0);
|
|
3422
|
-
__decorate([Property({ schema: {
|
|
3423
|
-
type: "number",
|
|
3424
|
-
description: "偏移量"
|
|
3425
|
-
} }), __decorateMetadata("design:type", Number)], OffsetPagination.prototype, "offset", void 0);
|
|
3426
|
-
OffsetPagination = __decorate([Model({ schema: {
|
|
3427
|
-
description: "基于偏移量的分页响应,通过 offset + limit 定位数据位置,适用于传统翻页场景",
|
|
3428
|
-
additionalProperties: false
|
|
3429
|
-
} }), __decorateMetadata("design:paramtypes", [Number, Object])], OffsetPagination);
|
|
3430
|
-
//#endregion
|
|
3431
|
-
//#region src/modules/core/models/cursor-pagination.ts
|
|
3432
|
-
var _ref$12;
|
|
3433
|
-
let CursorPagination = class CursorPagination {
|
|
3434
|
-
total;
|
|
3435
|
-
limit;
|
|
3436
|
-
startCursor;
|
|
3437
|
-
endCursor;
|
|
3438
|
-
hasNextPage;
|
|
3439
|
-
hasPrevPage;
|
|
3440
|
-
constructor(cursor) {
|
|
3441
|
-
this.total = cursor.totalCount;
|
|
3442
|
-
this.limit = cursor.length;
|
|
3443
|
-
this.startCursor = cursor.startCursor;
|
|
3444
|
-
this.endCursor = cursor.endCursor;
|
|
3445
|
-
this.hasNextPage = cursor.hasNextPage;
|
|
3446
|
-
this.hasPrevPage = cursor.hasPrevPage;
|
|
3447
|
-
}
|
|
3448
|
-
};
|
|
3449
|
-
__decorate([Property({ schema: {
|
|
3450
|
-
type: "number",
|
|
3451
|
-
description: "总数"
|
|
3452
|
-
} }), __decorateMetadata("design:type", Number)], CursorPagination.prototype, "total", void 0);
|
|
3453
|
-
__decorate([Property({ schema: {
|
|
3454
|
-
type: "number",
|
|
3455
|
-
description: "每页数量"
|
|
3456
|
-
} }), __decorateMetadata("design:type", Number)], CursorPagination.prototype, "limit", void 0);
|
|
3457
|
-
__decorate([Property({ schema: {
|
|
3458
|
-
type: "string",
|
|
3459
|
-
description: "开始游标",
|
|
3460
|
-
nullable: true
|
|
3461
|
-
} }), __decorateMetadata("design:type", Object)], CursorPagination.prototype, "startCursor", void 0);
|
|
3462
|
-
__decorate([Property({ schema: {
|
|
3463
|
-
type: "string",
|
|
3464
|
-
description: "结束游标",
|
|
3465
|
-
nullable: true
|
|
3466
|
-
} }), __decorateMetadata("design:type", Object)], CursorPagination.prototype, "endCursor", void 0);
|
|
3467
|
-
__decorate([Property({ schema: {
|
|
3468
|
-
type: "boolean",
|
|
3469
|
-
description: "是否有下一页"
|
|
3470
|
-
} }), __decorateMetadata("design:type", Boolean)], CursorPagination.prototype, "hasNextPage", void 0);
|
|
3471
|
-
__decorate([Property({ schema: {
|
|
3472
|
-
type: "boolean",
|
|
3473
|
-
description: "是否有上一页"
|
|
3474
|
-
} }), __decorateMetadata("design:type", Boolean)], CursorPagination.prototype, "hasPrevPage", void 0);
|
|
3475
|
-
CursorPagination = __decorate([Model({ schema: {
|
|
3476
|
-
description: "基于游标的分页响应,通过 startCursor/endCursor 定位数据位置,适用于无限滚动等场景",
|
|
3477
|
-
additionalProperties: false
|
|
3478
|
-
} }), __decorateMetadata("design:paramtypes", [typeof (_ref$12 = typeof Cursor !== "undefined" && Cursor) === "function" ? _ref$12 : Object])], CursorPagination);
|
|
3479
|
-
//#endregion
|
|
3480
|
-
//#region src/modules/core/models/slice.ts
|
|
3481
|
-
var Slice = class Slice {
|
|
3482
|
-
data;
|
|
3483
|
-
pagination;
|
|
3484
|
-
constructor(data, pagination) {
|
|
3485
|
-
this.data = data;
|
|
3486
|
-
this.pagination = pagination;
|
|
3487
|
-
}
|
|
3488
|
-
*[Symbol.iterator]() {
|
|
3489
|
-
for (const item of this.data) yield item;
|
|
3490
|
-
}
|
|
3491
|
-
static fromOffset(data, total, parameters) {
|
|
3492
|
-
return new Slice(data, new OffsetPagination(total, parameters));
|
|
3493
|
-
}
|
|
3494
|
-
static fromCursor(cursor) {
|
|
3495
|
-
const pagination = new CursorPagination(cursor);
|
|
3496
|
-
return new Slice(cursor.items, pagination);
|
|
3497
|
-
}
|
|
3498
|
-
map(fn) {
|
|
3499
|
-
return new Slice(this.data.map(fn), this.pagination);
|
|
3500
|
-
}
|
|
3501
|
-
};
|
|
3502
|
-
//#endregion
|
|
3503
|
-
//#region src/modules/core/converters/pick-type/pick-type.ts
|
|
3504
|
-
/**
|
|
3505
|
-
* 从模型中选取指定属性,生成新的子集类型。
|
|
3506
|
-
*
|
|
3507
|
-
* 类似 TypeScript 的 `Pick<T, K>`,同时保留 class-validator、class-transformer 和 Swagger 的元数据。
|
|
3508
|
-
*
|
|
3509
|
-
* @param classRef - 使用 `@Model()` 标注的类引用
|
|
3510
|
-
* @param keys - 需要选取的属性名数组
|
|
3511
|
-
* @returns 仅包含指定属性的新类
|
|
3512
|
-
*
|
|
3513
|
-
* @example
|
|
3514
|
-
* ```typescript
|
|
3515
|
-
* const CreateUserDTO = PickType(UserDTO, ['name', 'email'])
|
|
3516
|
-
* ```
|
|
3517
|
-
*/
|
|
3518
|
-
function PickType(classRef, keys) {
|
|
3519
|
-
if (!ModelRegister.isModel(classRef)) throw new TypeError("PickType only accepts a class annotated with @Model().");
|
|
3520
|
-
const PickTypeClass = PickType$1(classRef, keys);
|
|
3521
|
-
for (const key of keys) {
|
|
3522
|
-
ModelRegister.copyProperty(classRef, PickTypeClass, key);
|
|
3523
|
-
const metadataKeys = Reflect.getMetadataKeys(classRef.prototype, key);
|
|
3524
|
-
for (const metadataKey of metadataKeys) {
|
|
3525
|
-
const metadata = Reflect.getMetadata(metadataKey, classRef.prototype, key);
|
|
3526
|
-
if (Reflect.hasMetadata(metadataKey, PickTypeClass.prototype, key)) continue;
|
|
3527
|
-
Reflect.defineMetadata(metadataKey, metadata, PickTypeClass.prototype, key);
|
|
3528
|
-
}
|
|
3529
|
-
}
|
|
3530
|
-
return PickTypeClass;
|
|
3531
|
-
}
|
|
3532
|
-
//#endregion
|
|
3533
|
-
//#region src/modules/core/converters/omit-type/omit-type.ts
|
|
3534
|
-
/**
|
|
3535
|
-
* 从模型中排除指定属性,生成新的类型。
|
|
3536
|
-
*
|
|
3537
|
-
* 类似 TypeScript 的 `Omit<T, K>`,同时保留 class-validator、class-transformer 和 Swagger 的元数据。
|
|
3538
|
-
*
|
|
3539
|
-
* @param classRef - 使用 `@Model()` 标注的类引用
|
|
3540
|
-
* @param keys - 需要排除的属性名数组
|
|
3541
|
-
* @returns 排除了指定属性的新类
|
|
3542
|
-
*
|
|
3543
|
-
* @example
|
|
3544
|
-
* ```typescript
|
|
3545
|
-
* const UpdateUserDTO = OmitType(UserDTO, ['id', 'createdAt'])
|
|
3546
|
-
* ```
|
|
3547
|
-
*/
|
|
3548
|
-
function OmitType(classRef, keys) {
|
|
3549
|
-
if (!ModelRegister.isModel(classRef)) throw new TypeError("OmitType only accepts a class annotated with @Model().");
|
|
3550
|
-
const OmitTypeClass = OmitType$1(classRef, keys);
|
|
3551
|
-
for (const propertyKey of ModelRegister.getModelPropertyKeys(classRef)) {
|
|
3552
|
-
if (keys.includes(propertyKey)) continue;
|
|
3553
|
-
ModelRegister.copyProperty(classRef, OmitTypeClass, propertyKey);
|
|
3554
|
-
const metadataKeys = Reflect.getMetadataKeys(classRef.prototype, propertyKey);
|
|
3555
|
-
for (const metadataKey of metadataKeys) {
|
|
3556
|
-
const metadata = Reflect.getMetadata(metadataKey, classRef.prototype, propertyKey);
|
|
3557
|
-
if (Reflect.hasMetadata(metadataKey, OmitTypeClass.prototype, propertyKey)) continue;
|
|
3558
|
-
Reflect.defineMetadata(metadataKey, metadata, OmitTypeClass.prototype, propertyKey);
|
|
3559
|
-
}
|
|
3560
|
-
}
|
|
3561
|
-
return OmitTypeClass;
|
|
3562
|
-
}
|
|
3563
|
-
//#endregion
|
|
3564
|
-
//#region src/modules/core/converters/partial-type/partial-type.ts
|
|
3565
|
-
/**
|
|
3566
|
-
* 将模型的所有属性变为可选,生成新的 Partial 类型。
|
|
3567
|
-
*
|
|
3568
|
-
* 类似 TypeScript 的 `Partial<T>`,同时保留 class-validator、class-transformer 和 Swagger 的元数据。
|
|
3569
|
-
*
|
|
3570
|
-
* @param classRef - 使用 `@Model()` 标注的类引用
|
|
3571
|
-
* @returns 所有属性均为可选的新类
|
|
3572
|
-
*
|
|
3573
|
-
* @example
|
|
3574
|
-
* ```typescript
|
|
3575
|
-
* const PatchUserDTO = PartialType(UserDTO)
|
|
3576
|
-
* ```
|
|
3577
|
-
*/
|
|
3578
|
-
function PartialType(classRef) {
|
|
3579
|
-
const PartialTypeClass = PartialType$1(classRef);
|
|
3580
|
-
for (const propertyKey of ModelRegister.getModelPropertyKeys(classRef)) {
|
|
3581
|
-
ModelRegister.copyProperty(classRef, PartialTypeClass, propertyKey);
|
|
3582
|
-
const propertyMetadata = ModelRegister.getProperty(PartialTypeClass, propertyKey);
|
|
3583
|
-
if (propertyMetadata) ModelRegister.setProperty(PartialTypeClass, propertyKey, {
|
|
3584
|
-
...propertyMetadata,
|
|
3585
|
-
optional: true
|
|
3586
|
-
});
|
|
3587
|
-
const metadataKeys = Reflect.getMetadataKeys(classRef.prototype, propertyKey);
|
|
3588
|
-
for (const metadataKey of metadataKeys) {
|
|
3589
|
-
const metadata = Reflect.getMetadata(metadataKey, classRef.prototype, propertyKey);
|
|
3590
|
-
if (Reflect.hasMetadata(metadataKey, PartialTypeClass.prototype, propertyKey)) continue;
|
|
3591
|
-
Reflect.defineMetadata(metadataKey, metadata, PartialTypeClass.prototype, propertyKey);
|
|
3592
|
-
}
|
|
3593
|
-
}
|
|
3594
|
-
return PartialTypeClass;
|
|
3595
|
-
}
|
|
3596
|
-
//#endregion
|
|
3597
|
-
//#region src/modules/core/converters/intersection-type/intersection-type.ts
|
|
3598
|
-
/**
|
|
3599
|
-
* 将多个模型类合并为一个交叉类型。
|
|
3600
|
-
*
|
|
3601
|
-
* 类似 TypeScript 的交叉类型 `A & B`,同时合并 class-validator、class-transformer 和 Swagger 的元数据。
|
|
3602
|
-
*
|
|
3603
|
-
* @param classRefs - 需要合并的模型类引用列表
|
|
3604
|
-
* @returns 包含所有类属性的新类
|
|
3605
|
-
*
|
|
3606
|
-
* @example
|
|
3607
|
-
* ```typescript
|
|
3608
|
-
* const CreateUserDTO = IntersectionType(UserBaseDTO, UserProfileDTO)
|
|
3609
|
-
* ```
|
|
3610
|
-
*/
|
|
3611
|
-
function IntersectionType(...classRefs) {
|
|
3612
|
-
const IntersectionTypeClass = IntersectionType$1(...classRefs);
|
|
3613
|
-
for (const classRef of classRefs) for (const propertyKey of ModelRegister.getModelPropertyKeys(classRef)) {
|
|
3614
|
-
ModelRegister.copyProperty(classRef, IntersectionTypeClass, propertyKey);
|
|
3615
|
-
const metadataKeys = Reflect.getMetadataKeys(classRef.prototype, propertyKey);
|
|
3616
|
-
for (const metadataKey of metadataKeys) {
|
|
3617
|
-
const metadata = Reflect.getMetadata(metadataKey, classRef.prototype, propertyKey);
|
|
3618
|
-
if (Reflect.hasMetadata(metadataKey, IntersectionTypeClass.prototype, propertyKey)) continue;
|
|
3619
|
-
Reflect.defineMetadata(metadataKey, metadata, IntersectionTypeClass.prototype, propertyKey);
|
|
3620
|
-
}
|
|
3621
|
-
}
|
|
3622
|
-
return IntersectionTypeClass;
|
|
3623
|
-
}
|
|
3624
|
-
//#endregion
|
|
3625
|
-
//#region src/modules/core/converters/order-query-type/get-order-query-schema.ts
|
|
3626
|
-
function getOrderQueryEnum() {
|
|
3627
|
-
return {
|
|
3628
|
-
type: "string",
|
|
3629
|
-
enum: [
|
|
3630
|
-
"asc",
|
|
3631
|
-
"asc nulls last",
|
|
3632
|
-
"asc nulls first",
|
|
3633
|
-
"desc",
|
|
3634
|
-
"desc nulls last",
|
|
3635
|
-
"desc nulls first"
|
|
3636
|
-
]
|
|
3637
|
-
};
|
|
3638
|
-
}
|
|
3639
|
-
function getOrderQueryMapSchema(classRef) {
|
|
3640
|
-
const properties = {};
|
|
3641
|
-
const schema = {
|
|
3642
|
-
type: "object",
|
|
3643
|
-
properties,
|
|
3644
|
-
required: []
|
|
3645
|
-
};
|
|
3646
|
-
for (const propertyKey of ModelRegister.getModelPropertyKeys(classRef)) {
|
|
3647
|
-
const propertyMetadata = ModelRegister.getProperty(classRef, propertyKey);
|
|
3648
|
-
if (!!propertyMetadata?.association) properties[propertyKey] = getOrderQueryMapSchema(propertyMetadata.association.type());
|
|
3649
|
-
else properties[propertyKey] = getOrderQueryEnum();
|
|
3650
|
-
}
|
|
3651
|
-
return schema;
|
|
3652
|
-
}
|
|
3653
|
-
function getOrderQuerySchema(classRef) {
|
|
3654
|
-
const orderQueryMapSchema = getOrderQueryMapSchema(classRef);
|
|
3655
|
-
return {
|
|
3656
|
-
description: `Order query for ${classRef.name}`,
|
|
3657
|
-
oneOf: [{
|
|
3658
|
-
type: "array",
|
|
3659
|
-
items: orderQueryMapSchema
|
|
3660
|
-
}, orderQueryMapSchema],
|
|
3661
|
-
required: false
|
|
3662
|
-
};
|
|
3663
|
-
}
|
|
3664
|
-
//#endregion
|
|
3665
|
-
//#region src/modules/core/converters/order-query-type/order-query-type.ts
|
|
3666
|
-
/**
|
|
3667
|
-
* 根据模型类自动生成排序查询 DTO 类型。
|
|
3668
|
-
*
|
|
3669
|
-
* 生成的类型包含 `orderBy` 属性,其值通过 JSON Schema 校验,确保只能按模型中的属性排序。
|
|
3670
|
-
*
|
|
3671
|
-
* @param classRef - 使用 `@Model()` 标注的类引用
|
|
3672
|
-
* @returns 包含 `orderBy` 属性的查询 DTO 类
|
|
3673
|
-
*
|
|
3674
|
-
* @example
|
|
3675
|
-
* ```typescript
|
|
3676
|
-
* @Model()
|
|
3677
|
-
* class User {
|
|
3678
|
-
* @Property()
|
|
3679
|
-
* name: string
|
|
3680
|
-
*
|
|
3681
|
-
* @Property()
|
|
3682
|
-
* createdAt: Date
|
|
3683
|
-
* }
|
|
3684
|
-
*
|
|
3685
|
-
* const UserOrderQuery = OrderQueryType(User)
|
|
3686
|
-
* // 允许按 name、createdAt 排序
|
|
3687
|
-
* ```
|
|
3688
|
-
*/
|
|
3689
|
-
function OrderQueryType(classRef) {
|
|
3690
|
-
const schema = getOrderQuerySchema(classRef);
|
|
3691
|
-
class OrderQueryClass {
|
|
3692
|
-
orderBy;
|
|
3693
|
-
}
|
|
3694
|
-
Property({
|
|
3695
|
-
optional: true,
|
|
3696
|
-
schema
|
|
3697
|
-
})(OrderQueryClass.prototype, "orderBy");
|
|
3698
|
-
MatchJsonSchema(schema, { message: "Invalid order query" })(OrderQueryClass.prototype, "orderBy");
|
|
3699
|
-
return OrderQueryClass;
|
|
3700
|
-
}
|
|
3701
|
-
//#endregion
|
|
3702
|
-
//#region src/modules/core/converters/list-response-body-type/list-response-body-type.ts
|
|
3703
|
-
/**
|
|
3704
|
-
* 生成列表响应体类型,将数据包裹在 `{ data: T[], meta: { pagination } }` 结构中。
|
|
3705
|
-
*
|
|
3706
|
-
* 支持 offset 和 cursor 两种分页模式,通过 `fromSlice()` 静态方法从分页切片创建响应。
|
|
3707
|
-
*
|
|
3708
|
-
* @param classRef - 列表项的模型类引用
|
|
3709
|
-
* @param mode - 分页模式:`'offset'` 或 `'cursor'`,不传则同时支持两种
|
|
3710
|
-
* @returns 包含 `data` 和 `meta.pagination` 的列表响应体类
|
|
3711
|
-
*
|
|
3712
|
-
* @example
|
|
3713
|
-
* ```typescript
|
|
3714
|
-
* const UserListResponse = ListResponseBodyType(UserDTO, 'offset')
|
|
3715
|
-
*
|
|
3716
|
-
* @Controller('users')
|
|
3717
|
-
* class UserController {
|
|
3718
|
-
* @Get()
|
|
3719
|
-
* async findAll(@PageQuery('offset') page): Promise<UserListResponse> {
|
|
3720
|
-
* const slice = await this.userService.findAll(page)
|
|
3721
|
-
* return UserListResponse.fromSlice(slice)
|
|
3722
|
-
* }
|
|
3723
|
-
* }
|
|
3724
|
-
* ```
|
|
3725
|
-
*/
|
|
3726
|
-
function ListResponseBodyType(classRef, mode) {
|
|
3727
|
-
class ResponseBodyMeta {
|
|
3728
|
-
pagination;
|
|
3729
|
-
}
|
|
3730
|
-
class ListResponseBody {
|
|
3731
|
-
data;
|
|
3732
|
-
meta;
|
|
3733
|
-
constructor(data, meta) {
|
|
3734
|
-
this.data = data;
|
|
3735
|
-
this.meta = meta;
|
|
3736
|
-
}
|
|
3737
|
-
static fromSlice(slice) {
|
|
3738
|
-
const data = slice.data;
|
|
3739
|
-
return new ListResponseBody(data, { pagination: slice.pagination });
|
|
3740
|
-
}
|
|
3741
|
-
toJSON() {
|
|
3742
|
-
return {
|
|
3743
|
-
data: this.data.map((item) => serializeModel(item, classRef)),
|
|
3744
|
-
meta: serializeModel(this.meta, ResponseBodyMeta)
|
|
3745
|
-
};
|
|
3746
|
-
}
|
|
3747
|
-
}
|
|
3748
|
-
__decorate([List({ type: () => classRef }), __decorateMetadata("design:type", Array)], ListResponseBody.prototype, "data", void 0);
|
|
3749
|
-
ApiExtraModels(CursorPagination, OffsetPagination)(ListResponseBody);
|
|
3750
|
-
Composite({
|
|
3751
|
-
type: () => Object,
|
|
3752
|
-
schema: {
|
|
3753
|
-
type: "object",
|
|
3754
|
-
properties: { pagination: mode === "cursor" ? { $ref: getSchemaPath(CursorPagination) } : mode === "offset" ? { $ref: getSchemaPath(OffsetPagination) } : { oneOf: [{ $ref: getSchemaPath(OffsetPagination) }, { $ref: getSchemaPath(CursorPagination) }] } },
|
|
3755
|
-
additionalProperties: true,
|
|
3756
|
-
required: ["pagination"]
|
|
3757
|
-
}
|
|
3758
|
-
})(ListResponseBody.prototype, "meta");
|
|
3759
|
-
return ListResponseBody;
|
|
3760
|
-
}
|
|
3761
|
-
//#endregion
|
|
3762
|
-
//#region src/modules/core/converters/response-body-type/response-body-type.ts
|
|
3763
|
-
/**
|
|
3764
|
-
* 生成标准响应体类型,将数据包裹在 `{ data: T }` 结构中。
|
|
3765
|
-
*
|
|
3766
|
-
* 返回的类包含 `from()` 静态方法和 `toJSON()` 序列化支持。
|
|
3767
|
-
*
|
|
3768
|
-
* @param classRef - 响应数据的模型类引用
|
|
3769
|
-
* @returns 包含 `data` 属性的响应体类
|
|
3770
|
-
*
|
|
3771
|
-
* @example
|
|
3772
|
-
* ```typescript
|
|
3773
|
-
* const UserResponse = ResponseBodyType(UserDTO)
|
|
3774
|
-
*
|
|
3775
|
-
* @Controller('users')
|
|
3776
|
-
* class UserController {
|
|
3777
|
-
* @Get(':id')
|
|
3778
|
-
* async findOne(@Param('id') id: string): Promise<UserResponse> {
|
|
3779
|
-
* const user = await this.userService.findOne(id)
|
|
3780
|
-
* return UserResponse.from(user)
|
|
3781
|
-
* }
|
|
3782
|
-
* }
|
|
3783
|
-
* ```
|
|
3784
|
-
*/
|
|
3785
|
-
function ResponseBodyType(classRef) {
|
|
3786
|
-
class ResponseBodyClass {
|
|
3787
|
-
constructor(data) {
|
|
3788
|
-
this.data = data;
|
|
3789
|
-
}
|
|
3790
|
-
static from(data, meta) {
|
|
3791
|
-
return new ResponseBodyClass(data);
|
|
3792
|
-
}
|
|
3793
|
-
toJSON() {
|
|
3794
|
-
return { data: serializeModel(this.data, classRef) };
|
|
3795
|
-
}
|
|
3796
|
-
}
|
|
3797
|
-
Composite({
|
|
3798
|
-
type: () => classRef,
|
|
3799
|
-
schema: { type: () => classRef }
|
|
3800
|
-
})(ResponseBodyClass.prototype, "data");
|
|
3801
|
-
return ResponseBodyClass;
|
|
3802
|
-
}
|
|
3803
|
-
//#endregion
|
|
3804
3826
|
//#region src/modules/open-bao/open-bao.module-definition.ts
|
|
3805
3827
|
const { ConfigurableModuleClass: ConfigurableModuleClass$4, MODULE_OPTIONS_TOKEN: MODULE_OPTIONS_TOKEN$4 } = new ConfigurableModuleBuilder().build();
|
|
3806
3828
|
//#endregion
|