@jayfong/x-server 2.112.0 → 2.114.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/_cjs/cli/cli.js +0 -0
- package/lib/_cjs/cli/templates/models.ts +23 -1
- package/lib/_cjs/core/handler.js +20 -1
- package/lib/cli/templates/models.ts +23 -1
- package/lib/core/handler.d.ts +1 -0
- package/lib/core/handler.js +21 -2
- package/lib/core/types.d.ts +9 -0
- package/lib/types/dto.d.ts +6 -1
- package/package.json +2 -2
package/lib/_cjs/cli/cli.js
CHANGED
|
File without changes
|
|
@@ -12,7 +12,29 @@ const adapter = new PrismaMariaDb({
|
|
|
12
12
|
password: dbUri.password,
|
|
13
13
|
database: dbUri.pathname.slice(1),
|
|
14
14
|
});
|
|
15
|
-
export const prismaClient = new PrismaClient({ adapter })
|
|
15
|
+
export const prismaClient = new PrismaClient({ adapter })
|
|
16
|
+
// https://github.com/prisma/prisma/issues/7550#issuecomment-2437006346
|
|
17
|
+
.$extends({
|
|
18
|
+
name: 'findManyAndCount',
|
|
19
|
+
model: {
|
|
20
|
+
$allModels: {
|
|
21
|
+
findManyAndCount<Model, Args>(
|
|
22
|
+
this: Model,
|
|
23
|
+
args: Prisma.Exact<Args, Prisma.Args<Model, 'findMany'>>,
|
|
24
|
+
): Promise<{
|
|
25
|
+
data: Prisma.Result<Model, Args, 'findMany'>;
|
|
26
|
+
total: number;
|
|
27
|
+
}> {
|
|
28
|
+
return prismaClient
|
|
29
|
+
.$transaction([
|
|
30
|
+
(this as any).findMany(args),
|
|
31
|
+
(this as any).count({ where: (args as any).where }),
|
|
32
|
+
])
|
|
33
|
+
.then(([data, total]: any) => ({ data, total }));
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
});
|
|
16
38
|
|
|
17
39
|
x.dispose.add(() => prismaClient.$disconnect());
|
|
18
40
|
|
package/lib/_cjs/core/handler.js
CHANGED
|
@@ -22,6 +22,7 @@ class Handler {
|
|
|
22
22
|
this.options = options;
|
|
23
23
|
this.requestDataSchema = void 0;
|
|
24
24
|
this.requestDataSchemaVae = void 0;
|
|
25
|
+
this.requestHandle = void 0;
|
|
25
26
|
this.handle = async (data, ctx) => {
|
|
26
27
|
// 前置 hook
|
|
27
28
|
if (Handler.hooks.length) {
|
|
@@ -72,7 +73,7 @@ class Handler {
|
|
|
72
73
|
}
|
|
73
74
|
}
|
|
74
75
|
try {
|
|
75
|
-
let res = await this.
|
|
76
|
+
let res = await this.requestHandle(data, ctx);
|
|
76
77
|
if (res instanceof _http_redirect.HttpRedirect) {
|
|
77
78
|
ctx.redirect(res.url, res.permanently);
|
|
78
79
|
return {};
|
|
@@ -188,6 +189,24 @@ class Handler {
|
|
|
188
189
|
} else if (options.requestDataSchema) {
|
|
189
190
|
this.requestDataSchema = options.requestDataSchema(_validator.yup);
|
|
190
191
|
}
|
|
192
|
+
if (options.concurrency != null) {
|
|
193
|
+
if (typeof options.concurrency === 'number') {
|
|
194
|
+
this.requestHandle = (0, _vtils.asyncLimit)(async (data, ctx) => options.handle(data, ctx), {
|
|
195
|
+
concurrency: options.concurrency
|
|
196
|
+
});
|
|
197
|
+
} else {
|
|
198
|
+
const {
|
|
199
|
+
count,
|
|
200
|
+
groupBy
|
|
201
|
+
} = options.concurrency;
|
|
202
|
+
this.requestHandle = (0, _vtils.asyncLimit)(async (data, ctx) => options.handle(data, ctx), {
|
|
203
|
+
concurrency: count,
|
|
204
|
+
groupBy: groupBy
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
} else {
|
|
208
|
+
this.requestHandle = options.handle;
|
|
209
|
+
}
|
|
191
210
|
}
|
|
192
211
|
static addHook(hook) {
|
|
193
212
|
this.hooks.push(hook);
|
|
@@ -12,7 +12,29 @@ const adapter = new PrismaMariaDb({
|
|
|
12
12
|
password: dbUri.password,
|
|
13
13
|
database: dbUri.pathname.slice(1),
|
|
14
14
|
});
|
|
15
|
-
export const prismaClient = new PrismaClient({ adapter })
|
|
15
|
+
export const prismaClient = new PrismaClient({ adapter })
|
|
16
|
+
// https://github.com/prisma/prisma/issues/7550#issuecomment-2437006346
|
|
17
|
+
.$extends({
|
|
18
|
+
name: 'findManyAndCount',
|
|
19
|
+
model: {
|
|
20
|
+
$allModels: {
|
|
21
|
+
findManyAndCount<Model, Args>(
|
|
22
|
+
this: Model,
|
|
23
|
+
args: Prisma.Exact<Args, Prisma.Args<Model, 'findMany'>>,
|
|
24
|
+
): Promise<{
|
|
25
|
+
data: Prisma.Result<Model, Args, 'findMany'>;
|
|
26
|
+
total: number;
|
|
27
|
+
}> {
|
|
28
|
+
return prismaClient
|
|
29
|
+
.$transaction([
|
|
30
|
+
(this as any).findMany(args),
|
|
31
|
+
(this as any).count({ where: (args as any).where }),
|
|
32
|
+
])
|
|
33
|
+
.then(([data, total]: any) => ({ data, total }));
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
});
|
|
16
38
|
|
|
17
39
|
x.dispose.add(() => prismaClient.$disconnect());
|
|
18
40
|
|
package/lib/core/handler.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ export declare class Handler<TReqData extends any = void, TResData extends any =
|
|
|
6
6
|
readonly options: XHandler.Options<TReqData, TResData, TReqMethod>;
|
|
7
7
|
private requestDataSchema;
|
|
8
8
|
readonly requestDataSchemaVae: vae.VaeSchemaOf<TReqDataRequired>;
|
|
9
|
+
private requestHandle;
|
|
9
10
|
constructor(options: XHandler.Options<TReqData, TResData, TReqMethod>);
|
|
10
11
|
handle: XHandler.Handle<TReqData, TResData, TReqMethod>;
|
|
11
12
|
private handleHttp;
|
package/lib/core/handler.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { HTTPError as GotHTTPError, ParseError as GotParseError, RequestError as GotRequestError, TimeoutError as GotTimeoutError } from 'got';
|
|
2
2
|
import LZString from 'lz-string';
|
|
3
|
-
import { base64UrlEncode, DataPacker, rot13, toSingleLineString } from 'vtils';
|
|
3
|
+
import { asyncLimit, base64UrlEncode, DataPacker, rot13, toSingleLineString } from 'vtils';
|
|
4
4
|
import * as vae from 'vtils/vae';
|
|
5
5
|
import { getZhCN, yup } from 'vtils/validator';
|
|
6
6
|
import { HttpError } from "../core/http_error";
|
|
@@ -16,6 +16,7 @@ export class Handler {
|
|
|
16
16
|
this.options = options;
|
|
17
17
|
this.requestDataSchema = void 0;
|
|
18
18
|
this.requestDataSchemaVae = void 0;
|
|
19
|
+
this.requestHandle = void 0;
|
|
19
20
|
this.handle = async (data, ctx) => {
|
|
20
21
|
// 前置 hook
|
|
21
22
|
if (Handler.hooks.length) {
|
|
@@ -66,7 +67,7 @@ export class Handler {
|
|
|
66
67
|
}
|
|
67
68
|
}
|
|
68
69
|
try {
|
|
69
|
-
let res = await this.
|
|
70
|
+
let res = await this.requestHandle(data, ctx);
|
|
70
71
|
if (res instanceof HttpRedirect) {
|
|
71
72
|
ctx.redirect(res.url, res.permanently);
|
|
72
73
|
return {};
|
|
@@ -182,6 +183,24 @@ export class Handler {
|
|
|
182
183
|
} else if (options.requestDataSchema) {
|
|
183
184
|
this.requestDataSchema = options.requestDataSchema(yup);
|
|
184
185
|
}
|
|
186
|
+
if (options.concurrency != null) {
|
|
187
|
+
if (typeof options.concurrency === 'number') {
|
|
188
|
+
this.requestHandle = asyncLimit(async (data, ctx) => options.handle(data, ctx), {
|
|
189
|
+
concurrency: options.concurrency
|
|
190
|
+
});
|
|
191
|
+
} else {
|
|
192
|
+
const {
|
|
193
|
+
count,
|
|
194
|
+
groupBy
|
|
195
|
+
} = options.concurrency;
|
|
196
|
+
this.requestHandle = asyncLimit(async (data, ctx) => options.handle(data, ctx), {
|
|
197
|
+
concurrency: count,
|
|
198
|
+
groupBy: groupBy
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
} else {
|
|
202
|
+
this.requestHandle = options.handle;
|
|
203
|
+
}
|
|
185
204
|
}
|
|
186
205
|
static addHook(hook) {
|
|
187
206
|
this.hooks.push(hook);
|
package/lib/core/types.d.ts
CHANGED
|
@@ -167,6 +167,15 @@ export declare namespace XHandler {
|
|
|
167
167
|
* @default 默认为服务的设置
|
|
168
168
|
*/
|
|
169
169
|
useAsyncContext?: boolean;
|
|
170
|
+
/**
|
|
171
|
+
* 并发控制
|
|
172
|
+
*/
|
|
173
|
+
concurrency?: number | {
|
|
174
|
+
/** 并发数量 */
|
|
175
|
+
count?: number;
|
|
176
|
+
/** 分组函数 */
|
|
177
|
+
groupBy?: (data: TReqData, ctx: Context) => string;
|
|
178
|
+
};
|
|
170
179
|
/**
|
|
171
180
|
* 处理器
|
|
172
181
|
*/
|
package/lib/types/dto.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Merge, OmitStrict, PartialBy, RequiredBy, RequiredDeep, Simplify } from 'vtils/types';
|
|
1
|
+
import type { Merge, OmitStrict, PartialBy, RequiredBy, RequiredDeep, Simplify } from 'vtils/types';
|
|
2
2
|
/** 选中的必填,同时能将部分选填,并能覆盖 */
|
|
3
3
|
export type MakeDtoRequired<TRawData extends Record<any, any>, TPickKey extends keyof TRawData = never, TPartialKey extends [TPickKey] extends [never] ? keyof TRawData : TPickKey = never, TOverride extends {
|
|
4
4
|
[K in [TPickKey] extends [never] ? keyof TRawData : TPickKey]?: any;
|
|
@@ -79,3 +79,8 @@ export type MakeDtoIncludeWithRequiredAndPartial<TRawData extends Record<any, an
|
|
|
79
79
|
} = never> = Simplify<Merge<PartialBy<RequiredBy<Pick<TRawData, TIncludeKey>, TRequiredKey>, TPartialKey>, [
|
|
80
80
|
TOverride
|
|
81
81
|
] extends [never] ? {} : TOverride>>;
|
|
82
|
+
/** 添加分页参数 */
|
|
83
|
+
export type MakeDtoWithPagination<TRawData extends Record<any, any>> = TRawData & {
|
|
84
|
+
pageNumber: number;
|
|
85
|
+
pageSize: number;
|
|
86
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jayfong/x-server",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.114.0",
|
|
4
4
|
"license": "ISC",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"main": "lib/_cjs/index.js",
|
|
@@ -79,7 +79,7 @@
|
|
|
79
79
|
"unplugin-macros": "^0.16.0",
|
|
80
80
|
"utf-8-validate": "^5.0.9",
|
|
81
81
|
"vscode-generate-index-standalone": "^1.7.1",
|
|
82
|
-
"vtils": "^4.
|
|
82
|
+
"vtils": "^4.138.0",
|
|
83
83
|
"yaml": "^2.3.1",
|
|
84
84
|
"yargs": "^17.4.1"
|
|
85
85
|
},
|